使用 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 进行图像深度学习