Boboiazumi's picture
Upload 3 files
d9d5c3b verified
from torch import nn
class CNN(nn.Module):
def __init__(self, num_classes):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.bn1 = nn.BatchNorm2d(32)
self.bn2 = nn.BatchNorm2d(64)
self.bn3 = nn.BatchNorm2d(128)
self.fc1 = nn.Linear(128 * 16 * 16, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 128)
self.fc4 = nn.Linear(128 ,num_classes)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x = nn.functional.relu(self.conv1(x))
x = self.bn1(x)
x = self.pool(x)
x = nn.functional.relu(self.conv2(x))
x = self.bn2(x)
x = self.pool(x)
x = nn.functional.relu(self.conv3(x))
x = self.bn3(x)
x = self.pool(x)
x = x.view(-1, 128 * 16 * 16)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.dropout(x)
x = nn.functional.relu(self.fc3(x))
x = self.dropout(x)
x = self.fc4(x)
return x