-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
151 lines (124 loc) · 4.84 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
import os
import torch
import numpy as np
import segmentation_models_pytorch as smp
import random
from torch.utils.data import DataLoader
import config
from datasets import DRACDataset
from preprocess import get_training_augmentation, get_preprocessing, get_validation_augmentation
from utils import AUC, Specificity,Dice
if __name__ == '__main__':
random_seed = 1387
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed)
#cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
ENCODER = config.ENCODER
ENCODER_WEIGHTS = config.ENCODER_WEIGHTS
CLASSES = config.CLASSES
ACTIVATION = config.ACTIVATION
DEVICE = config.DEVICE
NAME = config.NAME
# create segmentation model with pretrained encoder
model = smp.UnetPlusPlus(
encoder_name=ENCODER,
encoder_weights=ENCODER_WEIGHTS,
classes=len(CLASSES),
in_channels=3,
activation=ACTIVATION,
)
preprocessing_fn = smp.encoders.get_preprocessing_fn(ENCODER, ENCODER_WEIGHTS)
DATA_DIR = config.DATA_DIR
classes = {'IMA':'1. Intraretinal Microvascular Abnormalities', 'NA':'2. Nonperfusion Areas', 'NE':'3. Neovascularization', 'NE_nohealth':'3. Neovascularization'}
x_train_dir = os.path.join(DATA_DIR, '1. Original Images', 'a. Training Set')
y_train_dir = os.path.join(DATA_DIR, '2. Groundtruths', 'a. Training Set', classes[CLASSES[0]])
x_valid_dir = os.path.join(DATA_DIR, '1. Original Images', 'val')
y_valid_dir = os.path.join(DATA_DIR, '2. Groundtruths', 'val', classes[CLASSES[0]])
train_dataset = DRACDataset(
x_train_dir,
y_train_dir,
augmentation=get_training_augmentation(),
preprocessing=get_preprocessing(preprocessing_fn),
)
valid_dataset = DRACDataset(
x_valid_dir,
y_valid_dir,
augmentation=get_validation_augmentation(),
preprocessing=get_preprocessing(preprocessing_fn),
)
train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=8)
valid_loader = DataLoader(valid_dataset, batch_size=1, shuffle=False, num_workers=4)
# Dice/F1 score - https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
# IoU/Jaccard score - https://en.wikipedia.org/wiki/Jaccard_index
# loss = smp.utils.losses.BCELoss()
loss = smp.utils.losses.DiceLoss()
# loss = FocalLoss()
# loss = smp.losses.DiceLoss(mode='binary')
# loss = smp.losses.SoftBCEWithLogitsLoss()
metrics = [
smp.utils.metrics.IoU(threshold=0.5),
Dice(threshold=0.5)
# smp.utils.metrics.Dice(threshold=0.5),
# AUC(threshold=0.5),
# smp.utils.metrics.Recall(threshold=0.5),
# Specificity(threshold=0.5)
]
optimizer = torch.optim.Adam([
dict(params=model.parameters(), lr=1e-4), # origin 0.0001
])
scheduler=torch.optim.lr_scheduler.StepLR(optimizer, step_size=25, gamma=0.6)
# create epoch runners
# it is a simple loop of iterating over dataloader`s samples
train_epoch = smp.utils.train.TrainEpoch(
model,
loss=loss,
metrics=metrics,
optimizer=optimizer,
device=DEVICE,
verbose=True,
)
valid_epoch = smp.utils.train.ValidEpoch(
model,
loss=loss,
metrics=metrics,
device=DEVICE,
verbose=True,
)
if not os.path.exists('./models'):
os.makedirs('./models')
if not os.path.exists('./logs'):
os.makedirs('./logs')
log_txt = open('./logs/'+CLASSES[0]+'_'+NAME+'_best_model.txt', 'w')
# train model for 100 epochs
max_score = 0
best_epoch = 0
for i in range(0, 100):
print('\nEpoch: {}'.format(i))
train_logs = train_epoch.run(train_loader)
valid_logs = valid_epoch.run(valid_loader)
log_txt.write('\nEpoch: {} '.format(i))
log_txt.write('iou_score: {} '.format(valid_logs['iou_score']))
log_txt.write('dice_score: {} '.format(valid_logs['dice']))
log_txt.flush()
# do something (save model, change lr, etc.)
if max_score < valid_logs['dice']:
max_score = valid_logs['dice']
# if max_score < valid_logs['iou_score']:
# max_score = valid_logs['iou_score']
best_epoch = i
torch.save(model, './models/' + CLASSES[0] + '_'+NAME+'_best_model.pth')
print('Model saved!')
if CLASSES[0] != 'IMA':
scheduler.step()
else:
if i == 25:
optimizer.param_groups[0]['lr'] = 1e-5
print('Decrease decoder learning rate to 1e-5!')
log_txt.write('\nBest epoch: {} '.format(best_epoch))
log_txt.write('Best dice_score: {} '.format(max_score))
log_txt.flush()
log_txt.close()