forked from v1xerunt/StageNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
362 lines (295 loc) · 18.2 KB
/
train.py
File metadata and controls
362 lines (295 loc) · 18.2 KB
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 numpy as np
import argparse
import os
import imp
import re
import pickle
import random
import matplotlib.pyplot as plt
import matplotlib as mpl
RANDOM_SEED = 12345
np.random.seed(RANDOM_SEED)
random.seed(RANDOM_SEED)
import torch
from torch import nn
import torch.nn.utils.rnn as rnn_utils
from torch.utils import data
from torch.autograd import Variable
import torch.nn.functional as F
torch.manual_seed(RANDOM_SEED)
torch.cuda.manual_seed(RANDOM_SEED)
torch.backends.cudnn.deterministic=True
from utils import utils
from utils.readers import DecompensationReader
from utils.preprocessing import Discretizer, Normalizer
from utils import metrics
from utils import common_utils
from model import StageNet
def parse_arguments(parser):
parser.add_argument('--test_mode', type=int, default=0, help='Test SA-CRNN on MIMIC-III dataset')
parser.add_argument('--data_path', type=str, metavar='<data_path>', help='The path to the MIMIC-III data directory')
parser.add_argument('--file_name', type=str, metavar='<data_path>', help='File name to save model')
parser.add_argument('--small_part', type=int, default=0, help='Use part of training data')
parser.add_argument('--batch_size', type=int, default=128, help='Training batch size')
parser.add_argument('--epochs', type=int, default=50, help='Training epochs')
parser.add_argument('--lr', type=float, default=0.001, help='Learing rate')
parser.add_argument('--input_dim', type=int, default=76, help='Dimension of visit record data')
parser.add_argument('--rnn_dim', type=int, default=384, help='Dimension of hidden units in RNN')
parser.add_argument('--output_dim', type=int, default=1, help='Dimension of prediction target')
parser.add_argument('--dropout_rate', type=float, default=0.5, help='Dropout rate')
parser.add_argument('--dropconnect_rate', type=float, default=0.5, help='Dropout rate in RNN')
parser.add_argument('--dropres_rate', type=float, default=0.3, help='Dropout rate in residue connection')
parser.add_argument('--K', type=int, default=10, help='Value of hyper-parameter K')
parser.add_argument('--chunk_level', type=int, default=3, help='Value of hyper-parameter K')
args = parser.parse_args()
return args
if __name__ == '__main__':
parser = argparse.ArgumentParser()
args = parse_arguments(parser)
if args.test_mode == 1:
print('Preparing test data ... ')
train_data_loader = common_utils.DeepSupervisionDataLoader(dataset_dir=os.path.join(
args.data_path, 'train'), listfile=os.path.join(args.data_path, 'train_listfile.csv'), small_part=True)
discretizer = Discretizer(timestep=1.0, store_masks=True,
impute_strategy='previous', start_time='zero')
discretizer_header = discretizer.transform(train_data_loader._data["X"][0])[1].split(',')
cont_channels = [i for (i, x) in enumerate(discretizer_header) if x.find("->") == -1]
normalizer = Normalizer(fields=cont_channels)
normalizer_state = 'decomp_normalizer'
normalizer_state = os.path.join(os.path.dirname(args.data_path), normalizer_state)
normalizer.load_params(normalizer_state)
test_data_loader = common_utils.DeepSupervisionDataLoader(dataset_dir=os.path.join(args.data_path, 'test'),
listfile=os.path.join(args.data_path, 'test_listfile.csv'), small_part=args.small_part)
test_data_gen = utils.BatchGenDeepSupervision(test_data_loader, discretizer,
normalizer, args.batch_size,
shuffle=False, return_names=True)
print('Constructing model ... ')
device = torch.device("cuda:0" if torch.cuda.is_available() == True else 'cpu')
print("available device: {}".format(device))
model = StageNet(76+17, 384, 10, 1, 3, 0.3, 0.3, 0.3).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
checkpoint = torch.load('./saved_weights/StageNet')
save_chunk = checkpoint['chunk']
print("last saved model is in chunk {}".format(save_chunk))
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
model.eval()
with torch.no_grad():
cur_test_loss = []
test_true = []
test_pred = []
for each_batch in range(test_data_gen.steps):
test_data = next(test_data_gen)
test_name = test_data['names']
test_data = test_data['data']
test_x = torch.tensor(test_data[0][0], dtype=torch.float32).to(device)
test_mask = torch.tensor(test_data[0][1], dtype=torch.float32).unsqueeze(-1).to(device)
test_y = torch.tensor(test_data[1], dtype=torch.float32).to(device)
tmp = torch.zeros(test_x.size(0),17, dtype=torch.float32).to(device)
test_interval = torch.zeros((test_x.size(0),test_x.size(1),17), dtype=torch.float32).to(device)
for i in range(test_x.size(1)):
cur_ind = test_x[:,i,-17:]
tmp+=(cur_ind == 0).float()
test_interval[:, i, :] = cur_ind * tmp
tmp[cur_ind==1] = 0
if test_mask.size()[1] > 400:
test_x = test_x[:, :400, :]
test_mask = test_mask[:, :400, :]
test_y = test_y[:, :400, :]
test_interval = test_interval[:, :400, :]
test_x = torch.cat((test_x, test_interval), dim=-1)
test_time = torch.ones((test_x.size(0), test_x.size(1)), dtype=torch.float32).to(device)
test_output, test_dis = model(test_x, test_time, device)
masked_test_output = test_output * test_mask
test_loss = test_y * torch.log(masked_test_output + 1e-7) + (1 - test_y) * torch.log(1 - masked_test_output + 1e-7)
test_loss = torch.sum(test_loss, dim=1) / torch.sum(test_mask, dim=1)
test_loss = torch.neg(torch.sum(test_loss))
cur_test_loss.append(test_loss.cpu().detach().numpy())
for m, t, p in zip(test_mask.cpu().numpy().flatten(), test_y.cpu().numpy().flatten(), test_output.cpu().detach().numpy().flatten()):
if np.equal(m, 1):
test_true.append(t)
test_pred.append(p)
print('Test loss = %.4f'%(np.mean(np.array(cur_test_loss))))
print('\n')
test_pred = np.array(test_pred)
test_pred = np.stack([1 - test_pred, test_pred], axis=1)
test_ret = metrics.print_metrics_binary(test_true, test_pred)
else:
''' Prepare training data'''
print('Preparing training data ... ')
train_data_loader = common_utils.DeepSupervisionDataLoader(dataset_dir=os.path.join(
args.data_path, 'train'), listfile=os.path.join(args.data_path, 'train_listfile.csv'), small_part=args.small_part)
val_data_loader = common_utils.DeepSupervisionDataLoader(dataset_dir=os.path.join(
args.data_path, 'train'), listfile=os.path.join(args.data_path, 'val_listfile.csv'), small_part=args.small_part)
discretizer = Discretizer(timestep=1.0, store_masks=True,
impute_strategy='previous', start_time='zero')
discretizer_header = discretizer.transform(train_data_loader._data["X"][0])[1].split(',')
cont_channels = [i for (i, x) in enumerate(discretizer_header) if x.find("->") == -1]
normalizer = Normalizer(fields=cont_channels)
normalizer_state = 'decomp_normalizer'
normalizer_state = os.path.join(os.path.dirname(args.data_path), normalizer_state)
normalizer.load_params(normalizer_state)
train_data_gen = utils.BatchGenDeepSupervision(train_data_loader, discretizer,
normalizer, args.batch_size, shuffle=True, return_names=True)
val_data_gen = utils.BatchGenDeepSupervision(val_data_loader, discretizer,
normalizer, args.batch_size, shuffle=False, return_names=True)
'''Model structure'''
print('Constructing model ... ')
device = torch.device("cuda:0" if torch.cuda.is_available() == True else 'cpu')
print("available device: {}".format(device))
model = StageNet(args.input_dim+17, args.rnn_dim, args.K, args.output_dim, args.chunk_level, args.dropconnect_rate, args.dropout_rate, args.dropres_rate).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
'''Train phase'''
print('Start training ... ')
train_loss = []
val_loss = []
batch_loss = []
max_auprc = 0
file_name = './saved_weights/'+args.file_name
for each_chunk in range(args.epochs):
cur_batch_loss = []
model.train()
for each_batch in range(train_data_gen.steps):
batch_data = next(train_data_gen)
batch_name = batch_data['names']
batch_data = batch_data['data']
batch_x = torch.tensor(batch_data[0][0], dtype=torch.float32).to(device)
batch_mask = torch.tensor(batch_data[0][1], dtype=torch.float32).unsqueeze(-1).to(device)
batch_y = torch.tensor(batch_data[1], dtype=torch.float32).to(device)
tmp = torch.zeros(batch_x.size(0),17, dtype=torch.float32).to(device)
batch_interval = torch.zeros((batch_x.size(0),batch_x.size(1),17), dtype=torch.float32).to(device)
for i in range(batch_x.size(1)):
cur_ind = batch_x[:,i,-17:]
tmp+=(cur_ind == 0).float()
batch_interval[:, i, :] = cur_ind * tmp
tmp[cur_ind==1] = 0
if batch_mask.size()[1] > 400:
batch_x = batch_x[:, :400, :]
batch_mask = batch_mask[:, :400, :]
batch_y = batch_y[:, :400, :]
batch_interval = batch_interval[:, :400, :]
batch_x = torch.cat((batch_x, batch_interval), dim=-1)
batch_time = torch.ones((batch_x.size(0), batch_x.size(1)), dtype=torch.float32).to(device)
optimizer.zero_grad()
cur_output, _ = model(batch_x, batch_time, device)
masked_output = cur_output * batch_mask
loss = batch_y * torch.log(masked_output + 1e-7) + (1 - batch_y) * torch.log(1 - masked_output + 1e-7)
loss = torch.sum(loss, dim=1) / torch.sum(batch_mask, dim=1)
loss = torch.neg(torch.sum(loss))
cur_batch_loss.append(loss.cpu().detach().numpy())
loss.backward()
optimizer.step()
if each_batch % 50 == 0:
print('Chunk %d, Batch %d: Loss = %.4f'%(each_chunk, each_batch, cur_batch_loss[-1]))
batch_loss.append(cur_batch_loss)
train_loss.append(np.mean(np.array(cur_batch_loss)))
print("\n==>Predicting on validation")
with torch.no_grad():
model.eval()
cur_val_loss = []
valid_true = []
valid_pred = []
for each_batch in range(val_data_gen.steps):
valid_data = next(val_data_gen)
valid_name = valid_data['names']
valid_data = valid_data['data']
valid_x = torch.tensor(valid_data[0][0], dtype=torch.float32).to(device)
valid_mask = torch.tensor(valid_data[0][1], dtype=torch.float32).unsqueeze(-1).to(device)
valid_y = torch.tensor(valid_data[1], dtype=torch.float32).to(device)
tmp = torch.zeros(valid_x.size(0),17, dtype=torch.float32).to(device)
valid_interval = torch.zeros((valid_x.size(0),valid_x.size(1),17), dtype=torch.float32).to(device)
for i in range(valid_x.size(1)):
cur_ind = valid_x[:,i,-17:]
tmp+=(cur_ind == 0).float()
valid_interval[:, i, :] = cur_ind * tmp
tmp[cur_ind==1] = 0
if valid_mask.size()[1] > 400:
valid_x = valid_x[:, :400, :]
valid_mask = valid_mask[:, :400, :]
valid_y = valid_y[:, :400, :]
valid_interval = valid_interval[:, :400, :]
valid_x = torch.cat((valid_x, valid_interval), dim=-1)
valid_time = torch.ones((valid_x.size(0), valid_x.size(1)), dtype=torch.float32).to(device)
valid_output, valid_dis = model(valid_x, valid_time, device)
masked_valid_output = valid_output * valid_mask
valid_loss = valid_y * torch.log(masked_valid_output + 1e-7) + (1 - valid_y) * torch.log(1 - masked_valid_output + 1e-7)
valid_loss = torch.sum(valid_loss, dim=1) / torch.sum(valid_mask, dim=1)
valid_loss = torch.neg(torch.sum(valid_loss))
cur_val_loss.append(valid_loss.cpu().detach().numpy())
for m, t, p in zip(valid_mask.cpu().numpy().flatten(), valid_y.cpu().numpy().flatten(), valid_output.cpu().detach().numpy().flatten()):
if np.equal(m, 1):
valid_true.append(t)
valid_pred.append(p)
val_loss.append(np.mean(np.array(cur_val_loss)))
print('Valid loss = %.4f'%(val_loss[-1]))
print('\n')
valid_pred = np.array(valid_pred)
valid_pred = np.stack([1 - valid_pred, valid_pred], axis=1)
ret = metrics.print_metrics_binary(valid_true, valid_pred)
print()
cur_auprc = ret['auprc']
if cur_auprc > max_auprc:
max_auprc = cur_auprc
state = {
'net': model.state_dict(),
'optimizer': optimizer.state_dict(),
'chunk': each_chunk
}
torch.save(state, file_name)
print('\n------------ Save best model ------------\n')
'''Evaluate phase'''
print('Testing model ... ')
checkpoint = torch.load(file_name)
save_chunk = checkpoint['chunk']
print("last saved model is in chunk {}".format(save_chunk))
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
model.eval()
test_data_loader = common_utils.DeepSupervisionDataLoader(dataset_dir=os.path.join(args.data_path, 'test'),
listfile=os.path.join(args.data_path, 'test_listfile.csv'), small_part=args.small_part)
test_data_gen = utils.BatchGenDeepSupervision(test_data_loader, discretizer,
normalizer, args.batch_size,
shuffle=False, return_names=True)
with torch.no_grad():
torch.manual_seed(RANDOM_SEED)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(RANDOM_SEED)
cur_test_loss = []
test_true = []
test_pred = []
for each_batch in range(test_data_gen.steps):
test_data = next(test_data_gen)
test_name = test_data['names']
test_data = test_data['data']
test_x = torch.tensor(test_data[0][0], dtype=torch.float32).to(device)
test_mask = torch.tensor(test_data[0][1], dtype=torch.float32).unsqueeze(-1).to(device)
test_y = torch.tensor(test_data[1], dtype=torch.float32).to(device)
tmp = torch.zeros(test_x.size(0),17, dtype=torch.float32).to(device)
test_interval = torch.zeros((test_x.size(0),test_x.size(1),17), dtype=torch.float32).to(device)
for i in range(test_x.size(1)):
cur_ind = test_x[:,i,-17:]
tmp+=(cur_ind == 0).float()
test_interval[:, i, :] = cur_ind * tmp
tmp[cur_ind==1] = 0
if test_mask.size()[1] > 400:
test_x = test_x[:, :400, :]
test_mask = test_mask[:, :400, :]
test_y = test_y[:, :400, :]
test_interval = test_interval[:, :400, :]
test_x = torch.cat((test_x, test_interval), dim=-1)
test_time = torch.ones((test_x.size(0), test_x.size(1)), dtype=torch.float32).to(device)
test_output, test_dis = model(test_x, test_time, device)
masked_test_output = test_output * test_mask
test_loss = test_y * torch.log(masked_test_output + 1e-7) + (1 - test_y) * torch.log(1 - masked_test_output + 1e-7)
test_loss = torch.sum(test_loss, dim=1) / torch.sum(test_mask, dim=1)
test_loss = torch.neg(torch.sum(test_loss))
cur_test_loss.append(test_loss.cpu().detach().numpy())
for m, t, p in zip(test_mask.cpu().numpy().flatten(), test_y.cpu().numpy().flatten(), test_output.cpu().detach().numpy().flatten()):
if np.equal(m, 1):
test_true.append(t)
test_pred.append(p)
print('Test loss = %.4f'%(np.mean(np.array(cur_test_loss))))
print('\n')
test_pred = np.array(test_pred)
test_pred = np.stack([1 - test_pred, test_pred], axis=1)
test_ret = metrics.print_metrics_binary(test_true, test_pred)