簡體   English   中英

了解 Sagemaker Object 的 output 檢測預測

[英]understanding the output of Sagemaker Object Detection prediction

我需要幫助了解 Amazon Sagemaker 對象檢測算法的 output。

這是我的基本目標:識別乒乓球何時在比賽中,並在圖像幀中標記它的位置。

來自視頻源的示例圖像:

比賽中沒有球 在此處輸入圖像描述

到目前為止的步驟: 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 的輸入通道所期望的那樣,我使用 function 將注釋分離到訓練和驗證文件夾中。
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 object 檢測器。 我注意到,鑒於我見過的其他示例,這些超參數是“不尋常的”: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 function,並將 model 部署到端點:
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 文檔說期望 output 采用以下格式:

this.json 文件中的每一行都包含一個表示檢測到的 object 的數組。 這些 object arrays 中的每一個都包含六個數字的列表。 第一個數字是預測的 class label。 第二個數字是檢測的相關置信度分數。 最后四個數字代表邊界框坐標 [xmin, ymin, xmax, ymax]。 這些 output 邊界框角索引由整體圖像大小標准化。 請注意,此編碼不同於 input.json 格式使用的編碼。 例如,在檢測結果的第一個條目中,0.3088374733924866 是邊界框的左坐標(左上角的 x 坐標)作為整個圖像寬度的比率,0.07030484080314636 是頂部坐標(y 坐標邊界框的左上角)作為整個圖像高度的比率,0.7110607028007507 是邊界框的右坐標(右下角的 x 坐標)作為整個圖像寬度的比率,0.9345266819000244 是邊界框的底部坐標(右下角的 y 坐標)作為整體圖像高度的比率。

我們來看一張測試圖:

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

測試圖像 thumb0470

它有一個帶有這個邊界框的球 [132,105,19,19](讀作 x-top-left、y-top-left、box-width、box-height)。

鑒於我的對象檢測器經過訓練可以檢測 ONE class (num_classes=1),我預計這張圖片會出現這種 output:

{'預測':[[1.0, 0.71, 0.55, 0.239, 0.629, 0.283]]}

相反,我得到了這個 output:

{'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,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], [0.0, 1.0, 0.0, 0.0, 1.0, 0.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], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 1.0]]}

所以現在的問題是:為什么這個 model 給了我 400 個 JSON 元素,而不是一個?

我目前的假設:這個 object 檢測 model 訓練很弱(很有可能,因為這只是第一次通過,圖像太少),單次檢測器正在識別它認為是“乒乓球”的 400 個實例在圖像中。

但即使我的假設是正確的,為什么 output 重復了這么多? 表格有 178 個相同的“預測”

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

如果被解釋,則意味着:

0.0 - 我沒有定義的 class object “0”。 所以我認為這意味着“沒有球在比賽中”

1.0 - 100% 置信度

0.0 - xmin position 作為寬度的比率 = 0

0.0 - ymin position 作為高度的比率 = 0

1.0 - xmax position 作為寬度的比率 = 240

0.0 - ymax position 作為高度的比率 = 0

坐標 [xmin: 0, ymin: 0, xmax: 240, ymax: 0] 就像在第一個像素上畫一條線。

使用 matplotlib 可視化的預測

謝謝你的幫助!

------- 根據 Ryo 的回答進行編輯 ------

將類別 ID 重新映射到 index-base 0 就像一個魅力。 以下是僅 2,000 個標記圖像的結果:

檢測到乒乓球 1 檢測到乒乓球 2

這是 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)))

雖然 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)

在 Amazon SageMaker 文檔中,他們使用 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

訓練 model 后,您必須檢查每個損失是否減少。

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)

如果您有任何問題,請告訴我們。

來自: https://docs.aws.amazon.com/sagemaker/latest/dg/algo-object-detection-tech-notes.html

Object 檢測算法從已知的 object 類別集合中識別和定位圖像中對象的所有實例

這解釋了為什么您從 predict 獲取響應數組中的 400 個項目。

這里的output數據是錯誤的。 您已將它正確映射到圖像的頂部,但它的高度為 0,因此它基本上不存在。

來自: https://docs.aws.amazon.com/sagemaker/latest/dg/object-detection.html#object-detection-inputoutput

“類別”屬性存儲 class 索引和 class 名稱之間的映射。 class 索引應連續編號,編號應從 0 開始。“類別”屬性對於 annotation.json 文件是可選的

您的類別數組以 class id 1 開頭。

您提供了從標簽工具中獲得的樣本 json,但沒有在文件generated中生成的 json 樣本。 查看 output 的樣本也會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM