Spaces:
Sleeping
Sleeping
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class BasicCNN(nn.Module): | |
| def __init__(self, num_classes=39): | |
| super().__init__() | |
| self.conv1 = nn.Conv2d(3, 32, 3, padding=1) | |
| self.bn1 = nn.BatchNorm2d(32) | |
| self.conv2 = nn.Conv2d(32, 64, 3, padding=1) | |
| self.bn2 = nn.BatchNorm2d(64) | |
| self.conv3 = nn.Conv2d(64, 128, 3, padding=1) | |
| self.bn3 = nn.BatchNorm2d(128) | |
| self.conv4 = nn.Conv2d(128, 256, 3, padding=1) | |
| self.bn4 = nn.BatchNorm2d(256) | |
| self.gap = nn.AdaptiveAvgPool2d((1, 1)) #global average pooling | |
| self.fc = nn.Linear(256, num_classes) #fc classifier | |
| self.dropout = nn.Dropout(0.3) #regularise | |
| def forward(self, x): | |
| x = F.relu(self.bn1(self.conv1(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = F.relu(self.bn2(self.conv2(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = F.relu(self.bn3(self.conv3(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = F.relu(self.bn4(self.conv4(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = self.gap(x) | |
| x = torch.flatten(x, 1) | |
| x = self.dropout(x) | |
| x = self.fc(x) | |
| return x |