我需要帮助来理解Amazon Sagemaker对象检测算法的输出结果。
我的最终目标是:识别乒乓球在比赛中的位置,并在图像帧中标记其位置。
来自视频源的样本图像:
到目前为止的步骤:1. 我从一场乒乓球比赛中提取了n个视频帧。
-
我使用RectLabel手动标注了乒乓球的位置。
-
使用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}]}
- 我使用了一个函数将注释分成训练和验证文件夹,符合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)))
- 我将文件移动到Amazon S3桶中,按SageMaker的要求设置了四个通道(文件夹):/train, /validation, /train_annotation, 和 /validation_annotation。
num_annotated_files = len(jsons)train_split_pct = 0.70num_train_jsons = int(num_annotated_files * train_split_pct)random.shuffle(jsons) # 随机/打乱JSON文件以减少对*顺序*帧的依赖train_jsons = jsons[:num_train_jsons]val_jsons = jsons[num_train_jsons:]# 将训练文件移动到训练文件夹中for i in train_jsons: image_file = './images/'+i.split('.')[0]+'.png' shutil.move(image_file, './train/') shutil.move('./generated/'+i, './train_annotation/')# 将验证文件移动到验证文件夹中for i in val_jsons: image_file = './images/'+i.split('.')[0]+'.png' shutil.move(image_file, './validation/') shutil.move('./generated/'+i, './validation_annotation/')### 上传到S3import sagemakerfrom sagemaker import get_execution_rolerole = sagemaker.get_execution_role()sess = sagemaker.Session()from sagemaker.amazon.amazon_estimator import get_image_uritraining_image = get_image_uri(sess.boto_region_name, 'object-detection', repo_version="latest")bucket = 'pp-balls-object-detection' # 自定义桶名称# 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)
- 我创建了一个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)
- 我设置了对象检测器的训练/验证位置,调用了.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')
- 我通过传递一个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)
- 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坐标,左上角y坐标,框宽度,框高度)。
鉴于我的对象检测器被训练检测一个类别(num_classes=1),我期望对于这张图像得到这样的输出:
{‘prediction’: [[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.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.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.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个“乒乓球”实例。
但即使我的假设是正确的,为什么输出会重复这么多?有178个相同的’predictions’形式如下
[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重新映射到索引基数0非常有效。以下是仅使用2000个标记图像的结果:
以下是基于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 jsonimport loggingdef 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