Explorar o código

moved some functions to utils. changed YAML structure a bit

Dimitri Korsch %!s(int64=6) %!d(string=hai) anos
pai
achega
4ccdf94a6f
Modificáronse 3 ficheiros con 88 adicións e 37 borrados
  1. 4 14
      nabirds/annotations/base.py
  2. 20 0
      nabirds/utils/__init__.py
  3. 64 23
      scripts/info_files/info.yml

+ 4 - 14
nabirds/annotations/base.py

@@ -5,14 +5,7 @@ import logging
 from os.path import join, isfile, isdir
 from collections import defaultdict, OrderedDict
 
-try:
-	from yaml import CLoader as Loader, CDumper as Dumper
-except ImportError:
-	from yaml import Loader, Dumper
-
-import yaml
-
-from nabirds.utils import attr_dict
+from nabirds.utils import read_info_file, feature_file_name
 from nabirds.dataset import Dataset
 
 class BaseAnnotations(abc.ABC):
@@ -57,8 +50,7 @@ class BaseAnnotations(abc.ABC):
 			return self.info.PARTS[self.part_type]
 
 	def root_from_infofile(self, info_file, parts=None, feature_model=None):
-		with open(info_file) as f:
-			self.info = attr_dict(yaml.load(f, Loader=Loader))
+		self.info = read_info_file(info_file)
 
 		dataset_info = self.dataset_info
 		annot_dir = join(self.data_root, dataset_info.folder, dataset_info.annotations)
@@ -95,11 +87,9 @@ class BaseAnnotations(abc.ABC):
 
 		if None not in [subset, self.feature_model]:
 			tried = []
+			model_info = self.info[self.feature_model]
 			for subset_phony in BaseAnnotations.FEATURE_PHONY[subset]:
-				features = "{subset}_{suffix}.{model}.npz".format(
-					subset=subset_phony,
-					suffix=dataset_info.feature_suffix,
-					model=self.feature_model)
+				features = feature_file_name(subset_phony, dataset_info, model_info)
 				feature_path = join(self.root, "features", features)
 				if isfile(feature_path): break
 				tried.append(feature_path)

+ 20 - 0
nabirds/utils/__init__.py

@@ -1,5 +1,25 @@
 import numpy as np
 
+try:
+	from yaml import CLoader as Loader, CDumper as Dumper
+except ImportError:
+	from yaml import Loader, Dumper
+
+import yaml
+
+from os.path import isfile
+
+def feature_file_name(subset, part_info, model_info):
+
+	return "{subset}{suffix}.{model}.npz".format(
+			subset=subset,
+			suffix=part_info.feature_suffix,
+			model=model_info.folder)
+
+def read_info_file(fpath):
+	with open(fpath) as f:
+		return attr_dict(yaml.load(f, Loader=Loader))
+
 def random_idxs(idxs, rnd=None, n_parts=None):
 
 	if rnd is None or isinstance(rnd, int):

+ 64 - 23
scripts/info_files/info.yml

@@ -5,21 +5,30 @@ MODEL_DIR: models
 
 ############ Existing models
 MODELS:
-  InceptionV3:    &inception
+  inception:    &inception
+    class: InceptionV3
     folder: inception
     weights: ft_inat/model.ckpt.npz
     ft_weights: ft_inat/model.ckpt.npz
 
-  TFInceptionV3:  &inception_tf
+  inception_tf:  &inception_tf
+    class: InceptionV3_TF
     folder: inception_tf
     weights: ft_inat/inception_v3_iNat_299.ckpt
     ft_weights: ft_inat/inception_v3_iNat_299.ckpt
 
-  ResNet50:       &resnet50
+  resnet:       &resnet50
+    class: ResnetLayers
     folder: resnet
     weights: model.npz
     ft_weights: ft_cub200/model.npz
 
+  vgg19:       &vgg19
+    class: VGG19Layers
+    folder: vgg19
+    weights: model.npz
+    ft_weights: ft_cub200/model.npz
+
 ############ Existing Datasets
 DATASETS:
   CUB200:         &cub200
@@ -31,59 +40,91 @@ DATASETS:
     annotations: ORIGINAL
 
 ############ Existing Part Annotations and Part Features
-PARTS:
-  CARS_UNI:         &cars_uni
-    <<: *cars
-    is_uniform: true
-    feature_suffix: 26parts_uniform
-    rescale_size: !!int -1
-    scales:
-     - 0.2
+PART_TYPES:
+  GLOBAL:         &parts_global
+    feature_suffix: ""
 
   UNI:            &parts_uni
-    <<: *cub200
+    # <<: *cub200
     is_uniform: true
-    feature_suffix: 26parts_uniform
+    feature_suffix: _26parts_uniform
     rescale_size: !!int -1
     scales:
      - 0.2
 
   GT:             &parts_gt
-    <<: *cub200
-    feature_suffix: 16parts_gt
+    # <<: *cub200
+    feature_suffix: _16parts_gt
     rescale_size: !!int -1
     scales:
      - 0.31
 
   GT2:            &parts_gt2
-    <<: *cub200
+    # <<: *cub200
     annotations: cub200_11_regrouped
-    feature_suffix: 5parts_gt
+    feature_suffix: _5parts_gt
     rescale_size: !!int -1
     scales:
       - 0.31
 
   NAC:            &parts_nac
-    <<: *cub200
+    # <<: *cub200
     annotations: NAC/2017-bilinear
-    feature_suffix: 20parts
+    feature_suffix: _20parts
     rescale_size: !!int 224
     scales:
       - 0.31
       - 0.45
 
   L1_pred:        &parts_l1p
-    <<: *cub200
+    # <<: *cub200
     annotations: cub200_11_L1_pred
-    feature_suffix: 5parts_L1_pred
+    feature_suffix: _5parts_L1_pred
     rescale_size: !!int 299
     scales:
       - 0.31
 
   L1_full:        &parts_l1f
-    <<: *cub200
+    # <<: *cub200
     annotations: cub200_11_L1_full
-    feature_suffix: 5parts_L1_full
+    feature_suffix: _5parts_L1_full
     rescale_size: !!int 299
     scales:
       - 0.31
+
+PARTS:
+  #### No Parts Annotations
+  CUB200_GLOBAL:
+    <<: *cub200
+    <<: *parts_global
+
+  CARS_GLOBAL:
+    <<: *cars
+    <<: *parts_global
+
+  #### With Parts Annotations
+  CUB200_UNI:
+    <<: *cub200
+    <<: *parts_uni
+
+  CUB200_GT:
+    <<: *cub200
+    <<: *parts_gt
+
+  CUB200_GT2:
+    <<: *cub200
+    <<: *parts_gt2
+
+  CUB200_NAC:
+    <<: *cub200
+    <<: *parts_nac
+
+  CUB200_L1_pred:
+    <<: *cub200
+    <<: *parts_l1p
+
+  CUB200_L1_full:
+    <<: *cub200
+    <<: *parts_l1f
+
+