Kaynağa Gözat

ignored mock data. fixed the index shift while loading part annotations

Dimitri Korsch 6 yıl önce
ebeveyn
işleme
3918467153

+ 12 - 10
nabirds/annotations/base.py

@@ -8,10 +8,10 @@ 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
+# def _parse_index(idx, offset):
+# 	if idx.isdigit():
+# 		idx = str(int(idx) - offset)
+# 	return idx
 
 class BBoxMixin(abc.ABC):
 	def _load_bounding_boxes(self):
@@ -160,17 +160,19 @@ class BaseAnnotations(abc.ABC):
 		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_parts(self, idx_offset=0):
+	def _load_parts(self):
 		assert self.has_parts, "Part locations were not loaded!"
 		# this part is quite slow... TODO: some runtime improvements?
-		idx_to_parts = defaultdict(list)
+		uuid_to_parts = defaultdict(list)
 		for content in [i.split() for i in self._part_locs]:
-			uuid = _parse_index(content[0], idx_offset)
-			idx_to_parts[uuid].append([float(c) for c in content[1:]])
+			uuid = content[0]
+			assert uuid in self.uuids, \
+				"Could not find UUID \"\" from part annotations in image annotations!".format(uuid)
+			uuid_to_parts[uuid].append([float(c) for c in content[1:]])
 
-		idx_to_parts = dict(idx_to_parts)
+		uuid_to_parts = dict(uuid_to_parts)
 		self.part_locs = np.stack([
-			idx_to_parts[uuid] for uuid in self.uuids]).astype(int)
+			uuid_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/cars.py

@@ -42,7 +42,7 @@ class CARS_Annotations(BaseAnnotations, BBoxMixin):
 
 		# load only if present
 		if self.has_parts:
-			super(CARS_Annotations, self)._load_parts(idx_offset=1)
+			super(CARS_Annotations, self)._load_parts()
 
 		self._load_bounding_boxes()
 

+ 1 - 1
nabirds/annotations/cub.py

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

+ 1 - 1
nabirds/annotations/flowers.py

@@ -42,7 +42,7 @@ class FLOWERS_Annotations(BaseAnnotations, BBoxMixin):
 
 		# load only if present
 		if self.has_parts:
-			super(FLOWERS_Annotations, self)._load_parts(idx_offset=0)
+			super(FLOWERS_Annotations, self)._load_parts()
 
 		self._load_bounding_boxes()
 

+ 1 - 1
nabirds/annotations/inat.py

@@ -61,7 +61,7 @@ class INAT19_Annotations(BaseAnnotations):
 
 		# load only if present
 		if self.has_parts:
-			super(INAT19_Annotations, self)._load_parts(idx_offset=1)
+			super(INAT19_Annotations, self)._load_parts()
 
 		self._load_bounding_boxes()
 

+ 4 - 0
scripts/config.sh

@@ -0,0 +1,4 @@
+source /home/korsch/.anaconda3/etc/profile.d/conda.sh
+conda activate chainer4
+
+PYTHON="python" #-m cProfile -o profile"

+ 1 - 1
scripts/display.sh

@@ -1,5 +1,5 @@
 #!/usr/bin/env bash
-PYTHON=python
+source config.sh
 
 ############## Possible calls ##############
 

+ 2 - 1
scripts/tests.sh

@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
+source config.sh
 
 export BASE_DIR="."
 
-python -m unittest discover -s ${1:-".."}
+$PYTHON -m unittest discover -s ${1:-".."}

+ 1 - 0
tests/.gitignore

@@ -0,0 +1 @@
+mock_data

+ 0 - 10
tests/mock_data/mock1/ORIGINAL/images.txt

@@ -1,10 +0,0 @@
-0 images0.jpg
-1 images1.jpg
-2 images2.jpg
-3 images3.jpg
-4 images4.jpg
-5 images5.jpg
-6 images6.jpg
-7 images7.jpg
-8 images8.jpg
-9 images9.jpg

+ 0 - 10
tests/mock_data/mock1/ORIGINAL/labels.txt

