0

模型训练后,我有几个项目:

Checkpoint file
model.ckpt.index file
model.ckpt.meta file
model.ckpt file
a graph.pbtxt file.

我使用官方的 freeze_graph.py 将模型冻结到freeze_model.pb

我已将 output_node_names 设置为 InceptionResnetV2/Logits/Predictions 并将输入设置为 -prefix/batch:0。

所以,我使用这个脚本加载冻结图:

import tensorflow as tf
from scipy.misc import imread, imresize
import numpy as np

img = imread("./test.jpg")
img = imresize(img, (299,299,3))
img = img.astype(np.float32)
img = np.expand_dims(img, 0)

labels_dict = {0:'normal', 1:'not'}

#Define the filename of the frozen graph
graph_filename = "./frozen_model.pb"

#Create a graph def object to read the graph
with tf.gfile.GFile(graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())

Construct the graph and import the graph from graphdef
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)

#We define the input and output node we will feed in
input_node = graph.get_tensor_by_name('import/batch:0')
output_node = graph.get_tensor_by_name('import/InceptionResnetV2/Logits/Predictions:0')

with tf.Session() as sess:
    predictions = sess.run(output_node, feed_dict = {input_node: img})
    print predictions
    label_predicted = np.argmax(predictions[0])

print 'Predicted result:', labels_dict[label_predicted]

结果总是得到索引 0 - 这意味着 - 正常,而实际上并非如此。

我做错了什么?当我使用预训练的 inception-resnet-v2 训练和评估数据集时,准确率为 70%

4

1 回答 1

0

首先,您必须对输入图像进行预处理(输入图像范围应在 [-1, 1] 范围内)。在“expand_dims”之前,您可以添加以下行:

img -= 127.5
img /= 127.5

其次,如果您使用您提到的冻结脚本,您的输入层可能如下:

input_node = graph.get_tensor_by_name('import/input:0')

于 2018-10-17T07:14:40.010 回答