我正在尝试使用Slim API加载Tensorflow检查点。我所做的仅仅是为函数内部的模型创建初始化器,如下所示:
def generate_image_feature_map_with_resnet(self, cnn_input, name="Pre-trained ResNet101"):
"""
Computation graph defnition (with the help of tf.slim) for a ResNet101 architecture to extract image feature maps.
"""
with slim.arg_scope(resnet.resnet_arg_scope()):
features, _ = resnet.resnet_v1_101(inputs=cnn_input, is_training=True)
variables_to_restore = slim.get_model_variables("resnet_v1_101")
print("Restored variables: ", variables_to_restore)
init_fn = slim.assign_from_checkpoint_fn(os.path.join(self.path_to_pretrained_cnn_weights, 'resnet_v1_101.ckpt'),
slim.get_model_variables(variables_to_restore))
# Flatten feature maps
flattened = tf.layers.flatten(
inputs=features,
name="flatten_features"
)
return flattened, init_fn然后,我在主程序中运行init_fn操作。我还使用扁平化的返回值来进一步计算图形定义。但是,在加载变量时,我遇到了以下错误:
WARNING:tensorflow:From C:\Users\Ali Gholami\Desktop\BSc\Visual-Question-Answering-with-Stacked-Attention-Networks\code\tensorflow-implementation\nets\resnet_v1.py:242: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
Restored variables: [<tf.Variable 'resnet_v1_101/conv1/weights:0' shape=(7, 7, 3, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/conv1/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/conv1/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/conv1/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/conv1/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/weights:0' shape=(1, 1, 64, 256) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/gamma:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/beta:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/moving_mean:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/moving_variance:0' shape=(256,)
dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv1/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv2/weights:0' shape=(3, 3, 64, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv3/weights:0' shape=(1, 1, 64, 256) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_1/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv1/weights:0' shape=(1, 1, 256, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv2/weights:0' shape=(3, 3, 64, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv3/weights:0' shape=(1, 1, 64, 256) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_2/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv1/weights:0' shape=(1, 1, 256, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(64,)
dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv2/weights:0'
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* shape=(3, 3, 64, 64) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(64,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv3/weights:0' shape=(1, 1, 64, 256) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block1/unit_3/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(256,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/shortcut/weights:0' shape=(1, 1, 256, 512) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/shortcut/BatchNorm/gamma:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/shortcut/BatchNorm/beta:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/shortcut/BatchNorm/moving_mean:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/shortcut/BatchNorm/moving_variance:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv1/weights:0' shape=(1, 1, 256, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv2/weights:0' shape=(3, 3, 128, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv3/weights:0' shape=(1, 1, 128, 512) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_1/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv1/weights:0' shape=(1, 1, 512, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv2/weights:0' shape=(3, 3, 128, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv3/weights:0' shape=(1, 1, 128, 512) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_2/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv1/weights:0' shape=(1, 1, 512, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv1/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv1/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv1/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv1/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv2/weights:0' shape=(3, 3, 128, 128) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv2/BatchNorm/gamma:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv2/BatchNorm/beta:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv2/BatchNorm/moving_mean:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv2/BatchNorm/moving_variance:0' shape=(128,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv3/weights:0' shape=(1, 1, 128, 512) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv3/BatchNorm/gamma:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(512,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block2/unit_3/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape='resnet_v1_101/block4/unit_3/bottleneck_v1/conv3/BatchNorm/beta:0' shape=(2048,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block4/unit_3/bottleneck_v1/conv3/BatchNorm/moving_mean:0' shape=(2048,) dtype=float32_ref>, <tf.Variable 'resnet_v1_101/block4/unit_3/bottleneck_v1/conv3/BatchNorm/moving_variance:0' shape=(2048,) dtype=float32_ref>]
*/
Traceback (most recent call last):
File "main.py", line 7, in <module>
vqa_model.build_model()
File "C:\Users\Ali Gholami\Desktop\BSc\Visual-Question-Answering-with-Stacked-Attention-Networks\code\tensorflow-implementation\vqa_san.py", line 171, in build_model
self.image_feature_map, self.pre_trained_cnn_weights_init = feature_extractor.generate_image_feature_map_with_resnet(self.img)
File "C:\Users\Ali Gholami\Desktop\BSc\Visual-Question-Answering-with-Stacked-Attention-Networks\code\tensorflow-implementation\feature_extractor.py", line 48, in generate_image_feature_map_with_resnet
slim.get_model_variables(variables_to_restore))
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 344, in get_model_variables
return get_variables(scope, suffix, ops.GraphKeys.MODEL_VARIABLES)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 331, in get_variables
return ops.get_collection(collection, scope)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 5674, in get_collection
return get_default_graph().get_collection(key, scope)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3692, in get_collection
regex = re.compile(scope)
File "C:\ProgramData\Anaconda3\lib\re.py", line 233, in compile
return _compile(pattern, flags)
File "C:\ProgramData\Anaconda3\lib\re.py", line 289, in _compile
p, loc = _cache[type(pattern), pattern, flags]
TypeError: unhashable type: 'list'这个问题显然涉及到Tensorflow的内部实现。有人可以帮我加载这些检查点吗?
发布于 2019-01-15 03:01:02
打字错误?
init_fn = slim.assign_from_checkpoint_fn(os.path.join(self.path_to_pretrained_cnn_weights, 'resnet_v1_101.ckpt'), variables_to_restore)https://stackoverflow.com/questions/54184517
复制相似问题