In [8]:
require('nn')
require('image')
require('loadcaffe')
require('cunn')
torch.setdefaulttensortype('torch.FloatTensor')
-- 准备输入图像
imgFile = './dog2.png'
--imgFile = './lion.png'
ximg = image.loadPNG(imgFile, 3)
itorch.image(ximg)
ximg = ximg:float() * 256
ximg[1] = ximg[1] - 103.939
ximg[2] = ximg[2] - 116.779
ximg[3] = ximg[3] - 123.68
print(ximg:size())
Out[8]:
In [2]:
-- 载入分类文本
classText = torch.load('synset.t7','ascii')
-- 加载模型文件
cnn = loadcaffe.load('vgg19/VGG_ILSVRC_19_layers_deploy.prototxt', 'vgg19/VGG_ILSVRC_19_layers.caffemodel', 'nn')
cnn:evaluate()
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
Out[2]:
In [9]:
-- 直接得到分类结果
local score, obj = cnn:forward(ximg):max(1)
targetObj = obj[1]
print(score[1] .. " : " .. classText[obj[1]])
Out[9]:
In [10]:
local K = 48 --用8x8的去mask原图
local S = 5
local W,H = 224,224
heatMap = torch.zeros(math.floor((W-K+1)/S+1), math.floor((H-K+1)/S+1) )
local xx,yy = 1,1
cnn:cuda()
ximg = ximg:cuda()
for y = 1, H-K+1, S do
xx = 1
for x = 1, W-K+1, S do
local xximg = ximg:clone()
xximg[{{},{y,y+K-1},{x,x+K-1} }] = -128
local scores = cnn:forward(xximg)
heatMap[yy][xx] = scores[targetObj]
xx = xx + 1
end
yy = yy + 1
end
In [11]:
heatBMP = heatMap:clone()
local maxv = heatBMP:max()
local minv = heatBMP:min()
heatBMP = heatBMP - minv
heatBMP = heatBMP / (maxv - minv)
heatBMP = heatBMP * (-1) + 1
heatBMP = image.scale(heatBMP, 224, 224)
local showImage = image.loadPNG(imgFile, 3)
showImage = image.rgb2yuv(showImage)
showImage[1] = (heatBMP * 4 + showImage[1]) / 5
showImage = image.yuv2rgb(showImage)
itorch.image(heatBMP)
itorch.image(showImage)
In [ ]: