码迷,mamicode.com
首页 > 其他好文 > 详细

vgg的grad作为激活值来展示图片物体

时间:2018-10-24 17:45:11      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:vgg   nsf   pre   UNC   put   stat   state   imp   out   

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作为激活值来展示图片物体

标签:vgg   nsf   pre   UNC   put   stat   state   imp   out   

原文地址:https://www.cnblogs.com/waldenlake/p/9844521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!