-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_read.lua
234 lines (205 loc) · 7.32 KB
/
data_read.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require 'pl'
require 'image'
stringx.import()
local Dataset = torch.class('Dataset')
function load_txt(filename,image_dir,zero_as_minus_one)
local data = {}
local txt = io.open(filename)
for line in txt:lines() do
line = line:split(' ')
local instance = {}
instance.fileloc = image_dir..line[1]
instance.class = line[2]
instance.crop = {lu = {x = tonumber(line[3]), y = tonumber(line[4]) },
rl = {x = tonumber(line[5]), y = tonumber(line[6]) } }
instance.attribute = {}
for i=7,70 do
instance.attribute[i-6] = tonumber(line[i])
if( zero_as_minus_one and instance.attribute[i-6] == 0) then
instance.attribute[i-6] = -1
end
end
data[#data+1] = instance
end
txt:close()
return data
end
function Dataset:__init(path,image_dir,test_dir,mean,std,batch_size,loss_function)
--local zero_as_minus_one = loss_function == 'margin'
self.train_data = load_txt(path..'apascal_train1.txt',image_dir, zero_as_minus_one )
self.val_data = load_txt(path..'apascal_test.txt',image_dir, zero_as_minus_one )
self.test_data = load_txt(path..'ayahoo_test.txt',test_dir, zero_as_minus_one )
self.mean = mean
self.std = std
self.batch_size = batch_size
self.loss_function = loss_function
end
function Dataset:get_image_attribute(data,index)
if( #data < index ) then
print 'index error'
end
local channels = {'r','g','b'}
local mean = {0.411,0.3812,0.3574}
std = {0.2688,0.2582,0.2612}
instance = data[index]
if( instance.preprocessed == nil ) then
--if( instance.resized == nil ) then
local im = image.load(instance.fileloc)
local cropped_image = image.crop(im, instance.crop.lu.x, instance.crop.lu.y, instance.crop.rl.x, instance.crop.rl.y )
local resized = image.scale(cropped_image,224,224)
for i, channel in ipairs(channels) do
resized[{ i,{},{} }]:add(-mean[i])
resized[{ i,{},{} }]:div(std[i])
end
local preprocessed = resized
instance.preprocessed = torch.Tensor(3,224,224):zero()
instance.preprocessed:copy(preprocessed)
end
return instance.preprocessed, instance.attribute,instance.class
end
function Dataset:get_image_attribute_test(data,index)
if( #data < index ) then
print 'index error'
end
local channels = {'r','g','b'}
local mean = {0.411,0.3812,0.3574}
local mean1 = {103.939,116.779,123.68}
std = {0.2688,0.2582,0.2612}
instance = data[index]
if( instance.preprocessed == nil ) then
--if( instance.resized == nil ) then
local im = image.load(instance.fileloc)
local cropped_image = image.crop(im, instance.crop.lu.x, instance.crop.lu.y, instance.crop.rl.x, instance.crop.rl.y )
local resized = image.scale(cropped_image,224,224)
for i, channel in ipairs(channels) do
resized[{ i,{},{} }]:add(-mean[i])
resized[{ i,{},{} }]:div(std[i])
end
local preprocessed = resized
instance.preprocessed = torch.Tensor(3,224,224):zero()
instance.preprocessed:copy(preprocessed)
end
--print (instance.attribute)
return instance.preprocessed, instance.attribute,instance.class
end
function Dataset:size()
return #self.train_data, #self.val_data,#self.test_data
end
function Dataset:get_samples(train_or_val)
local inputs = torch.Tensor(self.batch_size, 3, 224, 224):zero()
local classes ={}
local class1 = torch.Tensor(self.batch_size):zero()
local labels = {}
for i = 1,64 do
labels[i] = torch.Tensor(self.batch_size):zero()
end
local data
if( train_or_val == 'train' ) then
data = self.train_data
else
data = self.val_data
end
for i = 1,self.batch_size do
local im, attr,class = self:get_image_attribute(data, math.floor(math.random() * #data) + 1 )
class1[{i}]= class
inputs:select(1,i):copy( im )
for label_i = 1,64 do
labels[label_i][i] = attr[label_i]
end
end
return inputs, labels,class1
end
function Dataset:get_test_samples(index)
local inputs = torch.Tensor(1, 3, 224, 224):zero()
local labels = {}
for i = 1,64 do
labels[i] = torch.Tensor(1):zero()
end
local data = self.test_data
local im, attr = self:get_image_attribute_test(data,index)
inputs:select(1,i):copy( im )
for label_i = 1,64 do
labels[label_i][1] = attr[label_i]
end
return inputs, labels
end
function Dataset:get_val_samples(index)
local inputs = torch.Tensor(1, 3, 224, 224):zero()
local labels = {}
for i = 1,64 do
labels[i] = torch.Tensor(1):zero()
end
local data = self.val_data
local im, attr = self:get_image_attribute(data, index)
inputs:select(1,i):copy( im )
for label_i = 1,64 do
labels[label_i][1] = attr[label_i]
end
return inputs, labels
end
function Dataset:get_train_sample_co()
local co = coroutine.create( function(this)
for i = 1, #this.train_data, this.batch_size do
local remain = math.min(this.batch_size, #this.train_data-i)
local inputs = torch.Tensor(remain, 3, 224, 224):zero()
local labels = {}
for label_i = 1,64 do
labels[label_i] = torch.Tensor(remain):zero()
end
for j = 1,remain do
local im, attr = this:get_image_attribute(this.train_data, i+j-1)
inputs:select(1,j):copy(im)
for label_i = 1,64 do
labels[label_i][j] = attr[label_i]
end
end
coroutine.yield( inputs, labels )
end
end
)
return co
end
function Dataset:get_valid_sample_co()
local co = coroutine.create( function(this)
for i = 1, #this.val_data, this.batch_size do
local remain = math.min(this.batch_size, #this.val_data-i)
local inputs = torch.Tensor(remain, 3, 224, 224):zero()
local labels = {}
for label_i = 1,64 do
labels[label_i] = torch.Tensor(remain):zero()
end
for j = 1,remain do
local im, attr,class = this:get_image_attribute(this.val_data, i+j-1)
inputs:select(1,j):copy(im)
for label_i = 1,64 do
labels[label_i][j] = attr[label_i]
end
end
coroutine.yield( inputs, labels )
end
end
)
return co
end
function Dataset:get_test_sample_co()
local co = coroutine.create( function(this)
for i = 1, #this.test_data, this.batch_size do
local remain = math.min(this.batch_size, #this.test_data-i)
local inputs = torch.Tensor(remain, 3, 224, 224):zero()
local labels = {}
for label_i = 1,64 do
labels[label_i] = torch.Tensor(remain):zero()
end
for j = 1,remain do
local im, attr = this:get_image_attribute(this.test_data, i+j-1)
inputs:select(1,j):copy(im)
for label_i = 1,64 do
labels[label_i][j] = attr[label_i]
end
end
coroutine.yield( inputs, labels )
end
end
)
return co
end