-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsect_debris_classifier.py
More file actions
176 lines (142 loc) · 5.27 KB
/
Copy pathinsect_debris_classifier.py
File metadata and controls
176 lines (142 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"""Runtime insect-vs-debris classifier for BugPicker detector crops."""
from __future__ import annotations
from pathlib import Path
from typing import Any
import cv2
import torch
import torch.nn as nn
from PIL import Image
from torchvision import models, transforms
CLASS_NAMES = ["debris", "insect"]
IMAGE_SIZE = 224
DROPOUT = 0.20
def select_device(requested: str = "auto") -> torch.device:
if requested != "auto":
return torch.device(requested)
if torch.cuda.is_available():
return torch.device("cuda")
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return torch.device("mps")
return torch.device("cpu")
def build_model(
architecture: str,
number_of_classes: int,
) -> nn.Module:
if architecture == "efficientnet_b0":
model = models.efficientnet_b0(weights=None)
input_features = model.classifier[1].in_features
model.classifier = nn.Sequential(
nn.Dropout(p=DROPOUT),
nn.Linear(input_features, number_of_classes),
)
return model
raise ValueError(
f"Unsupported classifier architecture {architecture!r}. "
"This production patch currently supports efficientnet_b0."
)
def build_inference_transform():
return transforms.Compose(
[
transforms.Resize(int(IMAGE_SIZE * 1.14)),
transforms.CenterCrop(IMAGE_SIZE),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
),
]
)
class InsectDebrisClassifier:
"""Load one checkpoint and classify OpenCV BGR crops."""
def __init__(
self,
model_path: Path,
architecture: str = "efficientnet_b0",
threshold: float = 0.50,
device: str = "auto",
) -> None:
self.model_path = Path(model_path).resolve()
if not self.model_path.exists():
raise FileNotFoundError(
f"Insect/debris classifier checkpoint not found: {self.model_path}"
)
self.device = select_device(device)
self.threshold = float(threshold)
self.transform = build_inference_transform()
checkpoint: Any = torch.load(
self.model_path,
map_location=self.device,
weights_only=False,
)
if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
state_dict = checkpoint["model_state_dict"]
checkpoint_class_names = checkpoint.get("class_names")
elif isinstance(checkpoint, dict):
# Support a raw state_dict checkpoint.
state_dict = checkpoint
checkpoint_class_names = None
else:
raise TypeError(
"Classifier checkpoint must be a state_dict or a dictionary "
"containing model_state_dict."
)
self.class_names = list(
checkpoint_class_names
if checkpoint_class_names
else CLASS_NAMES
)
if "debris" not in self.class_names:
raise ValueError(
f"Checkpoint class_names must contain 'debris'; found {self.class_names}"
)
insect_label = None
for candidate in ("insect", "specimen"):
if candidate in self.class_names:
insect_label = candidate
break
if insect_label is None:
raise ValueError(
"Checkpoint class_names must contain 'insect' or 'specimen'; "
f"found {self.class_names}"
)
self.debris_index = self.class_names.index("debris")
self.insect_index = self.class_names.index(insect_label)
self.model = build_model(
architecture=architecture,
number_of_classes=len(self.class_names),
)
self.model.load_state_dict(state_dict)
self.model.to(self.device)
self.model.eval()
print(
"Loaded insect/debris classifier "
f"model={self.model_path} "
f"architecture={architecture} "
f"device={self.device} "
f"threshold={self.threshold:.2f} "
f"classes={self.class_names}",
flush=True,
)
def classify_bgr(self, image_bgr) -> dict[str, Any]:
if image_bgr is None or image_bgr.size == 0:
raise ValueError("Classifier received an empty image crop")
image_rgb = cv2.cvtColor(
image_bgr,
cv2.COLOR_BGR2RGB,
)
image = Image.fromarray(image_rgb)
tensor = self.transform(image).unsqueeze(0).to(self.device)
with torch.inference_mode():
logits = self.model(tensor)
probabilities = torch.softmax(logits, dim=1)[0].detach().cpu()
insect_probability = float(probabilities[self.insect_index])
debris_probability = float(probabilities[self.debris_index])
would_pick = insect_probability >= self.threshold
return {
"class": "insect" if would_pick else "debris",
"insect_probability": insect_probability,
"debris_probability": debris_probability,
"threshold": self.threshold,
"would_pick": would_pick,
}