Browse Source

added NABirds dataset infos

Dimitri Korsch 6 years ago
parent
commit
038f926e4d

+ 8 - 3
nabirds/annotations/base.py

@@ -8,6 +8,11 @@ from collections import defaultdict, OrderedDict
 from nabirds.utils import read_info_file, feature_file_name
 from nabirds.dataset import Dataset
 
+def _parse_index(idx, offset):
+	if idx.isdigit():
+		idx = str(int(idx) - offset)
+	return idx
+
 class BaseAnnotations(abc.ABC):
 
 	FEATURE_PHONY = dict(train=["train"], test=["test", "val"])
@@ -143,12 +148,12 @@ class BaseAnnotations(abc.ABC):
 		# this part is quite slow... TODO: some runtime improvements?
 		idx_to_parts = defaultdict(list)
 		for content in [i.split() for i in self._part_locs]:
-			idx = int(content[0]) - idx_offset
-			idx_to_parts[idx].append([float(c) for c in content[1:]])
+			uuid = _parse_index(content[0], idx_offset)
+			idx_to_parts[uuid].append([float(c) for c in content[1:]])
 
 		idx_to_parts = dict(idx_to_parts)
 		self.part_locs = np.stack([
-			idx_to_parts[self.uuid_to_idx[uuid]] for uuid in self.uuids]).astype(int)
+			idx_to_parts[uuid] for uuid in self.uuids]).astype(int)
 
 		if hasattr(self, "_part_names") and self._part_names is not None:
 			self._load_part_names()

+ 1 - 1
nabirds/annotations/cub.py

@@ -45,7 +45,7 @@ class CUB_Annotations(BaseAnnotations):
 		self.test_split = np.logical_not(self.train_split)
 
 	def _load_parts(self):
-		super(CUB_Annotations, self)._load_parts()
+		super(CUB_Annotations, self)._load_parts(idx_offset=0)
 		# set part idxs from 1-idxs to 0-idxs
 		self.part_locs[..., 0] -= 1
 

+ 7 - 0
nabirds/annotations/nab.py

@@ -1,3 +1,5 @@
+import numpy as np
+
 from os.path import join
 
 from nabirds.utils import _MetaInfo
@@ -15,6 +17,7 @@ class NAB_Annotations(BaseAnnotations):
 			labels_file="labels.txt",
 			hierarchy_file="hierarchy.txt",
 			split_file="train_test_split.txt",
+			bounding_box_dtype=np.dtype([(v, np.int32) for v in "xywh"]),
 			parts_file=join("parts", "part_locs.txt"),
 			part_names_file=join("parts", "parts.txt"),
 		)
@@ -28,3 +31,7 @@ class NAB_Annotations(BaseAnnotations):
 			[info.part_names_file, "_part_names"],
 		]
 		return info
+
+	def bounding_box(self, uuid):
+		return np.array((0,0,1,1),
+			dtype=self.meta.bounding_box_dtype)

+ 28 - 18
nabirds/dataset/part/annotation.py

@@ -1,6 +1,7 @@
 import numpy as np
 
 from matplotlib.patches import Rectangle
+from functools import partial
 
 from nabirds import utils
 from .base import BasePart
@@ -20,32 +21,41 @@ class LocationPart(BasePart):
 	def as_annotation(self):
 		return np.array([self._id, self.x, self.y, self.is_visible])
 
-	def crop(self, image, ratio=None, padding_mode="edge", *args, **kwargs):
+	def _generic_op(self, image, ratio, op):
 		ratio = ratio or self._ratio
-		_h, _w, c = utils.dimensions(image)
+		_h, _w, _ = utils.dimensions(image)
 		w, h = int(_w * ratio), int(_h * ratio)
