-
Notifications
You must be signed in to change notification settings - Fork 7
/
Cifar100BinToTensor.lua
33 lines (28 loc) · 1.11 KB
/
Cifar100BinToTensor.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
require 'torch'
os.execute('wget -c http://www.cs.toronto.edu/~kriz/cifar-100-binary.tar.gz')
os.execute('tar -xvf cifar-100-binary.tar.gz')
local function convertCifar100BinToTorchTensor(inputFname, outputFname)
local m=torch.DiskFile(inputFname, 'r'):binary()
m:seekEnd()
local length = m:position() - 1
local nSamples = length / 3074 -- 1 coarse-label byte, 1 fine-label byte, 3072 pixel bytes
assert(nSamples == math.floor(nSamples), 'expecting numSamples to be an exact integer')
m:seek(1)
local coarse = torch.ByteTensor(nSamples)
local fine = torch.ByteTensor(nSamples)
local data = torch.ByteTensor(nSamples, 3, 32, 32)
for i=1,nSamples do
coarse[i] = m:readByte()
fine[i] = m:readByte()
local store = m:readByte(3072)
data[i]:copy(torch.ByteTensor(store))
end
local out = {}
out.data = data
out.label = fine
out.labelCoarse = coarse
print(out)
torch.save(outputFname, out)
end
convertCifar100BinToTorchTensor('cifar-100-binary/train.bin', 'cifar100-train.t7')
convertCifar100BinToTorchTensor('cifar-100-binary/test.bin', 'cifar100-test.t7')