【问题标题】:understanding the output of Sagemaker Object Detection prediction了解 Sagemaker 对象检测预测的输出
【发布时间】:2020-02-23 19:04:18
【问题描述】:

我需要帮助来了解 Amazon Sagemaker 对象检测算法的输出。

这是我的基本目标:识别乒乓球何时在比赛中,并在图像框架中标记它的位置。

来自视频源的示例图像:

到目前为止的步骤: 1. 我从乒乓球比赛中获取了 n 个视频帧。

  1. 我使用 RectLabel 手动标注了乒乓球的位置。

  2. 使用 RectLabel,我将这些标签转换为 JSON 文件。此处示例:

{"images":[
    {"id":1,"file_name":"thumb0462.png","width":0,"height":0},
    {"id":2,"file_name":"thumb0463.png","width":0,"height":0},
    {"id":3,"file_name":"thumb0464.png","width":0,"height":0},
    ...
    {"id":4582,"file_name":"thumb6492.png","width":0,"height":0}],
"annotations":[
    {"area":198,"iscrowd":0,"id":1,"image_id":5,"category_id":1,"segmentation":[[59,152,76,152,76,142,59,142]],"bbox":[59,142,18,11]},
    {"area":221,"iscrowd":0,"id":2,"image_id":6,"category_id":1,"segmentation":[[83,155,99,155,99,143,83,143]],"bbox":[83,143,17,13]},
    {"area":399,"iscrowd":0,"id":3,"image_id":8,"category_id":1,"segmentation":[[118,144,136,144,136,124,118,124]],"bbox":[118,124,19,21]},
    {"area":361,"iscrowd":0,"id":4,"image_id":9,"category_id":1,"segmentation":[[132,123,150,123,150,105,132,105]],"bbox":[132,105,19,19]},
    ...
"categories":[{"name":"pp_ball","id":1}]
}
  1. 正如 SageMaker 的输入通道所期望的那样,我使用了一个函数将注释分离到训练和验证文件夹中。
file_name = './pp-ball-annotations.json'
with open(file_name) as f:
    js = json.load(f)
    images = js['images']
    categories = js['categories']
    annotations = js['annotations']
    for i in images:
        jsonFile = i['file_name']
        jsonFile = jsonFile.split('.')[0] + '.json'

        line = {}
        line['file'] = i['file_name']
        line['image_size'] = [{
            'width': int(i['width']),
            'height': int(i['height']),
            'depth': 3
        }]
        line['annotations'] = []
        line['categories'] = []
        for j in annotations:
            if j['image_id'] == i['id'] and len(j['bbox']) > 0:
                line['annotations'].append({
                    'class_id': int(j['category_id']),
                    'top': int(j['bbox'][1]),
                    'left': int(j['bbox'][0]),
                    'width': int(j['bbox'][2]),
                    'height': int(j['bbox'][3])
                })
                class_name = ''
                for k in categories:
                    if int(j['category_id']) == k['id']:
                        class_name = str(k['name'])
                assert class_name is not ''
                line['categories'].append({
                    'class_id': int(j['category_id']),
                    'name': class_name
                })
        if line['annotations']:
            with open(os.path.join('generated', jsonFile), 'w') as p:
                json.dump(line, p)

jsons = os.listdir('generated')
print ('There are {} images that have annotation files'.format(len(jsons)))
  1. 我根据 SageMaker 的要求将文件移动到了具有四个通道(文件夹)的 Amazon S3 存储桶中:/train、/validation、/train_annotation 和 /validation_annotation。
num_annotated_files = len(jsons)
train_split_pct = 0.70
num_train_jsons = int(num_annotated_files * train_split_pct)
random.shuffle(jsons) # randomize/shuffle the JSONs to reduce reliance on *sequenced* frames
train_jsons = jsons[:num_train_jsons]
val_jsons = jsons[num_train_jsons:]

#Moving training files to the training folders
for i in train_jsons:
    image_file = './images/'+i.split('.')[0]+'.png'
    shutil.move(image_file, './train/')
    shutil.move('./generated/'+i, './train_annotation/')

#Moving validation files to the validation folders
for i in val_jsons:
    image_file = './images/'+i.split('.')[0]+'.png'
    shutil.move(image_file, './validation/')
    shutil.move('./generated/'+i, './validation_annotation/')


### Upload to S3
import sagemaker
from sagemaker import get_execution_role

role = sagemaker.get_execution_role()
sess = sagemaker.Session()

from sagemaker.amazon.amazon_estimator import get_image_uri
training_image = get_image_uri(sess.boto_region_name, 'object-detection', repo_version="latest")

bucket = 'pp-balls-object-detection' # custom bucket name.
# bucket = sess.default_bucket()
prefix = 'rect-label-test'

train_channel = prefix + '/train'
validation_channel = prefix + '/validation'
train_annotation_channel = prefix + '/train_annotation'
validation_annotation_channel = prefix + '/validation_annotation'

sess.upload_data(path='train', bucket=bucket, key_prefix=train_channel)
sess.upload_data(path='validation', bucket=bucket, key_prefix=validation_channel)
sess.upload_data(path='train_annotation', bucket=bucket, key_prefix=train_annotation_channel)
sess.upload_data(path='validation_annotation', bucket=bucket, key_prefix=validation_annotation_channel)

s3_train_data = 's3://{}/{}'.format(bucket, train_channel)
s3_validation_data = 's3://{}/{}'.format(bucket, validation_channel)
s3_train_annotation = 's3://{}/{}'.format(bucket, train_annotation_channel)
s3_validation_annotation = 's3://{}/{}'.format(bucket, validation_annotation_channel)
  1. 使用某些超参数创建了 SageMaker 对象检测器。我注意到,鉴于我见过的其他示例,这些超参数是“不寻常的”:num_classes = 1、use_pretrained_model=0 和 image_shape = 438。
s3_output_location = 's3://{}/{}/output'.format(bucket, prefix)

od_model = sagemaker.estimator.Estimator(training_image,
                                         role,
                                         train_instance_count=1,
                                         train_instance_type='ml.p3.2xlarge',
                                         train_volume_size = 50,
                                         train_max_run = 360000,
                                         input_mode = 'File',
                                         output_path=s3_output_location,
                                         sagemaker_session=sess)

od_model.set_hyperparameters(base_network='resnet-50',
                             use_pretrained_model=0,
                             num_classes=1,
                             mini_batch_size=15,
                             epochs=30,
                             learning_rate=0.001,
                             lr_scheduler_step='10',
                             lr_scheduler_factor=0.1,
                             optimizer='sgd',
                             momentum=0.9,
                             weight_decay=0.0005,
                             overlap_threshold=0.5,
                             nms_threshold=0.45,
                             image_shape=438,
                             label_width=600,
                             num_training_samples=num_train_jsons)
  1. 我为对象检测器设置了训练/验证位置,称为 .fit 函数,并将模型部署到端点:
train_data = sagemaker.session.s3_input(s3_train_data, distribution='FullyReplicated',
                        content_type='image/png', s3_data_type='S3Prefix')
validation_data = sagemaker.session.s3_input(s3_validation_data, distribution='FullyReplicated',
                             content_type='image/png', s3_data_type='S3Prefix')
train_annotation = sagemaker.session.s3_input(s3_train_annotation, distribution='FullyReplicated',
                             content_type='image/png', s3_data_type='S3Prefix')
validation_annotation = sagemaker.session.s3_input(s3_validation_annotation, distribution='FullyReplicated',
                             content_type='image/png', s3_data_type='S3Prefix')

data_channels = {'train': train_data, 'validation': validation_data,
                 'train_annotation': train_annotation, 'validation_annotation':validation_annotation}

od_model.fit(inputs=data_channels, logs=True)

object_detector = od_model.deploy(initial_instance_count = 1,
                             instance_type = 'ml.m4.xlarge')
  1. 我通过向端点传递一个以字节为单位的 PNG 文件来调用端点:
file_with_path = 'test/thumb0695.png'
with open(file_with_path, 'rb') as image:
            f = image.read()
            b = bytearray(f)
            ne = open('n.txt', 'wb')
            ne.write(b)

        results = object_detector.predict(b)
        detections = json.loads(results)
        print(detections)
  1. AWS Sagemaker 文档说期望输出格式如下:

此 .json 文件中的每一行都包含一个表示检测到的对象的数组。这些对象数组中的每一个都包含六个数字的列表。第一个数字是预测的类标签。第二个数字是检测的相关置信度分数。最后四个数字代表边界框坐标 [xmin, ymin, xmax, ymax]。这些输出边界框角索引通过整体图像大小进行归一化。请注意,此编码不同于输入 .json 格式使用的编码。例如,在检测结果的第一个条目中,0.3088374733924866 是边界框的左坐标(左上角的 x 坐标)作为整个图像宽度的比率,0.07030484080314636 是顶部坐标(y 坐标边界框的左上角)作为整个图像高度的比率,0.7110607028007507 是边界框的右坐标(右下角的 x 坐标)作为整个图像宽度的比率,0.9345266819000244 是边界框的底部坐标(右下角的 y 坐标)作为整体图像高度的比率。

我们来看一张测试图:

{"id":9,"file_name":"thumb0470.png","width":438,"height":240}

它有一个带有这个边界框 [132,105,19,19] 的球(读作 x-top-left、y-top-left、box-width、box-height)。

鉴于我的对象检测器经过训练可以检测 ONE 类 (num_classes=1),我希望这张图片会出现这种输出:

{'预测': [[1.0, 0.71, 0.55, 0.239, 0.629, 0.283]]}

相反,我得到了这个输出:

{'prediction': [[0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1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0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0]]}

那么现在的问题:为什么这个模型给了我 400 个 JSON 元素,而不是一个?

我目前的假设:这个物体检测模型训练很弱(很有可能,因为这只是第一次通过,图像太少),单次检测器正在识别它认为的 400 个“ping”实例乒乓球”在图像中。

但是即使我的假设是正确的,为什么输出会重复这么多呢?有 178 个相同的“预测”形式

[0.0, 1.0, 0.0, 0.0, 1.0, 0.0]

如果被解释,则意味着:

0.0 - 我没有定义的类对象“0”。所以我认为这意味着“没有球在比赛中”

1.0 - 100% 置信度

0.0 - xmin 位置与宽度的比率 = 0

0.0 - ymin 位置与高度的比率 = 0

1.0 - xmax 位置与宽度的比率 = 240

0.0 - ymax 位置与高度的比率 = 0

坐标 [xmin: 0, ymin: 0, xmax: 240, ymax: 0] 就像在第一个像素上画一条线。

感谢您的帮助!

------- 根据 Ryo 的回答编辑 ------

将类别 ID 重新映射到 index-base 0 就像一个魅力。以下是仅 2,000 个标记图像的结果:

这是 Ryo 的有用回答后的代码:

def fixCategoryId(category_id):
    return category_id - 1;

with open(file_name) as f:
    js = json.load(f)
    images = js['images']
    categories = js['categories']
    annotations = js['annotations']
    for i in images:
        jsonFile = i['file_name']
        jsonFile = jsonFile.split('.')[0] + '.json'

        line = {}
        line['file'] = i['file_name']
        line['image_size'] = [{
            'width': int(i['width']),
            'height': int(i['height']),
            'depth': 3
        }]
        line['annotations'] = []
        line['categories'] = []
        for j in annotations:
            if j['image_id'] == i['id'] and len(j['bbox']) > 0:
                line['annotations'].append({
                    'class_id': fixCategoryId(int(j['category_id'])),
                    'top': int(j['bbox'][1]),
                    'left': int(j['bbox'][0]),
                    'width': int(j['bbox'][2]),
                    'height': int(j['bbox'][3])
                })
                class_name = ''
                for k in categories:
                    if int(j['category_id']) == k['id']:
                        class_name = str(k['name'])
                assert class_name is not ''
                line['categories'].append({
                    'class_id': fixCategoryId(int(j['category_id'])),
                    'name': class_name
                })
        if line['annotations']:
            with open(os.path.join('generated', jsonFile), 'w') as p:
                json.dump(line, p)

jsons = os.listdir('generated')
print ('There are {} images that have annotation files'.format(len(jsons)))

【问题讨论】:

    标签: python machine-learning computer-vision object-detection amazon-sagemaker


    【解决方案1】:

    虽然 COCO JSON 文件中的“category_id”从 1 开始,但 Amazon SageMaker JSON 文件中的“class_id”从 0 开始。

    你的转化码应该是这样的。

    def fixCategoryId(category_id):
        return category_id - 1;
    
    with open(coco_json_path) as f:
        js = json.load(f)
        images = js['images']
        categories = js['categories']
        annotations = js['annotations']
        for i in images:
            jsonFile = i['file_name']
            jsonFile = jsonFile.split('.')[0] + '.json'
    
            line = {}
            line['file'] = i['file_name']
            line['image_size'] = [{
                'width': int(i['width']),
                'height': int(i['height']),
                'depth': 3
            }]
            line['annotations'] = []
            line['categories'] = []
            for j in annotations:
                if j['image_id'] == i['id'] and len(j['bbox']) > 0:
                    line['annotations'].append({
                        'class_id': fixCategoryId(int(j['category_id'])),
                        'top': int(j['bbox'][1]),
                        'left': int(j['bbox'][0]),
                        'width': int(j['bbox'][2]),
                        'height': int(j['bbox'][3])
                    })
                    class_name = ''
                    for k in categories:
                        if int(j['category_id']) == k['id']:
                            class_name = str(k['name'])
                    assert class_name is not ''
                    line['categories'].append({
                        'class_id': fixCategoryId(int(j['category_id'])),
                        'name': class_name
                    })
            if line['annotations']:
                with open(os.path.join(sagemaker_json_path, jsonFile), 'w') as p:
                    json.dump(line, p)
    

    In the Amazon SageMaker doc,他们正在使用 get_coco_mapper() 执行此操作。

    import json
    import logging
    
    def get_coco_mapper():
        original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20,
                        21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                        41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
                        61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80,
                        81, 82, 84, 85, 86, 87, 88, 89, 90]
        iter_counter = 0
        COCO = {}
        for orig in original_list:
            COCO[orig] = iter_counter
            iter_counter += 1
        return COCO
    

    训练完模型后,你必须检查每个损失是否减少了。

    od_model.fit(inputs=data_channels, logs=True)
    
    [11/04/2019 09:26:46 INFO 140651482974016] #quality_metric: host=algo-1, epoch=499, batch=11 train cross_entropy <loss>=(0.20304460724736212)
    [11/04/2019 09:26:46 INFO 140651482974016] #quality_metric: host=algo-1, epoch=499, batch=11 train smooth_l1 <loss>=(0.06970448779799958)
    

    如果您有任何问题,请告诉我们。

    【讨论】:

    • 出色的观察和非常感谢代码 sn-p @RyoKawamura。工作就像一个魅力:) 我将添加来自 2,000 个标记图像的结果
    【解决方案2】:

    发件人:https://docs.aws.amazon.com/sagemaker/latest/dg/algo-object-detection-tech-notes.html

    对象检测算法识别并定位所有实例 来自已知对象类别集合的图像中的对象

    这解释了为什么您从 predict 获取响应数组中的 400 个项目。

    这里的输出数据是错误的。您已将其正确映射到图像的顶部,但它的高度为 0,因此它基本上无处。

    发件人:https://docs.aws.amazon.com/sagemaker/latest/dg/object-detection.html#object-detection-inputoutput

    “categories”属性存储类索引之间的映射 和班级名称。类索引应连续编号,并且 编号应该从 0 开始。“类别”属性是 注释 .json 文件可选

    您的类别数组以类 id 1 开头。

    您提供了从标签工具中获得的示例 json,但在文件 generated 中没有您生成的 json 示例。查看该输出的示例也会很有帮助。

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2021-06-30
      • 2019-06-09
      • 2016-02-05
      • 1970-01-01
      • 2018-10-21
      • 2021-10-06
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多