forked from ngessert/isic2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
741 lines (703 loc) · 43.4 KB
/
eval.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import torchvision
from torchvision import datasets, models as tv_models
from torch.utils.data import DataLoader
from torchsummary import summary
import numpy as np
import models
import threading
import pickle
from pathlib import Path
import math
import os
import sys
from glob import glob
import re
import gc
import importlib
import time
import csv
import sklearn.preprocessing
import utils
from sklearn.utils import class_weight
import imagesize
# add configuration file
# Dictionary for model configuration
mdlParams = {}
# Import machine config
pc_cfg = importlib.import_module('pc_cfgs.'+sys.argv[1])
mdlParams.update(pc_cfg.mdlParams)
# If there is another argument, its which checkpoint should be used
if len(sys.argv) > 6:
if 'last' in sys.argv[6]:
mdlParams['ckpt_name'] = 'checkpoint-'
else:
mdlParams['ckpt_name'] = 'checkpoint_best-'
if 'first' in sys.argv[6]:
mdlParams['use_first'] = True
else:
mdlParams['ckpt_name'] = 'checkpoint-'
# Set visible devices
mdlParams['numGPUs']= [[int(s) for s in re.findall(r'\d+',sys.argv[6])][-1]]
cuda_str = ""
for i in range(len(mdlParams['numGPUs'])):
cuda_str = cuda_str + str(mdlParams['numGPUs'][i])
if i is not len(mdlParams['numGPUs'])-1:
cuda_str = cuda_str + ","
print("Devices to use:",cuda_str)
os.environ["CUDA_VISIBLE_DEVICES"] = cuda_str
# If there is another argument, also use a meta learner
if len(sys.argv) > 7:
if 'HAMONLY' in sys.argv[7]:
mdlParams['eval_on_ham_only'] = True
# Import model config
model_cfg = importlib.import_module('cfgs.'+sys.argv[2])
mdlParams_model = model_cfg.init(mdlParams)
mdlParams.update(mdlParams_model)
# Path name where model is saved is the fourth argument
if 'NONE' in sys.argv[5]:
mdlParams['saveDirBase'] = mdlParams['saveDir'] + sys.argv[2]
else:
mdlParams['saveDirBase'] = sys.argv[5]
# Third is multi crop yes no
if 'multi' in sys.argv[3]:
if 'rand' in sys.argv[3]:
mdlParams['numRandValSeq'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][0]
print("Random sequence number",mdlParams['numRandValSeq'])
else:
mdlParams['numRandValSeq'] = 0
mdlParams['multiCropEval'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-1]
mdlParams['voting_scheme'] = sys.argv[4]
if 'scale' in sys.argv[3]:
print("Multi Crop and Scale Eval with crop number:",mdlParams['multiCropEval']," Voting scheme: ",mdlParams['voting_scheme'])
mdlParams['orderedCrop'] = False
mdlParams['scale_min'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-2]/100.0
elif 'determ' in sys.argv[3]:
# Example application: multideterm5sc3f2
mdlParams['deterministic_eval'] = True
mdlParams['numCropPositions'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-3]
num_scales = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-2]
all_scales = [1.0,0.5,0.75,0.25,0.9,0.6,0.4]
mdlParams['cropScales'] = all_scales[:num_scales]
mdlParams['cropFlipping'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-1]
print("deterministic eval with crops number",mdlParams['numCropPositions'],"scales",mdlParams['cropScales'],"flipping",mdlParams['cropFlipping'])
mdlParams['multiCropEval'] = mdlParams['numCropPositions']*len(mdlParams['cropScales'])*mdlParams['cropFlipping']
mdlParams['offset_crop'] = 0.2
elif 'order' in sys.argv[3]:
mdlParams['orderedCrop'] = True
if mdlParams.get('var_im_size',False):
# Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
mdlParams['cropPositions'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
#mdlParams['imSizes'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
for u in range(len(mdlParams['im_paths'])):
height, width = imagesize.get(mdlParams['im_paths'][u])
if width < mdlParams['input_size'][0]:
height = int(mdlParams['input_size'][0]/float(width))*height
width = mdlParams['input_size'][0]
if height < mdlParams['input_size'][0]:
width = int(mdlParams['input_size'][0]/float(height))*width
height = mdlParams['input_size'][0]
if mdlParams.get('resize_large_ones') is not None:
if width == mdlParams['large_size'] and height == mdlParams['large_size']:
width, height = (mdlParams['resize_large_ones'],mdlParams['resize_large_ones'])
ind = 0
for i in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
for j in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
mdlParams['cropPositions'][u,ind,0] = mdlParams['input_size'][0]/2+i*((width-mdlParams['input_size'][1])/(np.sqrt(mdlParams['multiCropEval'])-1))
mdlParams['cropPositions'][u,ind,1] = mdlParams['input_size'][1]/2+j*((height-mdlParams['input_size'][0])/(np.sqrt(mdlParams['multiCropEval'])-1))
#mdlParams['imSizes'][u,ind,0] = curr_im_size[0]
ind += 1
# Sanity checks
#print("Positions",mdlParams['cropPositions'])
# Test image sizes
height = mdlParams['input_size'][0]
width = mdlParams['input_size'][1]
for u in range(len(mdlParams['im_paths'])):
height_test, width_test = imagesize.get(mdlParams['im_paths'][u])
if width_test < mdlParams['input_size'][0]:
height_test = int(mdlParams['input_size'][0]/float(width_test))*height_test
width_test = mdlParams['input_size'][0]
if height_test < mdlParams['input_size'][0]:
width_test = int(mdlParams['input_size'][0]/float(height_test))*width_test
height_test = mdlParams['input_size'][0]
if mdlParams.get('resize_large_ones') is not None:
if width_test == mdlParams['large_size'] and height_test == mdlParams['large_size']:
width_test, height_test = (mdlParams['resize_large_ones'],mdlParams['resize_large_ones'])
test_im = np.zeros([width_test,height_test])
for i in range(mdlParams['multiCropEval']):
im_crop = test_im[np.int32(mdlParams['cropPositions'][u,i,0]-height/2):np.int32(mdlParams['cropPositions'][u,i,0]-height/2)+height,np.int32(mdlParams['cropPositions'][u,i,1]-width/2):np.int32(mdlParams['cropPositions'][u,i,1]-width/2)+width]
if im_crop.shape[0] != mdlParams['input_size'][0]:
print("Wrong shape",im_crop.shape[0],mdlParams['im_paths'][u])
if im_crop.shape[1] != mdlParams['input_size'][1]:
print("Wrong shape",im_crop.shape[1],mdlParams['im_paths'][u])
else:
# Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
mdlParams['cropPositions'] = np.zeros([mdlParams['multiCropEval'],2],dtype=np.int64)
if mdlParams['multiCropEval'] == 5:
numCrops = 4
elif mdlParams['multiCropEval'] == 7:
numCrops = 9
mdlParams['cropPositions'] = np.zeros([9,2],dtype=np.int64)
else:
numCrops = mdlParams['multiCropEval']
ind = 0
for i in range(np.int32(np.sqrt(numCrops))):
for j in range(np.int32(np.sqrt(numCrops))):
mdlParams['cropPositions'][ind,0] = mdlParams['input_size'][0]/2+i*((mdlParams['input_size_load'][0]-mdlParams['input_size'][0])/(np.sqrt(numCrops)-1))
mdlParams['cropPositions'][ind,1] = mdlParams['input_size'][1]/2+j*((mdlParams['input_size_load'][1]-mdlParams['input_size'][1])/(np.sqrt(numCrops)-1))
ind += 1
# Add center crop
if mdlParams['multiCropEval'] == 5:
mdlParams['cropPositions'][4,0] = mdlParams['input_size_load'][0]/2
mdlParams['cropPositions'][4,1] = mdlParams['input_size_load'][1]/2
if mdlParams['multiCropEval'] == 7:
mdlParams['cropPositions'] = np.delete(mdlParams['cropPositions'],[3,7],0)
# Sanity checks
print("Positions val",mdlParams['cropPositions'])
# Test image sizes
test_im = np.zeros(mdlParams['input_size_load'])
height = mdlParams['input_size'][0]
width = mdlParams['input_size'][1]
for i in range(mdlParams['multiCropEval']):
im_crop = test_im[np.int32(mdlParams['cropPositions'][i,0]-height/2):np.int32(mdlParams['cropPositions'][i,0]-height/2)+height,np.int32(mdlParams['cropPositions'][i,1]-width/2):np.int32(mdlParams['cropPositions'][i,1]-width/2)+width,:]
print("Shape",i+1,im_crop.shape)
print("Multi Crop with order with crop number:",mdlParams['multiCropEval']," Voting scheme: ",mdlParams['voting_scheme'])
if 'flip' in sys.argv[3]:
# additional flipping, example: flip2multiorder16
mdlParams['eval_flipping'] = [int(s) for s in re.findall(r'\d+',sys.argv[3])][-2]
print("Additional flipping",mdlParams['eval_flipping'])
else:
print("Multi Crop Eval with crop number:",mdlParams['multiCropEval']," Voting scheme: ",mdlParams['voting_scheme'])
mdlParams['orderedCrop'] = False
else:
mdlParams['multiCropEval'] = 0
mdlParams['orderedCrop'] = False
# Set training set to eval mode
mdlParams['trainSetState'] = 'eval'
if mdlParams['numClasses'] == 9 and mdlParams.get('no_c9_eval',False):
num_classes = mdlParams['numClasses']-1
else:
num_classes = mdlParams['numClasses']
# Save results in here
allData = {}
allData['f1Best'] = np.zeros([mdlParams['numCV']])
allData['sensBest'] = np.zeros([mdlParams['numCV'],num_classes])
allData['specBest'] = np.zeros([mdlParams['numCV'],num_classes])
allData['accBest'] = np.zeros([mdlParams['numCV']])
allData['waccBest'] = np.zeros([mdlParams['numCV'],num_classes])
allData['aucBest'] = np.zeros([mdlParams['numCV'],num_classes])
allData['convergeTime'] = {}
allData['bestPred'] = {}
allData['bestPredMC'] = {}
allData['targets'] = {}
allData['extPred'] = {}
allData['f1Best_meta'] = np.zeros([mdlParams['numCV']])
allData['sensBest_meta'] = np.zeros([mdlParams['numCV'],num_classes])
allData['specBest_meta'] = np.zeros([mdlParams['numCV'],num_classes])
allData['accBest_meta'] = np.zeros([mdlParams['numCV']])
allData['waccBest_meta'] = np.zeros([mdlParams['numCV'],num_classes])
allData['aucBest_meta'] = np.zeros([mdlParams['numCV'],num_classes])
#allData['convergeTime'] = {}
allData['bestPred_meta'] = {}
allData['targets_meta'] = {}
if not (len(sys.argv) > 8):
for cv in range(mdlParams['numCV']):
# Reset model graph
importlib.reload(models)
#importlib.reload(torchvision)
# Collect model variables
modelVars = {}
modelVars['device'] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(modelVars['device'])
# Def current CV set
mdlParams['trainInd'] = mdlParams['trainIndCV'][cv]
if 'valIndCV' in mdlParams:
mdlParams['valInd'] = mdlParams['valIndCV'][cv]
# Def current path for saving stuff
if 'valIndCV' in mdlParams:
mdlParams['saveDir'] = mdlParams['saveDirBase'] + '/CVSet' + str(cv)
else:
mdlParams['saveDir'] = mdlParams['saveDirBase']
# Potentially calculate setMean to subtract
if mdlParams['subtract_set_mean'] == 1:
mdlParams['setMean'] = np.mean(mdlParams['images_means'][mdlParams['trainInd'],:],(0))
print("Set Mean",mdlParams['setMean'])
# Potentially only HAM eval
if mdlParams.get('eval_on_ham_only',False):
print("Old val inds",len(mdlParams['valInd']))
mdlParams['valInd'] = np.intersect1d(mdlParams['valInd'],mdlParams['HAM10000_inds'])
print("New val inds, HAM only",len(mdlParams['valInd']))
# balance classes
if mdlParams['balance_classes'] < 3 or mdlParams['balance_classes'] == 7 or mdlParams['balance_classes'] == 11:
class_weights = class_weight.compute_class_weight('balanced',np.unique(np.argmax(mdlParams['labels_array'][mdlParams['trainInd'],:],1)),np.argmax(mdlParams['labels_array'][mdlParams['trainInd'],:],1))
print("Current class weights",class_weights)
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
elif mdlParams['balance_classes'] == 3 or mdlParams['balance_classes'] == 4:
# Split training set by classes
not_one_hot = np.argmax(mdlParams['labels_array'],1)
mdlParams['class_indices'] = []
for i in range(mdlParams['numClasses']):
mdlParams['class_indices'].append(np.where(not_one_hot==i)[0])
# Kick out non-trainind indices
mdlParams['class_indices'][i] = np.setdiff1d(mdlParams['class_indices'][i],mdlParams['valInd'])
#print("Class",i,mdlParams['class_indices'][i].shape,np.min(mdlParams['class_indices'][i]),np.max(mdlParams['class_indices'][i]),np.sum(mdlParams['labels_array'][np.int64(mdlParams['class_indices'][i]),:],0))
elif mdlParams['balance_classes'] == 5 or mdlParams['balance_classes'] == 6 or mdlParams['balance_classes'] == 13:
# Other class balancing loss
class_weights = 1.0/np.mean(mdlParams['labels_array'][mdlParams['trainInd'],:],axis=0)
print("Current class weights",class_weights)
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
elif mdlParams['balance_classes'] == 9:
# Only use HAM indicies for calculation
print("Balance 9")
indices_ham = mdlParams['trainInd'][mdlParams['trainInd'] < 25331]
if mdlParams['numClasses'] == 9:
class_weights_ = 1.0/np.mean(mdlParams['labels_array'][indices_ham,:8],axis=0)
#print("class before",class_weights_)
class_weights = np.zeros([mdlParams['numClasses']])
class_weights[:8] = class_weights_
class_weights[-1] = np.max(class_weights_)
else:
class_weights = 1.0/np.mean(mdlParams['labels_array'][indices_ham,:],axis=0)
print("Current class weights",class_weights)
if isinstance(mdlParams['extra_fac'], float):
class_weights = np.power(class_weights,mdlParams['extra_fac'])
else:
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
# Set up dataloaders
# Meta scaler
if mdlParams.get('meta_features',None) is not None and mdlParams['scale_features']:
mdlParams['feature_scaler_meta'] = sklearn.preprocessing.StandardScaler().fit(mdlParams['meta_array'][mdlParams['trainInd'],:])
#print("scaler mean",mdlParams['feature_scaler_meta'].mean_,"var",mdlParams['feature_scaler_meta'].var_)
# For train
dataset_train = utils.ISICDataset(mdlParams, 'trainInd')
# For val
dataset_val = utils.ISICDataset(mdlParams, 'valInd')
if mdlParams['multiCropEval'] > 0:
modelVars['dataloader_valInd'] = DataLoader(dataset_val, batch_size=mdlParams['multiCropEval'], shuffle=False, num_workers=8, pin_memory=True)
else:
modelVars['dataloader_valInd'] = DataLoader(dataset_val, batch_size=mdlParams['batchSize'], shuffle=False, num_workers=8, pin_memory=True)
modelVars['dataloader_trainInd'] = DataLoader(dataset_train, batch_size=mdlParams['batchSize'], shuffle=True, num_workers=8, pin_memory=True)
# For test
if 'testInd' in mdlParams:
dataset_test = utils.ISICDataset(mdlParams, 'testInd')
if mdlParams['multiCropEval'] > 0:
modelVars['dataloader_testInd'] = DataLoader(dataset_test, batch_size=mdlParams['multiCropEval'], shuffle=False, num_workers=8, pin_memory=True)
else:
modelVars['dataloader_testInd'] = DataLoader(dataset_test, batch_size=mdlParams['batchSize'], shuffle=False, num_workers=8, pin_memory=True)
modelVars['model'] = models.getModel(mdlParams)()
# Original input size
#if 'Dense' not in mdlParams['model_type']:
# print("Original input size",modelVars['model'].input_size)
#print(modelVars['model'])
if 'Dense' in mdlParams['model_type']:
if mdlParams['input_size'][0] != 224:
modelVars['model'] = utils.modify_densenet_avg_pool(modelVars['model'])
#print(modelVars['model'])
num_ftrs = modelVars['model'].classifier.in_features
modelVars['model'].classifier = nn.Linear(num_ftrs, mdlParams['numClasses'])
#print(modelVars['model'])
elif 'dpn' in mdlParams['model_type']:
num_ftrs = modelVars['model'].classifier.in_channels
modelVars['model'].classifier = nn.Conv2d(num_ftrs,mdlParams['numClasses'],[1,1])
#modelVars['model'].add_module('real_classifier',nn.Linear(num_ftrs, mdlParams['numClasses']))
#print(modelVars['model'])
elif 'efficient' in mdlParams['model_type']:
# Do nothing, output is prepared
num_ftrs = modelVars['model']._fc.in_features
modelVars['model']._fc = nn.Linear(num_ftrs, mdlParams['numClasses'])
elif 'wsl' in mdlParams['model_type']:
num_ftrs = modelVars['model'].fc.in_features
modelVars['model'].fc = nn.Linear(num_ftrs, mdlParams['numClasses'])
else:
num_ftrs = modelVars['model'].last_linear.in_features
modelVars['model'].last_linear = nn.Linear(num_ftrs, mdlParams['numClasses'])
# modify model
if mdlParams.get('meta_features',None) is not None:
modelVars['model'] = models.modify_meta(mdlParams,modelVars['model'])
modelVars['model'] = modelVars['model'].to(modelVars['device'])
#summary(modelVars['model'], (mdlParams['input_size'][2], mdlParams['input_size'][0], mdlParams['input_size'][1]))
# Loss, with class weighting
# Loss, with class weighting
if mdlParams['balance_classes'] == 3 or mdlParams['balance_classes'] == 0 or mdlParams['balance_classes'] == 12:
modelVars['criterion'] = nn.CrossEntropyLoss()
elif mdlParams['balance_classes'] == 8:
modelVars['criterion'] = nn.CrossEntropyLoss(reduce=False)
elif mdlParams['balance_classes'] == 6 or mdlParams['balance_classes'] == 7:
modelVars['criterion'] = nn.CrossEntropyLoss(weight=torch.cuda.FloatTensor(class_weights.astype(np.float32)),reduce=False)
elif mdlParams['balance_classes'] == 10:
modelVars['criterion'] = utils.FocalLoss(mdlParams['numClasses'])
elif mdlParams['balance_classes'] == 11:
modelVars['criterion'] = utils.FocalLoss(mdlParams['numClasses'],alpha=torch.cuda.FloatTensor(class_weights.astype(np.float32)))
else:
modelVars['criterion'] = nn.CrossEntropyLoss(weight=torch.cuda.FloatTensor(class_weights.astype(np.float32)))
# Observe that all parameters are being optimized
modelVars['optimizer'] = optim.Adam(modelVars['model'].parameters(), lr=mdlParams['learning_rate'])
# Decay LR by a factor of 0.1 every 7 epochs
modelVars['scheduler'] = lr_scheduler.StepLR(modelVars['optimizer'], step_size=mdlParams['lowerLRAfter'], gamma=1/np.float32(mdlParams['LRstep']))
# Define softmax
modelVars['softmax'] = nn.Softmax(dim=1)
# Manually find latest chekcpoint, tf.train.latest_checkpoint is doing weird shit
files = glob(mdlParams['saveDir']+'/*')
#print(mdlParams['saveDir'])
#print("Files",files)
global_steps = np.zeros([len(files)])
for i in range(len(files)):
# Use meta files to find the highest index
if 'checkpoint' not in files[i]:
continue
if mdlParams['ckpt_name'] not in files[i]:
continue
# Extract global step
nums = [int(s) for s in re.findall(r'\d+',files[i])]
global_steps[i] = nums[-1]
# Create path with maximum global step found, if first is not wanted
global_steps = np.sort(global_steps)
if mdlParams.get('use_first') is not None:
chkPath = mdlParams['saveDir'] + '/' + mdlParams['ckpt_name'] + str(int(global_steps[-2])) + '.pt'
else:
chkPath = mdlParams['saveDir'] + '/' + mdlParams['ckpt_name'] + str(int(np.max(global_steps))) + '.pt'
print("Restoring: ",chkPath)
# Load
state = torch.load(chkPath)
# Initialize model and optimizer
modelVars['model'].load_state_dict(state['state_dict'])
#modelVars['optimizer'].load_state_dict(state['optimizer'])
# Construct pkl filename: config name, last/best, saved epoch number
pklFileName = sys.argv[2] + "_" + sys.argv[6] + "_" + str(int(np.max(global_steps))) + ".pkl"
modelVars['model'].eval()
if mdlParams['classification']:
print("CV Set ",cv+1)
print("------------------------------------")
# Training err first, deactivated
if 'trainInd' in mdlParams and False:
loss, accuracy, sensitivity, specificity, conf_matrix, f1, auc, waccuracy, predictions, targets, _ = utils.getErrClassification_mgpu(mdlParams, 'trainInd', modelVars)
print("Training Results:")
print("----------------------------------")
print("Loss",np.mean(loss))
print("F1 Score",f1)
print("Sensitivity",sensitivity)
print("Specificity",specificity)
print("Accuracy",accuracy)
print("Per Class Accuracy",waccuracy)
print("Weighted Accuracy",waccuracy)
print("AUC",auc)
print("Mean AUC", np.mean(auc))
if 'valInd' in mdlParams and (len(sys.argv) <= 8):
loss, accuracy, sensitivity, specificity, conf_matrix, f1, auc, waccuracy, predictions, targets, predictions_mc = utils.getErrClassification_mgpu(mdlParams, 'valInd', modelVars)
print("Validation Results:")
print("----------------------------------")
print("Loss",np.mean(loss))
print("F1 Score",f1)
print("Sensitivity",sensitivity)
print("Specificity",specificity)
print("Accuracy",accuracy)
print("Per Class Accuracy",waccuracy)
print("Weighted Accuracy",np.mean(waccuracy))
print("AUC",auc)
print("Mean AUC", np.mean(auc))
# Save results in dict
if 'testInd' not in mdlParams:
allData['f1Best'][cv] = f1
allData['sensBest'][cv,:] = sensitivity
allData['specBest'][cv,:] = specificity
allData['accBest'][cv] = accuracy
allData['waccBest'][cv,:] = waccuracy
allData['aucBest'][cv,:] = auc
allData['bestPred'][cv] = predictions
allData['bestPredMC'][cv] = predictions_mc
allData['targets'][cv] = targets
print("Pred shape",predictions.shape,"Tar shape",targets.shape)
if 'testInd' in mdlParams:
loss, accuracy, sensitivity, specificity, conf_matrix, f1, auc, waccuracy, predictions, targets, predictions_mc = utils.getErrClassification_mgpu(mdlParams, 'testInd', modelVars)
print("Test Results Normal:")
print("----------------------------------")
print("Loss",np.mean(loss))
print("F1 Score",f1)
print("Sensitivity",sensitivity)
print("Specificity",specificity)
print("Accuracy",accuracy)
print("Per Class Accuracy",waccuracy)
print("Weighted Accuracy",np.mean(waccuracy))
print("AUC",auc)
print("Mean AUC", np.mean(auc))
# Save results in dict
allData['f1Best'][cv] = f1
allData['sensBest'][cv,:] = sensitivity
allData['specBest'][cv,:] = specificity
allData['accBest'][cv] = accuracy
allData['waccBest'][cv,:] = waccuracy
allData['aucBest'][cv,:] = auc
else:
# TODO: Regression
print("Not Implemented")
# If there is an 8th argument, make extra evaluation for external set
if len(sys.argv) > 8:
for cv in range(mdlParams['numCV']):
# Reset model graph
importlib.reload(models)
#importlib.reload(torchvision)
# Collect model variables
modelVars = {}
modelVars['device'] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# define new folder, take care that there might be no labels
print("Creating predictions for path ",sys.argv[8])
# Add meta data
if mdlParams.get('meta_features',None) is not None:
mdlParams['meta_dict'] = {}
path1 = mdlParams['dataDir'] + '/meta_data/test_rez3_ll/meta_data_test.pkl'
# Open and load
with open(path1,'rb') as f:
meta_data = pickle.load(f)
# Write into dict
for k in range(len(meta_data['im_name'])):
feature_vector = []
if 'age_oh' in mdlParams['meta_features']:
if mdlParams['encode_nan']:
feature_vector.append(meta_data['age_oh'][k,:])
else:
feature_vector.append(meta_data['age_oh'][k,1:])
if 'age_num' in mdlParams['meta_features']:
feature_vector.append(np.array([meta_data['age_num'][k]]))
if 'loc_oh' in mdlParams['meta_features']:
if mdlParams['encode_nan']:
feature_vector.append(meta_data['loc_oh'][k,:])
else:
feature_vector.append(meta_data['loc_oh'][k,1:])
if 'sex_oh' in mdlParams['meta_features']:
if mdlParams['encode_nan']:
feature_vector.append(meta_data['sex_oh'][k,:])
else:
feature_vector.append(meta_data['sex_oh'][k,1:])
#print(feature_vector)
feature_vector = np.concatenate(feature_vector,axis=0)
#print("feature vector shape",feature_vector.shape)
mdlParams['meta_dict'][meta_data['im_name'][k]] = feature_vector
# Define the path
path1 = sys.argv[8]
# All files in that set
files = sorted(glob(path1+'/*'))
# Define new paths
mdlParams['im_paths'] = []
mdlParams['meta_list'] = []
for j in range(len(files)):
inds = [int(s) for s in re.findall(r'\d+',files[j])]
if 'ISIC_' in files[j]:
mdlParams['im_paths'].append(files[j])
if mdlParams.get('meta_features',None) is not None:
for key in mdlParams['meta_dict']:
if key in files[j]:
mdlParams['meta_list'].append(mdlParams['meta_dict'][key])
if mdlParams.get('meta_features',None) is not None:
# Meta data
mdlParams['meta_array'] = np.array(mdlParams['meta_list'])
# Add empty labels
mdlParams['labels_array'] = np.zeros([len(mdlParams['im_paths']),mdlParams['numClasses']],dtype=np.float32)
# Define everything as a valind set
mdlParams['valInd'] = np.array(np.arange(len(mdlParams['im_paths'])))
mdlParams['trainInd'] = mdlParams['valInd']
if mdlParams.get('var_im_size',False):
# Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
mdlParams['cropPositions'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
#mdlParams['imSizes'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
for u in range(len(mdlParams['im_paths'])):
height, width = imagesize.get(mdlParams['im_paths'][u])
if width < mdlParams['input_size'][0]:
height = int(mdlParams['input_size'][0]/float(width))*height
width = mdlParams['input_size'][0]
if height < mdlParams['input_size'][0]:
width = int(mdlParams['input_size'][0]/float(height))*width
height = mdlParams['input_size'][0]
if mdlParams.get('resize_large_ones') is not None:
if width == mdlParams['large_size'] and height == mdlParams['large_size']:
width, height = (mdlParams['resize_large_ones'],mdlParams['resize_large_ones'])
ind = 0
for i in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
for j in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
mdlParams['cropPositions'][u,ind,0] = mdlParams['input_size'][0]/2+i*((width-mdlParams['input_size'][1])/(np.sqrt(mdlParams['multiCropEval'])-1))
mdlParams['cropPositions'][u,ind,1] = mdlParams['input_size'][1]/2+j*((height-mdlParams['input_size'][0])/(np.sqrt(mdlParams['multiCropEval'])-1))
#mdlParams['imSizes'][u,ind,0] = curr_im_size[0]
ind += 1
# Sanity checks
#print("Positions",mdlParams['cropPositions'])
# Test image sizes
test_im = np.zeros(mdlParams['input_size_load'])
height = mdlParams['input_size'][0]
width = mdlParams['input_size'][1]
for u in range(len(mdlParams['im_paths'])):
height_test, width_test = imagesize.get(mdlParams['im_paths'][u])
if width_test < mdlParams['input_size'][0]:
height_test = int(mdlParams['input_size'][0]/float(width_test))*height_test
width_test = mdlParams['input_size'][0]
if height_test < mdlParams['input_size'][0]:
width_test = int(mdlParams['input_size'][0]/float(height_test))*width_test
height_test = mdlParams['input_size'][0]
if mdlParams.get('resize_large_ones') is not None:
if width_test == mdlParams['large_size'] and height_test == mdlParams['large_size']:
width_test, height_test = (mdlParams['resize_large_ones'],mdlParams['resize_large_ones'])
test_im = np.zeros([width_test,height_test])
for i in range(mdlParams['multiCropEval']):
im_crop = test_im[np.int32(mdlParams['cropPositions'][u,i,0]-height/2):np.int32(mdlParams['cropPositions'][u,i,0]-height/2)+height,np.int32(mdlParams['cropPositions'][u,i,1]-width/2):np.int32(mdlParams['cropPositions'][u,i,1]-width/2)+width]
if im_crop.shape[0] != mdlParams['input_size'][0]:
print("Wrong shape",im_crop.shape[0],mdlParams['im_paths'][u])
if im_crop.shape[1] != mdlParams['input_size'][1]:
print("Wrong shape",im_crop.shape[1],mdlParams['im_paths'][u])
mdlParams['saveDir'] = mdlParams['saveDirBase'] + '/CVSet' + str(cv)
# balance classes
if mdlParams['balance_classes'] < 3 or mdlParams['balance_classes'] == 7 or mdlParams['balance_classes'] == 11:
class_weights = class_weight.compute_class_weight('balanced',np.unique(np.argmax(mdlParams['labels_array'][mdlParams['trainInd'],:],1)),np.argmax(mdlParams['labels_array'][mdlParams['trainInd'],:],1))
print("Current class weights",class_weights)
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
elif mdlParams['balance_classes'] == 3 or mdlParams['balance_classes'] == 4:
# Split training set by classes
not_one_hot = np.argmax(mdlParams['labels_array'],1)
mdlParams['class_indices'] = []
for i in range(mdlParams['numClasses']):
mdlParams['class_indices'].append(np.where(not_one_hot==i)[0])
# Kick out non-trainind indices
mdlParams['class_indices'][i] = np.setdiff1d(mdlParams['class_indices'][i],mdlParams['valInd'])
#print("Class",i,mdlParams['class_indices'][i].shape,np.min(mdlParams['class_indices'][i]),np.max(mdlParams['class_indices'][i]),np.sum(mdlParams['labels_array'][np.int64(mdlParams['class_indices'][i]),:],0))
elif mdlParams['balance_classes'] == 5 or mdlParams['balance_classes'] == 6 or mdlParams['balance_classes'] == 13:
# Other class balancing loss
class_weights = 1.0/np.mean(mdlParams['labels_array'][mdlParams['trainInd'],:],axis=0)
print("Current class weights",class_weights)
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
elif mdlParams['balance_classes'] == 9:
# Only use official indicies for calculation
print("Balance 9")
indices_ham = mdlParams['trainInd'][mdlParams['trainInd'] < 25331]
if mdlParams['numClasses'] == 9:
class_weights_ = 1.0/np.mean(mdlParams['labels_array'][indices_ham,:8],axis=0)
#print("class before",class_weights_)
class_weights = np.zeros([mdlParams['numClasses']])
class_weights[:8] = class_weights_
class_weights[-1] = np.max(class_weights_)
else:
class_weights = 1.0/np.mean(mdlParams['labels_array'][indices_ham,:],axis=0)
print("Current class weights",class_weights)
if isinstance(mdlParams['extra_fac'], float):
class_weights = np.power(class_weights,mdlParams['extra_fac'])
else:
class_weights = class_weights*mdlParams['extra_fac']
print("Current class weights with extra",class_weights)
# Set up dataloaders
# Meta scaler
if mdlParams.get('meta_features',None) is not None and mdlParams['scale_features']:
mdlParams['feature_scaler_meta'] = sklearn.preprocessing.StandardScaler().fit(mdlParams['meta_array'][mdlParams['trainInd'],:])
#print("scaler mean",mdlParams['feature_scaler_meta'].mean_,"var",mdlParams['feature_scaler_meta'].var_)
# For train
dataset_train = utils.ISICDataset(mdlParams, 'trainInd')
# For val
dataset_val = utils.ISICDataset(mdlParams, 'valInd')
if mdlParams['multiCropEval'] > 0:
modelVars['dataloader_valInd'] = DataLoader(dataset_val, batch_size=mdlParams['multiCropEval'], shuffle=False, num_workers=8, pin_memory=True)
else:
modelVars['dataloader_valInd'] = DataLoader(dataset_val, batch_size=mdlParams['batchSize'], shuffle=False, num_workers=8, pin_memory=True)
modelVars['dataloader_trainInd'] = DataLoader(dataset_train, batch_size=mdlParams['batchSize'], shuffle=True, num_workers=8, pin_memory=True)
# Define model
modelVars['model'] = models.getModel(mdlParams)()
if 'Dense' in mdlParams['model_type']:
if mdlParams['input_size'][0] != 224:
modelVars['model'] = utils.modify_densenet_avg_pool(modelVars['model'])
#print(modelVars['model'])
num_ftrs = modelVars['model'].classifier.in_features
modelVars['model'].classifier = nn.Linear(num_ftrs, mdlParams['numClasses'])
#print(modelVars['model'])
elif 'dpn' in mdlParams['model_type']:
num_ftrs = modelVars['model'].classifier.in_channels
modelVars['model'].classifier = nn.Conv2d(num_ftrs,mdlParams['numClasses'],[1,1])
#modelVars['model'].add_module('real_classifier',nn.Linear(num_ftrs, mdlParams['numClasses']))
#print(modelVars['model'])
elif 'efficient' in mdlParams['model_type']:
# Do nothing, output is prepared
num_ftrs = modelVars['model']._fc.in_features
modelVars['model']._fc = nn.Linear(num_ftrs, mdlParams['numClasses'])
elif 'wsl' in mdlParams['model_type']:
num_ftrs = modelVars['model'].fc.in_features
modelVars['model'].fc = nn.Linear(num_ftrs, mdlParams['numClasses'])
else:
num_ftrs = modelVars['model'].last_linear.in_features
modelVars['model'].last_linear = nn.Linear(num_ftrs, mdlParams['numClasses'])
# modify model
if mdlParams.get('meta_features',None) is not None:
modelVars['model'] = models.modify_meta(mdlParams,modelVars['model'])
modelVars['model'] = modelVars['model'].to(modelVars['device'])
#summary(modelVars['model'], (mdlParams['input_size'][2], mdlParams['input_size'][0], mdlParams['input_size'][1]))
# Loss, with class weighting
# Loss, with class weighting
if mdlParams['balance_classes'] == 3 or mdlParams['balance_classes'] == 0 or mdlParams['balance_classes'] == 12:
modelVars['criterion'] = nn.CrossEntropyLoss()
elif mdlParams['balance_classes'] == 8:
modelVars['criterion'] = nn.CrossEntropyLoss(reduce=False)
elif mdlParams['balance_classes'] == 6 or mdlParams['balance_classes'] == 7:
modelVars['criterion'] = nn.CrossEntropyLoss(weight=torch.cuda.FloatTensor(class_weights.astype(np.float32)),reduce=False)
elif mdlParams['balance_classes'] == 10:
modelVars['criterion'] = utils.FocalLoss(mdlParams['numClasses'])
elif mdlParams['balance_classes'] == 11:
modelVars['criterion'] = utils.FocalLoss(mdlParams['numClasses'],alpha=torch.cuda.FloatTensor(class_weights.astype(np.float32)))
else:
modelVars['criterion'] = nn.CrossEntropyLoss(weight=torch.cuda.FloatTensor(class_weights.astype(np.float32)))
# Observe that all parameters are being optimized
modelVars['optimizer'] = optim.Adam(modelVars['model'].parameters(), lr=mdlParams['learning_rate'])
# Decay LR by a factor of 0.1 every 7 epochs
modelVars['scheduler'] = lr_scheduler.StepLR(modelVars['optimizer'], step_size=mdlParams['lowerLRAfter'], gamma=1/np.float32(mdlParams['LRstep']))
# Define softmax
modelVars['softmax'] = nn.Softmax(dim=1)
# Manually find latest chekcpoint, tf.train.latest_checkpoint is doing weird shit
files = glob(mdlParams['saveDir']+'/*')
global_steps = np.zeros([len(files)])
for i in range(len(files)):
# Use meta files to find the highest index
if 'checkpoint' not in files[i]:
continue
if mdlParams['ckpt_name'] not in files[i]:
continue
# Extract global step
nums = [int(s) for s in re.findall(r'\d+',files[i])]
global_steps[i] = nums[-1]
# Create path with maximum global step found, if first is not wanted
global_steps = np.sort(global_steps)
if mdlParams.get('use_first') is not None:
chkPath = mdlParams['saveDir'] + '/' + mdlParams['ckpt_name'] + str(int(global_steps[-2])) + '.pt'
else:
chkPath = mdlParams['saveDir'] + '/' + mdlParams['ckpt_name'] + str(int(np.max(global_steps))) + '.pt'
print("Restoring: ",chkPath)
# Load
state = torch.load(chkPath)
# Initialize model and optimizer
modelVars['model'].load_state_dict(state['state_dict'])
#modelVars['optimizer'].load_state_dict(state['optimizer'])
# Get predictions or learn on pred
modelVars['model'].eval()
# Get predictions
# Turn off the skipping of the last class
mdlParams['no_c9_eval'] = False
loss, accuracy, sensitivity, specificity, conf_matrix, f1, auc, waccuracy, predictions, targets, predictions_mc = utils.getErrClassification_mgpu(mdlParams, 'valInd', modelVars)
# Save predictions
allData['extPred'][cv] = predictions
print("extPred shape",allData['extPred'][cv].shape)
pklFileName = sys.argv[2] + "_" + sys.argv[6] + "_" + str(int(np.max(global_steps))) + "_predn.pkl"
# Mean results over all folds
np.set_printoptions(precision=4)
print("-------------------------------------------------")
print("Mean over all Folds")
print("-------------------------------------------------")
print("F1 Score",np.array([np.mean(allData['f1Best'])]),"+-",np.array([np.std(allData['f1Best'])]))
print("Sensitivtiy",np.mean(allData['sensBest'],0),"+-",np.std(allData['sensBest'],0))
print("Specificity",np.mean(allData['specBest'],0),"+-",np.std(allData['specBest'],0))
print("Mean Specificity",np.array([np.mean(allData['specBest'])]),"+-",np.array([np.std(np.mean(allData['specBest'],1))]))
print("Accuracy",np.array([np.mean(allData['accBest'])]),"+-",np.array([np.std(allData['accBest'])]))
print("Per Class Accuracy",np.mean(allData['waccBest'],0),"+-",np.std(allData['waccBest'],0))
print("Weighted Accuracy",np.array([np.mean(allData['waccBest'])]),"+-",np.array([np.std(np.mean(allData['waccBest'],1))]))
print("AUC",np.mean(allData['aucBest'],0),"+-",np.std(allData['aucBest'],0))
print("Mean AUC",np.array([np.mean(allData['aucBest'])]),"+-",np.array([np.std(np.mean(allData['aucBest'],1))]))
# Save dict with results
with open(mdlParams['saveDirBase'] + "/" + pklFileName, 'wb') as f:
pickle.dump(allData, f, pickle.HIGHEST_PROTOCOL)