line_net.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. from typing import Any, Callable, List, Optional, Tuple, Union
  2. import torch
  3. from torch import nn
  4. from torchvision.ops import MultiScaleRoIAlign
  5. from libs.vision_libs.models import MobileNet_V3_Large_Weights, mobilenet_v3_large
  6. from libs.vision_libs.models.detection.anchor_utils import AnchorGenerator
  7. from libs.vision_libs.models.detection.rpn import RPNHead, RegionProposalNetwork
  8. from libs.vision_libs.models.detection.ssdlite import _mobilenet_extractor
  9. from libs.vision_libs.models.detection.transform import GeneralizedRCNNTransform
  10. from libs.vision_libs.ops import misc as misc_nn_ops
  11. from libs.vision_libs.transforms._presets import ObjectDetection
  12. from .line_head import LineRCNNHeads
  13. from .line_predictor import LineRCNNPredictor
  14. from libs.vision_libs.models._api import register_model, Weights, WeightsEnum
  15. from libs.vision_libs.models._meta import _COCO_PERSON_CATEGORIES, _COCO_PERSON_KEYPOINT_NAMES, _COCO_CATEGORIES
  16. from libs.vision_libs.models._utils import _ovewrite_value_param, handle_legacy_interface
  17. from libs.vision_libs.models.resnet import resnet50, ResNet50_Weights, ResNet18_Weights, resnet18
  18. from libs.vision_libs.models.detection._utils import overwrite_eps
  19. from libs.vision_libs.models.detection.backbone_utils import _resnet_fpn_extractor, _validate_trainable_layers
  20. from libs.vision_libs.models.detection.faster_rcnn import FasterRCNN, TwoMLPHead, FastRCNNPredictor
  21. from .roi_heads import RoIHeads
  22. from .trainer import Trainer
  23. from ..base import backbone_factory
  24. from ..base.base_detection_net import BaseDetectionNet
  25. import torch.nn.functional as F
  26. from ..config.config_tool import read_yaml
  27. FEATURE_DIM = 8
  28. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  29. __all__ = [
  30. "LineNet",
  31. "LineNet_ResNet50_FPN_Weights",
  32. "LineNet_ResNet50_FPN_V2_Weights",
  33. "LineNet_MobileNet_V3_Large_FPN_Weights",
  34. "LineNet_MobileNet_V3_Large_320_FPN_Weights",
  35. "linenet_resnet50_fpn",
  36. "linenet_resnet50_fpn_v2",
  37. "linenet_mobilenet_v3_large_fpn",
  38. "linenet_mobilenet_v3_large_320_fpn",
  39. ]
  40. def _default_anchorgen():
  41. anchor_sizes = ((32,), (64,), (128,), (256,), (512,))
  42. aspect_ratios = ((0.5, 1.0, 2.0),) * len(anchor_sizes)
  43. return AnchorGenerator(anchor_sizes, aspect_ratios)
  44. class LineNet(BaseDetectionNet):
  45. # def __init__(self, cfg, **kwargs):
  46. # cfg = read_yaml(cfg)
  47. # self.cfg=cfg
  48. # backbone = cfg['backbone']
  49. # print(f'LineNet Backbone:{backbone}')
  50. # num_classes = cfg['num_classes']
  51. #
  52. # if backbone == 'resnet50_fpn':
  53. # backbone=backbone_factory.get_resnet50_fpn()
  54. # print(f'out_chanenels:{backbone.out_channels}')
  55. # elif backbone== 'mobilenet_v3_large_fpn':
  56. # backbone=backbone_factory.get_mobilenet_v3_large_fpn()
  57. # elif backbone=='resnet18_fpn':
  58. # backbone=backbone_factory.get_resnet18_fpn()
  59. #
  60. # self.__construct__(backbone=backbone, num_classes=num_classes, **kwargs)
  61. def __init__(
  62. self,
  63. backbone,
  64. num_classes=None,
  65. # transform parameters
  66. min_size=512,
  67. max_size=1333,
  68. image_mean=None,
  69. image_std=None,
  70. # RPN parameters
  71. rpn_anchor_generator=None,
  72. rpn_head=None,
  73. rpn_pre_nms_top_n_train=2000,
  74. rpn_pre_nms_top_n_test=1000,
  75. rpn_post_nms_top_n_train=2000,
  76. rpn_post_nms_top_n_test=1000,
  77. rpn_nms_thresh=0.7,
  78. rpn_fg_iou_thresh=0.7,
  79. rpn_bg_iou_thresh=0.3,
  80. rpn_batch_size_per_image=256,
  81. rpn_positive_fraction=0.5,
  82. rpn_score_thresh=0.0,
  83. # Box parameters
  84. box_roi_pool=None,
  85. box_head=None,
  86. box_predictor=None,
  87. box_score_thresh=0.05,
  88. box_nms_thresh=0.5,
  89. box_detections_per_img=100,
  90. box_fg_iou_thresh=0.5,
  91. box_bg_iou_thresh=0.5,
  92. box_batch_size_per_image=512,
  93. box_positive_fraction=0.25,
  94. bbox_reg_weights=None,
  95. # line parameters
  96. line_head=None,
  97. line_predictor=None,
  98. **kwargs,
  99. ):
  100. if not hasattr(backbone, "out_channels"):
  101. raise ValueError(
  102. "backbone should contain an attribute out_channels "
  103. "specifying the number of output channels (assumed to be the "
  104. "same for all the levels)"
  105. )
  106. if not isinstance(rpn_anchor_generator, (AnchorGenerator, type(None))):
  107. raise TypeError(
  108. f"rpn_anchor_generator should be of type AnchorGenerator or None instead of {type(rpn_anchor_generator)}"
  109. )
  110. if not isinstance(box_roi_pool, (MultiScaleRoIAlign, type(None))):
  111. raise TypeError(
  112. f"box_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(box_roi_pool)}"
  113. )
  114. if num_classes is not None:
  115. if box_predictor is not None:
  116. raise ValueError("num_classes should be None when box_predictor is specified")
  117. else:
  118. if box_predictor is None:
  119. raise ValueError("num_classes should not be None when box_predictor is not specified")
  120. out_channels = backbone.out_channels
  121. # cfg = read_yaml(cfg)
  122. # self.cfg=cfg
  123. if line_head is None:
  124. num_class = 5
  125. line_head = LineRCNNHeads(out_channels, num_class)
  126. if line_predictor is None:
  127. line_predictor = LineRCNNPredictor()
  128. if rpn_anchor_generator is None:
  129. rpn_anchor_generator = _default_anchorgen()
  130. if rpn_head is None:
  131. rpn_head = RPNHead(out_channels, rpn_anchor_generator.num_anchors_per_location()[0])
  132. rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
  133. rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)
  134. rpn = RegionProposalNetwork(
  135. rpn_anchor_generator,
  136. rpn_head,
  137. rpn_fg_iou_thresh,
  138. rpn_bg_iou_thresh,
  139. rpn_batch_size_per_image,
  140. rpn_positive_fraction,
  141. rpn_pre_nms_top_n,
  142. rpn_post_nms_top_n,
  143. rpn_nms_thresh,
  144. score_thresh=rpn_score_thresh,
  145. )
  146. if box_roi_pool is None:
  147. box_roi_pool = MultiScaleRoIAlign(featmap_names=["0", "1", "2", "3"], output_size=7, sampling_ratio=2)
  148. if box_head is None:
  149. resolution = box_roi_pool.output_size[0]
  150. representation_size = 1024
  151. box_head = TwoMLPHead(out_channels * resolution ** 2, representation_size)
  152. if box_predictor is None:
  153. representation_size = 1024
  154. box_predictor = BoxPredictor(representation_size, num_classes)
  155. roi_heads = RoIHeads(
  156. # Box
  157. box_roi_pool,
  158. box_head,
  159. box_predictor,
  160. line_head,
  161. line_predictor,
  162. box_fg_iou_thresh,
  163. box_bg_iou_thresh,
  164. box_batch_size_per_image,
  165. box_positive_fraction,
  166. bbox_reg_weights,
  167. box_score_thresh,
  168. box_nms_thresh,
  169. box_detections_per_img,
  170. )
  171. if image_mean is None:
  172. image_mean = [0.485, 0.456, 0.406]
  173. if image_std is None:
  174. image_std = [0.229, 0.224, 0.225]
  175. transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std, **kwargs)
  176. super().__init__(backbone, rpn, roi_heads, transform)
  177. self.roi_heads = roi_heads
  178. # self.roi_heads.line_head = line_head
  179. # self.roi_heads.line_predictor = line_predictor
  180. def train_by_cfg(self, cfg):
  181. # cfg = read_yaml(cfg)
  182. self.trainer = Trainer()
  183. self.trainer.train_cfg(model=self, cfg=cfg)
  184. class TwoMLPHead(nn.Module):
  185. """
  186. Standard heads for FPN-based models
  187. Args:
  188. in_channels (int): number of input channels
  189. representation_size (int): size of the intermediate representation
  190. """
  191. def __init__(self, in_channels, representation_size):
  192. super().__init__()
  193. self.fc6 = nn.Linear(in_channels, representation_size)
  194. self.fc7 = nn.Linear(representation_size, representation_size)
  195. def forward(self, x):
  196. x = x.flatten(start_dim=1)
  197. x = F.relu(self.fc6(x))
  198. x = F.relu(self.fc7(x))
  199. return x
  200. class LineNetConvFCHead(nn.Sequential):
  201. def __init__(
  202. self,
  203. input_size: Tuple[int, int, int],
  204. conv_layers: List[int],
  205. fc_layers: List[int],
  206. norm_layer: Optional[Callable[..., nn.Module]] = None,
  207. ):
  208. """
  209. Args:
  210. input_size (Tuple[int, int, int]): the input size in CHW format.
  211. conv_layers (list): feature dimensions of each Convolution layer
  212. fc_layers (list): feature dimensions of each FCN layer
  213. norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
  214. """
  215. in_channels, in_height, in_width = input_size
  216. blocks = []
  217. previous_channels = in_channels
  218. for current_channels in conv_layers:
  219. blocks.append(misc_nn_ops.Conv2dNormActivation(previous_channels, current_channels, norm_layer=norm_layer))
  220. previous_channels = current_channels
  221. blocks.append(nn.Flatten())
  222. previous_channels = previous_channels * in_height * in_width
  223. for current_channels in fc_layers:
  224. blocks.append(nn.Linear(previous_channels, current_channels))
  225. blocks.append(nn.ReLU(inplace=True))
  226. previous_channels = current_channels
  227. super().__init__(*blocks)
  228. for layer in self.modules():
  229. if isinstance(layer, nn.Conv2d):
  230. nn.init.kaiming_normal_(layer.weight, mode="fan_out", nonlinearity="relu")
  231. if layer.bias is not None:
  232. nn.init.zeros_(layer.bias)
  233. class BoxPredictor(nn.Module):
  234. """
  235. Standard classification + bounding box regression layers
  236. for Fast R-CNN.
  237. Args:
  238. in_channels (int): number of input channels
  239. num_classes (int): number of output classes (including background)
  240. """
  241. def __init__(self, in_channels, num_classes):
  242. super().__init__()
  243. self.cls_score = nn.Linear(in_channels, num_classes)
  244. self.bbox_pred = nn.Linear(in_channels, num_classes * 4)
  245. def forward(self, x):
  246. if x.dim() == 4:
  247. torch._assert(
  248. list(x.shape[2:]) == [1, 1],
  249. f"x has the wrong shape, expecting the last two dimensions to be [1,1] instead of {list(x.shape[2:])}",
  250. )
  251. x = x.flatten(start_dim=1)
  252. scores = self.cls_score(x)
  253. bbox_deltas = self.bbox_pred(x)
  254. return scores, bbox_deltas
  255. _COMMON_META = {
  256. "categories": _COCO_CATEGORIES,
  257. "min_size": (1, 1),
  258. }
  259. class LineNet_ResNet50_FPN_Weights(WeightsEnum):
  260. COCO_V1 = Weights(
  261. url="https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth",
  262. transforms=ObjectDetection,
  263. meta={
  264. **_COMMON_META,
  265. "num_params": 41755286,
  266. "recipe": "https://github.com/pytorch/vision/tree/main/references/detection#faster-r-cnn-resnet-50-fpn",
  267. "_metrics": {
  268. "COCO-val2017": {
  269. "box_map": 37.0,
  270. }
  271. },
  272. "_ops": 134.38,
  273. "_file_size": 159.743,
  274. "_docs": """These weights were produced by following a similar training recipe as on the paper.""",
  275. },
  276. )
  277. DEFAULT = COCO_V1
  278. class LineNet_ResNet50_FPN_V2_Weights(WeightsEnum):
  279. COCO_V1 = Weights(
  280. url="https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_v2_coco-dd69338a.pth",
  281. transforms=ObjectDetection,
  282. meta={
  283. **_COMMON_META,
  284. "num_params": 43712278,
  285. "recipe": "https://github.com/pytorch/vision/pull/5763",
  286. "_metrics": {
  287. "COCO-val2017": {
  288. "box_map": 46.7,
  289. }
  290. },
  291. "_ops": 280.371,
  292. "_file_size": 167.104,
  293. "_docs": """These weights were produced using an enhanced training recipe to boost the model accuracy.""",
  294. },
  295. )
  296. DEFAULT = COCO_V1
  297. class LineNet_MobileNet_V3_Large_FPN_Weights(WeightsEnum):
  298. COCO_V1 = Weights(
  299. url="https://download.pytorch.org/models/fasterrcnn_mobilenet_v3_large_fpn-fb6a3cc7.pth",
  300. transforms=ObjectDetection,
  301. meta={
  302. **_COMMON_META,
  303. "num_params": 19386354,
  304. "recipe": "https://github.com/pytorch/vision/tree/main/references/detection#faster-r-cnn-mobilenetv3-large-fpn",
  305. "_metrics": {
  306. "COCO-val2017": {
  307. "box_map": 32.8,
  308. }
  309. },
  310. "_ops": 4.494,
  311. "_file_size": 74.239,
  312. "_docs": """These weights were produced by following a similar training recipe as on the paper.""",
  313. },
  314. )
  315. DEFAULT = COCO_V1
  316. class LineNet_MobileNet_V3_Large_320_FPN_Weights(WeightsEnum):
  317. COCO_V1 = Weights(
  318. url="https://download.pytorch.org/models/fasterrcnn_mobilenet_v3_large_320_fpn-907ea3f9.pth",
  319. transforms=ObjectDetection,
  320. meta={
  321. **_COMMON_META,
  322. "num_params": 19386354,
  323. "recipe": "https://github.com/pytorch/vision/tree/main/references/detection#faster-r-cnn-mobilenetv3-large-320-fpn",
  324. "_metrics": {
  325. "COCO-val2017": {
  326. "box_map": 22.8,
  327. }
  328. },
  329. "_ops": 0.719,
  330. "_file_size": 74.239,
  331. "_docs": """These weights were produced by following a similar training recipe as on the paper.""",
  332. },
  333. )
  334. DEFAULT = COCO_V1
  335. @register_model()
  336. @handle_legacy_interface(
  337. weights=("pretrained", LineNet_ResNet50_FPN_Weights.COCO_V1),
  338. weights_backbone=("pretrained_backbone", ResNet50_Weights.IMAGENET1K_V1),
  339. )
  340. def linenet_resnet18_fpn(
  341. *,
  342. weights: Optional[LineNet_ResNet50_FPN_Weights] = None,
  343. progress: bool = True,
  344. num_classes: Optional[int] = None,
  345. weights_backbone: Optional[ResNet18_Weights] = ResNet18_Weights.IMAGENET1K_V1,
  346. trainable_backbone_layers: Optional[int] = None,
  347. **kwargs: Any,
  348. ) -> LineNet:
  349. # weights = LineNet_ResNet50_FPN_Weights.verify(weights)
  350. # weights_backbone = ResNet50_Weights.verify(weights_backbone)
  351. if weights is not None:
  352. weights_backbone = None
  353. num_classes = _ovewrite_value_param("num_classes", num_classes, len(weights.meta["categories"]))
  354. elif num_classes is None:
  355. num_classes = 91
  356. if weights_backbone is not None:
  357. print(f'resnet50 weights is not None')
  358. is_trained = weights is not None or weights_backbone is not None
  359. trainable_backbone_layers = _validate_trainable_layers(is_trained, trainable_backbone_layers, 5, 3)
  360. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  361. backbone = resnet18(weights=weights_backbone, progress=progress, norm_layer=norm_layer)
  362. backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers)
  363. model = LineNet(backbone, num_classes=num_classes, **kwargs)
  364. if weights is not None:
  365. model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
  366. if weights == LineNet_ResNet50_FPN_Weights.COCO_V1:
  367. overwrite_eps(model, 0.0)
  368. return model
  369. def linenet_resnet50_fpn(
  370. *,
  371. weights: Optional[LineNet_ResNet50_FPN_Weights] = None,
  372. progress: bool = True,
  373. num_classes: Optional[int] = None,
  374. weights_backbone: Optional[ResNet50_Weights] = ResNet50_Weights.IMAGENET1K_V1,
  375. trainable_backbone_layers: Optional[int] = None,
  376. **kwargs: Any,
  377. ) -> LineNet:
  378. """
  379. Faster R-CNN model with a ResNet-50-FPN backbone from the `Faster R-CNN: Towards Real-Time Object
  380. Detection with Region Proposal Networks <https://arxiv.org/abs/1506.01497>`__
  381. paper.
  382. .. betastatus:: detection module
  383. The input to the model is expected to be a list of tensors, each of shape ``[C, H, W]``, one for each
  384. image, and should be in ``0-1`` range. Different images can have different sizes.
  385. The behavior of the model changes depending on if it is in training or evaluation mode.
  386. During training, the model expects both the input tensors and a targets (list of dictionary),
  387. containing:
  388. - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
  389. ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
  390. - labels (``Int64Tensor[N]``): the class label for each ground-truth box
  391. The model returns a ``Dict[Tensor]`` during training, containing the classification and regression
  392. losses for both the RPN and the R-CNN.
  393. During inference, the model requires only the input tensors, and returns the post-processed
  394. predictions as a ``List[Dict[Tensor]]``, one for each input image. The fields of the ``Dict`` are as
  395. follows, where ``N`` is the number of detections:
  396. - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
  397. ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
  398. - labels (``Int64Tensor[N]``): the predicted labels for each detection
  399. - scores (``Tensor[N]``): the scores of each detection
  400. For more details on the output, you may refer to :ref:`instance_seg_output`.
  401. Faster R-CNN is exportable to ONNX for a fixed batch size with inputs images of fixed size.
  402. Example::
  403. >>> model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
  404. >>> # For training
  405. >>> images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
  406. >>> boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]
  407. >>> labels = torch.randint(1, 91, (4, 11))
  408. >>> images = list(image for image in images)
  409. >>> targets = []
  410. >>> for i in range(len(images)):
  411. >>> d = {}
  412. >>> d['boxes'] = boxes[i]
  413. >>> d['labels'] = labels[i]
  414. >>> targets.append(d)
  415. >>> output = model(images, targets)
  416. >>> # For inference
  417. >>> model.eval()
  418. >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
  419. >>> predictions = model(x)
  420. >>>
  421. >>> # optionally, if you want to export the model to ONNX:
  422. >>> torch.onnx.export(model, x, "faster_rcnn.onnx", opset_version = 11)
  423. Args:
  424. weights (:class:`~torchvision.models.detection.FasterRCNN_ResNet50_FPN_Weights`, optional): The
  425. pretrained weights to use. See
  426. :class:`~torchvision.models.detection.FasterRCNN_ResNet50_FPN_Weights` below for
  427. more details, and possible values. By default, no pre-trained
  428. weights are used.
  429. progress (bool, optional): If True, displays a progress bar of the
  430. download to stderr. Default is True.
  431. num_classes (int, optional): number of output classes of the model (including the background)
  432. weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The
  433. pretrained weights for the backbone.
  434. trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from
  435. final block. Valid values are between 0 and 5, with 5 meaning all backbone layers are
  436. trainable. If ``None`` is passed (the default) this value is set to 3.
  437. **kwargs: parameters passed to the ``torchvision.models.detection.faster_rcnn.FasterRCNN``
  438. base class. Please refer to the `source code
  439. <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/faster_rcnn.py>`_
  440. for more details about this class.
  441. .. autoclass:: torchvision.models.detection.FasterRCNN_ResNet50_FPN_Weights
  442. :members:
  443. """
  444. weights = LineNet_ResNet50_FPN_Weights.verify(weights)
  445. weights_backbone = ResNet50_Weights.verify(weights_backbone)
  446. if weights is not None:
  447. weights_backbone = None
  448. num_classes = _ovewrite_value_param("num_classes", num_classes, len(weights.meta["categories"]))
  449. elif num_classes is None:
  450. num_classes = 91
  451. if weights_backbone is not None:
  452. print(f'resnet50 weights is not None')
  453. is_trained = weights is not None or weights_backbone is not None
  454. trainable_backbone_layers = _validate_trainable_layers(is_trained, trainable_backbone_layers, 5, 3)
  455. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  456. backbone = resnet50(weights=weights_backbone, progress=progress, norm_layer=norm_layer)
  457. backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers)
  458. model = LineNet(backbone, num_classes=num_classes, **kwargs)
  459. if weights is not None:
  460. model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
  461. if weights == LineNet_ResNet50_FPN_Weights.COCO_V1:
  462. overwrite_eps(model, 0.0)
  463. return model
  464. @register_model()
  465. @handle_legacy_interface(
  466. weights=("pretrained", LineNet_ResNet50_FPN_V2_Weights.COCO_V1),
  467. weights_backbone=("pretrained_backbone", ResNet50_Weights.IMAGENET1K_V1),
  468. )
  469. def linenet_resnet50_fpn_v2(
  470. *,
  471. weights: Optional[LineNet_ResNet50_FPN_V2_Weights] = None,
  472. progress: bool = True,
  473. num_classes: Optional[int] = None,
  474. weights_backbone: Optional[ResNet50_Weights] = None,
  475. trainable_backbone_layers: Optional[int] = None,
  476. **kwargs: Any,
  477. ) -> LineNet:
  478. """
  479. Constructs an improved Faster R-CNN model with a ResNet-50-FPN backbone from `Benchmarking Detection
  480. Transfer Learning with Vision Transformers <https://arxiv.org/abs/2111.11429>`__ paper.
  481. .. betastatus:: detection module
  482. It works similarly to Faster R-CNN with ResNet-50 FPN backbone. See
  483. :func:`~torchvision.models.detection.fasterrcnn_resnet50_fpn` for more
  484. details.
  485. Args:
  486. weights (:class:`~torchvision.models.detection.FasterRCNN_ResNet50_FPN_V2_Weights`, optional): The
  487. pretrained weights to use. See
  488. :class:`~torchvision.models.detection.FasterRCNN_ResNet50_FPN_V2_Weights` below for
  489. more details, and possible values. By default, no pre-trained
  490. weights are used.
  491. progress (bool, optional): If True, displays a progress bar of the
  492. download to stderr. Default is True.
  493. num_classes (int, optional): number of output classes of the model (including the background)
  494. weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The
  495. pretrained weights for the backbone.
  496. trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from
  497. final block. Valid values are between 0 and 5, with 5 meaning all backbone layers are
  498. trainable. If ``None`` is passed (the default) this value is set to 3.
  499. **kwargs: parameters passed to the ``torchvision.models.detection.faster_rcnn.FasterRCNN``
  500. base class. Please refer to the `source code
  501. <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/faster_rcnn.py>`_
  502. for more details about this class.
  503. .. autoclass:: torchvision.models.detection.FasterRCNN_ResNet50_FPN_V2_Weights
  504. :members:
  505. """
  506. weights = LineNet_ResNet50_FPN_V2_Weights.verify(weights)
  507. weights_backbone = ResNet50_Weights.verify(weights_backbone)
  508. if weights is not None:
  509. weights_backbone = None
  510. num_classes = _ovewrite_value_param("num_classes", num_classes, len(weights.meta["categories"]))
  511. elif num_classes is None:
  512. num_classes = 91
  513. is_trained = weights is not None or weights_backbone is not None
  514. trainable_backbone_layers = _validate_trainable_layers(is_trained, trainable_backbone_layers, 5, 3)
  515. backbone = resnet50(weights=weights_backbone, progress=progress)
  516. backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers, norm_layer=nn.BatchNorm2d)
  517. rpn_anchor_generator = _default_anchorgen()
  518. rpn_head = RPNHead(backbone.out_channels, rpn_anchor_generator.num_anchors_per_location()[0], conv_depth=2)
  519. box_head = LineNetConvFCHead(
  520. (backbone.out_channels, 7, 7), [256, 256, 256, 256], [1024], norm_layer=nn.BatchNorm2d
  521. )
  522. model = LineNet(
  523. backbone,
  524. num_classes=num_classes,
  525. rpn_anchor_generator=rpn_anchor_generator,
  526. rpn_head=rpn_head,
  527. box_head=box_head,
  528. **kwargs,
  529. )
  530. if weights is not None:
  531. model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
  532. return model
  533. def _linenet_mobilenet_v3_large_fpn(
  534. *,
  535. weights: Optional[Union[LineNet_MobileNet_V3_Large_FPN_Weights, LineNet_MobileNet_V3_Large_320_FPN_Weights]],
  536. progress: bool,
  537. num_classes: Optional[int],
  538. weights_backbone: Optional[MobileNet_V3_Large_Weights],
  539. trainable_backbone_layers: Optional[int],
  540. **kwargs: Any,
  541. ) -> LineNet:
  542. if weights is not None:
  543. weights_backbone = None
  544. num_classes = _ovewrite_value_param("num_classes", num_classes, len(weights.meta["categories"]))
  545. elif num_classes is None:
  546. num_classes = 91
  547. is_trained = weights is not None or weights_backbone is not None
  548. trainable_backbone_layers = _validate_trainable_layers(is_trained, trainable_backbone_layers, 6, 3)
  549. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  550. backbone = mobilenet_v3_large(weights=weights_backbone, progress=progress, norm_layer=norm_layer)
  551. backbone = _mobilenet_extractor(backbone, True, trainable_backbone_layers)
  552. anchor_sizes = (
  553. (
  554. 32,
  555. 64,
  556. 128,
  557. 256,
  558. 512,
  559. ),
  560. ) * 3
  561. aspect_ratios = ((0.5, 1.0, 2.0),) * len(anchor_sizes)
  562. model = LineNet(
  563. backbone, num_classes, rpn_anchor_generator=AnchorGenerator(anchor_sizes, aspect_ratios), **kwargs
  564. )
  565. if weights is not None:
  566. model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
  567. return model
  568. @register_model()
  569. @handle_legacy_interface(
  570. weights=("pretrained", LineNet_MobileNet_V3_Large_320_FPN_Weights.COCO_V1),
  571. weights_backbone=("pretrained_backbone", MobileNet_V3_Large_Weights.IMAGENET1K_V1),
  572. )
  573. def linenet_mobilenet_v3_large_320_fpn(
  574. *,
  575. weights: Optional[LineNet_MobileNet_V3_Large_320_FPN_Weights] = None,
  576. progress: bool = True,
  577. num_classes: Optional[int] = None,
  578. weights_backbone: Optional[MobileNet_V3_Large_Weights] = MobileNet_V3_Large_Weights.IMAGENET1K_V1,
  579. trainable_backbone_layers: Optional[int] = None,
  580. **kwargs: Any,
  581. ) -> LineNet:
  582. """
  583. Low resolution Faster R-CNN model with a MobileNetV3-Large backbone tuned for mobile use cases.
  584. .. betastatus:: detection module
  585. It works similarly to Faster R-CNN with ResNet-50 FPN backbone. See
  586. :func:`~torchvision.models.detection.fasterrcnn_resnet50_fpn` for more
  587. details.
  588. Example::
  589. >>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(weights=FasterRCNN_MobileNet_V3_Large_320_FPN_Weights.DEFAULT)
  590. >>> model.eval()
  591. >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
  592. >>> predictions = model(x)
  593. Args:
  594. weights (:class:`~torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_320_FPN_Weights`, optional): The
  595. pretrained weights to use. See
  596. :class:`~torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_320_FPN_Weights` below for
  597. more details, and possible values. By default, no pre-trained
  598. weights are used.
  599. progress (bool, optional): If True, displays a progress bar of the
  600. download to stderr. Default is True.
  601. num_classes (int, optional): number of output classes of the model (including the background)
  602. weights_backbone (:class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The
  603. pretrained weights for the backbone.
  604. trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from
  605. final block. Valid values are between 0 and 6, with 6 meaning all backbone layers are
  606. trainable. If ``None`` is passed (the default) this value is set to 3.
  607. **kwargs: parameters passed to the ``torchvision.models.detection.faster_rcnn.FasterRCNN``
  608. base class. Please refer to the `source code
  609. <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/faster_rcnn.py>`_
  610. for more details about this class.
  611. .. autoclass:: torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_320_FPN_Weights
  612. :members:
  613. """
  614. weights = LineNet_MobileNet_V3_Large_320_FPN_Weights.verify(weights)
  615. weights_backbone = MobileNet_V3_Large_Weights.verify(weights_backbone)
  616. defaults = {
  617. "min_size": 320,
  618. "max_size": 640,
  619. "rpn_pre_nms_top_n_test": 150,
  620. "rpn_post_nms_top_n_test": 150,
  621. "rpn_score_thresh": 0.05,
  622. }
  623. kwargs = {**defaults, **kwargs}
  624. return _linenet_mobilenet_v3_large_fpn(
  625. weights=weights,
  626. progress=progress,
  627. num_classes=num_classes,
  628. weights_backbone=weights_backbone,
  629. trainable_backbone_layers=trainable_backbone_layers,
  630. **kwargs,
  631. )
  632. @register_model()
  633. @handle_legacy_interface(
  634. weights=("pretrained", LineNet_MobileNet_V3_Large_FPN_Weights.COCO_V1),
  635. weights_backbone=("pretrained_backbone", MobileNet_V3_Large_Weights.IMAGENET1K_V1),
  636. )
  637. def linenet_mobilenet_v3_large_fpn(
  638. *,
  639. weights: Optional[LineNet_MobileNet_V3_Large_FPN_Weights] = None,
  640. progress: bool = True,
  641. num_classes: Optional[int] = None,
  642. weights_backbone: Optional[MobileNet_V3_Large_Weights] = MobileNet_V3_Large_Weights.IMAGENET1K_V1,
  643. trainable_backbone_layers: Optional[int] = None,
  644. **kwargs: Any,
  645. ) -> LineNet:
  646. """
  647. Constructs a high resolution Faster R-CNN model with a MobileNetV3-Large FPN backbone.
  648. .. betastatus:: detection module
  649. It works similarly to Faster R-CNN with ResNet-50 FPN backbone. See
  650. :func:`~torchvision.models.detection.fasterrcnn_resnet50_fpn` for more
  651. details.
  652. Example::
  653. >>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(weights=FasterRCNN_MobileNet_V3_Large_FPN_Weights.DEFAULT)
  654. >>> model.eval()
  655. >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
  656. >>> predictions = model(x)
  657. Args:
  658. weights (:class:`~torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_FPN_Weights`, optional): The
  659. pretrained weights to use. See
  660. :class:`~torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_FPN_Weights` below for
  661. more details, and possible values. By default, no pre-trained
  662. weights are used.
  663. progress (bool, optional): If True, displays a progress bar of the
  664. download to stderr. Default is True.
  665. num_classes (int, optional): number of output classes of the model (including the background)
  666. weights_backbone (:class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The
  667. pretrained weights for the backbone.
  668. trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from
  669. final block. Valid values are between 0 and 6, with 6 meaning all backbone layers are
  670. trainable. If ``None`` is passed (the default) this value is set to 3.
  671. **kwargs: parameters passed to the ``torchvision.models.detection.faster_rcnn.FasterRCNN``
  672. base class. Please refer to the `source code
  673. <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/faster_rcnn.py>`_
  674. for more details about this class.
  675. .. autoclass:: torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_FPN_Weights
  676. :members:
  677. """
  678. weights = LineNet_MobileNet_V3_Large_FPN_Weights.verify(weights)
  679. weights_backbone = MobileNet_V3_Large_Weights.verify(weights_backbone)
  680. defaults = {
  681. "rpn_score_thresh": 0.05,
  682. }
  683. kwargs = {**defaults, **kwargs}
  684. return _linenet_mobilenet_v3_large_fpn(
  685. weights=weights,
  686. progress=progress,
  687. num_classes=num_classes,
  688. weights_backbone=weights_backbone,
  689. trainable_backbone_layers=trainable_backbone_layers,
  690. **kwargs,
  691. )