import tensorflow as tf
import numpy as np
def inference (images,keep_prob):
'''
Build the model
Args:
image: image batch,4D tensor,tf.float32,[batch_size,height,width,channels]
Returns:
output tensor with the computed logits,float,[batch_size,65]
'''
# conv1
with tf.variable_scope('conv1') as scope:
weights = tf.get_variable('weights',
shape = [3,3,3,32],
dtype = tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv = tf.nn.conv2d(images,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',
shape=[32],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv1 = tf.nn.relu(pre_activation,name= scope.name)
# conv2
with tf.variable_scope('conv2') as scope:
weights = tf.get_variable('weights',shape=[3,3,32,32],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv = tf.nn.conv2d(conv1,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',
shape=[32],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv2 = tf.nn.relu(pre_activation,name= scope.name)
with tf.variable_scope('max_pooling1') as scope:
pool1 = tf.nn.max_pool(conv2,ksize = [1,2,2,1],strides= [1,2,2,1],padding='VALID',name='pooling1')
#conv3
with tf.variable_scope('conv3') as scope:
weights = tf.get_variable('weights',shape=[3,3,32,64],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv = tf.nn.conv2d(pool1,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',shape=[64],dtype = tf.float32,initializer= tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv3 = tf.nn.relu(pre_activation,name=scope.name)
#conv4
with tf.variable_scope('conv4') as scope:
weights = tf.get_variable('weights',shape=[3,3,64,64],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv =tf.nn.conv2d(conv3,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',shape=[64],dtype=tf.float32,initializer=tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv4 = tf.nn.relu(pre_activation,name=scope.name)
with tf.variable_scope('max_pooling2') as scope:
pool2 = tf.nn.max_pool(conv4,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID',name='pooling2')
#conv5
with tf.variable_scope('conv5') as scope:
weights = tf.get_variable('weights',shape=[3,3,64,128],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv =tf.nn.conv2d(pool2,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',shape=[128],dtype=tf.float32,initializer=tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv5 = tf.nn.relu(pre_activation,name=scope.name)
#conv6
with tf.variable_scope('conv6') as scope:
weights = tf.get_variable('weights',shape=[3,3,128,128],dtype=tf.float32,initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32))
conv =tf.nn.conv2d(conv5,weights,strides=[1,1,1,1],padding='VALID')
biases = tf.get_variable('biases',shape=[128],dtype=tf.float32,initializer=tf.constant_initializer(0.1))
pre_activation = tf.nn.bias_add(conv,biases)
conv6 = tf.nn.relu(pre_activation,name=scope.name)
#pool3
with tf.variable_scope('max_pool3') as scope:
pool3 = tf.nn.max_pool(conv6,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID',name='pool3')
#%%
#fc1_flatten
# 第一个全连接层,识别车牌上的省
with tf.variable_scope('fc1') as scope:
shp = pool3.get_shape()
flattened_shape =shp[1].value*shp[2].value*shp[3].value
reshape = tf.reshape(pool3,[-1,flattened_shape])
fc1 = tf.nn.dropout(reshape,keep_prob,name='fc1_dropdot')
# 第二个全连接层,识别车牌上的市
with tf.variable_scope('fc21') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[65],
dtype=tf.float32,
initializer = tf.truncated_normal_initializer(0.1)
)
fc21 = tf.matmul(fc1,weights)+biases
#第三个全连接层识别,识别车牌第三位字母数字,以此类推4567
with tf.variable_scope('fc22') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[65],
dtype=tf.float32,
initializer = tf.truncated_normal_initializer(0.1)
)
fc22 = tf.matmul(fc1,weights)+biases
with tf.variable_scope('fc23') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[65],
dtype=tf.float32,
initializer = tf.truncated_normal_initializer(0.1)
)
fc23= tf.matmul(fc1,weights)+biases
with tf.variable_scope('fc24') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[65],
dtype=tf.float32,
initializer = tf.truncated_normal_initializer(0.1)
)
fc24 = tf.matmul(fc1,weights)+biases
with tf.variable_scope('fc25') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[65],
dtype=tf.float32,
initializer = tf.truncated_normal_initializer(0.1)
)
fc25 = tf.matmul(fc1,weights)+biases
with tf.variable_scope('fc26') as scope:
weights = tf.get_variable('weights',
shape=[flattened_shape,65],
评论5
最新资源