fasterrcnn_resnet50_fpn¶
- torchvision.models.detection.fasterrcnn_resnet50_fpn(*, weights: Optional[FasterRCNN_ResNet50_FPN_Weights] = None, progress: bool = True, num_classes: Optional[int] = None, weights_backbone: Optional[ResNet50_Weights] = ResNet50_Weights.IMAGENET1K_V1, trainable_backbone_layers: Optional[int] = None, **kwargs: Any) FasterRCNN [source]¶
具有來自 Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 論文的 ResNet-50-FPN 骨幹的 Faster R-CNN 模型。
警告
偵測模組處於 Beta 階段,不保證向後相容性。
模型的輸入預期為張量列表,每個張量的形狀為
[C, H, W]
,每個影像一個,且應在0-1
範圍內。不同的影像可以有不同的大小。模型的行為會根據其處於訓練還是評估模式而有所改變。
在訓練期間,模型期望輸入的 tensors 和 targets(字典列表),包含:
boxes (
FloatTensor[N, 4]
):ground-truth boxes,格式為[x1, y1, x2, y2]
,其中0 <= x1 < x2 <= W
且0 <= y1 < y2 <= H
。labels (
Int64Tensor[N]
):每個 ground-truth box 的類別標籤
在訓練期間,模型會回傳一個
Dict[Tensor]
,包含 RPN 和 R-CNN 的分類和迴歸損失。在推論期間,模型僅需要輸入的 tensors,並將後處理的預測結果回傳為
List[Dict[Tensor]]
,每個輸入圖片對應一個。Dict
的欄位如下,其中N
是偵測到的數量:boxes (
FloatTensor[N, 4]
):預測的 boxes,格式為[x1, y1, x2, y2]
,其中0 <= x1 < x2 <= W
且0 <= y1 < y2 <= H
。labels (
Int64Tensor[N]
):每個偵測的預測標籤scores (
Tensor[N]
):每個偵測的分數
有關輸出的更多詳細資訊,您可以參考實例分割模型。
Faster R-CNN 可以導出到 ONNX,適用於具有固定大小輸入圖像的固定批次大小。
範例
>>> model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT) >>> # For training >>> images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4) >>> boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4] >>> labels = torch.randint(1, 91, (4, 11)) >>> images = list(image for image in images) >>> targets = [] >>> for i in range(len(images)): >>> d = {} >>> d['boxes'] = boxes[i] >>> d['labels'] = labels[i] >>> targets.append(d) >>> output = model(images, targets) >>> # For inference >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x) >>> >>> # optionally, if you want to export the model to ONNX: >>> torch.onnx.export(model, x, "faster_rcnn.onnx", opset_version = 11)
- 參數:
weights (
FasterRCNN_ResNet50_FPN_Weights
, optional) – 要使用的預訓練權重。請參閱下方的FasterRCNN_ResNet50_FPN_Weights
以獲取更多詳細資訊和可能的值。預設情況下,不使用任何預訓練權重。progress (bool, optional) – 如果為 True,則將下載的進度條顯示到 stderr。預設值為 True。
num_classes (int, optional) – 模型的輸出類別數量(包括背景)
weights_backbone (
ResNet50_Weights
, optional) – backbone 的預訓練權重。trainable_backbone_layers (int, optional) – 從最後一個 block 開始,可訓練(非凍結)的層數。有效值介於 0 和 5 之間,其中 5 表示所有 backbone 層都是可訓練的。如果傳遞
None
(預設值),則此值設定為 3。**kwargs – 傳遞給
torchvision.models.detection.faster_rcnn.FasterRCNN
基底類別的參數。有關此類別的更多詳細資訊,請參閱原始碼。
- class torchvision.models.detection.FasterRCNN_ResNet50_FPN_Weights(value)[source]¶
上面的模型建構器接受以下值作為
weights
參數。FasterRCNN_ResNet50_FPN_Weights.DEFAULT
相當於FasterRCNN_ResNet50_FPN_Weights.COCO_V1
。您也可以使用字串,例如weights='DEFAULT'
或weights='COCO_V1'
。FasterRCNN_ResNet50_FPN_Weights.COCO_V1:
這些權重是通過遵循與論文上類似的訓練方法產生的。也可作為
FasterRCNN_ResNet50_FPN_Weights.DEFAULT
使用。box_map (在 COCO-val2017 上)
37.0
categories
__background__, person, bicycle, … (省略 88 個)
min_size
height=1, width=1
num_params
41755286
recipe
GFLOPS
134.38
檔案大小
159.7 MB
推論轉換可在
FasterRCNN_ResNet50_FPN_Weights.COCO_V1.transforms
中取得,並執行以下預處理操作:接受PIL.Image
,批次化的(B, C, H, W)
和單個的(C, H, W)
圖像torch.Tensor
物件。圖像被縮放到[0.0, 1.0]
。
使用
fasterrcnn_resnet50_fpn
的範例