[英]Tensorflow: Error while loading pre-trained ResNet model
我想使用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.applications的tf.keras.applications
?
根据错误,如果你没有以任何方式改变图形,这是你的整个源代码,它可能真的,真的很难调试。
如果您选择理智的tf.keras.applications.resnet50方式,您可以这样做:
import tensorflow
in_width, in_height, in_channels = 224, 224, 3
pretrained_resnet = tensorflow.keras.applications.ResNet50(
weights="imagenet",
include_top=False,
input_shape=(in_width, in_height, in_channels),
)
# You can freeze some layers if you want, depends on your task
# Make "top" (last 3 layers below) whatever fits your task as well
model = tensorflow.keras.models.Sequential(
[
pretrained_resnet,
tensorflow.keras.layers.Flatten(),
tensorflow.keras.layers.Dense(1024, activation="relu"),
tensorflow.keras.layers.Dense(10, activation="softmax"),
]
)
print(model.summary())
现在推荐这种方法,特别是考虑到即将推出的Tensorflow 2.0,理智性和可读性。 BTW。 此模型与Tensorflow提供的模型相同,它从IIRC传输。
您可以在链接文档和各种博客文章中阅读有关tf.keras.applications
更多信息,例如此一个或其他Web资源。
回答评论中的问题
How do I pass images to the network?
:如果要进行预测,请使用model.predict(image)
,其中image是np.array
。 就那么简单。
How do I access weights?
:好吧,这个更复杂......开玩笑,每一层都有.get_weights()
方法,它返回它的权重和偏差,你可以for layer in model.layers()
使用for layer in model.layers()
迭代图层。 您也可以使用model.get_weights()
一次获得所有权重。
总而言之,您将学习Keras并且比Tensorflow更有效率,而不是调试此问题。 他们有30秒的指导原因。
BTW。 Tensorflow默认发布了Keras,因此,Tensorflow的Keras风味是 Tensorflow的一部分(无论这听起来有多混乱)。 这就是我在我的例子中使用tensorflow
的原因。
似乎您可以使用Hub加载和微调Resnet50 , 如此链接中所述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.