Browse Source

Merge branch 'master' of triton.inf-cv.uni-jena.de:Fine-grained/nabirds

Dimitri Korsch 6 năm trước cách đây
mục cha
commit
2ee9f494aa

+ 3 - 0
Makefile

@@ -12,3 +12,6 @@ test_deploy:
 
 get_version:
 	@python -c "import nabirds; print('v{}'.format(nabirds.__version__))"
+
+run_tests:
+	@bash scripts/tests.sh .

+ 2 - 1
nabirds/__init__.py

@@ -1,4 +1,5 @@
 from .dataset import Dataset, ImageWrapperDataset
-from .annotations import NAB_Annotations, CUB_Annotations
+from .annotations import *
+from .utils import _MetaInfo
 
 __version__ = "0.3.4"

+ 2 - 0
nabirds/annotations/__init__.py

@@ -4,6 +4,8 @@ from .cars import CARS_Annotations
 from .inat import INAT19_Annotations
 from .flowers import FLOWERS_Annotations
 
+from .base import BaseAnnotations
+
 from cvargparse.utils import BaseChoiceType
 from functools import partial
 

+ 26 - 0
scripts/info_files/test_info.yml

@@ -0,0 +1,26 @@
+BASE_DIR: ../tests
+
+DATA_DIR: mock_data
+MODEL_DIR: mock_models
+
+MODELS:
+  model1: &model1
+    folder: model1
+    class_key: model1
+    weights: model1.npz
+
+DATASETS:
+  MOCK: &mock1
+    folder: mock1
+    annotations: ORIGINAL
+    n_classes: 20
+
+PART_TYPES:
+  GLOBAL:         &parts_global
+    feature_suffix: ""
+
+
+PARTS:
+  MOCK_GLOBAL1:
+    <<: *mock1
+    <<: *parts_global

+ 5 - 0
scripts/tests.sh

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

+ 0 - 0
tests/__init__.py


+ 9 - 0
tests/configs.py

@@ -0,0 +1,9 @@
+import os
+import abc
+
+from os.path import *
+
+class config(abc.ABC):
+	BASE_DIR = abspath(os.environ.get("BASE_DIR", "."))
+
+	INFO_FILE = join(BASE_DIR, "info_files", "test_info.yml")

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

@@ -0,0 +1,10 @@
+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

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

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

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

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

+ 104 - 0
tests/test_annotations.py

@@ -0,0 +1,104 @@
+import unittest
+import numpy as np
+import os
+
+from tests.configs import config
+from os.path import *
+
+
+from nabirds import BaseAnnotations, _MetaInfo
+from nabirds.utils import read_info_file
+
+class MockAnnotation(BaseAnnotations):
+	name = "MOCK"
+
+	@property
+	def meta(self):
+		info = _MetaInfo(
+			images_folder="images",
+			images_file="images.txt",
+			labels_file="labels.txt",
+			split_file="tr_ID.txt",
+			bounding_boxes="bounding_boxes.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"),
+		)
+
+		info.structure = [
+			[info.images_file, "_images"],
+			[info.labels_file, "labels"],
+			[info.split_file, "_split"],
+			[info.parts_file, "_part_locs"],
+			[info.part_names_file, "_part_names"],
+			[info.bounding_boxes, "_bounding_boxes"],
+		]
+		return info
+
+
+class AnnotationTests(unittest.TestCase):
+
+	def setUp(self):
+		self.info = read_info_file(config.INFO_FILE)
+		# self.annot = MockAnnotation()
+
+	def tearDown(self):
+		# clear mock data folder?
+		pass
+
+	def create_annotations(self, images, labels, split,
+		bboxes=False,
+		index_offset=0,
+		n_parts=None):
+
+		data_root = join(self.info.BASE_DIR, self.info.DATA_DIR)
+		dataset_info = self.info.DATASETS.MOCK
+		annot_dir = join(data_root, dataset_info.folder, dataset_info.annotations)
+
+		if not isdir(annot_dir):
+			os.makedirs(annot_dir)
+
+		fname = lambda name: join(annot_dir, name)
+
+		with open(fname("images.txt"), "w") as images_f,\
+			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)
+				print(lab, file=labels_f)
+				print(sp, file=split_f)
+
+		if bboxes:
+			with open(fname("bounding_boxes.txt"), "w") as bbox_f:
+				for i in range(index_offset, index_offset + len(images)):
+					print(i, 0, 0, 100, 100, file=bbox_f)
+
+		if n_parts is not None:
+			parts_dir = join(annot_dir, "parts")
+			if not isdir(parts_dir):
+				os.makedirs(parts_dir)
+			fname = lambda name: join(parts_dir, name)
+			with open(fname("parts.txt"), "w") as part_names_f, \
+				open(fname("part_locs.txt"), "w") as part_locs_f:
+
+				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 p in range(n_parts):
+						print(i, 10*p, 10*p, 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
+		)
+
+		annot = MockAnnotation(config.INFO_FILE)
+
+	def test_bar(self):
+		self.assertFalse(0)