File size: 3,872 Bytes
d21cb06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from unittest import TestCase

import numpy as np
import os.path as osp

from datumaro.components.project import Project, Dataset
from datumaro.components.extractor import (DatasetItem, Label,
    LabelCategories, AnnotationType
)
from datumaro.plugins.imagenet_format import ImagenetConverter
from datumaro.plugins.imagenet_format import ImagenetImporter
from datumaro.util.test_utils import TestDir, compare_datasets

class ImagenetFormatTest(TestCase):
    def test_can_save_and_load(self):
        source_dataset = Dataset.from_iterable([
            DatasetItem(id='1',
                image=np.ones((8, 8, 3)),
                annotations=[Label(0)]
            ),
            DatasetItem(id='2',
                image=np.ones((10, 10, 3)),
                annotations=[Label(1)]
            ),
            DatasetItem(id='3',
                image=np.ones((10, 10, 3)),
                annotations=[Label(0)]
            ),
            DatasetItem(id='4',
                image=np.ones((8, 8, 3)),
                annotations=[Label(2)]
            ),
        ], categories={
            AnnotationType.label: LabelCategories.from_iterable(
                'label_' + str(label) for label in range(3)),
        })

        with TestDir() as test_dir:
            ImagenetConverter.convert(source_dataset, test_dir, save_images=True)

            parsed_dataset = ImagenetImporter()(test_dir).make_dataset()

            compare_datasets(self, source_dataset, parsed_dataset,
                require_images=True)

    def test_can_save_and_load_with_multiple_labels(self):
        source_dataset = Dataset.from_iterable([
            DatasetItem(id='1',
                image=np.ones((8, 8, 3)),
                annotations=[Label(0), Label(1)]
            ),
            DatasetItem(id='2',
                image=np.ones((10, 10, 3)),
                annotations=[Label(0), Label(1)]
            ),
            DatasetItem(id='3',
                image=np.ones((10, 10, 3)),
                annotations=[Label(0), Label(2)]
            ),
            DatasetItem(id='4',
                image=np.ones((8, 8, 3)),
                annotations=[Label(2), Label(4)]
            ),
            DatasetItem(id='5',
                image=np.ones((10, 10, 3)),
                annotations=[Label(3), Label(4)]
            ),
            DatasetItem(id='6',
                image=np.ones((10, 10, 3)),
            ),
            DatasetItem(id='7',
                image=np.ones((8, 8, 3))
            ),
        ], categories={
            AnnotationType.label: LabelCategories.from_iterable(
                'label_' + str(label) for label in range(5)),
        })

        with TestDir() as test_dir:
            ImagenetConverter.convert(source_dataset, test_dir, save_images=True)

            parsed_dataset = ImagenetImporter()(test_dir).make_dataset()

            compare_datasets(self, source_dataset, parsed_dataset,
                require_images=True)


DUMMY_DATASET_DIR = osp.join(osp.dirname(__file__), 'assets', 'imagenet_dataset')

class ImagenetImporterTest(TestCase):
    def test_can_import(self):
        expected_dataset = Dataset.from_iterable([
            DatasetItem(id='1',
                image=np.ones((8, 8, 3)),
                annotations=[Label(0), Label(1)]
            ),
            DatasetItem(id='2',
                image=np.ones((10, 10, 3)),
                annotations=[Label(0)]
            ),
        ], categories={
            AnnotationType.label: LabelCategories.from_iterable(
                'label_' + str(label) for label in range(2)),
        })

        dataset = Project.import_from(DUMMY_DATASET_DIR, 'imagenet').make_dataset()

        compare_datasets(self, expected_dataset, dataset, require_images=True)

    def test_can_detect_imagenet(self):
        self.assertTrue(ImagenetImporter.detect(DUMMY_DATASET_DIR))