-
Notifications
You must be signed in to change notification settings - Fork 2
/
dcnv2_ecg_train.py
243 lines (187 loc) · 7.25 KB
/
dcnv2_ecg_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
import signal
import os
import argparse
import numpy as np
from tqdm import tqdm
import pandas as pd
import joblib
from collections import OrderedDict
from datetime import datetime
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torchvision import models
from utils import *
import Deform_CNN.arch as archs
from load_dataset import ECG
arch_names = archs.__dict__.keys()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--name', default=None,
help='model name: (default: arch+timestamp)')
parser.add_argument('--arch', '-a', metavar='ARCH', default='DeformCNN',
choices=arch_names,
help='model architecture: ' +
' | '.join(arch_names) +
' (default: Dcnv2EcgNet)')
parser.add_argument('--deform', default=True, type=str2bool,
help='use deform conv')
parser.add_argument('--modulation', default=True, type=str2bool,
help='use modulated deform conv')
parser.add_argument('--min-deform-layer', default=3, type=int,
help='minimum number of layer using deform conv')
parser.add_argument('--epochs', default=15, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--optimizer', default='SGD',
choices=['Adam', 'SGD'],
help='loss: ' +
' | '.join(['Adam', 'SGD']) +
' (default: Adam)')
parser.add_argument('--lr', '--learning-rate', default=0.0005, type=float,
metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.5, type=float,
help='momentum')
parser.add_argument('--weight-decay', default=1e-4, type=float,
help='weight decay')
parser.add_argument('--nesterov', default=False, type=str2bool,
help='nesterov')
parser.add_argument('--dcn', default=3, type=int,
help='number of layer using deform conv')
parser.add_argument('--cvn', default=3, type=int,
help='number of layer using conv')
args = parser.parse_args()
return args
def train(args, train_loader, model, criterion, optimizer, epoch, scheduler=None):
losses = AverageMeter()
scores = AverageMeter()
model.train()
for i, (input, target) in tqdm(enumerate(train_loader), total=len(train_loader)):
input = input.cuda()
target = target.cuda()
output = model(input)
loss = criterion(output, target.long())
acc = accuracy(output, target)[0]
losses.update(loss.item(), input.size(0))
scores.update(acc.item(), input.size(0))
# compute gradient and do optimizing step
optimizer.zero_grad()
loss.backward()
optimizer.step()
log = OrderedDict([
('loss', losses.avg),
('acc', scores.avg),
])
return log
def validate(args, val_loader, model, criterion):
losses = AverageMeter()
scores = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
for i, (input, target) in tqdm(enumerate(val_loader), total=len(val_loader)):
input = input.cuda()
target = target.cuda()
output = model(input)
loss = criterion(output, target.long())
acc = accuracy(output, target)[0]
losses.update(loss.item(), input.size(0))
scores.update(acc.item(), input.size(0))
log = OrderedDict([
('loss', losses.avg),
('acc', scores.avg),
])
return log
def main():
args = parse_args()
if args.name is None:
#args.name = '%s' %args.arch
args.name = 'ECG_Net'
if args.deform:
args.name += '_wDCN_1fc'
if args.modulation:
args.name += 'v2'
args.name += '_dcn-%d-' %args.dcn
args.name += 'cvn-%d' %args.cvn
if not os.path.exists('models/%s' %args.name):
os.makedirs('models/%s' %args.name)
print('Config -----')
for arg in vars(args):
print('%s: %s' %(arg, getattr(args, arg)))
print('------------')
with open('models/%s/args.txt' %args.name, 'w') as f:
for arg in vars(args):
print('%s: %s' %(arg, getattr(args, arg)), file=f)
joblib.dump(args, 'models/%s/args.pkl' %args.name)
criterion = nn.CrossEntropyLoss().cuda()
#cudnn.benchmark = True
# data loading code
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.repeat(1,1,1)),
transforms.Normalize((0.1307,), (0.3081,))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.repeat(1,1,1)),
transforms.Normalize((0.1307,), (0.3081,))
])
train_set = ECG(
train=True,
)
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=8,
#是否打乱训练数据True为是,False为否即不打乱
shuffle=False,
num_workers=8
)
test_set = ECG(
train=False,
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=8,
#是否打乱测试数据True为是,False为否即不打乱
shuffle=True,
num_workers=8)
num_classes = 9
# create model
model = archs.__dict__[args.arch](args, num_classes)
model = model.cuda()
print(model)
if args.optimizer == 'Adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr)
elif args.optimizer == 'SGD':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr,
momentum=args.momentum, weight_decay=args.weight_decay, nesterov=args.nesterov)
log = pd.DataFrame(index=[], columns=[
'epoch', 'lr', 'loss', 'acc', 'val_loss', 'val_acc'
])
best_acc = 0
for epoch in range(args.epochs):
print('Epoch [%d/%d]' %(epoch, args.epochs))
# train for one epoch
train_log = train(args, train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
val_log = validate(args, test_loader, model, criterion)
print('loss %.4f - acc %.4f - val_loss %.4f - val_acc %.4f'
%(train_log['loss'], train_log['acc'], val_log['loss'], val_log['acc']))
tmp = pd.Series([
epoch,
1e-1,
train_log['loss'],
train_log['acc'],
val_log['loss'],
val_log['acc'],
], index=['epoch', 'lr', 'loss', 'acc', 'val_loss', 'val_acc'])
log = log.append(tmp, ignore_index=True)
log.to_csv('models/%s/log.csv' %args.name, index=False)
if val_log['acc'] > best_acc:
torch.save(model.state_dict(), 'models/%s/model.pth' %args.name)
best_acc = val_log['acc']
print("=> saved best model")
if __name__ == '__main__':
main()