/
adversarial_depth.py
163 lines (123 loc) · 5.02 KB
/
adversarial_depth.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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Script for generating adversarial examples for depth models."""
import numpy as np
import os
from keras import backend as K
from matplotlib import pyplot as plt
from PIL import Image
# Image Paths
SINK_IMAGE = "images/sink.png"
BATHTUB_IMAGE = "images/bathtub.png"
def get_sink_to_bathtub_inputs(depth_model_wrapper, model):
"""Gets the input and target output for the sink to bathtub experiment.
Args:
depth_model_wrapper: A DepthModelWrapper instance that initializes the model
and predicts the depth map.
Returns:
The input image and target depth output.
"""
input_image = depth_model_wrapper.load_image(SINK_IMAGE)
target_depth_output = depth_model_wrapper.predict(
model, depth_model_wrapper.load_image(BATHTUB_IMAGE))
return input_image, target_depth_output
def to_multichannel(image):
"""Reshapes an image to have 3 channels.
Args:
image: A numpy array image to be converted to multichannel.
"""
if image.shape[2] == 3:
return image
image = image[:,:,0]
return np.stack((image, image, image), axis=2)
def display_image(image, is_input=False):
"""Displays an numpy array image with matplotlib.
Args:
image: A numpy array image to be displayed with matplotlib.
is_input: A boolean indicating whether the image is an input image.
"""
plt.figure(figsize=(10,5))
if is_input:
plt.imshow(to_multichannel(image[0]))
else:
rescaled = image[0][:,:,0]
plt.imshow(rescaled)
plt.show()
def display(depth_model_wrapper, model, original_image, altered_image):
"""Displays the original input image and depth output and the altered input
image and depth output
Args:
depth_model_wrapper: A DepthModelWrapper instance that initializes the model
and predicts the depth map.
model: The depth map model.
original_image: A numpy array containing the unaltered input image.
altered_image: A numpy array containing the altered input image.
"""
original_output = depth_model_wrapper.predict(model, original_image)
altered_output = depth_model_wrapper.predict(model, altered_image)
display_image(original_image, is_input=True)
display_image(original_output)
display_image(altered_image, is_input=True)
display_image(altered_output)
def retrieve_loss_and_gradients(input_image, loss_gradient_function):
"""Retrieves the adversarial loss and gradients from the model.
Args:
input_image: A numpy array containing the input image used to generate the
adversarial example.
"""
results = loss_gradient_function([input_image])
loss = results[0]
gradients = results[1]
return loss, gradients
def gradient_ascent(input_image, loss_gradient_function, iterations, step_size):
"""Runs gradient ascent on the input image, altering it into an adversarial
example.
Args:
input_image: A numpy array containing the input image used to generate the
adversarial example.
iterations: The number of iterations to run gradient ascent.
step_size: The step size value.
"""
for i in range(iterations):
loss, gradients = retrieve_loss_and_gradients(input_image, loss_gradient_function)
print('Loss at %d: %.3f' % (i, loss))
input_image -= step_size * gradients
return input_image
def generate_adversarial_example(depth_model_wrapper, iterations = 100,
step_size = 0.001):
"""Displays the original input image and depth output and the altered input
image and depth output
Args:
depth_model_wrapper: A DepthModelWrapper instance that initializes the model
and predicts the depth map.
"""
# Initialize model.
K.set_learning_phase(0)
model = depth_model_wrapper.initialize_model()
input_image, goal_output = get_sink_to_bathtub_inputs(
depth_model_wrapper, model)
# Set up loss function.
final_layer_output = depth_model_wrapper.get_final_layer(model)
scale_factor = K.prod(K.cast(K.shape(final_layer_output), 'float32'))
loss = K.sum(K.square(final_layer_output - goal_output)) / scale_factor
gradients = K.gradients(loss, model.input)[0]
gradients /= K.maximum(K.mean(K.abs(gradients)), K.epsilon()) # normalize
get_loss_and_gradients = K.function([model.input], [loss, gradients])
# Run gradient ascent on image input.
original_image = input_image.copy()
altered_image = gradient_ascent(input_image, get_loss_and_gradients,
iterations, step_size)
# Display results
display(depth_model_wrapper, model, original_image, altered_image)