有没有办法从Microsoft的自定义视觉对象检测model.pb文件中获取边界框? [英] Is there a way to get bounding boxes from the Microsoft's custom vision object detection model.pb file?

查看:89
本文介绍了有没有办法从Microsoft的自定义视觉对象检测model.pb文件中获取边界框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Microsoft自定义视觉model.pb文件获取检测到的特定对象的边界框?我知道我们可以通过对天蓝色的自定义视觉服务的API调用来实现。
例如,由于存在张量,因此我们可以从ssd冻结推断graph.pb文件中获取边界框。我们可以对自定义视觉的model.pb文件做同样的事情吗?

Is there a way to get bounding boxes of a particular object detected via Microsoft custom vision model.pb file? I know we can get that via API calls to the azure custom vision service. Say for example, we can get the bounding boxes from the ssd frozen inference graph.pb file as there are tensors present. Can we do the same for custom vision's model.pb file?

这是我正在使用的代码,用于输出张量流模型和输出。

This is the code that I am using the print out the operations for a tensorflow model and the output.

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

占位符:0 model_outputs:0 是输入, e输出。 占位符:0 的张量形状为(?, 416,416,3) model_outputs:0 输出形状为(1、13、13、30)的张量。如果我仅检测到一个对象,如何从 model_outputs:0 张量获取边界框。

The Placeholder:0 and model_outputs:0 are the inputs and the outputs. The Placeholder:0 takes a tensor of shape (?,416,416,3) and the model_outputs:0 outputs a tensor of shape (1, 13, 13, 30). If I am detecting just a single object, how do I get the bounding boxes from the model_outputs:0 tensor.

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

Where am I going wrong? Any suggestions are welcome.

推荐答案

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

You seem to be using python, so you can export the object-detection model from the customvision UI (select tensorflow options):

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

这将为您提供一个包含以下内容的zipfile:

which will give you a zipfile containing:

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

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

Put everything in one directory then simply execute the code:

python predict.py image.jpg

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

Hey presto! This will print out a list of dictionaries like

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

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

The coordinates (relative to top left) are normalized to the width and height of the image.

main(不是我的代码!):

Here is main (not my code!):

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)

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

which you can modify as you see fit. Good luck!

这篇关于有没有办法从Microsoft的自定义视觉对象检测model.pb文件中获取边界框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