-
Notifications
You must be signed in to change notification settings - Fork 6
/
mask.py
176 lines (133 loc) · 7.35 KB
/
mask.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
'''
BSD 3-Clause License
Copyright (c) 2020, Lawrence Livermore National Laboratory
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
'''
https://github.com/LLNL/fastcam
A toolkit for efficent computation of saliency maps for explainable
AI attribution.
This work was performed under the auspices of the U.S. Department of Energy
by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344
and was supported by the LLNL-LDRD Program under Project 18-ERD-021 and
Project 17-SI-003.
Software released as LLNL-CODE-802426.
See also: https://arxiv.org/abs/1911.11293
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd
# *******************************************************************************************************************
class DropMap(torch.autograd.Function):
r'''
When we created this, the torch.gt function did not seem to propagate gradients.
It might now, but we have not checked. This autograd function provides that.
'''
@staticmethod
def forward(ctx, p_map, k):
drop_map_byte = torch.gt(p_map,k)
drop_map = torch.as_tensor(drop_map_byte, dtype=p_map.dtype, device=p_map.device)
ctx.save_for_backward(drop_map)
return drop_map
@staticmethod
def backward(ctx, grad_output):
drop_map = ctx.saved_tensors
g_pmap = grad_output * drop_map[0]
r'''
Just return empty since we don't have use for this gradient.
'''
sz = g_pmap.size()
g_k = torch.empty((sz[0],1), dtype=g_pmap.dtype, device=g_pmap.device)
return g_pmap, g_k
# *******************************************************************************************************************
class SaliencyMaskDropout(nn.Module):
r'''
This will mask out an input tensor that can have arbitrary channels. It can also return the
binary mask it created from the saliency map. If it is used inline in a network, scale_map
should be set to True.
Parameters
keep_percent: A scalar from 0 to 1. This represents what percent of the image to keep.
return_layer_only: Tells us to just return the masked tensor only. Useful for putting layer into an nn.sequental.
scale_map: Scale the output like we would a dropout layer?
Will return
(1) The maksed tensor.
(2) The mask by itself.
'''
def __init__(self, keep_percent = 0.1, return_layer_only=False, scale_map=True):
super(SaliencyMaskDropout, self).__init__()
assert isinstance(keep_percent,float), "keep_percent should be a floating point value from 0 to 1"
assert keep_percent > 0, "keep_percent should be a floating point value from 0 to 1"
assert keep_percent <= 1.0, "keep_percent should be a floating point value from 0 to 1"
self.keep_percent = keep_percent
if scale_map:
self.scale = 1.0/keep_percent
else:
self.scale = 1.0
self.drop_percent = 1.0-self.keep_percent
self.return_layer_only = return_layer_only
def forward(self, x, sal_map):
assert torch.is_tensor(x), "Input x should be a Torch Tensor"
assert torch.is_tensor(sal_map), "Input sal_map should be a Torch Tensor"
sal_map_size = sal_map.size()
x_size = x.size()
assert len(x.size()) == 4, "Input x should have 4 dimensions [batch size x chans x height x width]"
assert len(sal_map.size()) == 3, "Input sal_map should be 3D [batch size x height x width]"
assert x_size[0] == sal_map_size[0], "x and sal_map should have same batch size"
assert x_size[2] == sal_map_size[1], "x and sal_map should have same height"
assert x_size[3] == sal_map_size[2], "x and sal_map should have same width"
sal_map = sal_map.reshape(sal_map_size[0], sal_map_size[1]*sal_map_size[2])
r'''
Using basically the same method we would to find the median, we find what value is
at n% in each saliency map.
'''
num_samples = int((sal_map_size[1]*sal_map_size[2])*self.drop_percent)
s = torch.sort(sal_map, dim=1)[0]
r'''
Here we can check that the saliency map has valid values between 0 to 1 since we
have sorted the image. It's cheap now.
'''
assert s[:,0] >= 0.0, "Saliency map should contain values within the range of 0 to 1"
assert s[:,-1] <= 1.0, "Saliency map should contain values within the range of 0 to 1"
r'''
Get the kth value for each image in the batch.
'''
k = s[:,num_samples]
k = k.reshape(sal_map_size[0], 1)
r'''
We will create the saliency mask but we use torch.autograd so that we can optionally
propagate the gradients backwards through the mask. k is assumed to be a dead-end, so
no gradients go to it.
'''
drop_map = DropMap.apply(sal_map, k)
drop_map = drop_map.reshape(sal_map_size[0], 1, sal_map_size[1]*sal_map_size[2])
x = x.reshape(x_size[0], x_size[1], x_size[2]*x_size[3])
r'''
Multiply the input by the mask, but optionally scale it like we would a dropout layer.
'''
x = x*drop_map*self.scale
x = x.reshape(x_size[0], x_size[1], x_size[2], x_size[3])
if self.return_layer_only:
return x
else:
return x, drop_map.reshape(sal_map_size[0], sal_map_size[1], sal_map_size[2])