-
Notifications
You must be signed in to change notification settings - Fork 140
/
Visual_Analysis.py
183 lines (145 loc) · 5.8 KB
/
Visual_Analysis.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.16.0
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# # Visual Analysis
#
# This notebook has been created to support two main purposes:
# * Based on an input image and a set of models, display the action-space probability distribution.
# * Based on an input image and a set of models, visualize which parts of the image the model looks at.
#
# ## Usage
#
# The workbook requires the following:
# * A set of raw images captured from the front camera of the car
# * One or more static model files (`model_*.pb`)
# * The `model_metadata.json`
#
# ## Contributions
#
# As usual, your ideas are very welcome and encouraged so if you have any suggestions either bring them to [the AWS DeepRacer Community](http://join.deepracing.io) or share as code contributions.
#
# ## Requirements
#
# Before you start using the notebook, you will need to install some dependencies. If you haven't yet done so, have a look at [The README.md file](/edit/README.md#running-the-notebooks) to find what you need to install.
#
# This workbook will require `tensorflow` and `cv2` to work.
#
# ## Installs and setups
#
# If you are using an AWS SageMaker Notebook or Sagemaker Studio Lab to run the log analysis, you will need to ensure you install required dependencies. To do that uncomment and run the following:
# +
# Make sure you have the required pre-reqs
# import sys
# # !{sys.executable} -m pip install --upgrade -r requirements.txt
# -
#
# ## Imports
#
# Run the imports block below:
# +
import json
import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.compat.v1.io.gfile import GFile
from deepracer.model import load_session, visualize_gradcam_discrete_ppo, rgb2gray
# -
# ## Configure and load files
#
# Provide the paths where the image and models are stored. Also define which iterations you would like to review.
img_selection = 'logs/sample-model/pictures/*.png'
model_path = 'logs/sample-model/model'
iterations = [15, 30, 48]
# Load the model metadata in, and define which sensor is in use.
with open("{}/model_metadata.json".format(model_path),"r") as jsonin:
model_metadata=json.load(jsonin)
my_sensor = [sensor for sensor in model_metadata['sensor'] if sensor != "LIDAR"][0]
display(model_metadata)
# Load in the pictures from the pre-defined path.
picture_files = sorted(glob.glob(img_selection))
display(picture_files)
action_names = []
degree_sign= u'\N{DEGREE SIGN}'
for action in model_metadata['action_space']:
action_names.append(str(action['steering_angle'])+ degree_sign + " "+"%.1f"%action["speed"])
display(action_names)
# ## Load the model files and process pictures
# We will now load in the models and process the pictures. Output is a nested list with size `n` models as the outer and `m` picture as the inner list. The inner list will contain a number of values equal to the
# +
model_inference = []
models_file_path = []
for n in iterations:
models_file_path.append("{}/model_{}.pb".format(model_path,n))
display(models_file_path)
for model_file in models_file_path:
model, obs, model_out = load_session(model_file, my_sensor, False)
arr = []
for f in picture_files[:]:
img = cv2.imread(f)
img = cv2.resize(img, dsize=(160, 120), interpolation=cv2.INTER_CUBIC)
img_arr = np.array(img)
img_arr = rgb2gray(img_arr)
img_arr = np.expand_dims(img_arr, axis=2)
current_state = {"observation": img_arr} #(1, 120, 160, 1)
y_output = model.run(model_out, feed_dict={obs:[img_arr]})[0]
arr.append (y_output)
model_inference.append(arr)
model.close()
tf.reset_default_graph()
# -
# ## Simulation Image Analysis - Probability distribution on decisions (actions)
#
# We will now show the probabilities per action for the selected picture and iterations. The higher the probability of one single action the more mature is the model. Comparing different models enables the developer to see how the model is becoming more certain over time.
PICTURE_INDEX=1
display(picture_files[PICTURE_INDEX])
# +
x = list(range(1,len(action_names)+1))
num_plots = len(iterations)
fig, ax = plt.subplots(num_plots,1,figsize=(20,3*num_plots),sharex=True,squeeze=False)
for p in range(0, num_plots):
ax[p][0].bar(x,model_inference[p][PICTURE_INDEX][::-1])
plt.setp(ax[p, 0], ylabel=os.path.basename(models_file_path[p]))
plt.xticks(x,action_names[::-1],rotation='vertical')
plt.show()
# -
# ## What is the model looking at?
#
# Gradcam: visual heatmap of where the model is looking to make its decisions. based on https://arxiv.org/pdf/1610.02391.pdf
# +
heatmaps = []
view_models = models_file_path[1:3]
for model_file in view_models:
model, obs, model_out = load_session(model_file, my_sensor, False)
arr = []
for f in picture_files:
img = cv2.imread(f)
img = cv2.resize(img, dsize=(160, 120), interpolation=cv2.INTER_CUBIC)
heatmap = visualize_gradcam_discrete_ppo(model, img, category_index=0, num_of_actions=len(action_names))
heatmaps.append(heatmap)
tf.reset_default_graph()
# +
fig, ax = plt.subplots(len(view_models),len(picture_files),
figsize=(7*len(view_models),2.5*len(picture_files)), sharex=True, sharey=True, squeeze=False)
for i in list(range(len(view_models))):
plt.setp(ax[i, 0], ylabel=os.path.basename(view_models[i]))
for j in list(range(len(picture_files))):
ax[i][j].imshow(heatmaps[i * len(picture_files) + j])
plt.setp(ax[-1:, j], xlabel=os.path.basename(picture_files[j]))
plt.show()
# -