forked from NeuroSYS-pl/objects_counting_dmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
241 lines (201 loc) · 7.21 KB
/
train.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""Main script used to train networks."""
import os
from typing import Union, Optional, List
import click
import torch
import numpy as np
import matplotlib.pyplot as plt
from data_loader import H5Dataset
from looper import Looper
from models import UNet, FCRN_A
@click.command()
@click.argument("data_path", type=click.Path(exists=True), required=True,
)
@click.option(
"-n",
"--network_architecture",
type=click.Choice(["UNet", "FCRN_A"]),
required=True,
help="Model to train.",
)
@click.option(
"-lr",
"--learning_rate",
default=1e-2,
help="Initial learning rate (lr_scheduler is applied).",
)
@click.option("-e", "--epochs", default=150, help="Number of training epochs.")
@click.option(
"--batch_size",
default=8,
help="Batch size for both training and validation dataloaders.",
)
@click.option(
"-hf",
"--horizontal_flip",
default=0.0,
help="The probability of horizontal flip for training dataset.",
)
@click.option(
"-vf",
"--vertical_flip",
default=0.0,
help="The probability of horizontal flip for tratining dataset.",
)
@click.option(
"-rt",
"--rotation_chance",
default=0.0,
help="The chance of a 2D rotation for the training dataset."
)
@click.option(
"--unet_filters",
default=64,
help="Number of filters for U-Net convolutional layers.",
)
@click.option(
"--convolutions", default=2, help="Number of layers in a convolutional block."
)
@click.option("--verbose", is_flag=True, help="Whether to log the detailed training information to console.")
@click.option("-s", "--save", type=click.Path(exists=False), help="Save plot and log data to dataset folder.")
def train(
data_path: str,
network_architecture: str,
learning_rate: float,
epochs: int,
batch_size: int,
horizontal_flip: float,
vertical_flip: float,
rotation_chance: float,
unet_filters: int,
convolutions: int,
verbose: bool,
save: str,
):
"""Train chosen model on selected dataset."""
# use GPU if avilable
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dataset = {} # training and validation HDF5-based datasets
dataloader = {} # training and validation dataloaders
for mode in ["train", "valid"]:
# expected HDF5 files in dataset_name/(train | valid).h5
path = os.path.join(data_path, f"{mode}.h5")
# turn on flips only for training dataset
dataset[mode] = H5Dataset(
path,
horizontal_flip if mode == "train" else 0,
vertical_flip if mode == "train" else 0,
rotation_chance if mode == "train" else 0
)
dataloader[mode] = torch.utils.data.DataLoader(
dataset[mode], batch_size=batch_size
)
# only UCSD dataset provides greyscale images instead of RGB
# input_channels = 1 if dataset_name == "ucsd" else 3
input_channels = 3
# initialize a model based on chosen network_architecture
network = {"UNet": UNet, "FCRN_A": FCRN_A}[network_architecture](
input_filters=input_channels, filters=unet_filters, N=convolutions
).to(device)
network = torch.nn.DataParallel(network)
# initialize loss, optimized and learning rate scheduler
loss = torch.nn.MSELoss()
optimizer = torch.optim.SGD(
network.parameters(), lr=learning_rate, momentum=0.9, weight_decay=1e-5
)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.1)
# create training and validation Loopers to handle a single epoch
train_looper = Looper(
network,
device,
loss,
optimizer,
dataloader["train"],
len(dataset["train"]),
)
valid_looper = Looper(
network,
device,
loss,
optimizer,
dataloader["valid"],
len(dataset["valid"]),
validation=True,
)
log_file = None
if save is not None:
os.makedirs(save, exist_ok=True)
id = 1
log_path = os.path.join(save, f"log{id}.txt")
while os.path.exists(log_path):
id += 1
log_path = os.path.join(save, f"log{id}.txt")
log_file = open(log_path, "a")
# current best results (lowest mean absolute error on validation set)
best_valid_mae = np.infty
for epoch in range(epochs):
_log(f"Epoch {epoch + 1}", log_file, True) #always print this
# run training epoch and update learning rate
train_looper.run()
lr_scheduler.step()
# run validation epoch
with torch.no_grad():
best_result = valid_looper.run()
_log(train_looper.get_results(), log_file, verbose)
_log(valid_looper.get_results(), log_file, verbose)
# update checkpoint if new best is reached
if best_result < best_valid_mae:
best_valid_mae = best_result
train_looper.update_best_values()
valid_looper.update_best_values()
torch.save(
network.state_dict(),
os.path.join(data_path, f"{network_architecture}.pth"),
)
_log(f"New best result: {best_result}", log_file, verbose)
_log("-" * 50, log_file, verbose)
if save is not None:
_plot(train_looper, save)
_plot(valid_looper, save)
_log(f"[Training done] Best valid MAE: {best_valid_mae}", log_file, True)
_log(f"[Training done] Best train MAE: {train_looper.best_mae}", log_file, True)
_log(f"[Training done] Best valid Precision: {valid_looper.best_precision}", log_file, True)
_log(f"[Training done] Best valid Recall: {valid_looper.best_recall}", log_file, True)
def _log(text: str, log_file=None, verbose=False):
"""Print text to file or CLI or both, depending on the options."""
if log_file is not None: print(text, file=log_file)
if verbose: print(text)
def _plot(looper: Looper, path):
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(16, 4))
"""Plot true vs predicted counts and loss."""
fig.suptitle('Training' if not looper.validation else 'Validation')
# true vs predicted counts
true_line = [[0, max(looper.best_true_values)]] * 2 # y = x
# ax[0].cla()
# ax[0].set_title("Object count")
ax[0].set_xlabel('Ground-truth count')
ax[0].set_ylabel('Predicted count')
ax[0].plot(*true_line, 'r-')
ax[0].scatter(looper.best_true_values, looper.best_predicted_values)
# loss
epochs = np.arange(1, len(looper.running_loss) + 1)
# ax[1].cla()
# ax[1].set_title('Training' if not looper.validation else 'Validation')
ax[1].set_xlabel('Epoch')
ax[1].set_ylabel('Loss')
ax[1].plot(epochs, looper.running_loss, "r", label="Loss")
#precision and recall
# ax[2].set_title('')
ax[2].set_ylabel("Evaluation (%)")
ax[2].plot(epochs, looper.mean_precisions, "b", label="Precision")
ax[2].plot(epochs, looper.mean_recalls, "g", label="Recall")
ax[2].legend()
prefix = "train" if not looper.validation else "valid"
id = 1
save_path = os.path.join(path, f"{prefix}{id}.png")
while os.path.exists(save_path):
id += 1
save_path = os.path.join(path, f"{prefix}{id}.png")
fig.savefig(save_path, bbox_inches='tight')
if __name__ == "__main__":
train()