Visualize feature map

No need to change the definition.
Instead of accessing e.g. model.conv2 you could use model.layer1[0] to get the first nn.Conv3d layer.

should be like this?

# Visualize conv filter
kernels = model.layer[0].detach()
fig, axarr = plt.subplots(kernels.size(0))
for idx in range(kernels.size(0)):
    axarr[idx].imshow(kernels[idx].squeeze())

correct?

it give me error. could you please help me :frowning:
AttributeError: ‘ConvNetRedo1’ object has no attribute ‘layer’

No, use my code snippet from above with forward hooks:

# Visualize feature maps
activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook

model.layer[0].register_forward_hook(get_activation('conv1'))
data, _ = dataset[0]
data.unsqueeze_(0)
output = model(data)

act = activation['conv1'].squeeze()
fig, axarr = plt.subplots(act.size(0))
for idx in range(act.size(0)):
    axarr[idx].imshow(act[idx])
2 Likes

the same error :
AttributeError: ‘ConvNetRedo1’ object has no attribute ‘layer’

Use model.layer1[0] instead.

it is solved many thanks

I’ve made a package for this MapExtrackt if any one is interested.

5 Likes

You can use something like this to visualize all the filter of that particular layer

Nice package! Is it compatible with 3DCNN as well?

Hi @ptrblck, if I have model like this:

I want to visualize output from “Mul”, what should I do? I dont think that get_activation('Mul') will work.
Thanks

You could either return this activation in your forward (or store in a list or dict), if it’s a functional call.
On the other hand, if this Mul operation is performed in an nn.Module, you could use forward hooks directly.

Hi @ptrblck, thanks for you reply, I actually can’t get your point. But here is my code:

I multiply out with x using * operator. So, how to get the output of this multiplication?

You could use:

def forward(self, x):
    ...
    act = out * x
    out = act + x
    return out, out1

to return the intermediate output of the multiplication together with the last output.

Alternatively you could also store act inside the module in a list via self.acts.append(out.detach()) or a global list etc.

1 Like

@Lewis_Morris Can you provide example code for using this package on self defined conv2d models?

It should work the same way as in the examples on GitHub. That being said someone who has used it has found that it didn’t work when the input had more than 4 channels. So I’ve tweaked it to work for them. If you are experiencing issues open an issue on git and I’d be more than happy to help. :ok_hand:

1 Like

Nice! Very easy to use package.

hi, i have the same problem, how did you solve it? Thanks

What issue did you have? Email me or open an issue on git hub and I’ll take a look

HI Ptrblck,

Sorry I want to see the outputs of the defined generator for each layer. Would you please help me how I can reach them to display?

class Generator994(nn.Module):
    def __init__(self,ngpu,nz,ngf):
        super(Generator994, self).__init__()
        self.ngpu=ngpu
        self.nz=nz
        self.ngf=ngf
        self.main = nn.Sequential(
            # input is Z, going into a convolution
            nn.ConvTranspose2d(self.nz, self.ngf * 8, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d( self.ngf * 4, self.ngf * 2, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d( self.ngf*2, 1, 3, 1, 0, bias=False),nn.Sigmoid()


        )

    def forward(self, input):
        return self.main(input)