-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdataload.lua
143 lines (125 loc) · 3.76 KB
/
dataload.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'torch'
local opt, datamean, datastd = ...
local tnt = require 'torchnet'
local image = require 'image'
local WIDTH, HEIGHT = 128,128 -- 320, 140 -- opt.imageSize, opt.imageSize
names = {}
test_names = {}
trainDataPath = "data_/csv/center.csv"
testDataPath = "data_/csv/test_center.csv"
trainDir = [[data_/train_images_center/]]
testDir = [[data_/test_center/]]
if string.find(opt.model, 'nvidia') ~= nil then
WIDTH, HEIGHT = 320, 140
trainDataPath = "data_/csv/center.csv"
end
if string.find(opt.model, 'rambo') ~= nil then
WIDTH, HEIGHT = 320, 160
trainDataPath = "data_/csv/center.csv"
end
if string.find(opt.model, 'resnet') ~= nil then
WIDTH, HEIGHT = 128, 128
end
if string.find(opt.model, 'imagenet') ~= nil then
WIDTH, HEIGHT = 224, 224
end
additive = 0
if string.find(opt.model, 'class') ~= nil then
-- print("Some classification stuff")
additive = 1
trainDataPath = "data_/csv/centerclasses.csv"
end
local DATA_PATH = (opt.data ~= '' and opt.data or './data_/')
torch.setdefaulttensortype('torch.DoubleTensor')
local END = 12
for file in lfs.dir(trainDir) do
name = string.sub(file,1,END)
names[name] = file
end
for file in lfs.dir(testDir) do
-- print(file)
name = string.sub(file,1,END)
test_names[name] = file
end
count = 0
function resize(img)
-- print("image size", img:size())
-- image.save('images/original' ..count .. '.png', img)
modimg = img[{{},{100,480},{}}]
-- image.save('images/resized' ..count ..'.png', modimg)
-- print("image size", modimg:size())
--image.display(modimg)
count = count + 1
return image.scale(modimg,WIDTH,HEIGHT)
end
function hsv(img)
return image.rgb2hsv(img)
end
function colortransform(img)
-- print(opt.t)
if (opt.t == 'rgb') then
return img
elseif (opt.t == 'hsv') then
return image.rgb2hsv(img)
elseif (opt.t == 'yuv') then
return image.rgb2yuv(img)
else
return img
end
end
function norm(img)
--[[maxer = torch.max(img)
miner = torch.min(img)
new = img
new = new - torch.mean(new)
new = new/math.max(maxer,-1*miner)
-- print(torch.max(new))
-- print(torch.min(new))--]]
return img
end
function addNoise(original_image)
local rand_angle = (torch.uniform(-.2,.2))
local rand_position_x = (torch.randn(1)*20)[1]
local rand_position_y = (torch.randn(1)*20)[1]
image_data = original_image:clone()
image_data = image.rotate(image_data, rand_angle)
image_data = image.translate(image_data, rand_position_x, rand_position_y)
return image_data
end
function transformInput(inp)
f = tnt.transform.compose{
[1] = resize,
[2] = colortransform,
-- [3] = norm
}
if opt.noise or torch.random(0,1) < 1 then
inp = addNoise(inp)
end --]]
-- image.display(f(inp))
return f(inp)
end
function getTrainSample(dataset, idx)
r = dataset[idx]
-- print(r)
file = string.format("%19d.jpg", r[1])
name = string.sub(file,1,END)
--print(file,names[name],name)
return transformInput(image.load(DATA_PATH .. 'train_images_center/'..names[name]))
end
function getTrainLabel(dataset, idx)
-- return torch.LongTensor{dataset[idx][9] + 1}
-- print(dataset[idx][2])
return torch.DoubleTensor{opt.scale*dataset[idx][2] + additive }
end
function getTestSample(dataset, idx)
file = string.format("%19d.jpg", dataset[idx])
name = string.sub(file,1,END)
file_name = DATA_PATH .. "/test_center/" .. test_names[name]
return transformInput(image.load(file_name))
end
function getSampleId(dataset, idx)
file = string.format("%19d", dataset[idx])
chopped = string.sub(test_names[string.sub(file,1,12)], 1, 19)
return chopped
--return torch.LongTensor{tonumber(chopped)}
end