使用tensorflow进行目标检测的预训练faster_rcnn

时间:2020-03-06 16:34:25

标签: python tensorflow

我想使用张量流的预训练模型进行对象分类。

我从tensorflow下载了faster_rcnn_inception_resnet_v2_atrous_lowproposals_oid_2018_01_28,但是在进行预测时遇到了一些麻烦。这是我运行的代码。

import tensorflow as tf
import cv2

model_folder = "faster_rcnn_inception_resnet_v2_atrous_lowproposals_oid_2018_01_28"

model_graph_file = model_folder+"/frozen_inference_graph.pb"
model_weights_file = model_folder+"/model.ckpt.data-00000-of-00001"

graph_def = tf.GraphDef()
graph_def.ParseFromString(tf.gfile.Open(model_graph_file,'rb').read())

with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def=graph_def, name="")

#for op in graph.get_operations():
#    print(op.name)

print([n.name + '=>' +  n.op for n in graph_def.node if n.op in ('Placeholder')])

input = graph.get_tensor_by_name('image_tensor:0')
classes = graph.get_tensor_by_name('detection_classes:0')
scores = graph.get_tensor_by_name('detection_scores:0')
boxes = graph.get_tensor_by_name('detection_boxes:0')

pandas_image = cv2.imread('resources/some_image.jpg')
print(pandas_image.shape)

with tf.Session(graph=graph) as sess:
    classes_out,scores_out,boxes_out  = sess.run([classes,scores,boxes],feed_dict={input:pandas_image})
    print(classes_out)
    print(scores_out)
    print(boxes_out)

但是我在sess.run()上遇到以下错误:

ValueError: Cannot feed value of shape (980, 1680, 3) for Tensor 'image_tensor:0', which has shape '(?, ?, ?, 3)'

我想我必须调整输入图像的大小以匹配输入层的大小。正确的方法是什么?我怎么知道输入层的大小?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

由于错误表明您传递给模型的张量的形状是错误的。
通常,模型期望图像的形状为(batch_size,224,244,3),但是该224可能会因模型而异,我建议您参考文档/示例代码来确定该大小。