Python Tensorflow:加载预训练的ResNet模型时出错
我想使用Tensorflow预先训练过的ResNet模型。我下载了模型的(Python Tensorflow:加载预训练的ResNet模型时出错,python,tensorflow,Python,Tensorflow,我想使用Tensorflow预先训练过的ResNet模型。我下载了模型的(resnet_v1.py)和(resnet_v1_50.ckpt)文件 我已经可以通过使用以下帖子解决错误ImportError:没有名为“nets”的模块:请参阅中的答案 现在我遇到以下错误,不知道该怎么办: NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Va
resnet_v1.py
)和(resnet_v1_50.ckpt
)文件
我已经可以通过使用以下帖子解决错误ImportError:没有名为“nets”的模块
:请参阅中的答案
现在我遇到以下错误,不知道该怎么办:
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})
谁能告诉我,为什么它不起作用?如何更改代码以使其运行?也许您可以从中使用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的合理性和可读性。
顺便说一句,该模型与Tensorflow提供的模型相同,它是从IIRC中转移过来的
您可以在链接文档和各种博客文章(如或其他web资源)中阅读更多关于tf.keras.applications
我在凯拉斯该怎么办
回答评论中的问题
:如果要进行预测,请使用如何将图像传递到网络?
,其中image是model.predict(image)
。就这么简单np.array
:嗯,这个更复杂。。。开玩笑吧,每个层都有如何获取权重?
方法,该方法返回其权重和偏差,您可以使用.get_weights()
。您可以使用对模型中的层进行迭代。layers()
模型一次获取所有权重。还可以获取权重()
tensorflow
使用Tensorflow的Hub解决方案
您似乎可以按照中所述使用Hub加载和微调Resnet50。我只是使用原始源代码加载了图形,而没有对图形做任何更改(我希望至少如此)。我不熟悉Keras,因此只想使用Tensorflow。如何将图像传递到网络?我还需要访问所有层的所有权重、偏差和激活。我如何使用Keras做到这一点?更新了我的答案。Keras不是Tensorflow,因此您可以学习它,使用它,它不会进入您的方式+有一个直观的API。哦,你可以使用convert Keras的模型,将其转换为tf.Estimator,如果你真正想要的是tf.Estimator。