Reputation: 761
I am trying to load a Tensorflow checkpoint using Slim API. What I do is simply creating the initializer for the model inside a function as follows:
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
Then, I run the init_fn operation in the main program. I also use flattened return value for further computation graph definition. However, when loading the variables, I face the following error:
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'
The problem is obviously mentioning inner implementations of Tensorflow. May someone help me with loading these checkpoints?
Upvotes: 0
Views: 143
Reputation: 2356
A typo ?
init_fn = slim.assign_from_checkpoint_fn(os.path.join(self.path_to_pretrained_cnn_weights, 'resnet_v1_101.ckpt'), variables_to_restore)
Upvotes: 1