-
Notifications
You must be signed in to change notification settings - Fork 1
/
micnn_train4_0.py
242 lines (200 loc) · 8.73 KB
/
micnn_train4_0.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
import os
import argparse
import json
from glob import glob
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import datasets.wsi_dataset_train as wsi_dataset
import models.mobilenet as mobilenet
import loss.censored_crossentropy_loss as cce_loss
from utils import ensure_dir
def load_last_model(model_path, net, net_pred, data_part):
models = glob('{}/*_rgb.pth'.format(model_path))
model_ids = [(int(f.split('_')[2]), f) for f in [p.split('/')[-1].split('.')[0] for p in models]]
if not model_ids:
print('No net loaded!')
epoch = -1
else:
if data_part == 1 or data_part == 3:
epoch, fn = max(model_ids, key=lambda item: item[0])
net.load_state_dict(torch.load('{}/{}.pth'.format(
model_path, fn))
)
if data_part == 2 or data_part == 3:
if data_part == 2:
epoch, _ = max(model_ids, key=lambda item: item[0])
net_pred.load_state_dict(torch.load('{}/model_epoch_{}_pred.pth'.format(
model_path, epoch))
)
print('{}.pth for patch classification loaded!'.format(fn))
return net, net_pred, epoch
def train(args, config, device):
wsi_root = config['tile_process']['WSIs']['output_path']
nu_seg_root = config['tile_process']['Nuclei_segs']['output_path']
tumor_pred_root = config['tile_process']['Tumor_preds']['output_path']
til_pred_root = config['tile_process']['TIL_preds']['output_path']
data_root = config['dataset']['data_root']
input_nc = config['dataset']['input_nc']
data_part = config['dataset']['data_part']
data_file_path = config['dataset']['data_file_path']
n_patches = config['dataset']['n_patches_per_wsi']
interval = config['dataset']['interval']
n_intervals = config['dataset']['n_intervals']
batch_size = config['dataset']['batch_size']
num_workers = config['dataset']['num_workers']
mask_root = config['dataset']['mask_root']
n_epochs = config['train']['n_epochs']
lr = config['train']['learning_rate']
output_dir = config['train']['output_dir']
log_freq = config['train']['log_freq']
save_freq = config['train']['save_freq']
valid_freq = config['valid']['valid_freq']
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
_6c = input_nc > 3
rgb_only = not _6c
# train_set = wsi_dataset.WSI_Dataset(data_root, csv_file_path, input_nc, transform, 'train', n_patches, interval, n_intervals)
# valid_set = wsi_dataset(data_root, csv_file_path, input_nc, transform, 'valid', n_patches, interval, n_intervals)
train_set = wsi_dataset.Patch_Data(
wsi_root=wsi_root,
nu_seg_root=nu_seg_root,
tumor_pred_root=tumor_pred_root,
til_pred_root=til_pred_root,
data_file_path=data_file_path,
mask_root=mask_root,
mode='train',
scale=args.scale,
round_no=0,
n_patches=n_patches,
interval=interval,
n_intervals=n_intervals,
rgb_only=rgb_only,
data_part=data_part
)
train_set.set_scale(args.scale)
train_set.set_round_no(0)
# num_workers = 2 # for debug
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=1,
shuffle=True,
num_workers=num_workers, # for debug
drop_last=False
)
'''
valid_loader = torch.utils.data.DataLoader(
valid_set,
batch_size=1,
shuffle=False,
num_workers=num_workers,
drop_last=False
)
'''
if data_part == 1 or data_part == 3:
model = mobilenet.mobilenet_v2(pretrained=False, progress=True, input_nc=3, num_classes=n_intervals)
if data_part == 2 or data_part == 3:
model_pred = mobilenet.mobilenet_v2(pretrained=False, progress=True, input_nc=3, num_classes=n_intervals)
if torch.cuda.device_count() > 1:
print("Use", torch.cuda.device_count(), "GPUs!")
if data_part == 1 or data_part == 3:
model = nn.DataParallel(model)
if data_part == 2 or data_part == 3:
model_pred = nn.DataParallel(model_pred)
if data_part == 1 or data_part == 3:
model = model.to(device)
if data_part == 2 or data_part == 3:
model_pred = model_pred.to(device)
ensure_dir(output_dir)
log_fn = '{}/log.txt'.format(output_dir)
ckpt_dir = '{}/checkpoints'.format(output_dir)
ensure_dir(ckpt_dir)
model, model_pred, epoch_prev = load_last_model(ckpt_dir, model, model_pred, data_part)
if data_part == 1 or data_part == 3:
model_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = optim.RMSprop(model_params, lr=lr)
criterion = cce_loss.CensoredCrossEntropyLoss()
if data_part == 2 or data_part == 3:
model_pred_params = filter(lambda p: p.requires_grad, model_pred.parameters())
optimizer_pred = optim.RMSprop(model_pred_params, lr=lr)
criterion_pred = cce_loss.CensoredCrossEntropyLoss()
if data_part == 1 or data_part == 3:
model.train()
if data_part == 2 or data_part == 3:
model_pred.train()
for epoch in range(epoch_prev+1, n_epochs+1):
if data_part == 1 or data_part == 3:
running_loss = 0
if data_part == 2 or data_part == 3:
running_loss_pred = 0
for idx, data in enumerate(train_loader, 0):
inputs, y, obs = data
inputs, y, obs = inputs[0].to(device), y.to(device), obs.to(device)
if data_part == 3:
imgs, preds = inputs[:, :3, :, :], inputs[:, 3:, :, :]
elif data_part == 1 or data_part == 2:
imgs = inputs
if data_part == 1 or data_part == 3:
optimizer.zero_grad()
output = model(imgs)
loss = criterion(output, y, obs)
loss.backward()
optimizer.step()
running_loss += loss.item()
if data_part == 2 or data_part == 3:
optimizer_pred.zero_grad()
output_pred = model_pred(preds)
loss_pred = criterion_pred(output_pred, y, obs)
loss_pred.backward()
optimizer_pred.step()
running_loss_pred += loss_pred.item()
print('epoch {}, idx {} done!'.format(epoch, idx))
if data_part == 1 or data_part == 3:
avg_loss = running_loss / (idx + 1)
if data_part == 2 or data_part == 3:
avg_loss_pred = running_loss_pred / (idx + 1)
if epoch % log_freq == 0:
if data_part == 3:
log_str = 'Epoch {:d}/{:d}, loss: {:.6f}, loss_pred: {:.6f}'.format(epoch, n_epochs, avg_loss, avg_loss_pred)
elif data_part == 1:
log_str = 'Epoch {:d}/{:d}, loss: {:.6f}'.format(epoch, n_epochs, avg_loss)
elif data_part == 2:
log_str = 'Epoch {:d}/{:d}, loss_pred: {:.6f}'.format(epoch, n_epochs, avg_loss_pred)
else:
pass
print(log_str)
with open(log_fn, 'a') as f:
f.write('{}\n'.format(log_str))
if epoch % save_freq == 0:
if data_part == 1 or data_part == 3:
torch.save(model.state_dict(), '{}/model_epoch_{}_rgb.pth'.format(ckpt_dir, epoch))
if data_part == 2 or data_part == 3:
torch.save(model_pred.state_dict(), '{}/model_epoch_{}_pred.pth'.format(ckpt_dir, epoch))
if False: # epoch % valid_freq == 0:
model.eval()
for idx, data in enumerate(valid_loader, 0):
imgs, y, obs = data
imgs = imgs[0]
output = model(imgs)
model.train()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch MICNN')
parser.add_argument('-c', '--config', default=None, type=str,
help='config file path (default: None)')
parser.add_argument('-s', '--scale', default=1, type=int,
help='scale (default: 1)')
parser.add_argument('-d', '--gpu_ids', default='0', type=str,
help='indices of GPUs to enable (default: 0)')
args = parser.parse_args()
if args.config:
# load config file
config = json.load(open(args.config))
else:
raise AssertionError("Configuration file need to be specified. Add '-c config.json', for example.")
if args.gpu_ids:
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_ids
device = torch.device("cuda:{0}".format(0) if torch.cuda.is_available() else "cpu")
train(args, config, device)