-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdd_Noise.py
162 lines (118 loc) · 4.98 KB
/
Add_Noise.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
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 loaddataset_AddNoise import ECG
import random
import os
import argparse
import csv
import glob
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='ScaledMNISTNet',
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')
parser.add_argument('--N', default='Null', type=str,
help='Add which Noise to the dataset')
args = parser.parse_args()
return args
# create model
def validate(args, val_loader, model, criterion):
# 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))
return output , scores.avg
def Add_Noise():
args = parse_args()
num_classes = 9
model = archs.__dict__[args.arch](args, num_classes)
model.load_state_dict(torch.load('model/ECG_Net_wDCNv2_dcn-'+str(args.dcn)+'-cvn-'+str(args.cvn)+'/model.pth'))
print(model)
model.cuda()
model.eval()
with open('./AddNOISE/DCN_'+str(args.dcn)+str(args.cvn)+'_result.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
# column name
writer.writerow(['Noise', 'Acc'])
Noise_type=["Null","GP","JX","JD"]
for i in Noise_type :
print(i)
Noise_type_name=i
test_set = ECG(
train=False,
Noise=i
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=1,
#是否打乱测试数据True为是,False为否即不打乱
shuffle=True,
num_workers=8)
scores = AverageMeter()
with torch.no_grad():
for i, (input, target) in tqdm(enumerate(test_loader), total=len(test_loader)):
input = input.cuda()
target = target.cuda()
predict= model(input)
acc = accuracy(predict, target)[0]
scores.update(acc.item(), input.size(0))
_, predicted= torch.max(predict, 1)
result=predicted[0]
record_name = target
answer = [Noise_type_name, scores.avg]
# write result
writer.writerow(answer)
print(scores.avg)
csvfile.close()
if __name__ == '__main__':
result = Add_Noise()