3 from ..config import PathField, BoolField
4 from ..representation import ClassificationAnnotation
5 from ..utils import read_txt, get_path
7 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
10 class ImageNetFormatConverterConfig(BaseFormatConverterConfig):
11 annotation_file = PathField()
12 labels_file = PathField(optional=True)
13 has_background = BoolField(optional=True)
16 class ImageNetFormatConverter(BaseFormatConverter):
17 __provider__ = 'imagenet'
19 _config_validator_type = ImageNetFormatConverterConfig
22 self.annotation_file = self.config['annotation_file']
23 self.labels_file = self.config.get('labels_file')
24 self.has_background = self.config.get('has_background', False)
28 for image in read_txt(get_path(self.annotation_file)):
29 image_name, label = image.split()
30 label = np.int64(label) if not self.has_background else np.int64(label) + 1
31 annotation.append(ClassificationAnnotation(image_name, label))
32 meta = self._create_meta(self.labels_file, self.has_background) if self.labels_file else None
34 return annotation, meta
37 def _create_meta(labels_file, has_background=False):
40 for i, line in enumerate(read_txt(get_path(labels_file))):
41 index_for_label = i if not has_background else i + 1
43 label = line[line.find(' ') + 1:]
44 labels[index_for_label] = label
47 labels[0] = 'background'
48 meta['backgound_label'] = 0
50 meta['label_map'] = labels