Browse Source

adapted moth scanner

Dimitri Korsch 3 years ago
parent
commit
92e3624a12

+ 3 - 2
labels/flat_moth_label_provider/moth_labels.py

@@ -21,8 +21,9 @@ class FlatMothLabels(LabelProvider):
         labels = []
         labels = []
         for key, entries in self.mapping.items():
         for key, entries in self.mapping.items():
             display_name = f"{entries['genus']} {entries['species']} ({entries['kr']})"
             display_name = f"{entries['genus']} {entries['species']} ({entries['kr']})"
-            app.logger.debug(key, display_name)
-            label = self.create_label(key, display_name)
+            reference = entries["kr"]
+            app.logger.debug(f"{reference} -> {display_name}")
+            label = self.create_label(reference, display_name)
             labels.append(label)
             labels.append(label)
 
 
         return labels
         return labels

+ 2 - 1
models/moth_scanner/configuration.json

@@ -26,6 +26,7 @@
     "model_type": "cvmodelz.InceptionV3",
     "model_type": "cvmodelz.InceptionV3",
     "input_size": 299,
     "input_size": 299,
     "weights": "classifier.npz",
     "weights": "classifier.npz",
-    "n_classes": 200
+    "n_classes": 200,
+    "mapping": "mapping.json"
   }
   }
 }
 }

+ 2 - 2
models/moth_scanner/scanner/__init__.py

@@ -33,8 +33,8 @@ class Scanner(Interface):
             if not info.selected:
             if not info.selected:
                 continue
                 continue
             x0, y0, x1, y1 = bbox
             x0, y0, x1, y1 = bbox
-            cls_id = self.classifier(bbox.crop(im, enlarge=True))
-            label = labels.get(str(cls_id), cls_id)
+            cls_ref = self.classifier(bbox.crop(im, enlarge=True))
+            label = labels.get(cls_ref, cls_ref)
             file.add_bounding_box(x0, y0, bbox.w, bbox.h, label=label)
             file.add_bounding_box(x0, y0, bbox.w, bbox.h, label=label)
 
 
     def read_image(self, path: str, mode: int = cv2.IMREAD_COLOR) -> np.ndarray:
     def read_image(self, path: str, mode: int = cv2.IMREAD_COLOR) -> np.ndarray:

+ 7 - 1
models/moth_scanner/scanner/classifier.py

@@ -1,4 +1,5 @@
 import chainer
 import chainer
+import json
 import numpy as np
 import numpy as np
 import typing as T
 import typing as T
 
 
@@ -27,6 +28,10 @@ class Classifier(object):
                                          strict=True,
                                          strict=True,
                                         )
                                         )
 
 
+        with open(Path(root, config.mapping)) as f:
+            mapping = json.load(f)
+            self._cls_id2ref = {int(key): value["kr"] for key, value in mapping.items()}
+
     def _transform(self, im: np.ndarray):
     def _transform(self, im: np.ndarray):
         _prepare = self.backbone.meta.prepare_func
         _prepare = self.backbone.meta.prepare_func
         size = (self.input_size, self.input_size)
         size = (self.input_size, self.input_size)
@@ -55,4 +60,5 @@ class Classifier(object):
             pred = self.backbone(x)
             pred = self.backbone(x)
         pred.to_cpu()
         pred.to_cpu()
 
 
-        return int(np.argmax(pred.array, axis=1))
+        cls_id = int(np.argmax(pred.array, axis=1))
+        return self._cls_id2ref.get(cls_id, str(cls_id))