-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
147 lines (134 loc) · 3.94 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import torch
import torch.nn as nn
import torchvision
import torchvision.models as models
# TODO Task 1c - Implement a SimpleBNConv
class SimpleBNConv(nn.Module):
def __init__(self):
super().__init__()
self.seq = nn.Sequential(
nn.Conv2d(3, 8, 3),
nn.BatchNorm2d(8),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(8, 8, 3),
nn.BatchNorm2d(8),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(8, 16, 3),
nn.BatchNorm2d(16),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(16, 16, 3),
nn.BatchNorm2d(16),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(16, 32, 3),
nn.BatchNorm2d(32),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(32, 32, 3),
nn.BatchNorm2d(32),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(32, 64, 3),
nn.BatchNorm2d(64),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(64, 128, 3),
nn.BatchNorm2d(128),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(128, 128, 3),
nn.BatchNorm2d(128),
nn.MaxPool2d(2),
nn.ReLU(),
# nn.Printer(),
nn.Flatten(),
nn.Linear(128*10*14, 7)
)
def forward(self, x):
x = self.seq(x)
return x
# TODO Task 1f - Create a model from a pre-trained model from the torchvision
# model zoo.
def construct_resnet18():
resnet18 = models.resnet18(pretrained=True)
AB = 0
for child in resnet18.children():
AB += 1
if AB < 4:
for param in child.parameters():
param.requires_grad = False
resnet18.fc = nn.Linear(512, 7)
return resnet18
# TODO Task 1f - Create your own models
class DropoutCovnet(nn.Module):
def __init__(self):
super().__init__()
self.seq = nn.Sequential(
nn.Conv2d(3, 8, 3),
nn.BatchNorm2d(8),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(8, 8, 3),
nn.BatchNorm2d(8),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(8, 16, 3),
nn.BatchNorm2d(16),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(16, 16, 3),
nn.BatchNorm2d(16),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(16, 32, 3),
nn.BatchNorm2d(32),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(32, 32, 3),
nn.BatchNorm2d(32),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(32, 64, 3),
nn.BatchNorm2d(64),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(64, 128, 3),
nn.BatchNorm2d(128),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(128, 128, 3),
nn.BatchNorm2d(128),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Dropout(0.4),
# nn.Printer(),
nn.Flatten(),
nn.Linear(128*10*14, 7)
)
def forward(self, x):
x = self.seq(x)
return x
# TODO Task 2c - Complete TextMLP
class TextMLP(nn.Module):
def __init__(self, vocab_size, sentence_len, hidden_size):
super().__init__()
self.seq = nn.Sequential(
nn.Embedding(vocab_size, hidden_size//2),
nn.Flatten(),
# ....
)
# TODO Task 2c - Create a model which uses distilbert-base-uncased
# NOTE: You will need to include the relevant import statement.
# class DistilBertForClassification(nn.Module):
# ....