[英]How to run predictions on image using a pretrained tensorflow model?
I have adapted this retrain.py script to use with several pretraineds model, after training is done this generates a 'retrained_graph.pb' which I then read and try to use to run predictions on an image using this code:我已经调整了这个retrain.py脚本以与几个预训练 model 一起使用,训练完成后,这会生成一个“retrained_graph.pb”,然后我阅读并尝试使用以下代码对图像运行预测:
def get_top_labels(image_data):
'''
Returns a list of labels and their probabilities
image_data: content of image as string
'''
with tf.compat.v1.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
return predictions
This works fine for inception_v3 model because it has a tensor called 'DecodeJpeg', other models I'm using such as inception_v4, mobilenet and inception_resnet_v2 don't.这适用于 inception_v3 model,因为它有一个名为“DecodeJpeg”的张量,而我使用的其他模型,如 inception_v4、mobilenet 和 inception_resnet_v2 则没有。
My question is can I add an ops to the graph, like the one used in add_jpeg_decoding
in the retrain.py script so that I can afterwards use that for prediction?我的问题是我可以在图表中添加一个操作,就像 retrain.py 脚本中add_jpeg_decoding
中使用的那样,以便之后我可以使用它进行预测?
Would it be possible to do something like this: predictions = sess.run(softmax_tensor, {image_data_tensor: image_data})
where image_data_tensor
is a variable that depends on what model I'm using?是否有可能做这样的事情: predictions = sess.run(softmax_tensor, {image_data_tensor: image_data})
其中image_data_tensor
是一个取决于我使用的 model 的变量?
I looked through stackoverflow and couldn't find a question that solves my problem, I'd really appreciate any help with this, thanks.我查看了stackoverflow,找不到解决我问题的问题,我非常感谢任何帮助,谢谢。 I need to at least know if it's possible.我至少需要知道这是否可能。 Sorry for repost I got no views on my first one.很抱歉重新发布我对我的第一个没有意见。
So after some research, I figured out a way, leaving an answer here in case someone needs it.所以经过一番研究,我想出了一个办法,在这里留下一个答案,以防有人需要它。 What you need to do is do the decoding yourself get a tensor from the image using t = read_tensor_from_image_file
found here , then you can run your predictions using this piece of code:您需要做的是自己进行解码,使用t = read_tensor_from_image_file
found here从图像中获取张量,然后您可以使用以下代码运行您的预测:
start = time.time()
results = sess.run(output_layer_name,
{input_layer_name: t})
end = time.time()
return results
usually input_layer_name = input:0
and output_layer_name = final_result:0
.通常input_layer_name = input:0
和output_layer_name = final_result:0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.