2

有没有办法获取通过 Microsoft 自定义视觉 model.pb 文件检测到的特定对象的边界框?我知道我们可以通过 API 调用 azure 自定义视觉服务来实现这一点。例如,我们可以从 ssd freeze inference graph.pb 文件中获取边界框,因为存在张量。我们可以对自定义视觉的 model.pb 文件做同样的事情吗?

这是我正在使用打印出 tensorflow 模型和输出的操作的代码。

detection_graph = tf.Graph()

with detection_graph.as_default():
    graph_def = tf.GraphDef()
    with tf.gfile.GFile('model.pb,'rb') as fid:
        serialized_graph = fid.read()
        graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(graph_def, name='')

with tf.Session(graph=detection_graph) as sess:
    ops = tf.get_default_graph().get_operations()
    for op in ops:
        for output in op.outputs:
            print(output.name)


Placeholder:0
layer1_conv/weights:0
layer1_conv/weights/read:0
layer1_conv/Conv2D:0
layer1_conv/biases:0
layer1_conv/biases/read:0
layer1_conv/BiasAdd:0
layer1_leaky/alpha:0
layer1_leaky/mul:0
layer1_leaky:0
pool1:0
layer2_conv/weights:0
layer2_conv/weights/read:0
layer2_conv/Conv2D:0
layer2_conv/biases:0
layer2_conv/biases/read:0
layer2_conv/BiasAdd:0
layer2_leaky/alpha:0
layer2_leaky/mul:0
layer2_leaky:0
pool2:0
layer3_conv/weights:0
layer3_conv/weights/read:0
layer3_conv/Conv2D:0
layer3_conv/biases:0
layer3_conv/biases/read:0
layer3_conv/BiasAdd:0
layer3_leaky/alpha:0
layer3_leaky/mul:0
layer3_leaky:0
pool3:0
layer4_conv/weights:0
layer4_conv/weights/read:0
layer4_conv/Conv2D:0
layer4_conv/biases:0
layer4_conv/biases/read:0
layer4_conv/BiasAdd:0
layer4_leaky/alpha:0
layer4_leaky/mul:0
layer4_leaky:0
pool4:0
layer5_conv/weights:0
layer5_conv/weights/read:0
layer5_conv/Conv2D:0
layer5_conv/biases:0
layer5_conv/biases/read:0
layer5_conv/BiasAdd:0
layer5_leaky/alpha:0
layer5_leaky/mul:0
layer5_leaky:0
pool5:0
layer6_conv/weights:0
layer6_conv/weights/read:0
layer6_conv/Conv2D:0
layer6_conv/biases:0
layer6_conv/biases/read:0
layer6_conv/BiasAdd:0
layer6_leaky/alpha:0
layer6_leaky/mul:0
layer6_leaky:0
pool6:0
layer7_conv/weights:0
layer7_conv/weights/read:0
layer7_conv/Conv2D:0
layer7_conv/biases:0
layer7_conv/biases/read:0
layer7_conv/BiasAdd:0
layer7_leaky/alpha:0
layer7_leaky/mul:0
layer7_leaky:0
layer8_conv/weights:0
layer8_conv/weights/read:0
layer8_conv/Conv2D:0
layer8_conv/biases:0
layer8_conv/biases/read:0
layer8_conv/BiasAdd:0
layer8_leaky/alpha:0
layer8_leaky/mul:0
layer8_leaky:0
m_outputs0/weights:0
m_outputs0/weights/read:0
m_outputs0/Conv2D:0
m_outputs0/biases:0
m_outputs0/biases/read:0
m_outputs0/BiasAdd:0
model_outputs:0

和是输入和输出Placeholder:0model_outputs:0采用Placeholder:0shape 的张量,输出 shape(?,416,416,3)model_outputs:0张量(1, 13, 13, 30)。如果我只检测一个对象,我如何从model_outputs:0张量中获取边界框。

我哪里错了?欢迎任何建议。

4

1 回答 1

1

您似乎正在使用 python,因此您可以从 customvision UI 中导出对象检测模型(选择 tensorflow 选项):

https://docs.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/export-model-python

这将为您提供一个 zipfile,其中包含:

labels.txt
model.pb
python/object_detection.py
python/predict.py

将所有内容放在一个目录中,然后只需执行代码:

python predict.py image.jpg

嘿,快!这将打印出一个字典列表,例如

{'boundingBox': {'width': 0.92610852, 'top': -0.06989955, 'height': 0.85869097, 'left': 0.03279033}, 'tagId': 3, 'tagName': 'myTagName', 'probability': 0.24879535}

坐标(相对于左上角)被标准化为图像的宽度和高度。

这是主要的(不是我的代码!):

def main(image_filename):
    # Load a TensorFlow model
    graph_def = tf.GraphDef()
    with tf.gfile.FastGFile(MODEL_FILENAME, 'rb') as f:
        graph_def.ParseFromString(f.read())

    # Load labels
    with open(LABELS_FILENAME, 'r') as f:
        labels = [l.strip() for l in f.readlines()]

    od_model = TFObjectDetection(graph_def, labels)

    image = Image.open(image_filename)
    predictions = od_model.predict_image(image)
    print(predictions)

您可以根据需要进行修改。祝你好运!

于 2019-02-26T13:32:45.593 回答