-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathImageScaler.lua
48 lines (40 loc) · 1.57 KB
/
ImageScaler.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
local ImageScaler, parent = torch.class('nn.ImageScaler', 'nn.Module')
require 'nnx'
--local cv = require 'cv'
--require 'cv.cudawarping' -- cv.imgproc on CPU, cv.cudawarping on GPU
function ImageScaler:__init(rows,cols)
parent.__init(self)
self.rows = rows
self.cols = cols
self._input = torch.Tensor()
self._gradOutput = torch.Tensor()
end
function ImageScaler:updateOutput(input)
-- ~580 examples/second
resizer = nn.SpatialReSampling{owidth=self.cols,oheight=self.rows}
self.output = resizer:forward(input:float())
return self.output:cuda()
-- ~400 examples/second
-- out = torch.Tensor(input:size(1),input:size(2),self.rows,self.cols):cuda()
-- for b=1,input:size(1) do
-- -- opencv requires images in [h,w,c] instead of [b,c,h,w] like Torch
-- x = input[{b,{},{},{}}]:permute(2,3,1)
-- if x:size(3)==1 then -- bug where opencv resize() squeezes singleton dimensions
-- y0 = cv.cuda.resize{x,{self.rows,self.cols}}
-- -- copy lets you add singleton dimension but doubles memory
-- y = torch.Tensor(self.rows,self.cols,1):copy(y0)
-- else
-- y = cv.cuda.resize{x,{self.rows,self.cols,x:size(1)}}
-- end
-- out[{b,{},{},{}}] = y:permute(3,1,2)
-- end
-- self.output = out
-- return self.output
end
function ImageScaler:updateGradInput(input, gradOutput)
resizer = nn.SpatialReSampling{owidth=self.cols,oheight=self.rows}
self.gradInput = resizer:updateGradInput(input:float(), gradOutput:float())
return self.gradInput:cuda()
--self.gradInput = torch.Tensor(input:size()):zeros()
--return self.gradInput
end