[英]Inference from a pre-trained model in tensorflow
I have a pre-trained model from which I am trying to do inference in tensorflow. 我有一个经过预训练的模型,我正在尝试从中进行张量流推断。 But I think I am missing something.
但是我想我缺少了一些东西。 Here is my architecture:
这是我的架构:
# Define a function to create a conv layer
def add_conv_layer(incoming=None, num_filters=None, kSize=None, pad='SAME', train=False, s=1,
layer_name=None, weights_param=None, bias_param=None, activation_fn=None):
with tf.variable_scope(layer_name):
previous_layer_output_shape = incoming.get_shape().as_list()[-1]
tshape = [kSize, kSize, previous_layer_output_shape, num_filters]
# Set weights for the layer
if weights_param is not None:
weights = tf.Variable(weights_param, name='weights', dtype=tf.float32)
weights = tf.reshape(weights, tshape)
else:
weights = init_weights(tshape)
print weights.shape
# Set bias for the layer
if bias_param is not None:
bias = tf.Variable(bias_param, name='bias', dtype=tf.float32)
else:
bias = init_bias(num_filters)
layer = tf.nn.conv2d(incoming, weights, strides=[1,s,s,1], padding=pad, name='layer_name') + bias
if activation_fn is not None:
layer = activation_fn(layer)
return layer
# Define a function to add a dense layer
def add_dense_layer(input_tensor=None, num_units=None, layer_name=None,
weight_params=None, bias_params=None, activation_fn=None):
with tf.variable_scope(layer_name):
previous_layer_output_shape = input_tensor.get_shape().as_list()[-1]
tshape = [previous_layer_output_shape, num_units]
# Add weights for the layer
if weight_params is not None:
weights = tf.Variable(weight_params, name='weights', dtype=tf.float32)
weights = tf.reshape(weights, tshape)
else:
weights = init_weights(tshape)
# Add bias to the layer
if bias_params is not None:
bias = tf.Variable(bias_params, name='bias', dtype=tf.float32)
else:
bias = init_bias(num_units)
layer = tf.matmul(input_tensor, weights) + bias
if activation_fn is not None:
layer = activation_fn(layer)
return layer
def build_network(features):
features = tf.reshape(features, [-1,224,224,3])
# Conv block 1
conv1_1 = add_conv_layer(features, num_filters=64, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv1_1']['weights'].value,
bias_param=f['conv1_1']['bias'].value,
layer_name='conv1_1')
conv1_2 = add_conv_layer(conv1_1, num_filters=64, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv1_2']['weights'].value,
bias_param=f['conv1_2']['bias'].value,
layer_name='conv1_2')
pool1 = tf.layers.max_pooling2d(inputs=conv1_2,pool_size=(2,2), strides=(2,2), name='pool1')
# Conv block 2
conv2_1 = add_conv_layer(pool1, num_filters=128, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv2_1']['weights'].value,
bias_param=f['conv2_1']['bias'].value,
layer_name='conv2_1')
conv2_2 = add_conv_layer(conv2_1, num_filters=128, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv2_2']['weights'].value,
bias_param=f['conv2_2']['bias'].value,
layer_name='conv2_2')
pool2 = tf.layers.max_pooling2d(inputs=conv2_2,pool_size=(2,2), strides=(2,2), name='pool2')
# Conv block 3
conv3_1 = add_conv_layer(pool2, num_filters=256, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv3_1']['weights'].value,
bias_param=f['conv3_1']['bias'].value,
layer_name='conv3_1')
conv3_2 = add_conv_layer(conv3_1, num_filters=256, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv3_2']['weights'].value,
bias_param=f['conv3_2']['bias'].value,
layer_name='conv3_2')
conv3_3 = add_conv_layer(conv3_2, num_filters=256, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv3_3']['weights'].value,
bias_param=f['conv3_3']['bias'].value,
layer_name='conv3_3')
pool3 = tf.layers.max_pooling2d(inputs=conv3_3,pool_size=(2,2), strides=(2,2), name='pool3')
# Conv block 4
conv4_1 = add_conv_layer(pool3, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv4_1']['weights'].value,
bias_param=f['conv4_1']['bias'].value,
layer_name='conv4_1')
conv4_2 = add_conv_layer(conv4_1, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv4_2']['weights'].value,
bias_param=f['conv4_2']['bias'].value,
layer_name='conv4_2')
conv4_3 = add_conv_layer(conv4_2, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv4_3']['weights'].value,
bias_param=f['conv4_3']['bias'].value,
layer_name='conv4_3')
pool4 = tf.layers.max_pooling2d(inputs=conv4_3,pool_size=(2,2), strides=(2,2), name='pool4')
# Conv block 5
conv5_1 = add_conv_layer(pool4, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv5_1']['weights'].value,
bias_param=f['conv5_1']['bias'].value,
layer_name='conv5_1')
conv5_2 = add_conv_layer(conv5_1, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv5_2']['weights'].value,
bias_param=f['conv5_2']['bias'].value,
layer_name='conv5_2')
conv5_3 = add_conv_layer(conv5_2, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
weights_param=f['conv5_3']['weights'].value,
bias_param=f['conv5_3']['bias'].value,
layer_name='conv5_3')
pool5 = tf.layers.max_pooling2d(inputs=conv5_3, pool_size=(7,7),strides=(7,7), name='img1_pool5')
flatten_layer = tf.contrib.layers.flatten(inputs=pool5)
fc6 = add_dense_layer(flatten_layer, num_units=1024, activation_fn=tf.nn.relu,
weight_params=f['fc6']['weights'].value,
bias_params= f['fc6']['bias'].value,
layer_name='fc6')
fc7 = add_dense_layer(fc6, num_units=256, activation_fn=tf.nn.relu,
weight_params=f['fc7']['weights'].value,
bias_params= f['fc7']['bias'].value,
layer_name='fc7')
output = add_dense_layer(fc7, num_units=2, activation_fn=tf.nn.softmax,
weight_params=f['out']['weights'].value,
bias_params= f['out']['bias'].value,
layer_name='output')
return output
Now, I want to run feed the build_network
function a new input and take the inference. 现在,我要运行
build_network
函数一个新的输入并进行推断。 I was doing this as shown(which I think is wrong): 我正在这样做,如图所示(我认为这是错误的):
init = tf.global_variables_initializer()
out = build_network(features)
with tf.Session() as sess:
sess.run(init)
print(out.eval())
But with this, I am getting same output for every input. 但是,有了这个,我为每个输入得到相同的输出。 Please help
请帮忙
Edited from the original :
Your
build_network
function creates the graph only.
从原始文件进行编辑 :
build_network
函数仅创建图形。
The
sess.run(init)
line initializes the network's weight with the default initializers for each weight, ie your network after that line is
not trained.
sess.run(init)
行使用每个权重的默认初始值设定项初始化网络的权重,即该行之后的网络
未经训练。
After adding to the code your implementation of te add_*_layer
functions, I see now that you do in fact initialize the weights. 在将代码中的te
add_*_layer
函数实现添加到代码中之后,我现在看到您实际上在初始化权重。 The further information in the comments to this answer suggests that the error might lie in the conversion of weights from Theano to Tensorflow or, as suggested by @Sebastian, in a difference in the data layout. 该答案的注释中的更多信息表明错误可能在于权重从Theano到Tensorflow的转换,或者如@Sebastian所建议的,由于数据布局的差异。 Without further information, though, it's hard for me to say more with certainty.
但是,如果没有更多信息,我很难确定地说更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.