I’m trying to visualize the output of a particular activation layer (LeakyReLU) through random image optimization, but for some reason all I got is noise. I will post my code, maybe there is something I’m overlooking here. This is a YOLO (Darknet53) network by the way. I’m attaching the hook before the first YOLO layer in the network.
# Tensor wrapper.
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor

# Apply Feature Visualization to each feature map. The random image should be of size CxHxW.
img = (np.uint8(
    np.random.uniform(
        150,
        180,
        (3,
         int(self.model.hyperparams['height']),
         int(self.model.hyperparams['height']))
    )) / 255)

# Convert numpy image to tensor and add 1 dimension, final outcome is 1xCxHxW.
img_tensor = torch.tensor(img).unsqueeze(0).type(Tensor).detach().requires_grad_(True)
optimizer = torch.optim.Adam([img_tensor], lr=0.1, weight_decay=1e-6)

for k in range(0, 30):
    optimizer.zero_grad()

    _ = self.model(img_tensor)

    loss = self.activation_output.mean()
    loss *= -1
    print('> Iteration: {} Loss: {:.2f}'.format(k + 1, loss.data.cpu().numpy()))
    loss.backward()

    optimizer.step()

    if k == 30 - 1:
        img = img_tensor[0].clone().data.cpu().numpy().transpose(1, 2, 0)  # Transform to HxWxC.

image = ToPILImage(mode='RGB')(img)

download1191×1227 1.52 MB