Skip to content

Commit 285bbd8

Browse files
authored
Merge pull request #9 from cauchyturing/update
up-to-date with tensorflow >= 1.13, add CAM example in FCN
2 parents 141e4ab + b003090 commit 285bbd8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

FCN.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,31 @@ def readucr(filename):
8484
#Print the testing results which has the lowest training loss.
8585
log = pd.DataFrame(hist.history)
8686
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()

MLP.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,3 @@ def readucr(filename):
8383
log = pd.DataFrame(hist.history)
8484
print(log.loc[log['loss'].idxmin]['loss'], log.loc[log['loss'].idxmin]['val_acc'])
8585

86-
87-

ResNet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def readucr(filename):
101101
X = data[:,1:]
102102
return X, Y
103103

104-
nb_epochs = 1000
104+
nb_epochs = 1500
105105

106106

107107
#flist = ['Adiac', 'Beef', 'CBF', 'ChlorineConcentration', 'CinC_ECG_torso', 'Coffee', 'Cricket_X', 'Cricket_Y', 'Cricket_Z',

0 commit comments

Comments
 (0)