PyTorch で学ぶ画像向け Deep Learning
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 で学ぶ画像向け Deep Learning