|
@@ -0,0 +1,76 @@
|
|
|
+import numpy as np
|
|
|
+import os
|
|
|
+import logging
|
|
|
+
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+from cvdatasets.annotations.base import BaseAnnotations
|
|
|
+from cvdatasets.annotations.base.parts_mixin import PartsMixin
|
|
|
+from cvdatasets.utils import _MetaInfo
|
|
|
+
|
|
|
+class INET_Annotations(PartsMixin, BaseAnnotations):
|
|
|
+
|
|
|
+ name="ImageNet"
|
|
|
+
|
|
|
+ @property
|
|
|
+ def meta(self):
|
|
|
+ info = _MetaInfo(
|
|
|
+ images_folder="",
|
|
|
+ train_content="ILSVRC2012_img_train",
|
|
|
+ val_content="ILSVRC2012_img_val",
|
|
|
+
|
|
|
+ img_extensions={".JPEG"}
|
|
|
+ )
|
|
|
+
|
|
|
+ info.structure = [
|
|
|
+ [info.train_content, "_train_content"],
|
|
|
+ [info.val_content, "_val_content"],
|
|
|
+ ]
|
|
|
+
|
|
|
+ return info
|
|
|
+
|
|
|
+ def read_content(self, folder_name, attr):
|
|
|
+ folder_path = self._path(folder_name)
|
|
|
+ logging.info(f"Loading images from folder \"{folder_path}\" ...")
|
|
|
+
|
|
|
+ _content = []
|
|
|
+ _skipped = 0
|
|
|
+ for path, folders, files in os.walk(folder_path):
|
|
|
+
|
|
|
+ path = Path(path)
|
|
|
+ for file in files:
|
|
|
+ fpath = path / file
|
|
|
+
|
|
|
+ if fpath.suffix not in self.meta.img_extensions:
|
|
|
+ _skipped += 1
|
|
|
+ continue
|
|
|
+
|
|
|
+ _content.append(fpath)
|
|
|
+
|
|
|
+ logging.info(f"Loaded {len(_content):,d} ({_skipped:,d} skipped) images from ")
|
|
|
+ setattr(self, attr, _content)
|
|
|
+
|
|
|
+
|
|
|
+ def _load_uuids(self):
|
|
|
+ train_uuid_fnames = [(fpath.name, str(fpath.relative_to(self.root))) for
|
|
|
+ fpath in self._train_content]
|
|
|
+
|
|
|
+ val_uuid_fnames = [(fpath.name, str(fpath.relative_to(self.root))) for
|
|
|
+ fpath in self._val_content]
|
|
|
+
|
|
|
+ uuid_fnames = train_uuid_fnames + val_uuid_fnames
|
|
|
+ self.uuids, self.images = map(np.array, zip(*uuid_fnames))
|
|
|
+ self.uuid_to_idx = {uuid: i for i, uuid in enumerate(self.uuids)}
|
|
|
+
|
|
|
+
|
|
|
+ def _load_labels(self):
|
|
|
+ train_labs = [fpath.parent.name for fpath in self._train_content]
|
|
|
+ val_labs = [fpath.parent.name for fpath in self._val_content]
|
|
|
+
|
|
|
+ self._classes, self.labels = np.unique(train_labs + val_labs, return_inverse=True)
|
|
|
+
|
|
|
+ def _load_split(self):
|
|
|
+ self.train_split = np.ones(len(self.uuids), dtype=bool)
|
|
|
+ self.train_split[len(self._train_content):] = False
|
|
|
+
|
|
|
+ self.test_split = np.logical_not(self.train_split)
|