greentfrapp / lucent

Lucid library adapted for PyTorch
Apache License 2.0
597 stars 89 forks source link

use custom model? #16

Closed dvschultz closed 3 years ago

dvschultz commented 3 years ago

Hi, I see its possible to use models from the modelzoo, is it possible to use a custom trained model? Ay documentation or direction would be appreciated.

humzaiqbal commented 3 years ago

Yeah, you can. Notice in the ModelZoo Colab Notebook they have the following code

from torchvision.models import resnet50

resnet50_model = resnet50(pretrained=True).to(device).eval()
_ = render.render_vis(resnet50_model, "layer4_1_conv1:121", show_inline=True)

You can treat out of the box models the exact same way as modelzoo ones

mhongg commented 3 years ago

I am trying to visualize a model that receives 1 channel input, but it shows

AssertionError: There are no saved feature maps. Make sure to put the model in eval mode, like so: model.to(device).eval(). See README for example.

It works fine if the model receives 3 channels input. How can I modify the code so that it can be used on a custom model that receives 1 channel input?

greentfrapp commented 3 years ago

I'll have an update over the weekend!

mhongg commented 3 years ago

Greatly appreciate your efforts!

greentfrapp commented 3 years ago

Hi @MHong1001, thanks for your interest in Lucent!

If your model only receives 1 channel input, you will need to disable preprocess and explicitly set transforms=[] (or your desired transformations. You should also initialize your own param_f input with channels=1.

Here's a code snippet that should help. The model here is simply a conv layer that takes a 1 channel input.

model = torch.nn.Sequential(torch.nn.Conv2d(1, 10, (3, 3))).to(device).eval()
param_f = lambda: param.image(128, channels=1)
# Alternatively, use pixel_image: param_f = lambda: param.pixel_image(shape=(1, 1, 128, 128))
_ = render.render_vis(model, "0:0", param_f, transforms=[], preprocess=False, show_inline=True)
miratshah27 commented 1 year ago
image

Please help out with this....

HTalNachmani commented 5 months ago

You certainly don't need the help anymore, but I'll leave this here in case that others get into the same problem. I was trying to run Lucent on a custom model, and I kept getting the same error "AssertionError: There are no saved feature maps". What solved it for me was to run an inference on the model once after initializing it and loading in the weights. After that Lucent worked normally! :)

Like this: image