forked from ndrplz/self-driving-car
-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize_activations.py
196 lines (154 loc) · 6.91 KB
/
visualize_activations.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
from load_data import preprocess
from os.path import join
import cv2
import numpy as np
import csv
from keras.models import Model
import matplotlib.pyplot as plt
from config import *
from keras.models import model_from_json
import os
import matplotlib.gridspec as gridspec
def normalize_in_0_255(tensor):
tensor -= tensor.min()
tensor /= tensor.max()
tensor *= 255.
return tensor
if __name__ == '__main__':
# directory in which activations are saved
elu1_out_dir = 'activations_conv1_no_train'
if not os.path.exists(elu1_out_dir):
os.makedirs(elu1_out_dir)
elu2_out_dir = 'activations_conv2_no_train'
if not os.path.exists(elu2_out_dir):
os.makedirs(elu2_out_dir)
elu3_out_dir = 'activations_conv3_no_train'
if not os.path.exists(elu3_out_dir):
os.makedirs(elu3_out_dir)
elu4_out_dir = 'activations_conv4_no_train'
if not os.path.exists(elu4_out_dir):
os.makedirs(elu4_out_dir)
with open('data/driving_log.csv', 'r') as f:
reader = csv.reader(f)
driving_data = [row for row in reader][1:]
# load model architecture
json_path = 'logs/model.json'
model = model_from_json(open(json_path).read())
# load model weights
weights_path = os.path.join('checkpoints', os.listdir('checkpoints')[-1])
print('Loading weights: {}'.format(weights_path))
model.load_weights(weights_path)
first_ELU = Model(input=model.layers[0].input, output=model.layers[3].output)
first_ELU.compile(optimizer='adam', loss='mse')
second_ELU = Model(input=model.layers[0].input, output=model.layers[6].output)
second_ELU.compile(optimizer='adam', loss='mse')
third_ELU = Model(input=model.layers[0].input, output=model.layers[9].output)
third_ELU.compile(optimizer='adam', loss='mse')
fourth_ELU = Model(input=model.layers[0].input, output=model.layers[12].output)
fourth_ELU.compile(optimizer='adam', loss='mse')
for i, data_row in enumerate(driving_data):
print('Frame {:06d} / {:06d}'.format(i, len(driving_data)))
# ELU 1 ######################################################################
plt.close('all')
# load current color frame
central_frame = cv2.imread(os.path.join('data', data_row[0]), cv2.IMREAD_COLOR)
gs = gridspec.GridSpec(4, 8)
ax = plt.subplot(gs[0, 3:5])
ax.set_axis_off()
ax.imshow(cv2.cvtColor(central_frame, cv2.COLOR_BGR2RGB))
# preprocess and add batch dimension
central_frame = preprocess(central_frame)
central_frame = central_frame[np.newaxis, :, :, :]
# z = np.random.rand(1, 31, 98, 24)
z = first_ELU.predict(central_frame)
z = normalize_in_0_255(z)
rows, cols = 3, 8
for r in range(rows):
for c in range(cols):
ax = plt.subplot(gs[r+1, c])
idx = r*cols + c
cur_act = z[0, :, :, idx]
cur_act = cv2.resize(cur_act, (NVIDIA_W, NVIDIA_H))
ax.set_axis_off()
ax.imshow(cur_act.astype(np.uint8), cmap='gray')
plt.tight_layout(pad=0.1, h_pad=-10, w_pad=-1.5)
filename = join(elu1_out_dir, 'conv1_{:06d}.jpg'.format(i))
plt.savefig(filename, facecolor='black', bbox_inches='tight')
# ELU 2 ######################################################################
plt.close('all')
# load current color frame
central_frame = cv2.imread(os.path.join('data', data_row[0]), cv2.IMREAD_COLOR)
gs = gridspec.GridSpec(5, 9)
ax = plt.subplot(gs[0, 3:6])
ax.set_axis_off()
ax.imshow(cv2.cvtColor(central_frame, cv2.COLOR_BGR2RGB))
# preprocess and add batch dimension
central_frame = preprocess(central_frame)
central_frame = central_frame[np.newaxis, :, :, :]
# z = np.random.rand(1, 14, 47, 36)
z = second_ELU.predict(central_frame)
z = normalize_in_0_255(z)
rows, cols = 4, 9
for r in range(rows):
for c in range(cols):
ax = plt.subplot(gs[r + 1, c])
idx = r * cols + c
cur_act = z[0, :, :, idx]
cur_act = cv2.resize(cur_act, (NVIDIA_W, NVIDIA_H))
ax.set_axis_off()
ax.imshow(cur_act.astype(np.uint8), cmap='gray')
plt.tight_layout(pad=0.1, h_pad=-10, w_pad=-1.5)
filename = join(elu2_out_dir, 'conv2_{:06d}.jpg'.format(i))
plt.savefig(filename, facecolor='black', bbox_inches='tight')
# ELU 3 ####################################################
plt.close('all')
# load current color frame
central_frame = cv2.imread(os.path.join('data', data_row[0]), cv2.IMREAD_COLOR)
gs = gridspec.GridSpec(7, 8)
ax = plt.subplot(gs[0, 3:5])
ax.set_axis_off()
ax.imshow(cv2.cvtColor(central_frame, cv2.COLOR_BGR2RGB))
# preprocess and add batch dimension
central_frame = preprocess(central_frame)
central_frame = central_frame[np.newaxis, :, :, :]
# z = np.random.rand(1, 14, 47, 36)
z = third_ELU.predict(central_frame)
z = normalize_in_0_255(z)
rows, cols = 6, 8
for r in range(rows):
for c in range(cols):
ax = plt.subplot(gs[r + 1, c])
idx = r * cols + c
cur_act = z[0, :, :, idx]
cur_act = cv2.resize(cur_act, (NVIDIA_W, NVIDIA_H))
ax.set_axis_off()
ax.imshow(cur_act.astype(np.uint8), cmap='gray')
plt.tight_layout(pad=0.1, h_pad=-10, w_pad=-1.5)
filename = join(elu3_out_dir, 'conv3_{:06d}.jpg'.format(i))
plt.savefig(filename, facecolor='black', bbox_inches='tight')
# ELU 4 ####################################################
plt.close('all')
# load current color frame
central_frame = cv2.imread(os.path.join('data', data_row[0]), cv2.IMREAD_COLOR)
gs = gridspec.GridSpec(9, 8)
ax = plt.subplot(gs[0, 3:5])
ax.set_axis_off()
ax.imshow(cv2.cvtColor(central_frame, cv2.COLOR_BGR2RGB))
# preprocess and add batch dimension
central_frame = preprocess(central_frame)
central_frame = central_frame[np.newaxis, :, :, :]
# z = np.random.rand(1, 14, 47, 36)
z = fourth_ELU.predict(central_frame)
z = normalize_in_0_255(z)
rows, cols = 8, 8
for r in range(rows):
for c in range(cols):
ax = plt.subplot(gs[r + 1, c])
idx = r * cols + c
cur_act = z[0, :, :, idx]
cur_act = cv2.resize(cur_act, (NVIDIA_W, NVIDIA_H))
ax.set_axis_off()
ax.imshow(cur_act.astype(np.uint8), cmap='gray')
plt.tight_layout(pad=0.4, h_pad=-2, w_pad=-1.5)
filename = join(elu4_out_dir, 'conv4_{:06d}.jpg'.format(i))
plt.savefig(filename, facecolor='black', bbox_inches='tight')