我已经使用tensorflow成功训练并保存了我的模型(图像分类器),所以现在我有.meta,索引和检查点文件。
我想为我的模型提供一个图像以供测试,所以我创建了另一个.py文件并恢复了我的模型:
with tf.Session() as sess:
saver = tf.train.import_meta_graph('model-24900.meta')
saver.restore(sess,"model-24900")
graph = tf.get_default_graph()
之后我尝试提供图像:
Prediction=sess.run([output],feed_dict={input_img : testImage,})
'输出'和input_img在另一个文件中定义(我已经构建并训练了模型,因此它们在我想要测试模型的文件中未定义。
这是我在火车档案中写的:
with tf.name_scope("Input") as scope:
input_img = tf.placeholder(dtype='float', shape=[None, 128, 128, 1], name="input")
with tf.name_scope("Target") as scope:
target_labels = tf.placeholder(dtype='float', shape=[None, 2], name="Targets")
nb = NetworkBuilder()
with tf.name_scope("ModelV2") as scope:
model = input_img
model = nb.attach_conv_layer(model, 32)
model = nb.attach_relu_layer(model)
model = nb.attach_conv_layer(model, 32)
model = nb.attach_relu_layer(model)
model = nb.attach_pooling_layer(model)
model = nb.attach_conv_layer(model, 64)
model = nb.attach_relu_layer(model)
model = nb.attach_conv_layer(model, 64)
model = nb.attach_relu_layer(model)
model = nb.attach_pooling_layer(model)
model = nb.attach_conv_layer(model, 128)
model = nb.attach_relu_layer(model)
model = nb.attach_conv_layer(model, 128)
model = nb.attach_relu_layer(model)
model = nb.attach_pooling_layer(model)
model = nb.flatten(model)
model = nb.attach_dense_layer(model, 200)
model = nb.attach_sigmoid_layer(model)
model = nb.attach_dense_layer(model, 32)
model = nb.attach_sigmoid_layer(model)
model = nb.attach_dense_layer(model, 2)
output = nb.attach_softmax_layer(model)
with tf.name_scope("Optimization") as scope:
global_step = tf.Variable(0, name='global_step', trainable=False)
cost = tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=target_labels)
cost = tf.reduce_mean(cost)
tf.summary.scalar("cost", cost)
optimizer = tf.train.AdamOptimizer().minimize(cost,global_step=global_step)
with tf.name_scope('accuracy') as scope:
correct_pred = tf.equal(tf.argmax(output, 1), tf.argmax(target_labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
所以我的问题是如何定义'输出'和' input_img' (我已经在火车文件中使用过)我的测试文件,所以我可以将图像提供给我的CNN模型
答案 0 :(得分:0)
1] 按.ckpt
文件进行测试并重新创建整个模型:
.py
文件中执行的输入张量和输出张量重新定义整个模型 2] 按.ckpt
文件进行测试并使用张量名称
.py
文件期间,您可以轻松获得在培训期间明确指定的.get_tensor_by_name("example:0")
函数的输入和输出张量sess.run
中使用这些张量,将数据输入到输入张量并获得预测 3] 按.pb
冻结图测试并使用张量名称
.pb
文件大.pb
冻结文件是一个无法训练的冻结图.get_tensor_by_name("example:0")
函数获取输入和输出张量并进行预测<强> =========================修订================== ======= 强>
有两种方法可以知道张量的名称:
1] 使用Tensor-Board:
2] 在代码中:
您可以这样做:
for node in graph_def.node:
print(node.name)
如果您想要特定张量的名称,您可以这样做:
x = tf.placeholder(tf.float32, [None, 784])
print(x.name)
.get_tensor_by_name(x.name)