16
16
17
17
18
18
__all__ = [
19
- "FasterRCNN" , "fasterrcnn_resnet50_fpn" , "fasterrcnn_mobilenet_v3_large_fpn"
19
+ "FasterRCNN" , "fasterrcnn_resnet50_fpn" , "fasterrcnn_mobilenet_v3_large_320_fpn" ,
20
+ "fasterrcnn_mobilenet_v3_large_fpn"
20
21
]
21
22
22
23
@@ -288,8 +289,10 @@ def forward(self, x):
288
289
model_urls = {
289
290
'fasterrcnn_resnet50_fpn_coco' :
290
291
'https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth' ,
292
+ 'fasterrcnn_mobilenet_v3_large_320_fpn_coco' :
293
+ 'https://download.pytorch.org/models/fasterrcnn_mobilenet_v3_large_320_fpn-907ea3f9.pth' ,
291
294
'fasterrcnn_mobilenet_v3_large_fpn_coco' :
292
- 'https://download.pytorch.org/models/fasterrcnn_mobilenet_v3_large_fpn-907ea3f9 .pth' ,
295
+ 'https://download.pytorch.org/models/fasterrcnn_mobilenet_v3_large_fpn-fb6a3cc7 .pth'
293
296
}
294
297
295
298
@@ -368,16 +371,38 @@ def fasterrcnn_resnet50_fpn(pretrained=False, progress=True,
368
371
return model
369
372
370
373
371
- def fasterrcnn_mobilenet_v3_large_fpn (pretrained = False , progress = True , num_classes = 91 , pretrained_backbone = True ,
372
- trainable_backbone_layers = None , min_size = 320 , max_size = 640 , rpn_score_thresh = 0.05 ,
373
- ** kwargs ):
374
+ def _fasterrcnn_mobilenet_v3_large_fpn (weights_name , pretrained = False , progress = True , num_classes = 91 ,
375
+ pretrained_backbone = True , trainable_backbone_layers = None , ** kwargs ):
376
+ trainable_backbone_layers = _validate_trainable_layers (
377
+ pretrained or pretrained_backbone , trainable_backbone_layers , 6 , 3 )
378
+
379
+ if pretrained :
380
+ pretrained_backbone = False
381
+ backbone = mobilenet_backbone ("mobilenet_v3_large" , pretrained_backbone , True ,
382
+ trainable_layers = trainable_backbone_layers )
383
+
384
+ anchor_sizes = ((32 , 64 , 128 , 256 , 512 , ), ) * 3
385
+ aspect_ratios = ((0.5 , 1.0 , 2.0 ),) * len (anchor_sizes )
386
+
387
+ model = FasterRCNN (backbone , num_classes , rpn_anchor_generator = AnchorGenerator (anchor_sizes , aspect_ratios ),
388
+ ** kwargs )
389
+ if pretrained :
390
+ if model_urls .get (weights_name , None ) is None :
391
+ raise ValueError ("No checkpoint is available for model {}" .format (weights_name ))
392
+ state_dict = load_state_dict_from_url (model_urls [weights_name ], progress = progress )
393
+ model .load_state_dict (state_dict )
394
+ return model
395
+
396
+
397
+ def fasterrcnn_mobilenet_v3_large_320_fpn (pretrained = False , progress = True , num_classes = 91 , pretrained_backbone = True ,
398
+ trainable_backbone_layers = None , ** kwargs ):
374
399
"""
375
- Constructs a Faster R-CNN model with a MobileNetV3-Large FPN backbone. It works similarly
376
- to Faster R-CNN with ResNet-50 FPN backbone. See `fasterrcnn_resnet50_fpn` for more details.
400
+ Constructs a low resolution Faster R-CNN model with a MobileNetV3-Large FPN backbone tunned for mobile use-cases.
401
+ It works similarly to Faster R-CNN with ResNet-50 FPN backbone. See `fasterrcnn_resnet50_fpn` for more details.
377
402
378
403
Example::
379
404
380
- >>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn (pretrained=True)
405
+ >>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn (pretrained=True)
381
406
>>> model.eval()
382
407
>>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
383
408
>>> predictions = model(x)
@@ -389,25 +414,49 @@ def fasterrcnn_mobilenet_v3_large_fpn(pretrained=False, progress=True, num_class
389
414
pretrained_backbone (bool): If True, returns a model with backbone pre-trained on Imagenet
390
415
trainable_backbone_layers (int): number of trainable (not frozen) resnet layers starting from final block.
391
416
Valid values are between 0 and 6, with 6 meaning all backbone layers are trainable.
392
- min_size (int): minimum size of the image to be rescaled before feeding it to the backbone
393
- max_size (int): maximum size of the image to be rescaled before feeding it to the backbone
394
- rpn_score_thresh (float): during inference, only return proposals with a classification score
395
- greater than rpn_score_thresh
396
417
"""
397
- trainable_backbone_layers = _validate_trainable_layers (
398
- pretrained or pretrained_backbone , trainable_backbone_layers , 6 , 3 )
418
+ weights_name = "fasterrcnn_mobilenet_v3_large_320_fpn_coco"
419
+ defaults = {
420
+ "min_size" : 320 ,
421
+ "max_size" : 640 ,
422
+ "rpn_pre_nms_top_n_test" : 150 ,
423
+ "rpn_post_nms_top_n_test" : 150 ,
424
+ "rpn_score_thresh" : 0.05 ,
425
+ }
399
426
400
- if pretrained :
401
- pretrained_backbone = False
402
- backbone = mobilenet_backbone ( "mobilenet_v3_large" , pretrained_backbone , True ,
403
- trainable_layers = trainable_backbone_layers )
427
+ kwargs = { ** defaults , ** kwargs }
428
+ return _fasterrcnn_mobilenet_v3_large_fpn ( weights_name , pretrained = pretrained , progress = progress ,
429
+ num_classes = num_classes , pretrained_backbone = pretrained_backbone ,
430
+ trainable_backbone_layers = trainable_backbone_layers , ** kwargs )
404
431
405
- anchor_sizes = ((32 , 64 , 128 , 256 , 512 , ), ) * 3
406
- aspect_ratios = ((0.5 , 1.0 , 2.0 ),) * len (anchor_sizes )
407
432
408
- model = FasterRCNN (backbone , num_classes , rpn_anchor_generator = AnchorGenerator (anchor_sizes , aspect_ratios ),
409
- min_size = min_size , max_size = max_size , rpn_score_thresh = rpn_score_thresh , ** kwargs )
410
- if pretrained :
411
- state_dict = load_state_dict_from_url (model_urls ['fasterrcnn_mobilenet_v3_large_fpn_coco' ], progress = progress )
412
- model .load_state_dict (state_dict )
413
- return model
433
+ def fasterrcnn_mobilenet_v3_large_fpn (pretrained = False , progress = True , num_classes = 91 , pretrained_backbone = True ,
434
+ trainable_backbone_layers = None , ** kwargs ):
435
+ """
436
+ Constructs a high resolution Faster R-CNN model with a MobileNetV3-Large FPN backbone.
437
+ It works similarly to Faster R-CNN with ResNet-50 FPN backbone. See `fasterrcnn_resnet50_fpn` for more details.
438
+
439
+ Example::
440
+
441
+ >>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True)
442
+ >>> model.eval()
443
+ >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
444
+ >>> predictions = model(x)
445
+
446
+ Args:
447
+ pretrained (bool): If True, returns a model pre-trained on COCO train2017
448
+ progress (bool): If True, displays a progress bar of the download to stderr
449
+ num_classes (int): number of output classes of the model (including the background)
450
+ pretrained_backbone (bool): If True, returns a model with backbone pre-trained on Imagenet
451
+ trainable_backbone_layers (int): number of trainable (not frozen) resnet layers starting from final block.
452
+ Valid values are between 0 and 6, with 6 meaning all backbone layers are trainable.
453
+ """
454
+ weights_name = "fasterrcnn_mobilenet_v3_large_fpn_coco"
455
+ defaults = {
456
+ "rpn_score_thresh" : 0.05 ,
457
+ }
458
+
459
+ kwargs = {** defaults , ** kwargs }
460
+ return _fasterrcnn_mobilenet_v3_large_fpn (weights_name , pretrained = pretrained , progress = progress ,
461
+ num_classes = num_classes , pretrained_backbone = pretrained_backbone ,
462
+ trainable_backbone_layers = trainable_backbone_layers , ** kwargs )
0 commit comments