Skip to content

Commit e1f61a4

Browse files
authored
Increase number of detections FasterRCNN will return (#95)
* fix: increase number of detections FasterRCNN will return * fix: set limit to something more reasonable
1 parent bf0fe16 commit e1f61a4

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

trapdata/api/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ async def process(data: PipelineRequest) -> PipelineResponse:
295295
logger.info(f"Returning {len(detections_to_return)} detections")
296296
# print(all_detections)
297297

298-
# If the number of detections is greater than 100, its suspicious. Log it.
299-
if len(detections_to_return) > 100:
298+
# If the number of detections is greater than 200, its suspicious. Log it.
299+
if len(detections_to_return) > 200:
300300
logger.warning(
301301
f"Detected {len(detections_to_return)} detections. "
302302
"This is suspicious and may contain duplicates."

trapdata/ml/models/localization.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pathlib
22

3-
import PIL.Image
43
import torch
54
import torchvision
65
import torchvision.models.detection.anchor_utils
@@ -206,9 +205,13 @@ class MothObjectDetector_FasterRCNN_2021(ObjectDetector):
206205
"Accurate but can be slow on a machine without GPU."
207206
)
208207
bbox_score_threshold = 0.99
208+
box_detections_per_img = 500
209209

210210
def get_model(self):
211-
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=None)
211+
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(
212+
weights=None,
213+
box_detections_per_img=self.box_detections_per_img,
214+
)
212215
num_classes = 2 # 1 class (object) + background
213216
in_features = model.roi_heads.box_predictor.cls_score.in_features
214217
model.roi_heads.box_predictor = (
@@ -249,6 +252,7 @@ class MothObjectDetector_FasterRCNN_2023(ObjectDetector):
249252
"Accurate but can be slow on a machine without GPU."
250253
)
251254
bbox_score_threshold = 0.80
255+
box_detections_per_img = 500
252256

253257
def get_model(self):
254258
num_classes = 2 # 1 class (object) + background
@@ -257,6 +261,7 @@ def get_model(self):
257261
name="fasterrcnn_resnet50_fpn",
258262
num_classes=num_classes,
259263
weights=None,
264+
box_detections_per_img=self.box_detections_per_img,
260265
)
261266
checkpoint = torch.load(self.weights, map_location=self.device)
262267
state_dict = checkpoint.get("model_state_dict") or checkpoint
@@ -293,6 +298,7 @@ class MothObjectDetector_FasterRCNN_MobileNet_2023(ObjectDetector):
293298
trainable_backbone_layers = 6 # all layers are trained
294299
anchor_sizes = (64, 128, 256, 512)
295300
num_classes = 2
301+
box_detections_per_img = 500
296302

297303
def get_model(self):
298304
norm_layer = torch.nn.BatchNorm2d
@@ -311,6 +317,7 @@ def get_model(self):
311317
anchor_sizes, aspect_ratios
312318
),
313319
rpn_score_thresh=0.05,
320+
box_detections_per_img=self.box_detections_per_img,
314321
)
315322
checkpoint = torch.load(self.weights, map_location=self.device)
316323
state_dict = checkpoint.get("model_state_dict") or checkpoint

0 commit comments

Comments
 (0)