Addax-Data-Science commited on
Commit
76df3c0
·
verified ·
1 Parent(s): fe0bdd7

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ ---
2
+ {}
3
+ ---
4
+
5
+ This repository contains open-source models redistributed for easy integration with [AddaxAI](https://addaxdatascience.com/addaxai/), hosted by [Addax Data Science](https://addaxdatascience.com/). Each model retains its original license (see license files) and attribution. We comply with all original license terms. Users must review and comply with individual model licenses before use. See below for detailed model information including original sources, licenses, and attributions.
6
+
7
+ For the owner, license, citation and links of this model, see the [AddaxAI model zoo](https://docs.addaxai.com/docs/reference/model-zoo).
always_crop_99710272_22x8_v12_epoch_00148.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d099632044e92a0446869e06e5eb282e24706d5266b4acf5b42f5f23e2b50ade
3
+ size 224393837
final-20260909.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ae6b9975c2d9f2b6bb3e4fc138cd950da6bc021196087b15be986cac8cd2d66b
3
+ size 224307613
inference.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ModelInference for the addax-sppnet model family.
3
+
4
+ Architecture: SpeciesNet GraphModule backbone (frozen) + a thin nn.Linear
5
+ head fine-tuned per region. Originally written for AddaxAI's legacy
6
+ classify_detections.py (Peter van Lunteren, 13 May 2025); ported here to
7
+ the WebUI's class-based ModelInference interface.
8
+
9
+ Files expected in the model directory:
10
+ - <model_fname>.pt fine-tuned head checkpoint, e.g. final-20260317.pt
11
+ - <backbone>.pt frozen SpeciesNet backbone, one of:
12
+ - always_crop_99710272_22x8_v12_epoch_00148.pt
13
+ - full_image_88545560_22x8_v12_epoch_00153.pt
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ # Allow loading checkpoints saved on a Windows runner on a POSIX machine.
19
+ import pathlib
20
+ import platform
21
+ from pathlib import Path
22
+
23
+ import numpy as np
24
+ import torch
25
+ import torch.nn as nn
26
+ import torch.nn.functional as F
27
+ from PIL import Image
28
+ from torchvision import transforms
29
+
30
+ if platform.system() != "Windows":
31
+ pathlib.WindowsPath = pathlib.PosixPath # type: ignore[assignment]
32
+
33
+ # Don't fail on truncated images during inference.
34
+ from PIL import ImageFile
35
+
36
+ ImageFile.LOAD_TRUNCATED_IMAGES = True
37
+
38
+
39
+ _BACKBONE_FILENAMES = (
40
+ "always_crop_99710272_22x8_v12_epoch_00148.pt",
41
+ "full_image_88545560_22x8_v12_epoch_00153.pt",
42
+ )
43
+
44
+
45
+ def _load_fx_checkpoint(weights_path: Path, map_location: str = "cpu") -> nn.Module:
46
+ """Load a SpeciesNet onnx2torch GraphModule.
47
+
48
+ The backbone is shipped as a torch.fx GraphModule. PyTorch 2.4+
49
+ requires `reduce_graph_module` to be in the safe-globals allowlist
50
+ when loading with `weights_only=True`; older versions don't have
51
+ this concept. Try both paths.
52
+ """
53
+ try:
54
+ from torch.fx.graph_module import reduce_graph_module
55
+ from torch.serialization import add_safe_globals
56
+ add_safe_globals([reduce_graph_module])
57
+ except Exception:
58
+ pass
59
+
60
+ try:
61
+ obj = torch.load(weights_path, map_location=map_location, weights_only=True)
62
+ except Exception:
63
+ obj = torch.load(weights_path, map_location=map_location, weights_only=False)
64
+
65
+ if hasattr(obj, "state_dict") and hasattr(obj, "forward"):
66
+ return obj
67
+ raise ValueError(f"{weights_path} is not a torch.nn.Module GraphModule")
68
+
69
+
70
+ class _FXClassifier(nn.Module):
71
+ """SpeciesNet backbone (frozen) + linear head."""
72
+
73
+ def __init__(
74
+ self,
75
+ backbone: nn.Module,
76
+ num_classes: int,
77
+ img_size: int = 480,
78
+ input_layout: str = "nhwc",
79
+ ) -> None:
80
+ super().__init__()
81
+ self.backbone = backbone
82
+ self.input_layout = input_layout.lower()
83
+
84
+ for p in self.backbone.parameters():
85
+ p.requires_grad = False
86
+ self.backbone.eval()
87
+
88
+ # Probe the backbone to discover output feature size at this
89
+ # img_size + layout combo, so the head matches exactly.
90
+ with torch.no_grad():
91
+ x = torch.zeros(1, 3, img_size, img_size)
92
+ if self.input_layout == "nhwc":
93
+ x = x.permute(0, 2, 3, 1).contiguous()
94
+ z = self.backbone(x)
95
+ z = self._pool(z)
96
+ in_features = z.shape[1]
97
+
98
+ self.head = nn.Linear(in_features, num_classes)
99
+
100
+ @staticmethod
101
+ def _pool(z: torch.Tensor) -> torch.Tensor:
102
+ if z.ndim == 4:
103
+ return F.adaptive_avg_pool2d(z, 1).flatten(1)
104
+ if z.ndim == 3:
105
+ return z.mean(dim=1)
106
+ return z.flatten(1)
107
+
108
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
109
+ if self.input_layout == "nhwc":
110
+ x = x.permute(0, 2, 3, 1).contiguous()
111
+ z = self.backbone(x)
112
+ z = self._pool(z)
113
+ return self.head(z)
114
+
115
+
116
+ class ModelInference:
117
+ """ModelInference for the addax-sppnet family (SpeciesNet backbone + linear head)."""
118
+
119
+ def __init__(self, model_dir: Path, model_path: Path) -> None:
120
+ self.model_dir = Path(model_dir)
121
+ self.model_path = Path(model_path)
122
+ self.model: _FXClassifier | None = None
123
+ self.device: torch.device | None = None
124
+ self._class_names: list[str] = []
125
+ self._preprocess: transforms.Compose | None = None
126
+
127
+ # ------------------------------------------------------------------
128
+ # Required interface
129
+ # ------------------------------------------------------------------
130
+
131
+ def check_gpu(self) -> bool:
132
+ try:
133
+ if torch.backends.mps.is_built() and torch.backends.mps.is_available():
134
+ return True
135
+ except Exception:
136
+ pass
137
+ return torch.cuda.is_available()
138
+
139
+ def load_model(self) -> None:
140
+ if self.check_gpu():
141
+ self.device = torch.device(
142
+ "mps" if torch.backends.mps.is_available() else "cuda"
143
+ )
144
+ else:
145
+ self.device = torch.device("cpu")
146
+
147
+ # Load fine-tuned head checkpoint.
148
+ try:
149
+ checkpoint = torch.load(
150
+ self.model_path, map_location=self.device, weights_only=True
151
+ )
152
+ except Exception:
153
+ checkpoint = torch.load(
154
+ self.model_path, map_location=self.device, weights_only=False
155
+ )
156
+
157
+ # Resolve backbone path. The fine-tuned model ships alongside
158
+ # one of two known backbone files, depending on the recipe.
159
+ backbone_path: Path | None = None
160
+ for name in _BACKBONE_FILENAMES:
161
+ candidate = self.model_dir / name
162
+ if candidate.exists():
163
+ backbone_path = candidate
164
+ break
165
+ if backbone_path is None:
166
+ raise FileNotFoundError(
167
+ "Backbone weights not found. Expected one of "
168
+ f"{_BACKBONE_FILENAMES} in {self.model_dir}."
169
+ )
170
+
171
+ backbone = _load_fx_checkpoint(backbone_path, map_location="cpu")
172
+ model = _FXClassifier(
173
+ backbone=backbone,
174
+ num_classes=checkpoint["num_classes"],
175
+ img_size=checkpoint["img_size"],
176
+ input_layout=checkpoint["input_layout"],
177
+ )
178
+ model.load_state_dict(checkpoint["model"])
179
+ self.model = model.to(self.device).eval()
180
+
181
+ self._class_names = list(checkpoint["class_names"])
182
+
183
+ norm = checkpoint["normalize"]
184
+ img_size = checkpoint["img_size"]
185
+ self._preprocess = transforms.Compose([
186
+ transforms.Resize((img_size, img_size), antialias=True),
187
+ transforms.ToTensor(),
188
+ transforms.Normalize(mean=norm["mean"], std=norm["std"]),
189
+ ])
190
+
191
+ def get_crop(
192
+ self, image: Image.Image, bbox: tuple[float, float, float, float]
193
+ ) -> Image.Image:
194
+ """Crop the bbox region. SpeciesNet head was trained on tight crops."""
195
+ W, H = image.size
196
+ x, y, w, h = bbox
197
+ left = max(0, int(round(x * W)))
198
+ top = max(0, int(round(y * H)))
199
+ right = min(W, int(round((x + w) * W)))
200
+ bottom = min(H, int(round((y + h) * H)))
201
+ if right <= left or bottom <= top:
202
+ return image
203
+ return image.crop((left, top, right, bottom))
204
+
205
+ def get_classification(self, crop: Image.Image) -> list[list]:
206
+ """Per-image inference. Returns [[name, prob], ...] for all classes."""
207
+ assert self.model is not None and self._preprocess is not None
208
+ if crop.mode != "RGB":
209
+ crop = crop.convert("RGB")
210
+ tensor = self._preprocess(crop).unsqueeze(0).to(self.device)
211
+ with torch.no_grad():
212
+ probs = F.softmax(self.model(tensor), dim=1).cpu().numpy()[0]
213
+ return [[self._class_names[i], float(probs[i])] for i in range(len(probs))]
214
+
215
+ def get_class_names(self) -> dict[str, str]:
216
+ """1-indexed mapping {id: class_name} for the output JSON."""
217
+ return {str(i + 1): name for i, name in enumerate(self._class_names)}
218
+
219
+ # ------------------------------------------------------------------
220
+ # Optional batch interface (5-15x GPU speedup vs per-crop calls)
221
+ # ------------------------------------------------------------------
222
+
223
+ def get_tensor(self, crop: Image.Image) -> np.ndarray:
224
+ assert self._preprocess is not None
225
+ if crop.mode != "RGB":
226
+ crop = crop.convert("RGB")
227
+ return self._preprocess(crop).numpy()
228
+
229
+ def classify_batch(self, batch: np.ndarray) -> list[list[list]]:
230
+ assert self.model is not None
231
+ tensor = torch.from_numpy(batch).to(self.device)
232
+ with torch.no_grad():
233
+ probs = F.softmax(self.model(tensor), dim=1).cpu().numpy()
234
+ return [
235
+ [[self._class_names[j], float(p[j])] for j in range(len(p))]
236
+ for p in probs
237
+ ]
taxonomy.csv ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ model_class,class,order,family,genus,species,variant
2
+ false detection,,,,,,
3
+ passerine,aves,passeriformes,,,,
4
+ sandpiper,aves,charadriiformes,scolopacidae,,,
5
+ black bear,mammalia,carnivora,ursidae,ursus,americanus,
6
+ grizzly,mammalia,carnivora,ursidae,ursus,arctos,
7
+ polar bear,mammalia,carnivora,ursidae,ursus,maritimus,
8
+ weasel,mammalia,carnivora,mustelidae,mustela,,
9
+ arctic hare,mammalia,lagomorpha,leporidae,lepus,arcticus,
10
+ snowshoe hare,mammalia,lagomorpha,leporidae,lepus,americanus,
11
+ red squirrel,mammalia,rodentia,sciuridae,tamiasciurus,hudsonicus,
12
+ moose,mammalia,artiodactyla,cervidae,alces,alces,
13
+ ptarmigan,aves,galliformes,phasianidae,lagopus,lagopus,
14
+ spruce grouse,aves,galliformes,phasianidae,canachites,canadensis,
15
+ golden plover,aves,charadriiformes,charadriidae,pluvialis,,
16
+ bald eagle,aves,accipitriformes,accipitridae,haliaeetus,leucocephalus,
17
+ golden eagle,aves,accipitriformes,accipitridae,aquila,chrysaetos,
18
+ glaucous gull,aves,charadriiformes,laridae,larus,hyperboreus,
19
+ herring gull,aves,charadriiformes,laridae,larus,argentatus,
20
+ parasitic jaeger,aves,charadriiformes,stercorariidae,stercorarius,parasiticus,
21
+ whimbrel,aves,charadriiformes,scolopacidae,numenius,phaeopus,
22
+ canada jay,aves,passeriformes,corvidae,perisoreus,canadensis,
23
+ raven,aves,passeriformes,corvidae,corvus,corax,
24
+ snow bunting,aves,passeriformes,calcariidae,plectrophenax,nivalis,
25
+ snow goose,aves,anseriformes,anatidae,anser,caerulescens,
26
+ lemming,mammalia,rodentia,cricetidae,,,
27
+ pine marten,mammalia,carnivora,mustelidae,martes,americana,
28
+ wolverine,mammalia,carnivora,mustelidae,gulo,gulo,
29
+ wolf,mammalia,carnivora,canidae,canis,lupus,
30
+ red fox adult,mammalia,carnivora,canidae,vulpes,vulpes,adult
31
+ red fox juvenile,mammalia,carnivora,canidae,vulpes,vulpes,juvenile
32
+ arctic fox adult,mammalia,carnivora,canidae,vulpes,lagopus,adult
33
+ arctic fox juvenile,mammalia,carnivora,canidae,vulpes,lagopus,juvenile
34
+ caribou,mammalia,artiodactyla,cervidae,rangifer,tarandus,
35
+ canada goose,aves,anseriformes,anatidae,branta,canadensis,
36
+ snowy owl,aves,strigiformes,strigidae,bubo,scandiacus,
37
+ northern harrier,aves,accipitriformes,accipitridae,circus,hudsonius,
38
+ swan,aves,anseriformes,anatidae,cygnus,,
39
+ crane,aves,gruiformes,gruidae,antigone,canadensis,
40
+ american robin,aves,passeriformes,turdidae,turdus,migratorius,