Deep Learning for Images with PyTorch
Michal Oleszak
Machine Learning Engineer
in_channels=1
in_channels=3
in_channels=4
from torchvision.transforms import functional
image = PIL.Image.open("dog.png")
num_channels = functional.get_image_num_channels(image)
print("Number of channels: ", num_channels)
Number of channels: 3
Input tensor Kernel Output tensor (feature map)
Conv2d
) and 2x2 (MaxPool2d
)
Input channel Kernel filters Output channels
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)
model = Net()
model.add_module('conv2', conv2)
print(model)
Net(
(conv1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
)
model.conv2
Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
nn.Sequential()
class BinaryImageClassification(nn.Module): def __init__(self): super(BinaryImageClassification, self).__init__()
self.conv_block = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) )
def forward(self, x): x = self.conv_block(x)
Deep Learning for Images with PyTorch