Tensorflow의 tfrecord 형식으로 바이너리 인스턴스 마스크의 가변 개수 직렬화
MS Coco 2014 데이터 세트의 경우 각 이미지에는 다양한 수의 경계 상자와 주석 파일에 제공된 인스턴스 다각형에서 얻을 수있는 해당 이진 인스턴스 마스크가 있습니다. pycocotools (특히 coco.py 파일)를 사용하여 이것을 달성합니다. 이제 Tensorflow의 tfrecords 형식을 사용하여 이미지 정보를 직렬화하고 싶습니다. 각 이미지 ID로 색인이 지정된 Python dict에 대한 주석을 읽은 후 다음과 같은 경계 상자의 가변 번호를 직렬화 할 수있었습니다.
x_min_values = []
x_max_values = []
y_min_values = []
y_max_values = []
for bb in bounding_boxes:
x_min_values.append(int(bb[0]))
y_min_values.append(int(bb[1]))
x_max_values.append(int(bb[2]))
y_max_values.append(int(bb[3]))
그런 다음 내 기능 사전을에서 사용 tf.train.Example
하기 위해 각 목록을 다음과 같이 int64 기능 목록으로 변환했습니다.
def _int64_feature_list(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
하지만 이제 문제는 인스턴스 마스크가 2 차원이므로 직렬화하는 데 어떤 전략을 사용해야할지 모르겠다는 것입니다. 세분화 마스크 에서처럼 마스크가 하나만있는 경우 배열을 평면화하고 64 비트 기능 목록을 작성한 다음 이미지 높이와 너비를 사용하여 역 직렬화 할 때 배열의 모양을 변경할 수 있지만 이렇게 할 수는 없습니다. 다양한 수의 마스크. 어떤 통찰력이라도 감사합니다.
답변
아래에 설명 된대로 FixedLenSequenceFeature 를 사용해야 합니다.
각각 3 개 및 2 개의 경계 상자가있는 2 개의 이미지 예
bounding_boxes = []
bounding_boxes.append(np.random.randint(low=0, high=2000,size=(3, 4)))
bounding_boxes.append(np.random.randint(low=0, high=2000,size=(2, 4)))
for i, box in enumerate(bounding_boxes):
print({i},box)
산출:
{0} [[1806 1172 1919 1547]
[1478 1654 498 1689]
[131515 1654 1586]]
{1} [[601 1473 1670 756]
[1791 993 1049 1793]]
#Write tfrecord
def _int64_feature(list_of_ints):
return tf.train.Feature(int64_list=tf.train.Int64List(value=list_of_ints))
out_path = './test.tfrec'
with tf.io.TFRecordWriter(out_path) as out:
for box in bounding_boxes:
example = tf.train.Example(features=tf.train.Features(feature={
'boxes': _int64_feature(np.array(box).flatten().tolist()),
}))
out.write(example.SerializeToString())
작성된 tfrecord를 확인하십시오.
ds = tf.data.TFRecordDataset(out_path)
for i, data in enumerate(ds):
process_each = {
'boxes': tf.io.FixedLenSequenceFeature([], dtype=tf.int64, allow_missing=True),
}
samples = tf.io.parse_example(data, process_each)
print(i, samples['boxes'].numpy().reshape(-1, 4))
산출:
0 [[1806 1172 1919 1547]
[1478 1654 498 1689]
[131515 1654 1586]]
1 [[601 1473 1670 756]
[1791 993 1049 1793]]