1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| class SuperNaiveModel(nn.Module): def __init__(self): super(SuperNaiveModel, self).__init__() self.conv0 = nn.Sequential( torch.nn.Conv2d(1, 64, kernel_size=5, stride=2, padding=3), torch.nn.BatchNorm2d(64, momentum=0.1), torch.nn.ReLU(), ) self.conv1 = torch.nn.Sequential( torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(stride=2, kernel_size=2)) self.conv2 = torch.nn.Sequential( torch.nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(stride=2, kernel_size=2)) self.conv20 = torch.nn.Sequential( torch.nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(stride=2, kernel_size=2)) self.conv3=torch.nn.Sequential( torch.nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1), torch.nn.BatchNorm2d(512, momentum=0.1), torch.nn.ReLU(), torch.nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1), torch.nn.BatchNorm2d(512, momentum=0.1), torch.nn.ReLU(), torch.nn.MaxPool2d(stride=2, kernel_size=2)) self.dense = torch.nn.Sequential( torch.nn.Linear(512 * 4 * 4, 20), nn.LogSoftmax(dim=1) )
def forward(self, x): x = x.reshape(x.shape[0], 1, 128, 128).float() x = self.conv0(x) x = self.conv1(x) x = self.conv2(x) x = self.conv20(x) x = self.conv3(x) x = x.view(x.shape[0], 512 * 4 * 4) x = self.dense(x) return x1
|