Sfoglia il codice sorgente

added ImageNet annotations

Dimitri Korsch 5 anni fa
parent
commit
4cffa73b23

+ 8 - 5
cvdatasets/annotations/__init__.py

@@ -1,12 +1,13 @@
-from cvdatasets.annotations.impl.cub import CUB_Annotations
 from cvdatasets.annotations.impl.birdsnap import BSNAP_Annotations
 from cvdatasets.annotations.impl.birdsnap import BSNAP_Annotations
-from cvdatasets.annotations.impl.nab import NAB_Annotations
 from cvdatasets.annotations.impl.cars import CARS_Annotations
 from cvdatasets.annotations.impl.cars import CARS_Annotations
-from cvdatasets.annotations.impl.inat import INAT19_Annotations
-from cvdatasets.annotations.impl.inat import INAT18_Annotations
-from cvdatasets.annotations.impl.flowers import FLOWERS_Annotations
+from cvdatasets.annotations.impl.cub import CUB_Annotations
 from cvdatasets.annotations.impl.dogs import DOGS_Annotations
 from cvdatasets.annotations.impl.dogs import DOGS_Annotations
+from cvdatasets.annotations.impl.flowers import FLOWERS_Annotations
 from cvdatasets.annotations.impl.hed import HED_Annotations
 from cvdatasets.annotations.impl.hed import HED_Annotations
+from cvdatasets.annotations.impl.imagenet import INET_Annotations
+from cvdatasets.annotations.impl.inat import INAT18_Annotations
+from cvdatasets.annotations.impl.inat import INAT19_Annotations
+from cvdatasets.annotations.impl.nab import NAB_Annotations
 from cvdatasets.annotations.impl.tigers import TIGERS_Annotations
 from cvdatasets.annotations.impl.tigers import TIGERS_Annotations
 
 
 from cvdatasets.annotations.base import BaseAnnotations
 from cvdatasets.annotations.base import BaseAnnotations
@@ -30,6 +31,8 @@ class AnnotationType(BaseChoiceType):
 	TIGERS = TIGERS_Annotations
 	TIGERS = TIGERS_Annotations
 	TIGERS_TEST = partial(TIGERS_Annotations)
 	TIGERS_TEST = partial(TIGERS_Annotations)
 
 
+	IMAGENET = INET_Annotations
+
 	INAT18 = INAT18_Annotations
 	INAT18 = INAT18_Annotations
 
 
 	INAT19 = INAT19_Annotations
 	INAT19 = INAT19_Annotations

+ 76 - 0
cvdatasets/annotations/impl/imagenet.py

@@ -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)

+ 10 - 1
scripts/info_files/info.yml

@@ -49,6 +49,11 @@ MODELS:
 
 
 ############ Existing Datasets
 ############ Existing Datasets
 DATASETS:
 DATASETS:
+  IMAGENET:         &inet
+    folder: ImageNet
+    annotations: "BJOERN"
+    n_classes: 1000
+
   CUB200:         &cub200
   CUB200:         &cub200
     folder: birds/cub200
     folder: birds/cub200
     annotations: "ORIGINAL"
     annotations: "ORIGINAL"
@@ -188,8 +193,12 @@ PART_TYPES:
       - 0.31
       - 0.31
 
 
 PARTS:
 PARTS:
-
   #### No Parts Annotations
   #### No Parts Annotations
+
+  IMAGENET_GLOBAL:
+    <<: *inet
+    <<: *parts_global
+
   CUB200_2FOLD_GLOBAL:
   CUB200_2FOLD_GLOBAL:
     <<: *cub200_2fold
     <<: *cub200_2fold
     <<: *parts_global
     <<: *parts_global