|
import torch |
|
from torch import nn |
|
|
|
class TinyVGG(nn.Module): |
|
""" |
|
Model architecture copying TinyVGG from: |
|
https://poloclub.github.io/cnn-explainer/ |
|
https://www.learnpytorch.io/04_pytorch_custom_datasets/#:~:text=class%20TinyVGG(,device)%0Amodel_0 |
|
""" |
|
def __init__(self, input_shape: int, hidden_units: int, output_shape: int) -> None: |
|
super().__init__() |
|
self.conv_block_1 = nn.Sequential( |
|
nn.Conv2d(in_channels=input_shape, |
|
out_channels=hidden_units, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1), |
|
nn.ReLU(), |
|
nn.Conv2d(in_channels=hidden_units, |
|
out_channels=hidden_units, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1), |
|
nn.ReLU(), |
|
nn.MaxPool2d(kernel_size=2, |
|
stride=2) |
|
) |
|
self.conv_block_2 = nn.Sequential( |
|
nn.Conv2d(hidden_units, hidden_units, kernel_size=3, padding=1), |
|
nn.ReLU(), |
|
nn.Conv2d(hidden_units, hidden_units, kernel_size=3, padding=1), |
|
nn.ReLU(), |
|
nn.MaxPool2d(2) |
|
) |
|
self.classifier = nn.Sequential( |
|
nn.Flatten(), |
|
|
|
|
|
nn.Linear(in_features=hidden_units*16*16, |
|
out_features=output_shape) |
|
) |
|
|
|
def forward(self, x: torch.Tensor): |
|
return self.classifier(self.conv_block_2(self.conv_block_1(x))) |
|
|