如何使用预训练的张量流模型对图像进行预测?

我已将此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并没有找到解决我的问题的问题,非常感谢您的帮助。 我至少需要知道是否有可能。 抱歉,我的第一个帖子没有发表意见。

dwtmwyc 回答:如何使用预训练的张量流模型对图像进行预测?

因此,在进行了一些研究之后,我想出了一种方法,在这里留下答案,以防有人需要。您需要做的是使用here找到的t = read_tensor_from_image_file进行解码,以从图像中得到张量,然后可以使用以下代码运行预测:

        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

本文链接:https://www.f2er.com/3138615.html

大家都在问