forked from hfawaz/dl-4-tsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·617 lines (481 loc) · 21.7 KB
/
utils.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
from builtins import print
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['font.family'] = 'sans-serif'
matplotlib.rcParams['font.sans-serif'] = 'Arial'
import os
import operator
import utils
from utils.constants import UNIVARIATE_DATASET_NAMES as DATASET_NAMES
from utils.constants import ARCHIVE_NAMES as ARCHIVE_NAMES
from utils.constants import CLASSIFIERS
from utils.constants import ITERATIONS
from utils.constants import MTS_DATASET_NAMES
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.preprocessing import LabelEncoder
from scipy.interpolate import interp1d
from scipy.io import loadmat
def readucr(filename):
data = np.loadtxt(filename, delimiter = ',')
Y = data[:,0]
X = data[:,1:]
return X, Y
def create_directory(directory_path):
if os.path.exists(directory_path):
return None
else:
try:
os.makedirs(directory_path)
except:
# in case another machine created the path meanwhile !:(
return None
return directory_path
def create_path(root_dir,classifier_name, archive_name):
output_directory = root_dir+'/results/'+classifier_name+'/'+archive_name+'/'
if os.path.exists(output_directory):
return None
else:
os.makedirs(output_directory)
return output_directory
def read_dataset(root_dir,archive_name,dataset_name):
datasets_dict = {}
if archive_name == 'mts_archive':
file_name = root_dir+'/archives/'+archive_name+'/'+dataset_name+'/'
x_train = np.load(file_name + 'x_train.npy')
y_train = np.load(file_name + 'y_train.npy')
x_test = np.load(file_name + 'x_test.npy')
y_test = np.load(file_name + 'y_test.npy')
datasets_dict[dataset_name] = (x_train.copy(), y_train.copy(), x_test.copy(),
y_test.copy())
else:
file_name = root_dir+'/archives/'+archive_name+'/'+dataset_name+'/'+dataset_name
x_train, y_train = readucr(file_name+'_TRAIN')
x_test, y_test = readucr(file_name+'_TEST')
datasets_dict[dataset_name] = (x_train.copy(),y_train.copy(),x_test.copy(),
y_test.copy())
return datasets_dict
def read_all_datasets(root_dir,archive_name, split_val = False):
datasets_dict = {}
dataset_names_to_sort = []
if archive_name == 'mts_archive':
for dataset_name in MTS_DATASET_NAMES:
root_dir_dataset = root_dir + '/archives/' + archive_name + '/' + dataset_name + '/'
x_train = np.load(root_dir_dataset+'x_train.npy')
y_train = np.load(root_dir_dataset+'y_train.npy')
x_test = np.load(root_dir_dataset+'x_test.npy')
y_test = np.load(root_dir_dataset+'y_test.npy')
datasets_dict[dataset_name] = (x_train.copy(), y_train.copy(), x_test.copy(),
y_test.copy())
else:
for dataset_name in DATASET_NAMES:
root_dir_dataset =root_dir+'/archives/'+archive_name+'/'+dataset_name+'/'
file_name = root_dir_dataset+dataset_name
x_train, y_train = readucr(file_name+'_TRAIN')
x_test, y_test = readucr(file_name+'_TEST')
datasets_dict[dataset_name] = (x_train.copy(),y_train.copy(),x_test.copy(),
y_test.copy())
dataset_names_to_sort.append((dataset_name,len(x_train)))
dataset_names_to_sort.sort(key=operator.itemgetter(1))
for i in range(len(DATASET_NAMES)):
DATASET_NAMES[i] = dataset_names_to_sort[i][0]
return datasets_dict
def get_func_length(x_train,x_test,func):
if func==min:
func_length = np.inf
else:
func_length = 0
n=x_train.shape[0]
for i in range(n):
func_length = func(func_length, x_train[i].shape[1])
n=x_test.shape[0]
for i in range(n):
func_length = func(func_length, x_test[i].shape[1])
return func_length
def transform_to_same_length(x,n_var,max_length):
n = x.shape[0]
# the new set in ucr form np array
ucr_x = np.zeros((n,max_length,n_var),dtype=np.float64)
# loop through each time series
for i in range(n):
mts = x[i]
curr_length = mts.shape[1]
idx= np.array(range(curr_length))
idx_new = np.linspace(0,idx.max(),max_length)
for j in range(n_var):
ts = mts[j]
# linear interpolation
f = interp1d(idx, ts, kind='cubic')
new_ts = f(idx_new)
ucr_x[i,:,j] = new_ts
return ucr_x
def transform_mts_to_ucr_format():
mts_root_dir = '/mnt/Other/mtsdata/'
mts_out_dir = '/mnt/nfs/casimir/archives/mts_archive/'
for dataset_name in MTS_DATASET_NAMES:
# print('dataset_name',dataset_name)
out_dir = mts_out_dir+dataset_name+'/'
# if create_directory(out_dir) is None:
# print('Already_done')
# continue
a = loadmat(mts_root_dir+dataset_name+'/'+dataset_name+'.mat')
a = a['mts']
a = a[0,0]
dt = a.dtype.names
dt = list(dt)
for i in range(len(dt)):
if dt[i] == 'train':
x_train=a[i].reshape(max(a[i].shape))
elif dt[i] == 'test':
x_test = a[i].reshape(max(a[i].shape))
elif dt[i]=='trainlabels':
y_train = a[i].reshape(max(a[i].shape))
elif dt[i]=='testlabels':
y_test = a[i].reshape(max(a[i].shape))
# x_train = a[1][0]
# y_train = a[0][:,0]
# x_test = a[3][0]
# y_test = a[2][:,0]
n_var = x_train[0].shape[0]
max_length = get_func_length(x_train,x_test,func=max)
min_length = get_func_length(x_train,x_test,func=min)
print(dataset_name, 'max',max_length,'min', min_length)
print()
#continue
x_train = transform_to_same_length(x_train,n_var,max_length)
x_test = transform_to_same_length(x_test,n_var,max_length)
# save them
np.save(out_dir+'x_train.npy',x_train)
np.save(out_dir+'y_train.npy',y_train)
np.save(out_dir+'x_test.npy',x_test)
np.save(out_dir+'y_test.npy',y_test)
print('Done')
def calculate_metrics(y_true, y_pred,duration,y_true_val=None,y_pred_val=None):
res = pd.DataFrame(data = np.zeros((1,4),dtype=np.float), index=[0],
columns=['precision','accuracy','recall','duration'])
res['precision'] = precision_score(y_true,y_pred,average='macro')
res['accuracy'] = accuracy_score(y_true,y_pred)
if not y_true_val is None:
# this is useful when transfer learning is used with cross validation
res['accuracy_val'] = accuracy_score(y_true_val,y_pred_val)
res['recall'] = recall_score(y_true,y_pred,average='macro')
res['duration'] = duration
return res
def transform_labels(y_train,y_test,y_val=None):
"""
Transform label to min equal zero and continuous
For example if we have [1,3,4] ---> [0,1,2]
"""
if not y_val is None :
# index for when resplitting the concatenation
idx_y_val = len(y_train)
idx_y_test = idx_y_val + len(y_val)
# init the encoder
encoder = LabelEncoder()
# concat train and test to fit
y_train_val_test = np.concatenate((y_train,y_val,y_test),axis =0)
# fit the encoder
encoder.fit(y_train_val_test)
# transform to min zero and continuous labels
new_y_train_val_test = encoder.transform(y_train_val_test)
# resplit the train and test
new_y_train = new_y_train_val_test[0:idx_y_val]
new_y_val = new_y_train_val_test[idx_y_val:idx_y_test]
new_y_test = new_y_train_val_test[idx_y_test:]
return new_y_train, new_y_val,new_y_test
else:
# no validation split
# init the encoder
encoder = LabelEncoder()
# concat train and test to fit
y_train_test = np.concatenate((y_train,y_test),axis =0)
# fit the encoder
encoder.fit(y_train_test)
# transform to min zero and continuous labels
new_y_train_test = encoder.transform(y_train_test)
# resplit the train and test
new_y_train = new_y_train_test[0:len(y_train)]
new_y_test = new_y_train_test[len(y_train):]
return new_y_train, new_y_test
def generate_results_csv(output_file_name, root_dir):
res = pd.DataFrame(data = np.zeros((0,7),dtype=np.float), index=[],
columns=['classifier_name','archive_name','dataset_name',
'precision','accuracy','recall','duration'])
for classifier_name in CLASSIFIERS:
for archive_name in ARCHIVE_NAMES:
datasets_dict = read_all_datasets(root_dir,archive_name)
for it in range(ITERATIONS):
curr_archive_name = archive_name
if it != 0 :
curr_archive_name = curr_archive_name +'_itr_'+str(it)
for dataset_name in datasets_dict.keys():
output_dir = root_dir+'/results/'+classifier_name+'/'\
+curr_archive_name+'/'+dataset_name+'/'+'df_metrics.csv'
if not os.path.exists(output_dir):
continue
df_metrics = pd.read_csv(output_dir)
df_metrics['classifier_name'] = classifier_name
df_metrics['archive_name'] = archive_name
df_metrics['dataset_name'] = dataset_name
res = pd.concat( (res,df_metrics) ,axis=0,sort=False)
res.to_csv(root_dir+output_file_name, index = False)
# aggreagte the accuracy for iterations on same dataset
res = pd.DataFrame({
'accuracy' : res.groupby(
['classifier_name','archive_name','dataset_name'])['accuracy'].mean()
}).reset_index()
return res
def plot_epochs_metric(hist, file_name, metric='loss'):
plt.figure()
plt.plot(hist.history[metric])
plt.plot(hist.history['val_'+metric])
plt.title('model '+metric)
plt.ylabel(metric,fontsize='large')
plt.xlabel('epoch',fontsize='large')
plt.legend(['train', 'val'], loc='upper left')
plt.savefig(file_name,bbox_inches='tight')
plt.close()
def save_logs_t_leNet(output_directory, hist, y_pred, y_true,duration ):
hist_df = pd.DataFrame(hist.history)
hist_df.to_csv(output_directory+'history.csv', index=False)
df_metrics = calculate_metrics(y_true,y_pred, duration)
df_metrics.to_csv(output_directory+'df_metrics.csv', index=False)
index_best_model = hist_df['loss'].idxmin()
row_best_model = hist_df.loc[index_best_model]
df_best_model = pd.DataFrame(data = np.zeros((1,6),dtype=np.float) , index = [0],
columns=['best_model_train_loss', 'best_model_val_loss', 'best_model_train_acc',
'best_model_val_acc', 'best_model_learning_rate','best_model_nb_epoch'])
df_best_model['best_model_train_loss'] = row_best_model['loss']
df_best_model['best_model_val_loss'] = row_best_model['val_loss']
df_best_model['best_model_train_acc'] = row_best_model['acc']
df_best_model['best_model_val_acc'] = row_best_model['val_acc']
df_best_model['best_model_nb_epoch'] = index_best_model
df_best_model.to_csv(output_directory+'df_best_model.csv', index=False)
# plot losses
plot_epochs_metric(hist, output_directory+'epochs_loss.png')
def save_logs(output_directory, hist, y_pred, y_true,duration,lr=True,y_true_val=None,y_pred_val=None):
hist_df = pd.DataFrame(hist.history)
hist_df.to_csv(output_directory+'history.csv', index=False)
df_metrics = calculate_metrics(y_true,y_pred, duration,y_true_val,y_pred_val)
df_metrics.to_csv(output_directory+'df_metrics.csv', index=False)
index_best_model = hist_df['loss'].idxmin()
row_best_model = hist_df.loc[index_best_model]
df_best_model = pd.DataFrame(data = np.zeros((1,6),dtype=np.float) , index = [0],
columns=['best_model_train_loss', 'best_model_val_loss', 'best_model_train_acc',
'best_model_val_acc', 'best_model_learning_rate','best_model_nb_epoch'])
df_best_model['best_model_train_loss'] = row_best_model['loss']
df_best_model['best_model_val_loss'] = row_best_model['val_loss']
df_best_model['best_model_train_acc'] = row_best_model['acc']
df_best_model['best_model_val_acc'] = row_best_model['val_acc']
if lr == True:
df_best_model['best_model_learning_rate'] = row_best_model['lr']
df_best_model['best_model_nb_epoch'] = index_best_model
df_best_model.to_csv(output_directory+'df_best_model.csv', index=False)
# for FCN there is no hyperparameters fine tuning - everything is static in code
# plot losses
plot_epochs_metric(hist, output_directory+'epochs_loss.png')
return df_metrics
def visualize_filter(root_dir):
import keras
classifier = 'fcn'
archive_name = 'UCR_TS_Archive_2015'
dataset_name = 'Gun_Point'
datasets_dict = read_dataset(root_dir,archive_name,dataset_name)
x_train = datasets_dict[dataset_name][0]
y_train = datasets_dict[dataset_name][1]
x_train = x_train.reshape(x_train.shape[0],x_train.shape[1],1)
model = keras.models.load_model(root_dir+'results/'+classifier+'/'+archive_name+'/'+dataset_name+'/best_model.hdf5')
# filters
filters = model.layers[1].get_weights()[0]
new_input_layer = model.inputs
new_output_layer = [model.layers[1].output]
new_feed_forward = keras.backend.function(new_input_layer,new_output_layer)
classes = np.unique(y_train)
colors = [(255/255,160/255,14/255),(181/255,87/255,181/255)]
colors_conv = [(210/255,0/255,0/255),(27/255,32/255,101/255)]
idx = 10
idx_filter = 1
filter = filters[:, 0, idx_filter]
plt.figure(1)
plt.plot(filter+0.5, color='gray', label='filter')
for c in classes:
c_x_train = x_train[np.where(y_train==c)]
convolved_filter_1 = new_feed_forward([c_x_train])[0]
idx_c = int(c)-1
plt.plot(c_x_train[idx],color=colors[idx_c],label='class'+str(idx_c)+'-raw')
plt.plot(convolved_filter_1[idx,:,idx_filter],color=colors_conv[idx_c],label='class'+str(idx_c)+'-conv')
plt.legend()
plt.savefig('convolution-'+dataset_name+'.pdf')
return 1
def viz_perf_themes(root_dir,df):
df_themes = df.copy()
themes_index = []
# add the themes
for dataset_name in df.index:
themes_index.append(utils.constants.dataset_types[dataset_name])
themes_index = np.array(themes_index)
themes, themes_counts = np.unique(themes_index, return_counts=True)
df_themes.index = themes_index
df_themes = df_themes.rank(axis=1, method='min', ascending=False)
df_themes = df_themes.where(df_themes.values == 1)
df_themes = df_themes.groupby(level=0).sum(axis=1)
df_themes['#'] = themes_counts
for classifier in CLASSIFIERS:
df_themes[classifier] = df_themes[classifier] / df_themes['#'] * 100
df_themes = df_themes.round(decimals=1)
df_themes.to_csv(root_dir + 'tab-perf-theme.csv')
def viz_perf_train_size(root_dir,df):
df_size = df.copy()
train_sizes = []
datasets_dict_ucr = read_all_datasets(root_dir, archive_name='UCR_TS_Archive_2015')
datasets_dict_mts = read_all_datasets(root_dir, archive_name='mts_archive')
datasets_dict = dict(datasets_dict_ucr, **datasets_dict_mts)
for dataset_name in df.index:
train_size = len(datasets_dict[dataset_name][0])
train_sizes.append(train_size)
train_sizes = np.array(train_sizes)
bins = np.array([0, 100, 400, 800, 99999])
train_size_index = np.digitize(train_sizes, bins)
train_size_index = bins[train_size_index]
df_size.index = train_size_index
df_size = df_size.rank(axis=1, method='min', ascending=False)
df_size = df_size.groupby(level=0, axis=0).mean()
df_size = df_size.round(decimals=2)
print(df_size.to_string())
df_size.to_csv(root_dir + 'tab-perf-train-size.csv')
def viz_perf_classes(root_dir,df):
df_classes = df.copy()
class_numbers = []
datasets_dict_ucr = read_all_datasets(root_dir, archive_name='UCR_TS_Archive_2015')
datasets_dict_mts = read_all_datasets(root_dir, archive_name='mts_archive')
datasets_dict = dict(datasets_dict_ucr, **datasets_dict_mts)
for dataset_name in df.index:
train_size = len(np.unique(datasets_dict[dataset_name][1]))
class_numbers.append(train_size)
class_numbers = np.array(class_numbers)
bins = np.array([0, 3, 4, 6, 8, 13, 9999])
class_numbers_index = np.digitize(class_numbers, bins)
class_numbers_index = bins[class_numbers_index]
df_classes.index = class_numbers_index
df_classes = df_classes.rank(axis=1, method='min', ascending=False)
df_classes = df_classes.groupby(level=0, axis=0).mean()
df_classes = df_classes.round(decimals=2)
print(df_classes.to_string())
df_classes.to_csv(root_dir + 'tab-perf-classes.csv')
def viz_perf_length(root_dir,df):
df_lengths = df.copy()
lengths = []
datasets_dict_ucr = read_all_datasets(root_dir, archive_name='UCR_TS_Archive_2015')
datasets_dict_mts = read_all_datasets(root_dir, archive_name='mts_archive')
datasets_dict = dict(datasets_dict_ucr, **datasets_dict_mts)
for dataset_name in df.index:
length = datasets_dict[dataset_name][0].shape[1]
lengths.append(length)
lengths = np.array(lengths)
bins = np.array([0, 81, 251, 451, 700, 1001, 9999])
lengths_index = np.digitize(lengths, bins)
lengths_index = bins[lengths_index]
df_lengths.index = lengths_index
df_lengths = df_lengths.rank(axis=1, method='min', ascending=False)
df_lengths = df_lengths.groupby(level=0, axis=0).mean()
df_lengths = df_lengths.round(decimals=2)
print(df_lengths.to_string())
df_lengths.to_csv(root_dir + 'tab-perf-lengths.csv')
def viz_plot(root_dir,df):
df_lengths = df.copy()
lengths = []
datasets_dict_ucr = read_all_datasets(root_dir, archive_name='UCR_TS_Archive_2015')
datasets_dict_mts = read_all_datasets(root_dir, archive_name='mts_archive')
datasets_dict = dict(datasets_dict_ucr, **datasets_dict_mts)
for dataset_name in df.index:
length = datasets_dict[dataset_name][0].shape[1]
lengths.append(length)
lengths_index = np.array(lengths)
df_lengths.index = lengths_index
plt.scatter(x=df_lengths['fcn'], y=df_lengths['resnet'])
plt.ylim(ymin=0,ymax=1.05)
plt.xlim(xmin=0,xmax=1.05)
# df_lengths['fcn']
plt.savefig(root_dir+'plot.pdf')
def viz_for_survey_paper(root_dir, filename='results-ucr-mts.csv'):
df = pd.read_csv(root_dir+filename, index_col=0)
df = df.T
df = df.round(decimals=2)
# get table performance per themes
# viz_perf_themes(root_dir,df)
# get table performance per train size
# viz_perf_train_size(root_dir,df)
# get table performance per classes
# viz_perf_classes(root_dir,df)
# get table performance per length
# viz_perf_length(root_dir,df)
# get plot
viz_plot(root_dir,df)
def viz_cam(root_dir):
import keras
import sklearn
classifier = 'fcn'
archive_name = 'UCR_TS_Archive_2015'
dataset_name = 'Meat'
if dataset_name == 'Gun_Point':
save_name = 'GunPoint'
else:
save_name = dataset_name
max_length = 2000
datasets_dict = read_dataset(root_dir, archive_name, dataset_name)
x_train = datasets_dict[dataset_name][0]
y_train = datasets_dict[dataset_name][1]
y_test = datasets_dict[dataset_name][3]
# transform to binary labels
enc = sklearn.preprocessing.OneHotEncoder()
enc.fit(np.concatenate((y_train, y_test), axis=0).reshape(-1, 1))
y_train_binary = enc.transform(y_train.reshape(-1, 1)).toarray()
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
model = keras.models.load_model(
root_dir + 'results/' + classifier + '/' + archive_name + '/' + dataset_name + '/best_model.hdf5')
# filters
w_k_c = model.layers[-1].get_weights()[0] # weights for each filter k for each class c
# the same input
new_input_layer = model.inputs
# output is both the original as well as the before last layer
new_output_layer = [model.layers[-3].output, model.layers[-1].output]
new_feed_forward = keras.backend.function(new_input_layer, new_output_layer)
classes = np.unique(y_train)
for c in classes:
plt.figure()
count =0
c_x_train = x_train[np.where(y_train==c)]
for ts in c_x_train:
ts = ts.reshape(1,-1,1)
[conv_out, predicted] = new_feed_forward([ts])
pred_label = np.argmax(predicted)
orig_label = np.argmax(enc.transform([[c]]))
if pred_label == orig_label:
cas = np.zeros(dtype=np.float, shape=(conv_out.shape[1]))
for k, w in enumerate(w_k_c[:, orig_label]):
cas += w * conv_out[0,:, k]
minimum = np.min(cas)
cas = cas - minimum
cas = cas / max(cas)
cas = cas * 100
x = np.linspace(0,ts.shape[1]-1,max_length,endpoint=True)
# linear interpolation to smooth
y = spline(range(ts.shape[1]),ts[0,:,0],x)
if any(y<-2.2 ):
continue
cas = spline(range(ts.shape[1]),cas,x)
cas = cas.astype(int)
plt.scatter(x=x,y=y,c=cas,cmap='jet', marker='.',s=1,vmin=0,vmax = 100)
if dataset_name == 'Gun_Point':
if c ==1:
plt.yticks([-1.0,0.0,1.0,2.0])
else:
plt.yticks([-2,-1.0,0.0,1.0,2.0])
count += 1
cbar = plt.colorbar()
# cbar.ax.set_yticklabels([100,75,50,25,0])
plt.savefig(root_dir+'/temp/'+classifier+'-cam-'+save_name+'-class-'+str(int(c))+'.png',bbox_inches='tight',dpi=1080)