-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
252 lines (206 loc) · 6.82 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
242
243
244
245
246
247
248
249
250
251
252
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import random_split
import matplotlib.pyplot as plt
import argparse
import os
from givtednet.model import GIVTEDNet
from tools.train import train_epoch
from tools.utils import EarlyStopping, AvgMeter
from tools.loss import LossFunction
from dataset.loader import get_dataset, get_loader
def parse_arguments():
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="Training configuration.")
# Add arguments
parser.add_argument(
"--epoch",
type=int,
default=100,
help="Training epochs.")
parser.add_argument(
"-lr",
"--learning_rate",
type=float,
default=1e-2,
help="Learning rate for training.")
parser.add_argument(
"--dropout",
type=float,
default=0.1,
help="Dropout rate for the model.")
parser.add_argument(
"--weight_decay",
type=float,
default=1e-3,
help="Weight decay for SGD.")
parser.add_argument(
"--momentum",
type=float,
default=0.9,
help="Momentum for SGD.")
parser.add_argument(
"--batch_size",
type=int,
default=16,
help="Training batch sizes.")
parser.add_argument(
"--image_size",
type=int,
default=224,
help="Training image size.")
parser.add_argument(
"--epsilon",
type=float,
default=1e-8,
help="Small value for numerical stability.")
parser.add_argument(
"--early_stopping_patience",
type=int,
default=10,
help="Early stopping patience.")
parser.add_argument(
"--lr_scheduler_cooldown",
type=int,
default=8,
help="Learning rate scheduler cooldown.")
parser.add_argument(
"--step_lr",
type=int,
default=3,
help="How many step to reduce the learning rate once the performance degrades during training.")
parser.add_argument(
"--dataset_name",
type=str,
required=True,
help="Dataset name.")
# Parse arguments from the command line
return parser.parse_args()
def train_fn():
# Parse arguments
config = parse_arguments()
best = 0.0
train_path = f"./experiment/{config.dataset_name}/TrainDataset"
train_save = f"./experiment/{config.dataset_name}/model_pth"
os.makedirs(train_save, exist_ok=True)
model = GIVTEDNet(config.dropout)
weight_pth = os.path.join(train_save, "GIVTEDNet.pth")
if os.path.exists(weight_pth):
print(f"Weight found: {weight_pth}")
if torch.cuda.is_available():
model.load_state_dict(torch.load(weight_pth))
else:
model.load_state_dict(
torch.load(
weight_pth,
map_location=torch.device('cpu')))
if torch.cuda.is_available():
model.cuda()
criterion = LossFunction(config.epsilon)
optimizer = torch.optim.SGD(
model.parameters(),
config.lr,
weight_decay=config.weight_decay,
momentum=config.momentum,
nesterov=True,
)
image_root = f"{train_path}/images/"
gt_root = f"{train_path}/masks/"
dataset = get_dataset(
image_root,
gt_root,
image_size=config.image_size,
)
n_train = int(len(dataset) * 0.9)
n_val = len(dataset) - n_train
train_set, val_set = random_split(dataset, [n_train, n_val])
loss_train = [0]
loss_val = [0]
train_loader = get_loader(train_set, config.batch_size)
val_loader = get_loader(val_set, config.batch_size)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
'max',
patience=config.step_lr,
cooldown=config.lr_scheduler_cooldown,
verbose=True,
)
early_stopping = EarlyStopping(patience=config.early_stopping_patience)
total_step = len(train_loader)
print("#" * 20, "Start Training", "#" * 20)
for n in range(1, config.epoch + 1):
loss = train_epoch(
model,
train_loader,
criterion,
optimizer,
n,
config.epoch,
config.batch_size,
total_step
).data.cpu().numpy()
loss_train.append(loss)
val_score = 0
loss_record = AvgMeter()
for i, pack in enumerate(val_loader, start=1):
model.eval()
images, gts = pack
images = Variable(images).cuda(
) if torch.cuda.is_available() else Variable(images)
gts = Variable(gts).cuda(
) if torch.cuda.is_available() else Variable(gts)
with torch.no_grad():
res = model(images)
loss = criterion(res, gts)
loss_record.update(loss.data, config.batch_size)
res = torch.sigmoid(res)
res = abs(res - res.min()) / \
(abs(res.max() - res.min()) + config.epsilon)
inter = ((res * gts)).sum(dim=(2, 3))
union = ((res + gts)).sum(dim=(2, 3))
dice = (2 * abs(inter)) / (abs(union) + config.epsilon)
dice = float(dice.mean().data.cpu().numpy())
val_score += dice
loss_val.append(loss_record.show().data.cpu().numpy())
val_score /= float(len(val_loader))
scheduler.step(val_score)
if not os.path.exists(train_save):
os.makedirs(train_save)
print("Best: ", best)
print("Val: ", val_score)
if val_score > best:
best = val_score
torch.save(
model.state_dict(),
os.path.join(
train_save,
f'GIVTEDNet_best.pth'))
torch.save(
model.state_dict(),
os.path.join(
train_save,
f'GIVTEDNet.pth'))
plt.plot(loss_train, color='r', label='train')
plt.plot(loss_val, color='b', label='validation')
plt.xlabel("epoch")
plt.ylabel("loss")
plt.legend()
plt.grid()
plt.savefig(f"./experiment/{config.dataset_name}/loss_plot.png")
plt.clf()
with open(f"./experiment/{config.dataset_name}/loss_history.txt", 'a') as f:
f.write("Loss Train: [")
for val in loss_train:
f.write(f" {val} ")
f.write("]\n")
f.write("Loss Validation: [")
for val in loss_val:
f.write(f" {val} ")
f.write("]\n")
f.write("-----------------------------------------------------------\n")
if early_stopping.early_stop(loss_val[-1]):
print(f"Training stopped at epoch: {n}")
break
if __name__ == "__main__":
train_fn()