-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
189 lines (147 loc) · 5.03 KB
/
main.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
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
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
import torch.optim as optim
from torch.utils.data import DataLoader
from Dataset import Pascal_Images
from Yolo_v1 import Yolo_v1
from Loss import Yolo_loss
from metrics import IoU
def get_pic(label):
centr_x_rel = label[..., 1]
centr_y_rel = label[..., 2]
width_rel = label[..., 3]
heigth_rel = label[..., 4]
cell_x = np.argmax(centr_x_rel) // 7
cell_y = np.argmax(centr_y_rel) % 7
center_x_bb = centr_x_rel[cell_x, cell_y]
center_y_bb = centr_y_rel[cell_x, cell_y]
width = width_rel[cell_x, cell_y] * 448
heigth = heigth_rel[cell_x, cell_y] * 448
center_x = 64 * (cell_x + center_x_bb)
center_y = 64 * (cell_y + center_y_bb)
x_min, x_max = center_x - width / 2, center_x + width / 2
y_min, y_max = center_y - heigth / 2, center_y + heigth / 2
return (
center_x,
center_y,
[x_min, x_max, x_max, x_min, x_min],
[y_min, y_min, y_max, y_max, y_min],
)
def get_bb_center_cells(label):
object_cells = []
# print("=", label.shape[0], label.shape[1], "=")
for i in range(label.shape[0]):
for j in range(label.shape[1]):
# print(i, j, label[i][j])
if any(label[i][j] != 0):
object_cells.append((i, j))
return object_cells
def get_boxes(labels, object_cells):
bboxes = []
for _cell in object_cells:
i, j = _cell
centr_x_rel = labels[i][j][1]
centr_y_rel = labels[i][j][2]
width_rel = labels[i][j][3]
heigth_rel = labels[i][j][4]
center_x = 64 * (i + centr_x_rel)
center_y = 64 * (j + centr_y_rel)
width = width_rel * 448
heigth = heigth_rel * 448
x_min, x_max = center_x - width / 2, center_x + width / 2
y_min, y_max = center_y - heigth / 2, center_y + heigth / 2
bboxes.append(
[
center_x,
center_y,
[x_min, x_max, x_max, x_min, x_min],
[y_min, y_min, y_max, y_max, y_min],
width,
heigth,
]
)
return bboxes
def get_boxes_wh(labels, object_cells):
bboxes = []
for _cell in object_cells:
i, j = _cell
centr_x_rel = labels[i][j][1]
centr_y_rel = labels[i][j][2]
width_rel = labels[i][j][3]
heigth_rel = labels[i][j][4]
center_x = 64 * (i + centr_x_rel)
center_y = 64 * (j + centr_y_rel)
width = width_rel * 448
heigth = heigth_rel * 448
bboxes.append([labels[i][j][0], center_x, center_y, width, heigth])
return bboxes
def get_xy(width, height):
x_min, x_max = center_x - width / 2, center_x + width / 2
y_min, y_max = center_y - height / 2, center_y + height / 2
return [x_min, x_max, x_max, x_min, x_min], [y_min, y_min, y_max, y_max, y_min]
# add mean avg precision
def train_epoch(model, optimizer, criterion):
loss_log = []
for x_batch, y_batch in train_loader: # <-
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
optimizer.zero_grad()
pred = model(x_batch)
loss = criterion(pred, y_batch)
loss_log.append(loss.item())
loss.backward()
optimizer.step()
return loss_log
@torch.no_grad()
def test_epoch(model, criterion):
loss_log = []
for x_batch, y_batch in test_loader: # <-
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch)
loss = criterion(pred, y_batch)
loss_log.append(loss.item())
return loss_log
def train(model, optimizer, criterion, epochs):
for epoch in range(epochs):
train_loss = train_epoch(model, optimizer, criterion)
# test_loss = test_epoch(model, criterion)
print(
"epoch: ",
epoch,
" | ",
"train loss: ",
np.mean(train_loss),
" | ",
)
# "test loss: ", np.mean(test_loss))
if __name__ == "__main__":
device = "cuda" if torch.cuda.is_available() else "cpu"
# Data directory
# There is only image and labels file names.
# Actual files are in folder '/content'.
train_path = "content/8examples.csv"
test_path = "content/8examples.csv"
# train/test dataframe
file_trian = pd.read_csv(train_path)
file_test = pd.read_csv(test_path)
# Dataset and DataLoader
train_dataset = Pascal_Images(file_trian)
train_loader = DataLoader(
train_dataset, pin_memory=True, batch_size=8, shuffle=True, drop_last=True
)
test_dataset = Pascal_Images(file_test)
test_loader = DataLoader(
test_dataset, pin_memory=True, batch_size=8, shuffle=True, drop_last=True
)
# Model and parameteres
model = Yolo_v1()
model.to(device)
WEIGHT_DECAY = 0
optimizer = optim.Adam(
params=model.parameters(), weight_decay=WEIGHT_DECAY, lr=2e-5
) # <-
criterion = Yolo_loss()
# for overfitting
epochs = 400
train(model, optimizer, criterion, epochs)