11import pathlib
22
3- import PIL .Image
43import torch
54import torchvision
65import 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