-
Notifications
You must be signed in to change notification settings - Fork 65
/
dataset.lua
172 lines (141 loc) · 4.8 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
require 'torch'
require 'image'
require 'paths'
local dataset = {}
-- load data from these directories
dataset.dirs = {}
-- load only images with these file extensions
dataset.fileExtension = ""
-- expected original height/width of images
dataset.originalHeight = 64
dataset.originalWidth = 64
-- desired height/width of images
dataset.height = 32
dataset.width = 32
-- desired channels of images (1=grayscale, 3=color)
--dataset.nbChannels = 3
dataset.colorSpace = "rgb"
-- cache for filepaths to all images
dataset.paths = nil
-- Set directories to load images from
-- @param dirs List of paths to directories
function dataset.setDirs(dirs)
dataset.dirs = dirs
end
-- Set file extension that images to load must have
-- @param fileExtension the file extension of the images
function dataset.setFileExtension(fileExtension)
dataset.fileExtension = fileExtension
end
-- Desired height of the images (will be resized if necessary)
-- @param scale The height of the images
function dataset.setHeight(height)
dataset.height = height
end
-- Desired height of the images (will be resized if necessary)
-- @param scale The height of the images
function dataset.setWidth(width)
dataset.width = width
end
-- Set desired number of channels for the images (1=grayscale, 3=color)
-- @param nbChannels The number of channels
function dataset.setNbChannels(nbChannels)
dataset.nbChannels = nbChannels
end
-- Loads the paths of all images in the defined files
-- (with defined file extensions)
function dataset.loadPaths()
local files = {}
local dirs = dataset.dirs
local ext = dataset.fileExtension
for i=1, #dirs do
local dir = dirs[i]
-- Go over all files in directory. We use an iterator, paths.files().
for file in paths.files(dir) do
-- We only load files that match the extension
if file:find(ext .. '$') then
-- and insert the ones we care about in our table
table.insert(files, paths.concat(dir,file))
end
end
-- sort for reproduceability
table.sort(files, function (a,b) return a < b end)
-- Check files
if #files == 0 then
error('given directory doesnt contain any files of type: ' .. ext)
end
end
dataset.paths = files
end
-- Load images from the dataset.
-- @param startAt Number of the first image.
-- @param count Count of the images to load.
-- @return Table of images. You can call :size() on that table to get the number of loaded images.
function dataset.loadImages(startAt, count)
local endBefore = startAt + count
if dataset.paths == nil then
dataset.loadPaths()
end
local N = math.min(count, #dataset.paths)
local images = torch.FloatTensor(N, 3, dataset.height, dataset.width)
for i=1,N do
local img = image.load(dataset.paths[i], dataset.nbChannels, "float")
img = image.scale(img, dataset.width, dataset.height)
images[i] = img
end
images = NN_UTILS.rgbToColorSpace(images, dataset.colorSpace)
local result = {}
result.data = images
function result:size()
return N
end
setmetatable(result, {
__index = function(self, index) return self.data[index] end,
__len = function(self) return self.data:size(1) end
})
return result
end
-- Loads a defined number of randomly selected images from
-- the cached paths (cached in loadPaths()).
-- @param count Number of random images.
-- @return List of Tensors
function dataset.loadRandomImages(count)
local images = dataset.loadRandomImagesFromPaths(count)
local data = torch.FloatTensor(#images, 3, dataset.height, dataset.width)
for i=1, #images do
data[i] = image.scale(images[i], dataset.width, dataset.height)
end
data = NN_UTILS.rgbToColorSpace(data, dataset.colorSpace)
local N = data:size(1)
local result = {}
result.scaled = data
function result:size()
return N
end
function result:normalize(mean, std)
mean, std = NN_UTILS.normalize(result.scaled, mean, std)
return mean, std
end
setmetatable(result, {
__index = function(self, index) return self.scaled[index] end,
__len = function(self) return self.scaled:size(1) end
})
return result
end
-- Loads randomly selected images from the cached paths.
-- TODO: merge with loadRandomImages()
-- @param count Number of images to load
-- @returns List of Tensors
function dataset.loadRandomImagesFromPaths(count)
if dataset.paths == nil then
dataset.loadPaths()
end
local shuffle = torch.randperm(#dataset.paths)
local images = {}
for i=1,math.min(shuffle:size(1), count) do
-- load each image
table.insert(images, image.load(dataset.paths[shuffle[i]], 3, "float"))
end
return images
end
return dataset