Merge pull request #2 from pj-ms/pinjin/index-from-zero

make index for img, class or annotation can start from zero
This commit is contained in:
TK 2021-09-23 17:15:07 -07:00 коммит произвёл GitHub
Родитель 6756f5f0d4 158bd558ae
Коммит b34b3bbc7c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 37 добавлений и 5 удалений

Просмотреть файл

@ -75,6 +75,8 @@ Here is an example with explanation of what a `DatasetInfo` looks like for coco
##### Coco JSON - Image classification
Here is one example of the train.json, val.json, or test.json in the `DatasetInfo` above. Note that the `"id"` for `images`, `annotations` and `categories` should be consecutive integers, **starting from 1**. Note that our lib might work with id starting from 0, but many tools like [CVAT](https://github.com/openvinotoolkit/cvat/issues/2085) and official [COCOAPI](https://github.com/cocodataset/cocoapi/issues/507) will fail.
``` {json}
{
"images": [{"id": 1, "width": 224.0, "height": 224.0, "file_name": "train_images.zip@siberian-kitten.jpg"},
@ -152,6 +154,8 @@ Each rows in the index file (`index_path`) is:
<image_filepath> <comma-separated-label-indices>
```
Note that the class/label index should start from zero.
Example:
``` {txt}
@ -168,6 +172,8 @@ The index file for OD is slightly different from IC. Each rows in the index file
<image_filepath> <label_filepath>
```
Same with classification, the class/label index should start from 0.
Example for `train_images.txt`:
``` {txt}

Просмотреть файл

@ -1,7 +1,7 @@
import setuptools
from os import path
VERSION = '0.1.0'
VERSION = '0.1.1'
# Get the long description from the README file
here = path.abspath(path.dirname(__file__))

Просмотреть файл

@ -211,6 +211,31 @@ class TestCreateCocoDatasetManifest(unittest.TestCase):
self.assertEqual(dataset_manifest.images[0].labels, [0])
self.assertEqual(dataset_manifest.images[1].labels, [0, 1])
def test_index_can_start_from_zero(self):
manifest_dict = {
"images": [{"id": 0, "width": 224.0, "height": 224.0, "file_name": "siberian-kitten.jpg"},
{"id": 1, "width": 224.0, "height": 224.0, "file_name": "kitten 3.jpg"}],
"annotations": [
{"id": 0, "category_id": 0, "image_id": 0},
{"id": 1, "category_id": 0, "image_id": 1},
{"id": 2, "category_id": 1, "image_id": 1}
], "categories": [{"id": 0, "name": "cat"}, {"id": 1, "name": "dog"}]
}
dataset_dict = copy.deepcopy(self.DATASET_INFO_DICT)
with tempfile.TemporaryDirectory() as tempdir:
dataset_dict['root_folder'] = ''
dataset_dict['type'] = 'classification_multilabel'
coco_file_path = os.path.join(tempdir, 'test.json')
with open(coco_file_path, 'w') as f:
json.dump(manifest_dict, f)
dataset_manifest = CocoManifestAdaptor.create_dataset_manifest(coco_file_path, DatasetTypes.IC_MULTILABEL)
self.assertIsInstance(dataset_manifest, DatasetManifest)
self.assertEqual(len(dataset_manifest.images), 2)
self.assertEqual(len(dataset_manifest.labelmap), 2)
self.assertEqual(dataset_manifest.images[0].labels, [0])
self.assertEqual(dataset_manifest.images[1].labels, [0, 1])
def test_object_detection(self):
manifest_dict = {
"images": [{"id": 1, "width": 224.0, "height": 224.0, "file_name": "siberian-kitten.jpg"},

Просмотреть файл

@ -439,8 +439,12 @@ class CocoManifestAdaptor:
images_by_id = {img['id']: ImageDataManifest(img['id'], get_full_sas_or_path(img['file_name']), img['width'], img['height'], []) for img in coco_manifest['images']}
label_dict_by_id = {cate['id']: cate['name'] for cate in coco_manifest['categories']}
label_starting_idx = min(label_dict_by_id.keys())
labelmap = [label_dict_by_id[i + label_starting_idx] for i in range(len(label_dict_by_id))]
for annotation in coco_manifest['annotations']:
c_id = annotation['category_id'] - 1
c_id = annotation['category_id'] - label_starting_idx
if 'bbox' in annotation:
label = [c_id] + annotation['bbox']
else:
@ -449,7 +453,4 @@ class CocoManifestAdaptor:
images = [x for x in images_by_id.values()]
images.sort(key=lambda x: x.id)
label_dict_by_id = {cate['id']: cate['name'] for cate in coco_manifest['categories']}
labelmap = [label_dict_by_id[i + 1] for i in range(len(label_dict_by_id))]
return DatasetManifest(images, labelmap, data_type)