-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrounder_models.py
More file actions
224 lines (154 loc) · 5.71 KB
/
grounder_models.py
File metadata and controls
224 lines (154 loc) · 5.71 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import torch
import torch.nn as nn
import torch.nn.functional as F
class CNN_grounder(nn.Module):
def __init__(self, num_symbols):
super(CNN_grounder, self).__init__()
self.conv1 = nn.Conv2d(3, 5, kernel_size=5)
self.conv2 = nn.Conv2d(5, 5, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.flat = nn.Flatten()
self.fc1 = nn.Linear(125, 50)
self.fc2 = nn.Linear(50, num_symbols)
self.softmax = nn.Softmax(dim=1) # TODO double check if correct (dim 0 should be batch size)
self.num_symbols = num_symbols
self.device = None
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 3))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 3))
# x = F.relu(F.max_pool2d(self.conv2_drop(x), 3))
x = self.flat(x)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
x = self.softmax(x)
return x
def to(self, device):
super().to(device)
self.device = device
return self
class GridworldClassifier(nn.Module):
def __init__(self, num_symbols): # 10 items da classificare
super(GridworldClassifier, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1) # Ridotto da 32 a 16 filtri
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1) # Ridotto da 64 a 32 filtri
self.pool = nn.MaxPool2d(2, 2) # Pooling invariato
self.fc1 = nn.Linear(32 * 16 * 16, 128) # Input più piccolo
self.fc2 = nn.Linear(128, num_symbols)
self.softmax = nn.Softmax(dim=-1)
self.num_symbols = num_symbols
self.device = None
def forward(self, x):
x = self.pool(F.relu(self.conv1(x))) # Output: 32x32
x = self.pool(F.relu(self.conv2(x))) # Output: 16x16
x = x.view(x.size(0), -1) # Flatten
x = F.relu(self.fc1(x))
x = self.fc2(x)
x = self.softmax(x)
return x
def to(self, device):
super().to(device)
self.device = device
return self
class ObjectCNN(nn.Module):
def __init__(self, input_size=(3,56,56), num_symbols=2):
super(ObjectCNN, self).__init__()
self.features = nn.Sequential(
# Layer 1
nn.Conv2d(3, 16, kernel_size=5, padding=2), # 3x56x56 -> 16x56x56
# nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(2), # -> 16x28x28
# Layer 2
nn.Conv2d(16, 32, kernel_size=3, padding=1), # -> 32x28x28
# nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2), # -> 32x14x14
# # Layer 3
nn.Conv2d(32, 64, kernel_size=3, padding=1), # -> 64x14x14
# nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2) # -> 64x7x7
)
with torch.no_grad():
dummy_input = torch.zeros((1, input_size[0], input_size[1], input_size[2]))
dummy_output = self.features(dummy_input)
self.flattened_size = dummy_output.view(1, -1).size(1)
self.classifier = nn.Sequential(
nn.Flatten(), # -> 4096
nn.Linear(self.flattened_size, 64), # -> 64
nn.ReLU(),
nn.Linear(64, num_symbols), # -> num_symbols
nn.Softmax(dim=-1)
)
self.num_symbols = num_symbols
self.device = None
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
def to(self, device):
super().to(device)
self.device = device
return self
class LidarGrounder(nn.Module):
def __init__(self, input_size=(76,), num_symbols=2):
super(LidarGrounder, self).__init__()
assert len(input_size) == 1
self.input_size = input_size[0]
self.num_symbols = num_symbols
self.hidden_size = 64
self.device = None
self.classifier = nn.Sequential(
nn.Linear(self.input_size, self.hidden_size),
nn.ReLU(),
nn.Linear(self.hidden_size, self.hidden_size),
nn.ReLU(),
nn.Linear(self.hidden_size, num_symbols),
nn.Softmax(dim=-1)
)
def forward(self, x):
x = self.classifier(x)
return x
def to(self, device):
super().to(device)
self.device = device
return self
class Linear_grounder_no_droput(nn.Module):
def __init__(self, num_inputs, hidden_size, num_symbols):
super(Linear_grounder_no_droput, self).__init__()
self.grounder = nn.Sequential(
nn.Linear(num_inputs, hidden_size),
nn.Tanh(),
nn.Linear(hidden_size, hidden_size),
nn.Softmax(dim=-1),
nn.Linear(hidden_size, num_symbols),
)
self.num_symbols = num_symbols
self.device = None
def forward(self, x):
return self.grounder(x)
def to(self, device):
super().to(device)
self.device = device
return self
class Linear_grounder(nn.Module):
def __init__(self, num_inputs, hidden_size, num_symbols):
super(Linear_grounder, self).__init__()
self.grounder = nn.Sequential(
nn.Linear(num_inputs, hidden_size),
nn.Dropout(0.2),
nn.Tanh(),
nn.Linear(hidden_size, hidden_size),
nn.Dropout(0.2),
nn.Softmax(dim=-1),
nn.Linear(hidden_size, num_symbols),
)
self.num_symbols = num_symbols
self.device = None
def forward(self, x):
return self.grounder(x)
def to(self, device):
super().to(device)
self.device = device
return self