@@ -84,3 +84,31 @@ def readucr(filename):
84
84
#Print the testing results which has the lowest training loss.
85
85
log = pd .DataFrame (hist .history )
86
86
print (log .loc [log ['loss' ].idxmin ]['loss' ], log .loc [log ['loss' ].idxmin ]['val_acc' ])
87
+
88
+
89
+ ############## Get CAM ################
90
+ import matplotlib .pyplot as plt
91
+ # from matplotlib.backends.backend_pdf import PdfPages
92
+
93
+ get_last_conv = keras .backend .function ([model .layers [0 ].input , keras .backend .learning_phase ()], [model .layers [- 3 ].output ])
94
+ last_conv = get_last_conv ([x_test [:100 ], 1 ])[0 ]
95
+
96
+ get_softmax = keras .backend .function ([model .layers [0 ].input , keras .backend .learning_phase ()], [model .layers [- 1 ].output ])
97
+ softmax = get_softmax (([x_test [:100 ], 1 ]))[0 ]
98
+ softmax_weight = model .get_weights ()[- 2 ]
99
+ CAM = np .dot (last_conv , softmax_weight )
100
+
101
+
102
+ # pp = PdfPages('CAM.pdf')
103
+ for k in range (20 ):
104
+ CAM = (CAM - CAM .min (axis = 1 , keepdims = True )) / (CAM .max (axis = 1 , keepdims = True ) - CAM .min (axis = 1 , keepdims = True ))
105
+ c = np .exp (CAM ) / np .sum (np .exp (CAM ), axis = 1 , keepdims = True )
106
+ plt .figure (figsize = (13 , 7 ));
107
+ plt .plot (x_test [k ].squeeze ());
108
+ plt .scatter (np .arange (len (x_test [k ])), x_test [k ].squeeze (), cmap = 'hot_r' , c = c [k , :, :, int (y_test [k ])].squeeze (), s = 100 );
109
+ plt .title (
110
+ 'True label:' + str (y_test [k ]) + ' likelihood of label ' + str (y_test [k ]) + ': ' + str (softmax [k ][int (y_test [k ])]))
111
+ plt .colorbar ();
112
+ # pp.savefig()
113
+ #
114
+ # pp.close()
0 commit comments