使用 PyTorch 進行影像深度學習
Michal Oleszak
Machine Learning Engineer
R-CNN 系列:R-CNN、Fast R-CNN、Faster R-CNN

R-CNN 系列:R-CNN、Fast R-CNN、Faster R-CNN

R-CNN 系列:R-CNN、Fast R-CNN、Faster R-CNN


import torch.nn as nn from torchvision.models import vgg16, VGG16_Weightsvgg = vgg16(weights=VGG16_Weights.DEFAULT)

import torch.nn as nn from torchvision.models import vgg16, VGG16_Weightsvgg = vgg16(weights=VGG16_Weights.DEFAULT)

.features:僅含卷積層import torch.nn as nn from torchvision.models import vgg16, VGG16_Weightsvgg = vgg16(weights=VGG16_Weights.DEFAULT)

.features:僅含卷積層.children():區塊中的所有層import torch.nn as nn from torchvision.models import vgg16, VGG16_Weightsvgg = vgg16(weights=VGG16_Weights.DEFAULT)backbone = nn.Sequential( *list(vgg.features.children()) )
nn.Sequential(*list()):將所有子層以清單放入順序模組*:將清單元素解包
.features:僅含卷積層.children():區塊中的所有層input_dimension = nn.Sequential(*list(
vgg_backbone.classifier.children())
)[0].in_features
classifier = nn.Sequential(
nn.Linear(input_dimension, 512),
nn.ReLU(),
nn.Linear(512, num_classes),
)
box_regressor = nn.Sequential(
nn.Linear(input_dimension, 32),
nn.ReLU(),
nn.Linear(32, 4),
)
class ObjectDetectorCNN(nn.Module): def __init__(self): super(ObjectDetectorCNN, self).__init__()vgg = vgg16(weights=VGG16_Weights.DEFAULT) self.backbone = nn.Sequential(*list(vgg.features.children()))input_features = nn.Sequential(*list(vgg.classifier.children()))[0].in_featuresself.classifier = nn.Sequential( nn.Linear(input_features, 512), nn.ReLU(), nn.Linear(512, 2), )self.box_regressor = nn.Sequential( nn.Linear(input_features, 32), nn.ReLU(), nn.Linear(32, 4), )
class ObjectDetector(nn.Module): (...) def forward(self, x):features = self.backbone(x)bboxes = self.regressor(features) classes = self.classifier(features) return bboxes, classes
unsqueeze() 加上批次維度nms())draw_bounding_boxes()使用 PyTorch 進行影像深度學習