Hi every one;
I need help, please. I have two images then I take the difference between these two images into one single image. I use Pretrained Mobile Neural Architecture Search (MNAS) to extract features only. I need to display the feature map of this image from the middle Conv layer of MNAS and the feature map of the final Conv layer before classifier.
I tried this snipped from @ptrblck but I have a problem.
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import models
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = models.mnasnet1_0(pretrained=True)
self.convNet = nn.Sequential (self.conv1)
self.convNet2= self.conv1.classifier=nn.Identity()
def forward(self, x):
x = self.convNet(x)
return x
img1 = './dataset/11/frame001.jpg'
img2= './dataset/11/frame004.jpg'
img1 = Image.open(str(img1))
img2 = Image.open(str(img2))
img1= transform=transforms.ToTensor()(img1)
img2= transform=transforms.ToTensor()(img2)
model = MyModel()
# Visualize feature maps
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
model.conv1.register_forward_hook(get_activation('conv1'))
img= torch.abs(img1 - img2)
img.unsqueeze_(0)
output = model(img)
act = activation['conv1'].squeeze()
fig, axarr = plt.subplots(act.size(0))
plt.imshow(act)
I get this error
File "C:\Users\Windows10\anaconda3\envs\Heyam\lib\site-packages\matplotlib\axes\_axes.py", line 5523, in imshow
im.set_data(X)
File "C:\Users\Windows10\anaconda3\envs\Heyam\lib\site-packages\matplotlib\image.py", line 709, in set_data
raise TypeError("Invalid shape {} for image data"
TypeError: Invalid shape (1280,) for image data