-
Notifications
You must be signed in to change notification settings - Fork 12
/
train-all-models.py
213 lines (158 loc) · 6.07 KB
/
train-all-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
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
import os
import json
import numpy as np
import torch
import torchvision
from datasets.afd import make_afd_loaders
from datasets.dunnings import make_dunnings_test_loader, make_dunnings_train_loader
from datasets.combo import make_combo_train_loaders
from models import FireClassifier, BACKBONES
from utils import accuracy_gpu
BATCH_SIZE = 32
EPOCHS = 10
DECREASE_LR_AFTER = 3
PRINT_EVERY = 100 # batches
EVAL_EVERY = 100
# Datasets are in data/ folder by default
file_path = os.path.realpath(__file__)
DATASETS_ROOT = os.path.dirname(file_path) + "/data"
DATASETS = {
# AFD only
"afd_train": DATASETS_ROOT + "/aerial_fire_dataset/train",
"afd_test": DATASETS_ROOT + "/aerial_fire_dataset/test/",
# Dunnings only
"dunnings_train": DATASETS_ROOT + "/dunnings/fire-dataset-dunnings/images-224x224/train",
"dunnings_test": DATASETS_ROOT + "/FIRE/dunnings/fire-dataset-dunnings/images-224x224/test",
# AFD + Dunnings
"combined_train": DATASETS_ROOT + "/combined_dunnings_afd/train",
"combined_test": DATASETS_ROOT + "/combined_dunnings_afd/test"
}
# dunnings_train = make_dunnings_train_loader(
# DATASETS["dunnings_train"], batch_size=BATCH_SIZE
# )
# test = make_dunnings_test_loader(DATASETS["dunnings_test"], batch_size=BATCH_SIZE)
# print(f"Loaded {len(test)} test batches with {len(test) * BATCH_SIZE} samples")
# Can be useful if we're retraining many times on the entire dataset
# completely memory extravagant but I have 256GB of RAM to use :)
# train, val = list(train), list(val)
for bbone in BACKBONES:
BATCH_SIZE = 16 if bbone == 'VGG16' else 32
train, val = make_combo_train_loaders(
DATASETS["combined_train"], batch_size=BATCH_SIZE
)
print(f"Loaded {len(train)} training batches with {len(train) * BATCH_SIZE} samples")
print(f"Loaded {len(val)} val batches with {len(val) * BATCH_SIZE} samples")
print(f"Training {bbone}")
device = torch.device("cuda:0")
do_val = True
do_test = False
history = {
"train_samples": [],
"train_acc": [],
"train_loss": [],
"val_acc": [],
"test_acc": [],
}
# bbone = "resnet50"
m = FireClassifier(backbone=bbone)
m = m.to(device)
criterion = torch.nn.BCELoss()
for epoch in range(EPOCHS):
optimizer = torch.optim.Adam(
m.parameters(),
lr=1e-4 if epoch < DECREASE_LR_AFTER else 1e-5,
weight_decay=1e-3
)
running_loss = []
running_acc = []
# epoch training
for i, data in enumerate(train):
m.train()
# get the inputs; data is a list of [inputs, labels]
inputs = data[0].to(device)
labels = data[1].to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
scores = m(inputs)
loss = criterion(scores[:, 0], labels.type_as(scores[:, 0]))
loss.backward()
optimizer.step()
pred = (scores >= 0.5).squeeze()
acc = accuracy_gpu(pred, labels)
# print statistics
running_loss.append(loss.item())
running_acc.append(acc)
if i % PRINT_EVERY == 0:
print(
f"epoch: {epoch+1:02d}, \
batch: {i:03d}, \
loss: {np.mean(running_loss):.3f}, \
training accuracy: {np.mean(running_acc):.3f}"
)
history["train_samples"].append(epoch * len(train) + i)
history["train_acc"].append(np.mean(running_acc))
history["train_loss"].append(np.mean(running_loss))
# del outputs, inputs, labels
if i % EVAL_EVERY == 0:
m.eval()
val_acc = []
# epoch val
for i, data in enumerate(val):
# get the inputs; data is a list of [inputs, labels]
inputs = data[0].to(device)
labels = data[1].to(device)
with torch.no_grad():
scores = m(inputs).squeeze()
pred = scores > 0.5
acc = accuracy_gpu(pred, labels)
val_acc.append(acc)
va = round(np.mean(val_acc), 4)
print(f"val accuracy {va}")
history["val_acc"].append(va)
#########################################
# on epoch end:
m.eval()
if do_val:
val_acc = []
# epoch val
for i, data in enumerate(val):
# get the inputs; data is a list of [inputs, labels]
inputs = data[0].to(device)
labels = data[1].to(device)
with torch.no_grad():
scores = m(inputs).squeeze()
pred = scores > 0.5
acc = accuracy_gpu(pred, labels)
val_acc.append(acc)
va = round(np.mean(val_acc), 4)
print(f"val accuracy {va}")
history["val_acc"].append(va)
else:
va = -1
if do_test:
test_acc = []
# epoch val
with torch.no_grad():
for i, data in enumerate(test):
# data has list entries: [inputs, labels]
inputs = data[0].to(device)
labels = data[1].to(device)
scores = m(inputs).squeeze()
pred = scores > 0.5
acc = accuracy_gpu(pred, labels)
test_acc.append(acc)
tst = np.mean(test_acc)
print(f"test_accuracy {tst}")
history["test_acc"].append(tst)
else:
tst = -1
fname = (
f"weights/{bbone}-epoch-{epoch+1}-val_acc={va:.4f}-test_acc={tst:.2f}.pt"
)
torch.save(m, fname)
print(f"Saved {fname}")
with open("log.json", "w") as f:
s = json.dumps(history)
f.write(s)
print(f"Finished Training: {bbone}")