forked from jonathan-hellwig/robot_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
229 lines (207 loc) · 8.3 KB
/
models.py
File metadata and controls
229 lines (207 loc) · 8.3 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
225
226
227
228
229
import torch
from torch import nn
from torchmetrics.classification import MulticlassAccuracy
import torch.nn.functional as F
import torch.optim as optim
import pytorch_lightning as pl
import utils
class DepthWise(nn.Module):
def __init__(
self, in_channels: int, out_channels: int, use_stride: bool = False
) -> None:
super().__init__()
self.batch_normalization = nn.BatchNorm2d(in_channels)
if use_stride:
self.conv2d_1 = nn.Conv2d(
in_channels,
in_channels,
3,
stride=2,
groups=in_channels,
bias=False,
padding=1,
)
else:
self.conv2d_1 = nn.Conv2d(
in_channels,
in_channels,
3,
groups=in_channels,
bias=False,
padding="same",
)
self.conv2d_2 = nn.Conv2d(in_channels, out_channels, 1, padding="same")
self.relu = nn.ReLU()
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.batch_normalization(x)
x = self.conv2d_1(x)
x = self.conv2d_2(x)
x = self.relu(x)
return x
class NormConv2dReLU(nn.Module):
def __init__(self, in_channels: int, out_channels: int) -> None:
super().__init__()
self.batch_normalization = nn.BatchNorm2d(in_channels)
self.conv2d = nn.Conv2d(
in_channels, out_channels, 3, bias=False, padding="same"
)
self.relu = nn.ReLU()
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.batch_normalization(x)
x = self.conv2d(x)
x = self.relu(x)
return x
# TODO: handle hyperparameters differently
class MultiClassJetNet(pl.LightningModule):
NUM_BOX_PARAMETERS = 4
def __init__(self, num_classes, num_boxes, learning_rate: float) -> None:
super().__init__()
self.mean_average_precisions = []
self.learning_rate = learning_rate
self.num_classes = num_classes
self.num_boxes = num_boxes
self.accuracy = MulticlassAccuracy(num_classes=num_classes + 1, average=None)
self.block_channels = [[24, 16, 16, 20], [20, 20, 20, 20, 24]]
self.input_layer = NormConv2dReLU(1, 16)
self.depth_wise_backbone = [DepthWise(16, 24, use_stride=True)]
for block_channel in self.block_channels:
for in_channels, out_channels in zip(
block_channel[:-2], block_channel[1:-1]
):
self.depth_wise_backbone.append(DepthWise(in_channels, out_channels))
self.depth_wise_backbone.append(
DepthWise(block_channel[-2], block_channel[-1], use_stride=True)
)
self.depth_wise_backbone = nn.Sequential(*self.depth_wise_backbone)
self.classifier_channels = [24, 24, 24, 24]
self.classifier = []
for classifier_channel in self.classifier_channels:
self.classifier.append(
NormConv2dReLU(classifier_channel, classifier_channel)
)
self.classifier = nn.Sequential(*self.classifier)
self.output_layer = nn.Conv2d(
24,
(self.num_classes + 1 + self.NUM_BOX_PARAMETERS) * self.num_boxes,
1,
padding="same",
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.input_layer(x)
x = self.depth_wise_backbone(x)
x = self.classifier(x)
x = self.output_layer(x)
return self._format_model_output(x)
def _format_model_output(
self, output: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
"""
output: (batch_size, num_anchors * (num_classes + 1 + 4), feature_map_height, feature_map_height)
"""
batch_size, _, feature_map_height, feature_map_width = output.shape
output = output.permute((0, 2, 3, 1))
output = output.reshape(
(
-1,
feature_map_height,
feature_map_width,
self.num_boxes,
self.NUM_BOX_PARAMETERS + self.num_classes + 1,
)
)
predicted_boxes = output[:, :, :, :, 0 : self.NUM_BOX_PARAMETERS].reshape(
(batch_size, -1, self.NUM_BOX_PARAMETERS)
)
predicted_class_logits = output[:, :, :, :, self.NUM_BOX_PARAMETERS :].reshape(
(-1, self.num_classes + 1)
)
return predicted_boxes, predicted_class_logits
def training_step(
self, batch: tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor], _
):
image, encoded_target_boxes, target_is_object, encoded_target_classes, _ = batch
encoded_predicted_boxes, predicted_class_logits = self(image)
mined_classification_loss, location_loss = self.bounding_box_loss(
encoded_target_boxes,
target_is_object,
encoded_target_classes,
encoded_predicted_boxes,
predicted_class_logits,
)
with torch.no_grad():
accuracy = self.accuracy(
predicted_class_logits, encoded_target_classes.flatten()
)
self.log("train/accuracy/no_object", accuracy[0])
self.log(
f"train/accuracy/robot",
accuracy[1],
)
self.log("train/loss/classification", mined_classification_loss)
self.log("train/loss/location", location_loss)
return mined_classification_loss + location_loss
def validation_step(
self, batch: tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor], _
):
image, encoded_target_boxes, target_is_object, encoded_target_classes, _ = batch
encoded_predicted_boxes, predicted_class_logits = self(image)
mined_classification_loss, location_loss = self.bounding_box_loss(
encoded_target_boxes,
target_is_object,
encoded_target_classes,
encoded_predicted_boxes,
predicted_class_logits,
)
with torch.no_grad():
accuracy = self.accuracy(
predicted_class_logits, encoded_target_classes.flatten()
)
self.log("val/accuracy/no_object", accuracy[0])
self.log(
f"val/accuracy/robot",
accuracy[1],
)
self.log("val/loss/classification", mined_classification_loss)
self.log("val/loss/location", location_loss)
def bounding_box_loss(
self,
target_boxes: torch.Tensor,
target_is_object: torch.Tensor,
target_classes: torch.Tensor,
predicted_boxes: torch.Tensor,
predicted_class_logits: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
selected_predicted_boxes = predicted_boxes[target_is_object]
print("selected predicted boxes is: ", selected_predicted_boxes)
selected_target_boxes = target_boxes[target_is_object]
print("selected target boxes is: ", selected_target_boxes)
number_of_positive = selected_predicted_boxes.size(0)
print("number of positive is: ", number_of_positive)
if number_of_positive > 0:
location_loss = (
F.smooth_l1_loss(
selected_predicted_boxes, selected_target_boxes, reduction="sum"
)
/ number_of_positive
)
else:
location_loss = 0.0
classfication_loss = F.cross_entropy(
predicted_class_logits,
target_classes.flatten(),
reduction="none",
)
positive_classification_loss = classfication_loss[target_is_object.flatten()]
negative_classification_loss = classfication_loss[~target_is_object.flatten()]
sorted_loss, _ = negative_classification_loss.sort(descending=True)
# Hard negative mining
number_of_negative = torch.clamp(
torch.tensor(3 * number_of_positive), max=sorted_loss.size(0)
)
mined_classification_loss = (
sorted_loss[:number_of_negative].sum() + positive_classification_loss.sum()
) / (number_of_negative + number_of_positive)
return mined_classification_loss, location_loss
def configure_optimizers(self) -> optim.Optimizer:
optimizer = optim.Adam(self.parameters(), lr=self.learning_rate)
return optimizer