简体   繁体   English

如何使用预训练的 tensorflow model 对图像进行预测?

[英]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:0output_layer_name = final_result:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于训练有素的Tensorflow模型进行预测? - How to make predictions based on a trained Tensorflow Model? 如何使用烤宽面条继续训练预训练模型 - how to continue training of a pretrained model using lasagne 使用元图张量流无法运行模型 - Failed to run the model using metagraph tensorflow Tensorflow,Keras预训练的MobileNetV2模型未下载 - Tensorflow, Keras pretrained MobileNetV2 Model doesn't download 在TensorFlow中访问预训练模型的权重(名称和值) - Accessing a pretrained model's weights (names and values) in TensorFlow 在Keras和Tensorflow中使用LSTM进行时间序列预测 - Using LSTM in Keras and tensorflow for time series predictions 如何使用 Tensorflow 模型进行预测? - How to forecast using the Tensorflow model? 如何使用 GPU 运行 tensorflow? - How to run tensorflow using GPU? 在Google Cloud上远程运行tensorflow,然后获取准确性/预测 - Run tensorflow remotely on google cloud and then get back accuracy/predictions 在Tensorflow对象检测中评估预训练模型时出错(tensorflow.python.framework.errors_impl.NotFoundError :) - Error when evaluating pretrained model in Tensorflow object-detection (tensorflow.python.framework.errors_impl.NotFoundError:)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM