def activations(self, x, layer_idx):
"""Return filter activations projected back to the input space, i.e.
images with shape (n_feature_maps, 3, 224, 224) for a particula layer.
The layer index is expected to be 0-based.
"""
if x.shape[0] != 1:
raise TypeError('Visualization is only supported for a single image at a time')
self.check_add_deconv_layers()
hs, unpooling_sizes = self.feature_map_activations(x)
hs = [h.data for h in hs]
activation_maps = []
n_activation_maps = hs[layer_idx].shape[1]
xp = self.xp
for i in range(n_activation_maps): # For each channel
h = hs[layer_idx].copy()
condition = xp.zeros_like(h)
condition[0][i] = 1 # Keep one feature map and zero all other
h = Variable(xp.where(condition, h, xp.zeros_like(h)))
for i in reversed(range(layer_idx+1)):
p = self.mps[i]
h = F.upsampling_2d(h, p.indexes, p.kh, p.sy, p.ph, unpooling_sizes[i])
for deconv in reversed(self.deconv_blocks[i]):
h = deconv(F.relu(h))
activation_maps.append(h.data)
return xp.concatenate(activation_maps)
评论列表
文章目录