因为自己也是一个初级小白学者,有很多的知识也是迷惑的,写博客一方面是为了和大家一起学习,另一方面的原因是想通过这种方式记录自己的学习历程,留下一些可以查看的轨迹。本内容主要是参考博客园中昵称为一只有恒心的小菜鸟的博客,我只是学习他所写内容的基础上做一下适合自己查看的笔记,希望对其他的童鞋能有所帮助。言归正传,本内容使用的数据集为SVHN,该数据集的介绍可以参考链接SVHN介绍:
1.先将用到的包require下:
require 'torch' -- torch
require 'image' -- to visualize the dataset
require 'nn' -- provides a normalization operator
if not opt then
print '==> processing options'
cmd = torch.CmdLine()
cmd:text()
cmd:text('SVHN Dataset Preprocessing')
cmd:text()
cmd:text('Options:')
cmd:option('-size', 'small', 'how many samples do we load: small | full | extra')
cmd:option('-visualize', true, 'visualize input data and weights during training')
cmd:text()
opt = cmd:parse(arg or {})
end
3.加载数据集:
train_file =torch.load( 'train_32x32.t7')
test_file = torch.load('test_32x32.t7')
extra_file =torch.load( 'extra_32x32.t7')
官网上下载是数据可能没有.T7格式,下载下来的数据是mat格式的,要转换成torch使用的t7格式,使用matio可以时间,首先需要安装matio,很简单,两条命令:
sudo apt-get install libmatio2
luarocks install matio
此时下载的数据是columns x rows x channels x num,但image.display()要求的数据组织形式是:num x channels x columns x rows,所以需要重新组织,下面是数据转换的代码:
matio = require'matio'
loaded = matio.load('train_32x32.mat')
trainData = {
data = loaded.X:permute(4,3,1,2) ,
labels = loaded.y[{{},{1}}],
size= function() return trsize end
}
唯一需要注意的是需要使用transpose()函数,这是因为在matlab中数据的表达一般是先列后行,而在torch中数据的表达一般是
先行后列,所以这里对后两维进行了转置,代码如下:
loaded = torch.load(extra_file,'ascii')
trainData = {
data = loaded.X:transpose(3,4),
labels = loaded.y[1],
size = function() return trsize end
}
4.设置训练集和测试集的大小
if opt.size == 'extra' then
print '==> using extra training data'
trsize = 73257 + 531131
tesize = 26032
elseif opt.size == 'full' then
print '==> using regular, full training data'
trsize = 73257
tesize = 26032
elseif opt.size == 'small' then
print '==> using reduced training data, for fast experiments'
trsize = 10000
tesize = 2000
end
5.当数据选择ectra时,对训练数据集进行拼接
if opt.size == 'extra' then
loaded = torch.load(extra_file,'ascii')
trdata = torch.Tensor(trsize,3,32,32)
trdata[{ {1,(#trainData.data)[1]} }] = trainData.data
trdata[{ {(#trainData.data)[1]+1,-1} }] = loaded.X:transpose(3,4)
trlabels = torch.Tensor(trsize)
trlabels[{ {1,(#trainData.labels)[1]} }] = trainData.labels
trlabels[{ {(#trainData.labels)[1]+1,-1} }] = loaded.y[1]
trainData = {
data = trdata,
labels = trlabels,
size = function() return trsize end
}
end
6.加载厕所数据集
loaded = matio.load('test_32x32.mat')
tempData = loaded.X:permute(4,3,1,2)
testData = {data = tempData, labels =loaded.y, size = function() return tesize end}
tempData = nil
+ 图像从RGB空间映射到YUV空间
+ Y通道使用 contrastive normalization operator进行局部规范化
+ 对所有的数据在每个通道进行规范化到0,1之间
-- RGB==>YUV
for i=1,trainData:size() do
trainData.data[i] = image.rgb2yuv(trainData.data[i]) -- 等价于 trainData.data[{{i},{},{},{}}]
end
for i=1,testData:size() do
testData.data[i] = image.rgb2yuv(testData.data[i])
end
-- Name Channels for convenience
channels = {'y','u','v'}
-- 单通道进行规范化
Mean={}
Std={}
for i=1, channel in ipairs(channels) do --此处和for i=1,3 do等价
Mean[i]= trainData.data[{{},{i},{},{}}]:mean()
Std[i] = trainData.data[{{},{i},{},{}}]:std()
trainData.data[{{},{i},{},{}}]=trainData.data[{{},{i},{},{}}]:csub(Mean[i])
trainData.data[{{},{i},{},{}}]=trainData.data[{{},{i},{},{}}]:div(Std[i])
end
for i=1,3 do
testData.data[{{},{i},{},{}}]:add(-Mean[i]) -- add 和csub
-- 这个用法见Tensor的手册,改变后替代原来数据,所以和上面是一样的
testData.data[{{},{i},{},{}}]:div(Std[i])
end
-- Define the normalization neighborhood:
neighborhood = image.gaussian1D(7)
-- Define our local normalization operator (It is an actual nn module,
-- which could be inserted into a trainable model):
normalization = nn.SpatialContrastiveNormalization(1, neighborhood):float()
-- Normalize all Y channels locally:
for i = 1,trainData:size() do
trainData.data[{ i,{1},{},{} }] = normalization:forward(trainData.data[{ i,{1},{},{} }]) --前向计算
end
for i = 1,testData:size() do
testData.data[{ i,{1},{},{} }] = normalization:forward(testData.data[{ i,{1},{},{} }])
end
for i,channel in ipairs(channels) do
trainMean = trainData.data[{ {},i }]:mean()
trainStd = trainData.data[{ {},i }]:std()
testMean = testData.data[{ {},i }]:mean()
testStd = testData.data[{ {},i }]:std()
print('training data, '..channel..'-channel, mean: ' .. trainMean)
print('training data, '..channel..'-channel, standard deviation: ' .. trainStd)
print('test data, '..channel..'-channel, mean: ' .. testMean)
print('test data, '..channel..'-channel, standard deviation: ' .. testStd)
end
8.最后是数据的可视化,显示了前256个数据Y,U,V通道上的效果
if opt.visualize then
first256Samples_y = trainData.data[{ {1,256},1 }]
first256Samples_u = trainData.data[{ {1,256},2 }]
first256Samples_v = trainData.data[{ {1,256},3 }]
image.display{image=first256Samples_y, nrow=16, legend='Some training examples: Y channel'}
image.display{image=first256Samples_u, nrow=16, legend='Some training examples: U channel'}
image.display{image=first256Samples_v, nrow=16, legend='Some training examples: V channel'}
end
把整体的代码附加的博客中。