Deep Learning for Images with PyTorch
Michal Oleszak
Machine Learning Engineer
R-CNN family: R-CNN, Fast-CNN, Faster CNN
R-CNN family: R-CNN, Fast-CNN, Faster CNN
R-CNN family: R-CNN, Fast-CNN, Faster CNN
import torch.nn as nn from torchvision.models import vgg16, VGG16_Weights
vgg = vgg16(weights=VGG16_Weights.DEFAULT)
import torch.nn as nn from torchvision.models import vgg16, VGG16_Weights
vgg = vgg16(weights=VGG16_Weights.DEFAULT)
.features
: only convolutional layersimport torch.nn as nn from torchvision.models import vgg16, VGG16_Weights
vgg = vgg16(weights=VGG16_Weights.DEFAULT)
.features
: only convolutional layers.children()
: all layers from blockimport torch.nn as nn from torchvision.models import vgg16, VGG16_Weights
vgg = vgg16(weights=VGG16_Weights.DEFAULT)
backbone = nn.Sequential( *list(vgg.features.children()) )
nn.Sequential(*list())
: all sub-layers are placed into a sequential block as a list*
: unpacks the elements from the list.features
: only convolutional layers.children()
: all layers from blockinput_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_features
self.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()
the image to add the batch dimensionnms()
) over model's outputdraw_bounding_boxes()
on top of the imageDeep Learning for Images with PyTorch