This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
78 lines (71 loc) · 2.89 KB
/
models.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import torch.nn as nn
from einops import rearrange
class VerySmallCNN(nn.Module):
def __init__(self, n_classes: int):
"""
Initializes the VerySmallCNN model.
Args:
n_classes (int): The number of classes in our output layer.
Because of the dataset, our number of classes will be 10.
"""
super(VerySmallCNN, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
)
self.dropout=nn.Dropout(p=0.4, inplace=True)
self.fc1 = nn.Linear(64 * 16 * 16, 512)
self.fc2 = nn.Linear(512, n_classes)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(2, 2)
def forward(self, x):
out = self.layers(x)
out = self.dropout(out)
out = self.pool(out)
out = rearrange(out, 'b c h w -> b (c h w)') # Flattening the output from 4D to 2D tensor.
out = self.relu(self.fc2(self.fc1(out)))
return out
class SmallCNN(nn.Module):
def __init__(self, n_classes: int):
"""
Initializes the larger SmallCNN model for the CIFAR-10 dataset.
Args:
n_classes (int): The number of classes in our output layer.
Because of the dataset, our number of classes will be 10.
"""
super(SmallCNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
)
self.layer2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
)
self.layer3 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.Dropout(p=0.4, inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1)
)
self.act = nn.Softmax(dim=1)
self.dropout=nn.Dropout(p=0.4, inplace=True)
self.fc1 = nn.Linear(128 * 16 * 16, 4096)
self.fc2 = nn.Linear(4096, n_classes)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(2, 2)
def forward(self, x):
out = self.layer2(self.layer1(x))
out = self.dropout(out)
out = self.pool(out)
out = rearrange(out, 'b c h w -> b (c h w)') # Flattening the output from 4D to 2D tensor.
out = self.relu(self.fc2(self.fc1(out)))
return out