Python 初始Resnet V2模型冻结
我使用InceptionResNet v2模型来训练一个使用转移学习的图像分类模型。我的模型很好用。问题在于冻结模型。 目前,我有: model.ckpt.meta model.ckpt.index model.ckpt 我使用教程通过将输出节点名称设置为InceptionResnetV2/Logits/Predictions来冻结模型,并且模型生成正确。我现在有一个名为model.pb的新文件 用于生成以冻结模型的代码:Python 初始Resnet V2模型冻结,python,tensorflow,Python,Tensorflow,我使用InceptionResNet v2模型来训练一个使用转移学习的图像分类模型。我的模型很好用。问题在于冻结模型。 目前,我有: model.ckpt.meta model.ckpt.index model.ckpt 我使用教程通过将输出节点名称设置为InceptionResnetV2/Logits/Predictions来冻结模型,并且模型生成正确。我现在有一个名为model.pb的新文件 用于生成以冻结模型的代码: import os import tensorflow as tf f
import os
import tensorflow as tf
from tensorflow.python.framework import graph_util
dir = os.path.dirname(os.path.realpath(__file__))
def freeze_graph(model_folder, output_node_names):
# We retrieve our checkpoint fullpath
checkpoint = tf.train.get_checkpoint_state(model_folder)
input_checkpoint = checkpoint.model_checkpoint_path
# We precise the file fullname of our freezed graph
absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
output_graph = absolute_model_folder + "/frozen_model.pb"
# Before exporting our graph, we need to precise what is our output node
# This is how TF decides what part of the Graph he has to keep and what part it can dump
# NOTE: this variable is plural, because you can have multiple output nodes
# output_node_names = "Accuracy/predictions"
# We clear devices to allow TensorFlow to control on which device it will load operations
clear_devices = True
# We import the meta graph and retrieve a Saver
saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)
# We retrieve the protobuf graph definition
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
# We start a session and restore the graph weights
with tf.Session() as sess:
saver.restore(sess, input_checkpoint)
# We use a built-in TF helper to export variables to constants
output_graph_def = graph_util.convert_variables_to_constants(
sess, # The session is used to retrieve the weights
input_graph_def, # The graph_def is used to retrieve the nodes
output_node_names.split(",") # The output node names are used to select the usefull nodes
)
# Finally we serialize and dump the output graph to the filesystem
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(output_graph_def.SerializeToString())
print("%d ops in the final graph." % len(output_graph_def.node))
当我想为这个模型提供输入时,问题就来了
首先,我使用以下方法加载模型图:
def load_graph(frozen_graph_filename):
# We load the protobuf file from the disk and parse it to retrieve the
# unserialized graph_def
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Then, we can use again a convenient built-in function to import a graph_def into the
# current default Graph
with tf.Graph().as_default() as graph:
tf.import_graph_def(
graph_def,
input_map=None,
return_elements=None,
name="prefix",
op_dict=None,
producer_op_list=None
)
return graph
然后,当我浏览图形ops时,我没有找到输入占位符
for op in graph.get_operations():
print(op.name)
第一个输入显示为:
前缀/批处理/先进先出队列
前缀/批处理/n
前缀/批次
前缀/接收resnetv2/Conv2d_1a_3x3/权重
前缀/接收resnetv2/Conv2d_1a_3x3/权重/读取
前缀/接收resnetv2/Conv2d_1a_3x3/卷积
前缀/接收resnetv2/Conv2d_1a_3x3/BatchNorm/beta
前缀/接收resnetv2/Conv2d_1a_3x3/BatchNorm/beta/read
前缀/接收resnetv2/Conv2d_1a_3x3/BatchNorm/矩/平均值/缩减指数
.
.
.
前缀/接收resnetv2/登录/预测
当我使用以下命令馈送图像时出现的错误:
img_path = 'img.jpg'
img_data = imread(img_path)
img_data = imresize(img_data, (299, 299, 3))
img_data = img_data.astype(np.float32)
img_data = np.expand_dims(img_data, 0)
# print('Starting Session, setting the GPU memory usage to %f' % args.gpu_memory)
# gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory)
# sess_config = tf.ConfigProto(gpu_options=gpu_options)
persistent_sess = tf.Session(graph=graph) # , config=sess_config)
input_node = graph.get_tensor_by_name('prefix/batch/fifo_queue:0')
output_node = graph.get_tensor_by_name('prefix/InceptionResnetV2/Logits/Predictions:0')
predictions = persistent_sess.run(output_node, feed_dict={input_node: [img_data]})
print(predictions)
label_predicted = np.argmax(predictions[0])
print(label_predicted)
错误:
File /ImageClassification_TransferLearning System/ModelTraining/model/model_frezzing.py", line 96, in <module>
predictions = persistent_sess.run(output_node, feed_dict={input_node: [img_data]})
File "\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1078, in _run
subfeed_dtype = subfeed_t.dtype.as_numpy_dtype
File "\Anaconda3\lib\site-packages\tensorflow\python\framework\dtypes.py", line 122, in as_numpy_dtype
return _TF_TO_NP[self._type_enum]
KeyError: 20
我发现了问题!!
我必须从名为:prefix/batch:0的输入op中输入模型
input_node = graph.get_tensor_by_name('prefix/batch:0')