Coder Social home page Coder Social logo

Comments (3)

berleon avatar berleon commented on June 8, 2024

Hi unnir,

I was not able to reproduce your error from your snippet. I adapted your snippet to use the CIFAR10 dataset (just for sanity checking and I did not had an ImageNet copy arround). It now runs through without any problem:

from IBA.pytorch import IBA, tensor_to_np_img, get_imagenet_folder, imagenet_transform
from IBA.utils import plot_saliency_map, to_unit_interval, load_monkeys

from torch.utils.data import DataLoader
from torchvision.models import vgg16, resnet34
from torchvision.datasets import CIFAR10
from torchvision.transforms import Resize, ToTensor, Compose
import torch
from torch import nn

# imagenet_dir = /path/to/imagenet/validation

# Load model
dev = 'cuda:0' if  torch.cuda.is_available() else 'cpu'
model = resnet34(pretrained=True)
model.fc = nn.Linear(512, 10)
model.to(dev)
model.eval()

# Add a Per-Sample Bottleneck at layer conv4_1
iba = IBA(model.layer2)

# Estimate the mean and variance of the feature map at this layer.
val_set = CIFAR10('/tmp/cifar', transform=Compose([Resize(224), ToTensor()]), download=True)
val_loader = DataLoader(val_set, batch_size=1, shuffle=True, num_workers=0)
iba.estimate(model, val_loader, n_samples=1, progbar=True)

# Load Image
monkeys, target = load_monkeys(pil=True)
monkeys_transform = imagenet_transform()(monkeys)

# fix target
target = 5

# Closure that returns the loss for one batch
model_loss_closure = lambda x: -torch.log_softmax(model(x), dim=1)[:, target].mean()

# Explain class target for the given image
saliency_map = iba.analyze(monkeys_transform.unsqueeze(0).to(dev), model_loss_closure, beta=10)

# display result
model_loss_closure = lambda x: -torch.log_softmax(model(x), 1)[:, target].mean()
heatmap = iba.analyze(monkeys_transform[None].to(dev), model_loss_closure )
plot_saliency_map(heatmap, tensor_to_np_img(monkeys_transform))

Please, let me know if this version does not work for you.

Best,
Leon

from iba.

unnir avatar unnir commented on June 8, 2024

it was my mistake. Sorry and thank you for your answer.

from iba.

berleon avatar berleon commented on June 8, 2024

No Problem, let me know if you encounter another problem.

from iba.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.