@@ -1,10 +0,0 @@
-0
-1
-2
-3
-4
-0
-1
-2
-3
-4

+ 0 - 10
tests/mock_data/mock1/ORIGINAL/tr_ID.txt

@@ -1,10 +0,0 @@
-1
-1
-1
-1
-1
-0
-0
-0
-0
-0

+ 41 - 20
tests/test_annotations.py

@@ -1,16 +1,20 @@
 import unittest
 import numpy as np
 import os
+import uuid
 
 from tests.configs import config
 from os.path import *
 
+from abc import ABC, abstractproperty
+
 
 from nabirds import BaseAnnotations, _MetaInfo
 from nabirds.utils import read_info_file
 
 class MockAnnotation(BaseAnnotations):
 	name = "MOCK"
+	index_offset = 0
 
 	@property
 	def meta(self):
@@ -35,21 +39,20 @@ class MockAnnotation(BaseAnnotations):
 		]
 		return info
 
-
-class AnnotationTests(unittest.TestCase):
-
-	def setUp(self):
-		self.info = read_info_file(config.INFO_FILE)
-		# self.annot = MockAnnotation()
+class BaseAnnotationTest(unittest.TestCase, ABC):
 
 	def tearDown(self):
 		# clear mock data folder?
 		pass
 
+	def setUp(self):
+		self.info = read_info_file(config.INFO_FILE)
+
 	def create_annotations(self, images, labels, split,
 		bboxes=False,
 		index_offset=0,
-		n_parts=None):
+		n_parts=None,
+		annot_params={}):
 
 		data_root = join(self.info.BASE_DIR, self.info.DATA_DIR)
 		dataset_info = self.info.DATASETS.MOCK
@@ -64,8 +67,8 @@ class AnnotationTests(unittest.TestCase):
 			open(fname("labels.txt"), "w") as labels_f,\
 			open(fname("tr_ID.txt"), "w") as split_f:
 
-			for i, (im, lab, sp) in enumerate(zip(images, labels, split), index_offset):
-				print(i, im, file=images_f)
+			for im, lab, sp in zip(images, labels, split):
+				print(*im, file=images_f)
 				print(lab, file=labels_f)
 				print(sp, file=split_f)
 
@@ -85,20 +88,38 @@ class AnnotationTests(unittest.TestCase):
 				for i in range(n_parts):
 					print(i, "part_{}".format(i), file=part_names_f)
 
-				for i in range(index_offset, len(images) + index_offset):
+				for i, imname in images:
 					for p in range(n_parts):
-						print(i, 10*p, 10*p, 1, file=part_locs_f)
+						print(i, p, 10*(p+1), 10*(p+1), 1, file=part_locs_f)
 
 
-	def test_foo(self):
-		self.create_annotations(
-			["images{}.jpg".format(i) for i in range(10)],
-			[i % 5 for i in range(10)],
-			[int(i < 5) for i in range(10)],
-			bboxes=False, n_parts=5
-		)
+		return MockAnnotation(
+			root_or_infofile=config.INFO_FILE,
+			**annot_params)
+
 
-		annot = MockAnnotation(config.INFO_FILE)
+class AnnotationTest(BaseAnnotationTest):
+
+	# @unittest.skip
+	def test_simple(self):
+		_annotation_params = dict(
+			images=[(i, "images{}.jpg".format(i)) for i in range(10)],
+			labels=[i % 5 for i in range(10)],
+			split=[int(i < 5) for i in range(10)],
+			bboxes=True,
+			n_parts=5
+		)
+		annot = self.create_annotations(**_annotation_params)
+
+	# @unittest.skip
+	def test_with_uuids(self):
+		_annotation_params = dict(
+			images=[(uuid.uuid4(), "images{}.jpg".format(i)) for i in range(10)],
+			labels=[i % 5 for i in range(10)],
+			split=[int(i < 5) for i in range(10)],
+			bboxes=True,
+			n_parts=5
+		)
+		annot = self.create_annotations(**_annotation_params)
 
-	def test_bar(self):
 		self.assertFalse(0)