python - Tensorflow:加载预训练的 ResNet 模型时出错
问题描述
我想使用来自 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})
谁能告诉我,为什么它不起作用?我如何必须更改我的代码才能使其运行?
解决方案
也许你可以使用 ResNet50 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、健全性和可读性。顺便提一句。这个模型和 Tensorflow 提供的一样,都是从 IIRC 转移过来的。
tf.keras.applications
您可以在链接的文档和各种博客文章中阅读更多相关信息,例如这个或其他网络资源。
我如何在 Keras
回答评论中的问题
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()
. 您也可以一次获得所有权重model.get_weights()
。
总而言之,您将在比调试此问题更短的时间内学习 Keras,并在其中比在 Tensorflow 中更有效率。他们有30 秒的指导是有原因的。
顺便提一句。Tensorflow 默认提供 Keras,因此,Tensorflow 的 Keras 风格是Tensorflow 的一部分(无论这听起来多么令人困惑)。这就是我tensorflow
在示例中使用的原因。
使用 Tensorflow 的 Hub 的解决方案
似乎您可以按照此链接中的说明使用 Hub加载和微调Resnet50。
推荐阅读
- c# - CSVHelper 版本 2,.QuoteAllFields 属性消失了,用什么代替它?
- javascript - NodeJS:使用 JIMP 后如何压缩(本地)文件
- javascript - 为什么 JSON.parse 不起作用但类型转换为
做过 - javascript - Angular 6 Typescript new Date 是在 UTC 中创建的,但我需要在我的本地时区
- python - 相同的模型在 Keras 和 Tensorflow 中产生一致的不同精度
- ios - grpc 在 Macbook 中运行 ionic 项目的问题
- mongoose - 在猫鼬模式中,如何一次声明具有相同子属性的多个属性?
- linux - 错误 net.sourceforge.tess4j.Tesseract - 无法加载库“gs”:在资源路径中找不到本机库 (linux-x86-64/libgs.so)
- sql-server - 实体框架错误:SQL Server 升级后“底层提供程序在打开时失败”
- android - “此 API 项目无权使用此 API” 使用 Google-Directions-Android