-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_hvd.py
362 lines (297 loc) · 11.2 KB
/
training_hvd.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import os
import sys, glob
import math
import argparse
import torch
import pandas as pd
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, DistributedSampler
from torch.nn import BCEWithLogitsLoss
from torch.optim import Adam
from dataset import Segmentation_dataset
import time
from datetime import timedelta
from models import Unet
from metric_losses import jaccard_coef
import horovod.torch as hvd
# custom learning rate optimised for 200 epochs
def custom_lr(optimizer, epoch, lr=0.001, num_workers=1):
# optimised for 200 epochs
for pm in optimizer.param_groups:
# print(epoch, pm["lr"])
if epoch < 80:
pm["lr"] = lr * num_workers
# if epoch == 0:
# pm["lr"] = lr
# elif epoch > 0 and epoch < 10:
# increment = ((lr * num_workers) - lr) / 10
# pm["lr"] = pm["lr"] + increment
# elif epoch >= 10 and epoch < 40:
# pm["lr"] = lr * num_workers
elif epoch >= 80 and epoch < 120:
pm["lr"] = lr / 2 * num_workers
elif epoch >= 120 and epoch < 150:
pm["lr"] = lr / 4 * num_workers
elif epoch >= 150:
pm["lr"] = lr / 8 * num_workers
def get_lr(optimizer):
for pm in optimizer.param_groups:
return pm["lr"]
# datasets and data loaders
def dataset(args, image_dir, mask_dir):
images = sorted(os.listdir(image_dir))
masks = sorted(os.listdir(mask_dir))
print(len(images))
# split in train and test
tr_images, ts_images, tr_masks, ts_masks = train_test_split(
images, masks, test_size=0.2, random_state=42
)
# repeat dataset
repeat = args.repeat
train_im = tr_images * repeat
train_ma = tr_masks * repeat
test_im = ts_images # * repeat
test_ma = ts_masks # * repeat
if args.augment == 0:
augment = False
else:
augment = True
# seg datasets
tr_set = Segmentation_dataset(
image_dir, mask_dir, train_im, train_ma, augment=augment
)
ts_set = Segmentation_dataset(
image_dir, mask_dir, test_im, test_ma, augment=augment
)
print(len(tr_set), len(ts_set))
def get_loader(ds, args, distribute=True):
ds_sampler = None
if distribute:
ds_sampler = DistributedSampler(
dataset=ds,
#shuffle=True,
num_replicas=hvd.size(),
rank=hvd.rank(),
)
data_loader = DataLoader(
dataset=ds,
batch_size=args.local_batch_size if distribute else args.global_batch_size,
pin_memory=args.use_gpu,
#shuffle=True,
sampler=ds_sampler,
drop_last=True,
)
return data_loader
train_dataloader = get_loader(tr_set, args, distribute=True)
test_dataloader = get_loader(ts_set, args, distribute=True)
return train_dataloader, test_dataloader
def train(args, train_dataloader, test_dataloader):
# get model
print("Creating the model ...")
sys.stdout.flush()
model = Unet(num_class=1).to(args.device)
# initialize loss function and optimizer
print("Creating the loss and optimizer ...")
sys.stdout.flush()
lossFunc = BCEWithLogitsLoss()
opt = Adam(model.parameters(), lr=args.lr)
# wrapping the optimizer
opt = hvd.DistributedOptimizer(opt, named_parameters=model.named_parameters(), op=hvd.Average)
# Horovod: broadcast parameters & optimizer state.
hvd.broadcast_parameters(model.state_dict(), root_rank=0)
hvd.broadcast_optimizer_state(opt, root_rank=0)
train_loss = []
test_loss = []
time_per_epoch = []
lr_save = []
trainSteps = len(train_dataloader)
testSteps = len(test_dataloader)
if hvd.rank() == 0:
print(trainSteps)
sys.stdout.flush()
# loop over epochs
train_time = time.time()
print("[INFO] training the network...")
sys.stdout.flush()
for e in range(args.epoch):
# set the model in training mode
model.train()
# initialize the total training and validation loss
totalTrainLoss = 0
totalTestLoss = 0
# epoch time
elapsed_train = time.time()
# loop over the training set
for (i, (x, y)) in enumerate(train_dataloader):
# send the input to the device
(x, y) = (
x.to(args.device, non_blocking=True),
y.to(args.device, non_blocking=True),
)
# perform a forward pass and calculate the training loss
pred = model(x)
loss = lossFunc(pred, y)
# first, zero out any previously accumulated gradients, then
# perform backpropagation, and then update model parameters
opt.zero_grad()
loss.backward()
opt.step()
# add the loss to the total training loss so far
totalTrainLoss += loss.item()
elapsed_train = time.time() - elapsed_train
lr_save.append(get_lr(opt))
custom_lr(opt, e + 1, lr=args.lr, num_workers=hvd.size())
# my_lr_scheduler.step()
# print(my_lr_scheduler.get_last_lr())
# calculate the average training and validation loss
avgTrainLoss = totalTrainLoss / trainSteps
# avgTestLoss = totalTestLoss / testSteps
# update our training history
# print the model training and time every epoch
if hvd.rank() == 0:
train_loss.append(avgTrainLoss)
# test_loss.append(avgTestLoss)
time_per_epoch.append(elapsed_train)
print("[INFO] EPOCH: {}/{}".format(e + 1, args.epoch))
print(
"Train loss: {:.6f}, elapsed time: {}".format(
avgTrainLoss, elapsed_train
)
)
sys.stdout.flush()
# custom_lr(opt, e + 1, lr=args.lr, num_workers=args.world_size)
# lr.append(get_lr(opt))
total_train_time = time.time() - train_time
df_save = pd.DataFrame()
if hvd.rank() == 0:
df_save["time_per_epoch"] = time_per_epoch
df_save["loss"] = train_loss
df_save["lr"] = lr_save
df_save["training_time"] = total_train_time
print("Elapsed execution time: " + str(total_train_time) + " sec")
sys.stdout.flush()
# with open("tr_loss.txt", "w") as f:
# print(loss_dict, file=f)
return model, df_save
def test(args, model, test_dataloader, df_save):
size = next(iter(test_dataloader))[0].shape
B, C, H, W = size[0], size[1], size[2], size[3]
# print("Test B,C,H,W", B, C, H, W)
testSteps = len(test_dataloader)
# saving the validation set in eval loop
inputs = torch.zeros((len(test_dataloader) * B, C, H, W))
labels = torch.zeros((len(test_dataloader) * B, C, H, W))
predicts = torch.zeros((len(test_dataloader) * B, C, H, W))
lossFunc = BCEWithLogitsLoss()
test_loss = []
totalTestLoss = 0
# switch off autograd
s = 0
elapsed_eval = time.time()
with torch.no_grad():
model.eval()
# loop over the validation set
for (x, y) in test_dataloader:
# send the input to the device
(x, y) = (x.to(args.device), y.to(args.device))
# make the predictions
pred = model(x)
# calculate the validation loss
totalTestLoss += lossFunc(pred, y).item()
# filling empty valid set tensors
# print("x,y shape:", x.shape, y.shape)
inputs[s : s + B] = x.cpu()
labels[s : s + B] = y.cpu()
predicts[s : s + B] = pred.cpu()
s += B
avgTestLoss = totalTestLoss / testSteps
# test_loss.append(avgTestLoss)
elapsed_eval = time.time() - elapsed_eval
# print(test_loss, len(test_loss))
# torch tensor outs
labels_n = labels.numpy() # .type(torch.int)
preds_n = (torch.sigmoid(predicts) > 0.5).type(torch.int).numpy()
IOU = jaccard_coef(labels_n, preds_n)
# df_save["val_loss"] = test_loss
df_save["test_time"] = elapsed_eval
df_save["iou"] = IOU
print("Test loss: {:.6f}, elapsed time: {}".format(avgTestLoss, elapsed_eval))
print("IOU on test: {}".format(IOU))
sys.stdout.flush()
return df_save
def main(args):
torch.manual_seed(1235)
hvd.init()
hvd.allreduce(torch.tensor([0]), name="Barrier")
args.local_batch_size = math.ceil(args.global_batch_size / hvd.size())
args.distributed = hvd.size() > 0
# Device configuration
print(f"Cuda available: {torch.cuda.is_available()} - Device count: {torch.cuda.device_count()}")
args.use_gpu = torch.cuda.is_available() and torch.cuda.device_count() > 0
if args.use_gpu:
torch.backends.cudnn.benchmark = True # enable built-in cuda auto tuner
torch.cuda.set_device(hvd.local_rank())
torch.cuda.manual_seed(42)
args.device = torch.device("cuda:%d" % hvd.local_rank())
if hvd.rank() == 0:
print("PyTorch Settings:")
settings_map = vars(args)
for name in sorted(settings_map.keys()):
print("--" + str(name) + ": " + str(settings_map[name]))
print("")
sys.stdout.flush()
image_dir = args.image_dir
mask_dir = args.mask_dir
if args.distributed:
hvd.allreduce(torch.tensor([0]), name="Barrier")
print(hvd.rank())
sys.stdout.flush()
train_dataloader, test_dataloader = dataset(args, image_dir, mask_dir)
if hvd.rank() == 0:
print(len(train_dataloader), len(test_dataloader))
sys.stdout.flush()
model, df_save = train(args, train_dataloader, test_dataloader)
if args.distributed:
hvd.allreduce(torch.tensor([0]), name="Barrier")
if hvd.rank() == 0:
df_save = test(args, model, test_dataloader, df_save)
df_save.to_csv("./log.csv", sep=",", float_format="%.6f")
# destory the process group again
if args.distributed:
hvd.allreduce(torch.tensor([0]), name="Barrier")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Training args")
parser.add_argument("--global_batch_size", type=int, help="8 or 16 or 32")
# parser.add_argument("--device", type=str, help="cuda" or "cpu", default="cuda")
parser.add_argument("--lr", type=float, help="ex. 0.001", default=0.001)
parser.add_argument("--repeat", type=int, help="for dataset repeat", default=2)
parser.add_argument("--epoch", type=int, help="iterations")
parser.add_argument(
"--image_dir",
type=str,
help="directory of images",
default=str(os.environ["WORK"]) + "/images_collective",
)
parser.add_argument(
"--mask_dir",
type=str,
help="directory of masks",
default=str(os.environ["WORK"]) + "/masks_collective",
)
parser.add_argument("--augment", type=int, help="0 is False, 1 is True", default=0)
parser.add_argument(
"--bucket_cap_mb",
required=False,
help="max message bucket size in mb",
type=int,
default=25,
)
parser.add_argument(
"--backend",
required=False,
help="Backend used by torch.distribute",
type=str,
default="nccl",
)
args = parser.parse_args()
main(args)