模型训练后,我有几个项目:
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%