-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_mrf.py
118 lines (105 loc) · 3.93 KB
/
plot_mrf.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
import os
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('golf.mplstyle') # load matplotlib style
save_path = 'multi-rf-fig/' # save path
data_path = 'multi-rf/' # data path
# ->> explore effect on multi-rfs (receptive fields, rf)
rfs = [
'block1_conv2', # front layer
'block2_conv2',
'block3_conv4',
'block4_conv4',
'block5_conv4', # back layer
]
def load_npz(name):
fullname = os.path.join(data_path, name)
npz = np.load(fullname)
swing, heatmap_ext, guided_gradcam = npz['swing'], npz['heatmap_ext'], npz['guided_gradcam']
predicted_class, actual_class = npz['predicted_class'], npz['actual_class']
return swing, heatmap_ext, guided_gradcam, predicted_class, actual_class
def plot_fn(swing, heatmap_ext, guided_gradcam, title, fullname_prefix):
figsize = (8, 4.5)
# ->> plot sg
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(swing[:, 0], label='SG1')
ax.plot(swing[:, 1], label='SG2')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'Strain [m$\epsilon$]')
ax.legend()
plt.savefig(fullname_prefix + '_sg.jpg')
plt.close()
# ->> plot acc.
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(swing[:, 2], label='AccX')
ax.plot(swing[:, 3], label='AccY')
ax.plot(swing[:, 4], label='AccZ')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'Acceleration [m/s$^{2}$]')
ax.legend()
plt.savefig(fullname_prefix + '_acc.jpg')
plt.close()
# ->> plot gyro.
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(swing[:, 5], label='GyroX')
ax.plot(swing[:, 6], label='GyroY')
ax.plot(swing[:, 7], label='GyroZ')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'Angular speed [deg/s]')
ax.legend()
plt.savefig(fullname_prefix + '_gyro.jpg')
plt.close()
# ->> plot guided grad-cam of sg
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(guided_gradcam[:, 0], label='SG1')
ax.plot(guided_gradcam[:, 1], label='SG2')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'SG guided grad-cam')
ax.legend()
plt.savefig(fullname_prefix + '_sg_ggcam.jpg')
plt.close()
# ->> plot guided grad-cam of acc.
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(guided_gradcam[:, 2], label='AccX')
ax.plot(guided_gradcam[:, 3], label='AccY')
ax.plot(guided_gradcam[:, 4], label='AccZ')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'Acc. guided grad-cam')
ax.legend()
plt.savefig(fullname_prefix + '_acc_ggcam.jpg')
plt.close()
# ->> plot guided grad-cam of gyro.
plt.clf()
fig, ax = plt.subplots(figsize=figsize)
ax.plot(guided_gradcam[:, 5], label='GyroX')
ax.plot(guided_gradcam[:, 6], label='GyroY')
ax.plot(guided_gradcam[:, 7], label='GyroZ')
ax.plot(heatmap_ext, linestyle='--', color='orangered', label='Heatmap')
ax.set_title(title)
ax.set_xlabel('Time [ms]')
ax.set_ylabel(r'Gyro. guided grad-cam')
ax.legend()
plt.savefig(fullname_prefix + '_gyro_ggcam.jpg')
plt.close()
def load_and_plot(name, save_fullname_prefix):
# ->> load swing
swing, heatmap_ext, guided_gradcam, predicted_class, actual_class = load_npz(name)
title = 'Predicted class: {}, Actual class: {}'.format(predicted_class, actual_class)
plot_fn(swing, heatmap_ext, guided_gradcam, title, save_fullname_prefix)
for rf in rfs:
load_and_plot(rf + '.npz', os.path.join(save_path, rf))