-		return super(LocationPart, self).crop(image, w, h,
+		x, y = self.xy
+
+		return op(image, x, y, w, h)
+
+	def crop(self, image, ratio=None, padding_mode="edge", *args, **kwargs):
+
+		def op(im, x, y, w, h):
+			return super(LocationPart, self).crop(im, w, h,
 				padding_mode, is_location=True)
 
+		return self._generic_op(image, ratio, op)
+
 	def reveal(self, im, ratio=None, *args, **kwargs):
-		_h, _w, c = utils.dimensions(im)
-		w, h = int(_w * ratio), int(_h * ratio)
-		x,y = self.xy
-		x, y = max(x - w // 2, 0), max(y - h // 2, 0)
-		return x, y, im[y:y+h, x:x+w]
 
-	def plot(self, im, ax, ratio, fill=False, linestyle="--", **kwargs):
+		def op(im, x, y, w, h):
+			x, y = max(x - w // 2, 0), max(y - h // 2, 0)
+			return x, y, im[y:y+h, x:x+w]
+
+		return self._generic_op(im, ratio, op)
+
+	def plot(self, im, ax, ratio=None, fill=False, linestyle="--", **kwargs):
 		if not self.is_visible: return
-		x, y = self.xy
-		_h, _w, c = utils.dimensions(im)
-		w, h = int(_w * ratio), int(_h * ratio)
-		ax.add_patch(Rectangle(
-			(x-w//2, y-h//2), w, h,
-			fill=fill, linestyle=linestyle,
-			**kwargs
-		))
 
-		ax.scatter(*self.middle, marker="x", color="white", alpha=0.8)
+		def op(im, x, y, w, h):
+			ax.add_patch(Rectangle(
+				(x-w//2, y-h//2), w, h,
+				fill=fill, linestyle=linestyle,
+				**kwargs))
+
+			ax.scatter(*self.middle, marker="x", color="white", alpha=0.8)
+		return self._generic_op(im, ratio, op)
 
 
 	@property

+ 33 - 12
scripts/info_files/info.yml

@@ -51,13 +51,18 @@ MODELS:
 ############ Existing Datasets
 DATASETS:
   CUB200:         &cub200
-    folder: birds
-    annotations: cub200_11_ORIGINAL
+    folder: birds/cub200
+    annotations: "ORIGINAL"
     n_classes: 200
 
+  NAB:         &nabirds
+    folder: birds/nabirds
+    annotations: "ORIGINAL"
+    n_classes: 555
+
   CARS:         &cars
     folder: cars
-    annotations: ORIGINAL
+    annotations: "ORIGINAL"
     n_classes: 196
 
   INAT19:         &inat19
@@ -82,7 +87,6 @@ PART_TYPES:
     feature_suffix: ""
 
   UNI:            &parts_uni
-    # <<: *cub200
     is_uniform: true
     feature_suffix: _26parts_uniform
     rescale_size: !!int -1
@@ -90,22 +94,18 @@ PART_TYPES:
      - 0.2
 
   GT:             &parts_gt
-    # <<: *cub200
     feature_suffix: _16parts_gt
     rescale_size: !!int -1
     scales:
      - 0.31
 
   GT2:            &parts_gt2
-    # <<: *cub200
-    annotations: cub200_11_regrouped
     feature_suffix: _5parts_gt
     rescale_size: !!int -1
     scales:
       - 0.31
 
   NAC:            &parts_nac
-    # <<: *cub200
     annotations: NAC/2017-bilinear
     feature_suffix: _20parts
     rescale_size: !!int 224
@@ -114,16 +114,12 @@ PART_TYPES:
       - 0.45
 
   L1_pred:        &parts_l1p
-    # <<: *cub200
-    annotations: cub200_11_L1_pred
     feature_suffix: _5parts_L1_pred
     rescale_size: !!int 299
     scales:
       - 0.31
 
   L1_full:        &parts_l1f
-    # <<: *cub200
-    annotations: cub200_11_L1_full
     feature_suffix: _5parts_L1_full
     rescale_size: !!int 299
     scales:
@@ -140,6 +136,10 @@ PARTS:
     <<: *cars
     <<: *parts_global
 
+  NAB_GLOBAL:
+    <<: *nabirds
+    <<: *parts_global
+
   INAT19_GLOBAL:
     <<: *inat19
     <<: *parts_global
@@ -156,7 +156,10 @@ PARTS:
 
   #### With Parts Annotations
 
+
+  ####################################
   # CUB200-2011
+  ####################################
 
   CUB200_UNI:
     <<: *cub200
@@ -169,6 +172,7 @@ PARTS:
   CUB200_GT2:
     <<: *cub200
     <<: *parts_gt2
+    annotations: cub200_11_regrouped
 
   CUB200_NAC:
     <<: *cub200
@@ -177,12 +181,26 @@ PARTS:
   CUB200_L1_pred:
     <<: *cub200
     <<: *parts_l1p
+    annotations: cub200_11_L1_pred
 
   CUB200_L1_full:
     <<: *cub200
     <<: *parts_l1f
+    annotations: cub200_11_L1_full
+
+
+  ####################################
+  # NA Birds
+  ####################################
 
+  NAB_GT:
+    <<: *nabirds
+    <<: *parts_gt
+
+
+  ####################################
   # Stanford Cars
+  ####################################
 
   CARS_L1_pred:
     <<: *cars
@@ -194,7 +212,10 @@ PARTS:
     <<: *parts_l1f
     annotations: L1_full
 
+
+  ####################################
   # iNaturalist 2019
+  ####################################
 
   INAT19_MINI_L1_pred:
     <<: *inat19_mini