Comments (3)
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.
it was my mistake. Sorry and thank you for your answer.
from iba.
No Problem, let me know if you encounter another problem.
from iba.
Related Issues (17)
- improve readme
- add tensorflow implementation & update api
- Modify pytorch IBA to hook model
- Some changes for the PyTorch IBA API
- Add PyTorch test
- create pypi package
- Question about KL-div HOT 1
- training readout does not work HOT 3
- Add test for Readout IBA
- Use of k=hwc for selecting beta HOT 2
- When training readout, loss is Nan HOT 1
- Installing IBA on Win10 throws codec error
- Closure for Readout Bottleneck
- Unneeded closure argument in example notebook
- Question about estimated values provided in IBA-paper-code
- IBA for different input dimensions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iba.