我想使用Tensorflow中的预训练ResNet模型。我下载了模型的代码(
我已经通过使用以下帖子解决了
现在我遇到了以下错误,不知道该怎么办:
这是我正在使用的代码,尝试加载模型:
resnet_v1.py
)和检查点(resnet_v1_50.ckpt
)文件,可以在这里找到。我已经通过使用以下帖子解决了
ImportError: No module named 'nets'
错误:请参见此处来自tsveti_iko的答案。现在我遇到了以下错误,不知道该怎么办:
NotFoundError (see above for traceback): Restoring from checkpoint failed.
This is most likely due to a Variable name or other graph key that is missing from the checkpoint.
Please ensure that you have not altered the graph expected based on the checkpoint. Original error:
Tensor name "resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/biases"
not found in checkpoint files /home/resnet_v1_50.ckpt
[[node save/RestoreV2 (defined at my_resnet.py:34) =
RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ...,
DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost
/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2
/tensor_names, save/RestoreV2/shape_and_slices)]]
这是我正在使用的代码,尝试加载模型:
import numpy as np
import tensorflow as tf
import resnet_v1
# Restore variables of resnet model
slim = tf.contrib.slim
# Paths
network_dir = "home/resnet_v1_50.ckpt"
# Image dimensions
in_width, in_height, in_channels = 224, 224, 3
# Placeholder
X = tf.placeholder(tf.float32, [None, in_width, in_height, in_channels])
# Define network graph
logits, activations = resnet_v1.resnet_v1_50(X, is_training=False)
prediction = tf.argmax(logits, 1)
with tf.Session() as sess:
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
saver.restore(sess, network_dir)
# Restore variables
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
# Feed random image into resnet
img = np.random.randn(1, in_width, in_height, in_channels)
pred = sess.run(prediction, feed_dict={X:img})
有人能告诉我为什么它不起作用吗?我该如何更改代码以使其运行?
tf.keras.estimator.model_to_estimator
将Keras的模型转换为tf.Estimator
。 - Szymon Maszke