-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrl_utils.py
108 lines (83 loc) · 3.4 KB
/
rl_utils.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
import torch
from torchvision.utils import make_grid
from captum.attr import GuidedBackprop, GuidedGradCam
class HookFeatures:
def __init__(self, module):
self.feature_hook = module.register_forward_hook(self.feature_hook_fn)
def feature_hook_fn(self, module, input, output):
self.features = output.clone().detach()
self.gradient_hook = output.register_hook(self.gradient_hook_fn)
def gradient_hook_fn(self, grad):
self.gradients = grad
def close(self):
self.feature_hook.remove()
self.gradient_hook.remove()
class ModelWrapper(torch.nn.Module):
def __init__(self, encoder, critic, action=None):
super(ModelWrapper, self).__init__()
self.encoder = encoder
self.critic = critic
self.action = action
def forward(self, obs):
obs = self.encoder(obs)
if self.action is None:
return self.critic(obs)[0]
return self.critic(obs, self.action)[0]
def compute_guided_backprop(obs, action, encoder, critic):
model = ModelWrapper(encoder, critic, action=action)
gbp = GuidedBackprop(model)
attribution = gbp.attribute(obs)
return attribution
def compute_guided_gradcam(obs, action, encoder, critic):
obs.requires_grad_()
obs.retain_grad()
model = ModelWrapper(encoder, critic, action=action)
gbp = GuidedGradCam(model,layer=model.model.encoder.head_cnn.layers)
attribution = gbp.attribute(obs,attribute_to_layer_input=True)
return attribution
def compute_vanilla_grad(critic_target, obs, action):
obs.requires_grad_()
obs.retain_grad()
q, q2 = critic_target(obs, action.detach())
q.sum().backward()
return obs.grad
def compute_attribution(encoder, critic, obs, action=None,method="guided_backprop"):
if method == "guided_backprop":
return compute_guided_backprop(obs, action, encoder, critic)
if method == 'guided_gradcam':
return compute_guided_gradcam(obs,action, encoder, critic)
def compute_features_attribution(critic_target, obs, action):
obs.requires_grad_()
obs.retain_grad()
hook = HookFeatures(critic_target.encoder)
q, _ = critic_target(obs, action.detach())
q.sum().backward()
features_gardients = hook.gradients
hook.close()
return obs.grad, features_gardients
def compute_attribution_mask(obs_grad, quantile=0.95):
mask = []
for i in [0, 3, 6]:
attributions = obs_grad[:, i : i + 3].abs().max(dim=1)[0]
q = torch.quantile(attributions.flatten(1), quantile, 1)
mask.append((attributions >= q[:, None, None]).unsqueeze(1).repeat(1, 3, 1, 1))
return torch.cat(mask, dim=1)
def make_obs_grid(obs, n=4):
sample = []
for i in range(n):
for j in range(0, 9, 3):
sample.append(obs[i, j : j + 3].unsqueeze(0))
sample = torch.cat(sample, 0)
return make_grid(sample, nrow=3) / 255.0
def make_attribution_pred_grid(attribution_pred, n=4):
return make_grid(attribution_pred[:n], nrow=1)
def make_obs_grad_grid(obs_grad, n=4):
sample = []
for i in range(n):
for j in range(0, 9, 3):
channel_attribution, _ = torch.max(obs_grad[i, j : j + 3], dim=0)
sample.append(channel_attribution[(None,) * 2] / channel_attribution.max())
sample = torch.cat(sample, 0)
q = torch.quantile(sample.flatten(1), 0.97, 1)
sample[sample <= q[:, None, None, None]] = 0
return make_grid(sample, nrow=3)