torch lua

torch 专栏收录该内容
4 篇文章 0 订阅

根据上一篇文章分7部分讲解了基于torch识别分类图片的例子,修改了部分代码,识别指定类型的图片分类识别。

1 生成用于训练的t7文件

-- generate training material t7 files
require 'io'
require 'xlua'
require 'image'

dataNum = 10
imgSize = 32

aImgs = torch.Tensor(batchSize, 3, imgSize, imgSize):zero():float()
label1 = {}

for idx = 1,dataNum do
    im_name = string.format('/home/cgy/torch_bak/image/Chinese flag/%02d.jpg',idx)
    --xlua.progress(idx, dataNum)
    local img1 = image.load(im_name)
    aImgs[idx] = image.scale(img1, imgSize, imgSize):float()
    label1[idx] = 1
end

print('Save Anchor Images: aImgs.t7: ')

--Create the table to save
label = torch.Tensor(label1)
data_to_write = { data = aImgs, label = label }

--Save the table in the /home
torch.save('/home/cgy/torch_bak/image/Chinese flag/train_TEST.t7', data_to_write)

2 测试待训练的t7文件

-- load test
require 'paths'
require 'nn'

trainset = torch.load('/home/cgy/torch_bak/image/Chinese flag/train_TEST.t7')

trainset.data = trainset.data:double()

for idx = 1,10 do
    --print(trainset.label[idx])
    itorch.image(trainset.data[idx])
end

3 设置训练参数,生成模型

require 'paths';
require 'nn';

trainset = torch.load('/home/cgy/torch_bak/image/Chinese flag/train_TEST.t7')

setmetatable(trainset,
    {__index = function(t,i)
        return {t.data[i],t.label[i]}
        end}
);

trainset.data = trainset.data:double()

function trainset:size() 
    return self.data:size(1) 
end

---Normalize data
mean = {}
stdv = {}
for i=1,3 do
    mean[i] = trainset.data[{ {}, {i}, {}, {}  }]:mean()
    print('Channel ' .. i .. ', Mean: ' .. mean[i])
    trainset.data[{ {}, {i}, {}, {}  }]:add(-mean[i])

    stdv[i] = trainset.data[{ {}, {i}, {}, {}  }]:std()
    print('Channel ' .. i .. ', Standard Deviation:' .. stdv[i])
    trainset.data[{ {}, {i}, {}, {}  }]:div(stdv[i])
end

--数据的预处理
net = nn.Sequential()

--change 1 channel to 3 channels
--net:add(nn.SpatialConvolution(1, 6, 5, 5))
net:add(nn.SpatialConvolution(3, 6, 5, 5)) 

net:add(nn.ReLU())                       
net:add(nn.SpatialMaxPooling(2,2,2,2))     
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())                       
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))                    
net:add(nn.Linear(16*5*5, 120))         
net:add(nn.ReLU())                       
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())                       
net:add(nn.Linear(84, 10))                  
net:add(nn.LogSoftMax()) 

criterion = nn.ClassNLLCriterion();

trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 50

trainer:train(trainset)

4 验证训练结果

for idx = 1,10 do
    itorch.image(trainset.data[idx])
    predicted = net:forward(trainset.data[idx])
    print(predicted:exp())
end
  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值