1

我对深度学习很陌生,我正在尝试使用 lua 进行分类。

我已经用 torch 和 lua 5.1 安装了数字,并且我训练了以下模型:

模型的图像

之后,我使用数字服务器进行了分类以测试示例,结果如下: 分类结果

我已经导出了模型,现在我正在尝试使用以下 lua 代码进行分类:

local image_url = '/home/delpech/mnist/test/5/04131.png'
local network_url = '/home/delpech/models/snapshot_30_Model.t7'
local network_name = paths.basename(network_url)

print '==> Loading network'
local net = torch.load(network_name)

--local net = torch.load(network_name):unpack():float()
net:evaluate()
print(net)

print '==> Loading synsets'
print 'Loads mapping from net outputs to human readable labels'
local synset_words = {}
--for line in io.lines'/home/delpech/models/labels.txt' do table.insert(synset_words, line:sub(11)) end
for line in io.lines'/home/delpech/models/labels.txt' do table.insert(synset_words, line) end

print 'synset words'
for line in io.lines'/home/delpech/models/labels.txt' do print(line) end

print '==> Loading image and imagenet mean'
local im = image.load(image_url)

print '==> Preprocessing'
local I = image.scale(im,28,28,'bilinear'):float()

print 'Propagate through the network, sort outputs in decreasing order and show 10 best classes'
local _,classes = net:forward(I):view(-1):sort(true)

for i=1,10 do
    print('predicted class '..tostring(i)..': ', synset_words[classes[i]])
end

但这里是输出:

delpech@delpech-K55VD:~/models$ lua classify.lua 

==> Downloading image and network
==> Loading network
nn.Sequential {
  [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> output]
  (1): nn.MulConstant
  (2): nn.SpatialConvolution(1 -> 20, 5x5)
  (3): nn.SpatialMaxPooling(2x2, 2,2)
  (4): nn.SpatialConvolution(20 -> 50, 5x5)
  (5): nn.SpatialMaxPooling(2x2, 2,2)
  (6): nn.View(-1)
  (7): nn.Linear(800 -> 500)
  (8): nn.ReLU
  (9): nn.Linear(500 -> 10)
  (10): nn.LogSoftMax
}
==> Loading synsets
Loads mapping from net outputs to human readable labels
synset words
0
1
2
3
4
5
6
7
8
9
==> Loading image and imagenet mean
==> Preprocessing
Propagate through the network, sort outputs in decreasing order and show 5 best classes
predicted class 1:  4
predicted class 2:  8
predicted class 3:  0
predicted class 4:  1
predicted class 5:  9
predicted class 6:  6
predicted class 7:  7
predicted class 8:  2
predicted class 9:  5
predicted class 10:     3

而这其实并不是数字提供的分类……

4

1 回答 1

0

好的,在搜索数字代码源后,看起来我错过了两件事:

  • 您必须在作业文件夹中获取平均图像并进行以下预处理:

    print '==> Preprocessing' for i=1,im_mean:size(1) do im[i]:csub(im_mean[i]) end

  • 以及我必须以这种方式加载图像并将每个像素乘以 255 的事实。

本地 im = image.load(image_url):type('torch.FloatTensor'):contiguous(); 我:穆尔(255)

这是总答案:

require 'image'
require 'nn'
require 'torch'
require 'paths'

local function main()

print '==> Downloading image and network'
local image_url = '/home/delpech/mnist/test/7/03079.png'
local network_url = '/home/delpech/models/snapshot_30_Model.t7'
local mean_url = '/home/delpech/models/mean.jpg'

print '==> Loading network'
local net = torch.load(network_url)
net:evaluate();

print '==> Loading synsets'
print 'Loads mapping from net outputs to human readable labels'
local synset_words = {}
for line in io.lines'/home/delpech/models/labels.txt' do table.insert(synset_words, line) end

print '==> Loading image and imagenet mean'
local im = image.load(image_url):type('torch.FloatTensor'):contiguous();--:contiguous()
im:mul(255)
local I = image.scale(im,28,28,'bilinear'):float()


local im_mean =  image.load(mean_url):type('torch.FloatTensor'):contiguous();
im_mean:mul(255)
local Imean = image.scale(im,28,28,'bilinear'):float()

print '==> Preprocessing'
for i=1,im_mean:size(1) do
    im[i]:csub(im_mean[i])
end

local _,classes = net:forward(im):sort(true);
for i=1,10 do
  print('predicted class '..tostring(i)..': ', synset_words[classes[i]])
end

end 


main()
于 2017-03-04T11:43:27.380 回答