使用从 digits 导出的 torch 模型进行分类 - lua 5.1
2017-3-1 15:50:25
收藏:0
阅读:75
评论:1
我非常新于深度学习,并且试图在 lua 中进行分类。
我已经安装了 torch 和 lua 5.1 的 digits,并训练了以下模型:
之后,我使用 digits 服务器进行了分类,以测试示例,这是结果:

我导出了模型,现在我正在尝试使用以下 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
而这实际上不是 digits 提供的分类结果...
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?


好的,经过对数字代码资源的搜索,我发现我错过了以下两个事情:
print '==> Preprocessing' for i=1,im\_mean:size(1) do im[i]:csub(im_mean[i]) endlocal im = image.load(image\_url):type('torch.FloatTensor'):contiguous(); im:mul(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()