【问题标题】:How to run predictions on image using a pretrained tensorflow model?如何使用预训练的张量流模型对图像进行预测?
【发布时间】:2019-11-08 10:38:27
【问题描述】:

我已经调整了这个retrain.py 脚本以与几个预训练模型一起使用, 训练完成后,这会生成一个“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

这适用于 inception_v3 模型,因为它有一个名为“DecodeJpeg”的张量,而我使用的其他模型,例如 inception_v4、mobilenet 和 inception_resnet_v2 则没有。

我的问题是我可以在图表中添加一个操作,就像在 retrain.py 脚本中的 add_jpeg_decoding 中使用的那样,以便我之后可以将其用于预测?

有没有可能做这样的事情: predictions = sess.run(softmax_tensor, {image_data_tensor: image_data}) 其中image_data_tensor 是一个变量,取决于我使用的模型?

我查看了 stackoverflow 并找不到可以解决我的问题的问题,非常感谢任何帮助,谢谢。 我至少需要知道这是否可能。 很抱歉重新发布我对我的第一个没有意见。

【问题讨论】:

    标签: python-3.x tensorflow pre-trained-model


    【解决方案1】:

    所以经过一番研究,我想出了一个办法,在这里留下一个答案,以防有人需要。你需要做的是自己解码,使用t = read_tensor_from_image_file找到here从图像中获取张量,然后你可以使用这段代码运行你的预测:

            start = time.time()
            results = sess.run(output_layer_name,
                               {input_layer_name: t})
            end = time.time()
            return results
    

    通常是input_layer_name = input:0output_layer_name = final_result:0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2020-02-29
      相关资源
      最近更新 更多