Spaces:
Sleeping
Sleeping
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class MovieposterNet(nn.Module): | |
| def __init__(self, num_classes=10): | |
| super(MovieposterNet, self).__init__() | |
| # Bloc 1 : 224 -> 112 | |
| self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) | |
| self.bn1 = nn.BatchNorm2d(16) | |
| # Bloc 2 : 112 -> 56 | |
| self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1) | |
| self.bn2 = nn.BatchNorm2d(32) | |
| # Bloc 3 : 56 -> 28 | |
| self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1) | |
| self.bn3 = nn.BatchNorm2d(64) | |
| # Bloc 4 : 28 -> 14 | |
| self.conv4 = nn.Conv2d(64, 128, kernel_size=3, padding=1) | |
| self.bn4 = nn.BatchNorm2d(128) | |
| self.pool = nn.MaxPool2d(2, 2) | |
| # Taille après 4 poolings : 224 / 2^4 = 14 | |
| # Entrée fc1 : 128 canaux * 14 * 14 = 25088 | |
| self.fc1 = nn.Linear(128 * 14 * 14, 512) | |
| self.dropout = nn.Dropout(0.5) # Limite le sur-apprentissage | |
| self.fc2 = nn.Linear(512, num_classes) | |
| def forward(self, x): | |
| x = self.pool(F.relu(self.bn1(self.conv1(x)))) | |
| x = self.pool(F.relu(self.bn2(self.conv2(x)))) | |
| x = self.pool(F.relu(self.bn3(self.conv3(x)))) | |
| x = self.pool(F.relu(self.bn4(self.conv4(x)))) | |
| x = torch.flatten(x, 1) | |
| x = F.relu(self.fc1(x)) | |
| x = self.dropout(x) | |
| x = self.fc2(x) | |
| return x | |
| def get_features(self, x): | |
| x = self.pool(F.relu(self.bn1(self.conv1(x)))) | |
| x = self.pool(F.relu(self.bn2(self.conv2(x)))) | |
| x = self.pool(F.relu(self.bn3(self.conv3(x)))) | |
| x = self.pool(F.relu(self.bn4(self.conv4(x)))) | |
| return torch.flatten(x, 1) |