-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestbestmodel.py
More file actions
executable file
·349 lines (310 loc) · 14.7 KB
/
testbestmodel.py
File metadata and controls
executable file
·349 lines (310 loc) · 14.7 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
########################################################
## Nicolo Savioli, PhD stuendet King's Collage London ##
########################################################
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as mpatches
import os
import numpy as np
import torch
from torch import nn
import torch.nn.functional as f
from torch.autograd import Variable
from model import thicknessnet
from loder import loder
import datetime
import shutil
import progressbar as pb
import warnings
#from grad_cam import GradCam
from torchvision import models, transforms
warnings.filterwarnings("ignore")
class Test():
def __init__(self,pathRootModel,pathTestset,\
pathSave,typeModel,typeGRU,\
frameSecond):
self.pathRootModel = pathRootModel
self.pathTestset = os.path.join(pathTestset,"test.h5")
self.typeModel = typeModel
self.typeGRU = typeGRU
self.pathSave = pathSave
self.model,\
self.savedir,\
self.ModelTag = self.getModel()
self.testData = loder(self.pathTestset,\
frameSecond)
self.criterion = self.getCriterion ()
# Exsecution:
self.getTest()
def setProgressbar(self,description,iteration):
widgets = [description, pb.Percentage(), ' ',
pb.Bar(marker=pb.RotatingMarker()), ' ', pb.ETA()]
timer = pb.ProgressBar(widgets=widgets, maxval=iteration).start()
return timer
def makedir(self,directory):
if not os.path.exists(directory):
os.makedirs(directory)
def getCriterion(self):
loss = torch.nn.MSELoss(size_average=True)
print("\n ==> Convert loss to CUDA ...")
loss = loss.cuda()
return loss
def getModel(self):
nameModel = None
if self.typeGRU == "copyframe":
if self.typeModel == "alexnet":
nameModel = "AlexNet"
elif self.typeModel == "densenet":
nameModel = "Densenet121"
elif self.typeModel == "inception":
nameModel = "InceptionV4"
elif self.typeModel == "resnet":
nameModel = "ResNet18"
elif self.typeModel == "vgg":
nameModel = "Vgg-E"
elif self.typeGRU == "unidir":
if self.typeModel == "alexnet":
nameModel = "AlexNet+GRU"
elif self.typeModel == "densenet":
nameModel = "Densenet121+GRU"
elif self.typeModel == "inception":
nameModel = "InceptionV4+GRU"
elif self.typeModel == "resnet":
nameModel = "ResNet18+GRU"
elif self.typeModel == "vgg":
nameModel = "Vgg-E+GRU"
elif self.typeGRU == "bidir":
if self.typeModel == "alexnet":
nameModel = "AlexNet+BiGRU"
elif self.typeModel == "densenet":
nameModel = "Densenet121+BiGRU"
elif self.typeModel == "inception":
nameModel = "InceptionV4+BiGRU"
elif self.typeModel == "resnet":
nameModel = "ResNet18+BiGRU"
elif self.typeModel == "vgg":
nameModel = "Vgg-E+BiGRU"
model = self.openModel(os.path.join(self.pathRootModel,\
nameModel,\
"model.pt"))
modelDir = os.path.join(self.pathSave,nameModel)
self.makedir(modelDir)
print("\n ==> Save model in dir : " + str(modelDir))
return model,modelDir,nameModel
'''
def get_weights():
for module in self.model.named_modules():
if module[0] == 'ConvGRU.Conv_ct':
GRUfeatures = module[1].weight.data
print(GRUfeatures)
'''
def reshape(self,vect):
vect = np.reshape(vect,(vect.shape[0],\
vect.shape[1]*vect.shape[2]))
return vect
def saveTXT(self,lossList,lossSavePath):
with open(lossSavePath, "wb") as f:
for i in xrange(lossList.shape[0]):
f.write(str(lossList[i]) +"\n")
def cleanVect(self,data):
listPat = []
for p in xrange(data.shape[0]):
listSeq = []
for i in xrange(data.shape[1]):
if data[p][i] > 0.5:
listSeq.append(data[p][i])
listPat.append(np.asarray(listSeq))
return np.asarray(listPat)
# Plots:
def getPlotPlotIMTDiam (self,IMTlist,Diamlist,\
labelYname,savePathFig,numPat):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title (self.ModelTag+" of MSE "+"patient "+str(numPat))
ax.plot (IMTlist, '-', label="IMT" ,color='b')
ax.plot (Diamlist, '-', label="Diam",color='r')
minLen = min(len(IMTlist),len(Diamlist))
plt.xlim(0, minLen)
ax.set_xlabel('Cardiac time [s]')
ax.set_ylabel(labelYname)
ax.legend();
fig.savefig(savePathFig)
def getPlotPlotGTPred (self,Predlist,GTlist,\
labelYname,savePathFig,numPat):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title (self.ModelTag +" GT vs Prediction "+"patient " + str(numPat))
ax.plot (GTlist, '-', label="GT of " + labelYname ,color='r')
ax.plot (Predlist, '-', label="Pred of " + labelYname ,color='g')
minLen = min(len(GTlist),len(Predlist))
plt.xlim(0, minLen)
ax.set_xlabel('Cardiac time [s]')
ax.set_ylabel('Distance [mm]')
ax.legend();
fig.savefig(savePathFig)
# Regression plots:
def reshapeVectPredGT(self,Pred,GT):
minLen = min (len(Pred),len(GT))
PredList = []
GTlist = []
for i in xrange(int(minLen)):
PredList.append(Pred[i])
GTlist.append (GT [i])
return np.asarray(PredList),\
np.asarray(GTlist)
def getRegresionPlot(self,Predlist,GTlist,\
labelYname,savePathFig,numPat):
fig = plt.figure()
ax = fig.add_subplot(111)
Predlist,\
GTlist = self.reshapeVectPredGT(Predlist,GTlist)
###################################
fit = np.polyfit(Predlist,GTlist,1)
fit_fn = np.poly1d(fit)
###################################
ax.set_title ("Regression analysis of "+ self.ModelTag +" "+"patient " + str(numPat))
#ax.plot (GTlist, '-', label="GT of " + labelYname ,color='r')
#ax.plot (Predlist, '-', label="Pred of " + labelYname ,color='g')
ax.plot(Predlist,GTlist, 'ro', Predlist, fit_fn(Predlist), '-k')
ax.set_xlabel('Automatic [mm]')
ax.set_ylabel('Manual [mm]')
ax.legend();
fig.savefig(savePathFig)
def getTest(self):
test_loss = 0
contIter = 0
loadData,loadLabels = self.testData.getTest()
print("\n ==> Testing of ["+ str(loadData.data.size()[0]) +"] patients ..." )
numIter = loadData.data.size()[0]
TestProg = self.setProgressbar("Test ",numIter*loadData.data.size()[1])
# MSE error
PatientlossDiamVal = []
PatientlossIMTVal = []
# Prediction net
PatientDiamVal_pred = []
PatientIMTVal_pred = []
# gt
PatientDiamVal_gt = []
PatientIMTVal_gt = []
for IterPatient in xrange(numIter):
# MSE error
SeqlossDiamVal = []
SeqlossIMTVal = []
# Prediction net
SeqDiamVal_pred = []
SeqIMTVal_pred = []
# gt
SeqDiamVal_gt = []
SeqIMTVal_gt = []
for IterSeq in xrange(loadData.data.size()[1]):
# MSE error
lossDiamVal = []
lossIMTVal = []
# Prediction net
DiamVal_pred = []
IMTVal_pred = []
# gt
DiamVal_gt = []
IMTVal_gt = []
testLoadData = loadData [IterPatient][IterSeq]
testLoadLabel = loadLabels[IterPatient][IterSeq]
Pred = self.model(testLoadData)
for IterFrames in xrange(testLoadLabel.data.size()[0]):
testIMTVal = self.criterion(Pred[IterFrames][0],\
testLoadLabel[IterFrames][0]).data[0]
testDiamVal = self.criterion(Pred[IterFrames][1],\
testLoadLabel[IterFrames][1]).data[0]
# MSE error
lossIMTVal.append (testIMTVal)
lossDiamVal.append (testDiamVal)
# Prediction net
IMTVal_pred.append (float(Pred[IterFrames][0].data.cpu().numpy()))
DiamVal_pred.append (float(Pred[IterFrames][1].data.cpu().numpy()))
# gt
IMTVal_gt.append (float(testLoadLabel[IterFrames][0].data.cpu().numpy()))
DiamVal_gt.append (float(testLoadLabel[IterFrames][1].data.cpu().numpy()))
contIter += 1
TestProg.update(contIter)
# MSE error
SeqlossIMTVal.append (np.asarray(lossIMTVal))
SeqlossDiamVal.append (np.asarray(lossDiamVal))
# Prediction net
SeqIMTVal_pred.append (np.asarray(IMTVal_pred))
SeqDiamVal_pred.append (np.asarray(DiamVal_pred))
# gt
SeqIMTVal_gt.append (np.asarray(IMTVal_gt))
SeqDiamVal_gt.append (np.asarray(DiamVal_gt))
# MSE error
PatientlossIMTVal.append (np.asarray(SeqlossIMTVal))
PatientlossDiamVal.append (np.asarray(SeqlossDiamVal))
# Prediction net
PatientIMTVal_pred.append (np.asarray(SeqIMTVal_pred))
PatientDiamVal_pred.append (np.asarray(SeqDiamVal_pred))
# gt
PatientIMTVal_gt.append (np.asarray(SeqIMTVal_gt))
PatientDiamVal_gt.append (np.asarray(SeqDiamVal_gt))
TestProg.finish()
# MSE error
vectPatientlossDiamVal = self.cleanVect(self.reshape(np.asarray(PatientlossDiamVal )))
vectPatientlossIMTVal = self.cleanVect(self.reshape(np.asarray(PatientlossIMTVal )))
# Prediction net
vectPatientDiamVal_pred = self.cleanVect(self.reshape(np.asarray(PatientDiamVal_pred )))
vectPatientIMTVal_pred = self.cleanVect(self.reshape(np.asarray(PatientIMTVal_pred )))
# gt
vectPatientDiamVal_gt = self.cleanVect(self.reshape(np.asarray(PatientDiamVal_gt )))
vectPatientIMTVal_gt = self.cleanVect(self.reshape(np.asarray(PatientIMTVal_gt )))
##### Save MSE in section of 20 cardicac phase on txt ####
for npat in xrange(vectPatientlossDiamVal.shape[0]):
rootPath = os.path.join(self.savedir,"patient_"+str(npat))
self.makedir(rootPath)
########################################################################
pathPatientlossDiamVal = os.path.join(rootPath,"Frames-MSE-Diam.txt" )
pathPatientlossIMTVal = os.path.join(rootPath,"Frames-MSE-aimt.txt" )
pathPatientDiamValPred = os.path.join(rootPath,"Frames-Pred-Diam.txt")
pathPatientIMTValPred = os.path.join(rootPath,"Frames-Pred-aimt.txt")
pathPatientDiamValGT = os.path.join(rootPath,"Frames-gt-Diam.txt" )
pathPatientIMTValGT = os.path.join(rootPath,"Frames-gt-aimt.txt" )
#########################################################################
self.saveTXT(vectPatientlossDiamVal [npat], pathPatientlossDiamVal )
self.saveTXT(vectPatientlossIMTVal [npat], pathPatientlossIMTVal )
#########################################################################
self.saveTXT(vectPatientDiamVal_pred[npat], pathPatientDiamValPred )
self.saveTXT(vectPatientIMTVal_pred [npat], pathPatientIMTValPred )
#########################################################################
self.saveTXT(vectPatientDiamVal_gt [npat], pathPatientDiamValGT )
self.saveTXT(vectPatientIMTVal_gt [npat], pathPatientIMTValGT )
#########################################################################
pathPatientlossDiamAimtValPlot = os.path.join(rootPath,"Frames-MSE-plot.jpg")
pathPatientIMTValpredgtPlot = os.path.join(rootPath,"Frames-aIMT-plot.jpg")
pathPatientDiamValpredgtPlot = os.path.join(rootPath,"Frames-Diam-plot.jpg")
pathPatientIMTValRegressionPlot = os.path.join(rootPath,"Frames-Regression-aIMT-plot.jpg")
pathPatientDiamValRegressionPlot = os.path.join(rootPath,"Frames-Regression-Diam-plot.jpg")
# General plots:
self.getPlotPlotIMTDiam(vectPatientlossIMTVal[npat],vectPatientlossDiamVal[npat],\
"MSE",pathPatientlossDiamAimtValPlot,npat)
self.getPlotPlotGTPred(vectPatientIMTVal_pred[npat],vectPatientIMTVal_gt[npat],\
"aIMT",pathPatientIMTValpredgtPlot,npat)
self.getPlotPlotGTPred(vectPatientDiamVal_pred[npat],vectPatientDiamVal_gt[npat],\
"Diam",pathPatientDiamValpredgtPlot,npat)
# Rgression plots:
self.getRegresionPlot(vectPatientIMTVal_pred[npat],vectPatientIMTVal_gt[npat],\
"aIMT",pathPatientIMTValRegressionPlot,npat)
self.getRegresionPlot(vectPatientDiamVal_pred[npat],vectPatientDiamVal_gt[npat],\
"Diam",pathPatientDiamValRegressionPlot,npat)
def getGRUfeatures(self,x):
GRUfeatures = self.model.ConvGRU(self.model.model(x))
return GRUfeatures
def getGRUfeatures(self, x):
x = self.reizeFeatures(x)
h_next = None
list_status = []
for time in xrange(x.data.size()[0]):
h_next = self.model.ConvGRU(self.model.model(x[time], h_next))
list_status.append(h_next.size(0))
return list_status
def openModel(self, pathModel):
model = torch.load(pathModel)
model.eval()
return model