Spaces:
Build error
Build error
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class CNN_4Layer(nn.Module): | |
| def __init__(self, in_channels, num_classes, filters=(16,32,64,128), dropout=0.25): | |
| super(CNN_4Layer, self).__init__() | |
| self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=filters[0], kernel_size=3, padding=1) | |
| self.conv2 = nn.Conv2d(in_channels=filters[0], out_channels=filters[1], kernel_size=3, padding=1) | |
| self.conv3 = nn.Conv2d(in_channels=filters[1], out_channels=filters[2], kernel_size=3, padding=1) | |
| self.conv4 = nn.Conv2d(in_channels=filters[2], out_channels=filters[3], kernel_size=3, padding=1) | |
| self.pool = nn.MaxPool2d(kernel_size=2, stride=2) | |
| self.dropout = nn.Dropout(dropout) | |
| self.fc1 = nn.Linear(filters[3] * 3 * 3, num_classes) | |
| def forward(self, x): | |
| x = F.relu(self.conv1(x)) | |
| x = self.pool(x) | |
| x = self.dropout(x) | |
| x = F.relu(self.conv2(x)) | |
| x = self.pool(x) | |
| x = self.dropout(x) | |
| x = F.relu(self.conv3(x)) | |
| x = self.pool(x) | |
| x = self.dropout(x) | |
| x = F.relu(self.conv4(x)) | |
| x = self.pool(x) | |
| x = self.dropout(x) | |
| x = x.reshape(x.shape[0], -1) | |
| x = self.fc1(x) | |
| return x |