-
Notifications
You must be signed in to change notification settings - Fork 40
/
ImageTransformer.lua
39 lines (35 loc) · 795 Bytes
/
ImageTransformer.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
local ImageTransformer = torch.class('nnf.ImageTransformer')
function ImageTransformer:__init(...)
self.mean_pix = {128/255,128/255,128/255}
self.raw_scale = 1
self.swap = nil
local args = ...
for k,v in pairs(args) do self[k] = v end
end
local function channel_swap(I,swap)
if not swap then
return I:clone()
end
local out = I.new():resizeAs(I)
for i=1,I:size(1) do
out[i] = I[swap[i] ]
end
return out
end
function ImageTransformer:preprocess(I)
local I = I
if I:dim() == 2 then
I = I:view(1,I:size(1),I:size(2))
end
if I:size(1) == 1 then
I = I:expand(3,I:size(2),I:size(3))
end
I = channel_swap(I,self.swap)
if self.raw_scale ~= 1 then
I:mul(self.raw_scale)
end
for i=1,3 do
I[i]:add(-self.mean_pix[i])
end
return I
end