1. 程式人生 > >vgg的grad作為激活值來展示圖片物體

vgg的grad作為激活值來展示圖片物體

vision span 什麽 normal size 激活 ali nbsp .com

import torch
import numpy
import torch.nn as nn
import torch.nn.functional as F
from PIL import Image
from torchvision import transforms
import torchvision.models as models

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],#這是imagenet
                                 std=[0.229, 0.224, 0.225])

tran
=transforms.Compose([ transforms.Resize((224,224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) im=./1.jpeg # im=‘./2.jpg‘ im=Image.open(im) im=tran(im) im.unsqueeze_(dim=0) im=torch.autograd.Variable(im,requires_grad=True) vgg
= models.vgg16() pre=torch.load(/home/qk/.torch/models/vgg16-397923af.pth) vgg.load_state_dict(pre) out=vgg(im) outnp=out.data[0] ind=int(numpy.argmax(outnp)) out[0][ind].backward() grad=im.grad grad.squeeze_(0) grad=grad*grad grad=grad.sum(keepdim=False,dim=0) grad=torch.sqrt(grad) rg=torch.max(grad) grad
=grad/rg*255. #太神奇了,為什麽有uint8就能出來激活圖,沒有就不行!!!!!這要是不搜索一下的話,怎麽可能debug出來呢? # im = Image.fromarray(grad.numpy(),‘L‘) # .eval() tensor->numpy array im = Image.fromarray(numpy.uint8(grad.numpy()),L) # .eval() tensor->numpy array im.save(grey.png) # input() from cls import d print(d[ind])

vgg的grad作為激活值來展示圖片物體