我尝试使用以下代码恢复模型:
new_saver = tf.train.import_meta_graph(model_path+'.meta')
new_saver.restore(sess, model_path)
g=tf.get_default_graph()
对于原始图表中的每个权重或偏差,我做了g.get_tensrr_by_name()
。
但是,当我尝试在deconv2d
图层上执行此操作时,如下所示:
def deconv2d(self,inputs, num_outputs, kernel_shape, g,scope,strides=[1, 1]):
with tf.variable_scope(scope) as scope:
weights_initializer = g.get_tensor_by_name("prsr/conditioning/deconv/Conv2d_transpose/weights:0")
biases_initializer = g.get_tensor_by_name("prsr/conditioning/deconv/Conv2d_transpose/biases:0")
return tf.contrib.layers.convolution2d_transpose(inputs=inputs, num_outputs=num_outputs,kernel_size=kernel_shape,stride=strides, \
padding='SAME', weights_initializer=weights_initializer,biases_initializer=biases_initializer)
失败并显示以下错误:
File "restore.py", line 41, in deconv2d
padding='SAME', weights_initializer=weights_initializer,biases_initializer=biases_initializer)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 177, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1126, in convolution2d_transpose
outputs = layer.apply(inputs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 323, in apply
return self.__call__(inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 289, in __call__
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/convolutional.py", line 1043, in build
dtype=self.dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1033, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 932, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 349, in get_variable
validate_shape=validate_shape, use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 278, in variable_getter
variable_getter=functools.partial(getter, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 228, in _add_variable
trainable=trainable and self.trainable)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1327, in layer_variable_getter
return _model_variable_getter(getter, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1316, in _model_variable_getter
custom_getter=getter, use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 177, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 259, in model_variable
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 177, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 214, in variable
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 341, in _true_getter
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 638, in _get_single_variable
raise ValueError("If initializer is a constant, do not specify shape.")
ValueError: If initializer is a constant, do not specify shape.
我不知道这引用了哪个shape
,我不认为weights_initializer
和biases_initializer
是常数,它们是张量,对吧?顺便说一下,我非常确定原始图表中存在这两个张量prsr/conditioning/deconv/Conv2d_transpose/weights
和prsr/conditioning/deconv/Conv2d_transpose/biasess
,因为我使用print_tensors_in_checkpoint_file
进行了检查,我实际上可以看到值。
那么如何恢复应用此tf.contrib.layers.convolution2d_transpose()
图层的模型?我在stackoverflow和github上搜索了很多,但没有任何效果。任何帮助,将不胜感激。
答案 0 :(得分:0)
weights_initializer
和bias_initializer
不是您认为的那样。你可能会认为这两个张量是去卷积中使用的权重的初始值,对吧?但是,初始化器参数是一个函数而不是一个应该看起来像这样的张量:
def my_initializer(shape, dtype=tf.float32, partition_info=None):
# do some computation to build up a tensor of the given shape
return that_tensor
然后您可以像这样使用此初始值设定项:
tf.contrib.layers.convolution2d_transpose(inputs=inputs, ..., weights_initializer=my_initializer)
因此,作为解决问题的方法,我认为以下内容应该有效:
def weights_initializer(shape, dtype=tf.float32, partition_info=None):
weights = tf.get_default_graph().get_tensor_by_name("prsr/conditioning/deconv/Conv2d_transpose/weights:0")
return weights
但是,在我看来,这感觉有些笨拙。为什么要加载图形,然后在新操作中使用预先训练的权重?在设置初始模型之前,为什么这些权重与此操作无关?
PS:在处理变量时,您可能会发现tf.get_variable
派上用场。如果您使用tf.get_variable
创建变量,则可以稍后再次使用tf.get_variable
检索这些变量,而无需调用繁琐的get_tensor_by_name
。查看this了解详情。