-
Notifications
You must be signed in to change notification settings - Fork 0
/
average_top1_confident.lua
82 lines (64 loc) · 2.07 KB
/
average_top1_confident.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'cudnn'
require 'inn'
require 'image'
require 'dcnn'
-- Loads the mapping from net outputs to human readable labels
function load_synset()
local file = io.open 'synset_words.txt'
local list = {}
while true do
local line = file:read()
if not line then break end
table.insert(list, string.sub(line,11))
end
return list
end
-- Converts an image from RGB to BGR format and subtracts mean
function preprocess(im, img_mean)
-- rescale the image
local im3 = image.scale(im,224,224,'bilinear')*255
-- RGB2BGR
local im4 = im3:clone()
im4[{1,{},{}}] = im3[{3,{},{}}]
im4[{3,{},{}}] = im3[{1,{},{}}]
-- subtract imagenet mean
return im4 - image.scale(img_mean, 224, 224, 'bilinear')
end
-- Lua implementation of PHP scandir function
function scandir(directory)
local i, t, popen = 0, {}, io.popen
for filename in popen('ls -a "'..directory..'"'):lines() do
if filename:match "%.png$" then
i = i + 1
t[i] = filename
end
end
return t, i
end
fileDir = '/usr/local/data/imagenet/train'
fileList, total_files = scandir( fileDir )
-- Setting up networks
print '==> Loading network'
--net = torch.load('/home/yusheng/Workspace/DeepLearning/models/zeilerNet/zeilerNet.net')
net = torch.load('/usr/local/data/zeilerNet/zeilerNet.net')
net:cuda()
-- as we want to classify, let's disable dropouts by enabling evaluation mode
net:evaluate()
print '==> Loading synsets'
synset_words = load_synset()
print '==> Loading image and imagenet mean'
image_name = 'Goldfish3.jpg'
--image_name = 'lena.jpg'
--image_name='people2.jpg'
img_mean_name = 'ilsvrc_2012_mean.t7'
im = image.load(image_name)
img_mean = torch.load(img_mean_name).img_mean:transpose(3,1)
-- Have to resize and convert from RGB to BGR and subtract mean
print '==> Preprocessing'
I = preprocess(im, img_mean)
-- Replace pooling by dual pooling
unpooling_layers = dcnn:ReplaceDualPoolingModule(net:get(1))
_,classes = net:forward(I:cuda()):view(-1):float():sort(true)
for i=1,5 do
print('predicted class '..tostring(i)..': ', synset_words[classes[i] ])
end