-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.lua
235 lines (186 loc) · 6.54 KB
/
dataset.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
-- This script loads the dataset: training data and test data
-- For each set, preprocessing, such as normalization, is adopted
-- Inspired by https://github.com/alykhantejani/siamese_network/blob/master/dataset.lua
require "paths"
require "torch"
massa = {}
massa.remote_path = "https://s3-ap-southeast-1.amazonaws.com/remotesensingdata/"
massa.root_folder = "data/massa.t7"
massa.trainset_path = paths.concat(massa.root_folder, "train.t7")
massa.testset_path = paths.concat(massa.root_folder, "test.t7")
------------------------------------------------------------
-- Download the dataset
------------------------------------------------------------
function massa.download (dataset)
if not paths.filep(massa.trainset_path) or not paths.filep(massa.testset_path) then
local tarfile = paths.basename(massa.remote_path)
-- download the dataset file, untar it and then remove it
os.execute("wget " .. massa.remote_path .. "; " .. "tar xvf " .. tarfile .. "; rm " .. tarfile)
end
end
------------------------------------------------------------
------------------------------------------------------------
-- Normalize the dataset
------------------------------------------------------------
function massa.load_normalized_dataset (filename, mean_, std_)
local file = torch.load(filename, "ascii")
local dataset = {}
dataset.data = file.data:type(torch.getdefaulttensortype())
dataset.labels = file.labels
local std = std_ or dataset.data:std()
local mean = mean_ or dataset.data:mean()
dataset.data:add(-mean)
dataset.data:mul(1.0/std)
dataset.std = std
dataset.mean = mean
function dataset:size()
return dataset.data:size(1)
end
local class_count = 0
local classes = {}
for i = 1, dataset.labels:size(1) do
if classes[dataset.labels[i]] == nil then
class_count = class_count + 1
table.insert(classes, dataset.labels[i])
end
end
dataset.class_count = class_count
-- The dataset has to be indexable by the [] operator so this next bit handles that
setmetatable(dataset, {__index = function(self, index)
local input = self.data[index]
local label_vector = self.labels[index]
local example = {input, label_vector}
return example
end })
return dataset
end
------------------------------------------------------------
------------------------------------------------------------
-- Load the dataset subset
------------------------------------------------------------
function massa.load_siamese_dataset_subset (filename, subset_size)
local file = torch.load(filename)
-- data structure: {
-- diffNorm: 20
-- imSrc: 368x3x300x300
-- imTar: 368x3x300x300
-- labels: 368x8
-- }
local all_dataSrc = file.imSrc
local all_dataTar = file.imTar
local all_labels = file.labels
local sizeData = all_dataSrc:size()[1]
if subset_size > 0 then
sizeData = subset_size
print("Use the subset of size: " .. sizeData)
end
--[[
local dataSrc = torch.Tensor(sizeData, all_dataSrc:size()[2], all_dataSrc:size()[3], all_dataSrc:size()[4])
local dataTar = torch.Tensor(sizeData, all_dataSrc:size()[2], all_dataSrc:size()[3], all_dataSrc:size()[4])
local labels = torch.Tensor(sizeData, all_labels:size()[2])
for i = 1, sizeData do
dataSrc[i] = all_dataSrc[i]:double():mul(1.0/255.0)
dataTar[i] = all_dataTar[i]:double():mul(1.0/255.0)
labels[i] = all_labels[i]
end
--]]
local mean1 = torch.DoubleTensor(3, 300, 300)
local mean2 = torch.DoubleTensor(3, 300, 300)
local std = 0
for i = 1, sizeData do
local src_tmp = all_dataSrc[i]:double():mul(1.0/255.0)
local tar_tmp = all_dataTar[i]:double():mul(1.0/255.0)
mean1 = mean1 + src_tmp + tar_tmp
mean2 = mean2 + torch.cmul(src_tmp, src_tmp) + torch.cmul(tar_tmp, tar_tmp)
end
mean1:mul(1.0/(sizeData*2))
mean2:mul(1.0/(sizeData*2))
std = torch.sqrt(mean2 - torch.cmul(mean1, mean1))
--[[
print("STD")
print(std:mean())
print("MEAN")
print(mean1:mean())
print(mean1:type())
print(mean1)
--]]
-- now we make the pairs (tensor of size (x, 2, 3, 300, 300) for training data)
paired_data = torch.ByteTensor(sizeData, 2, all_dataSrc:size(2), all_dataSrc:size(3), all_dataSrc:size(4))
paired_data_labels = torch.FloatTensor(sizeData, all_labels:size(2))
index = 1
for i = 1, sizeData do -- 2 for paired data
--[[
paired_data[i][1] = dataSrc[i]:clone()
paired_data[i][2] = dataTar[i]:clone()
--]]
paired_data[i][1] = all_dataSrc[i]
paired_data[i][2] = all_dataTar[i]
paired_data_labels[i] = all_labels[i]
end
local dataset = {}
dataset.data = paired_data
--dataset.data:add(-mean)
--dataset.data:mul(1.0/std)
dataset.labels = paired_data_labels
dataset.std = std:mean()
dataset.mean = mean1:mean()
--dataset.data = dataset.data:float()
--dataset.labels = dataset.labels:float()
function dataset:size()
return dataset.data:size(1)
end
-- The dataset has to be indexable by the [] operator so this next bit handles that
setmetatable(dataset, {__index = function (self, index)
local input = self.data[index]
local label = self.labels[index]
local example = {input, label}
return example
end })
return dataset
end
------------------------------------------------------------
------------------------------------------------------------
-- Load the whole dataset
------------------------------------------------------------
function massa.load_siamese_dataset (filename, subset_size)
return massa.load_siamese_dataset_subset(filename, subset_size)
end
------------------------------------------------------------
--[[
if not paths.dirp("data") then
os.execute("mkdir -p " .. train_dir)
os.execute("cd " .. train_dir)
print(sys.COLORS.red .. "<data> downloading dataset")
--os.execute("wget " .. www .. img_file)
--os.execute("tar -xvf " .. img_file)
else
print(sys.COLORS.red .. "<data> using the existing dataset")
end
------------------------------------------------------------
-- load or generate new dataset:
if paths.filep("train.t7") and paths.filep("test.t7") then
print(sys.COLORS.red .. "<data> loading previously generated dataset:")
trainData = torch.load("train.t7")
testData = torch.load("test.t7")
trSize = trainData:size(1)
teSize = testData:size(1)
else
print(sys.COLORS.red .. "<data> creating a new dataset from files:")
-- load files in directory
require "paths"
currentPath = paths.cwd() -- Current Working Directory
local trainDir = "./imgs"
files = {}
for file in paths.files(trainDir) do
if file:find("jpg" .. "$") then
table.insert(files, paths.concat(trainDir, file))
end
end
local trainImgNumber = #files
-- sort file names
table.sort(files, function(a, b) return a < b end)
print("Found files:")
print(files)
-- load images
end
--]]