-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathDenseTransformer2D.lua
More file actions
executable file
·53 lines (41 loc) · 1.66 KB
/
DenseTransformer2D.lua
File metadata and controls
executable file
·53 lines (41 loc) · 1.66 KB
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
require 'nn'
local AGGOF, parent = torch.class('nn.AffineGridGeneratorOpticalFlow2D', 'nn.Module')
--[[
AffineGridGeneratorOpticalFlow(height, width) :
AffineGridGeneratorOpticalFlow:updateOutput(transformMap)
AffineGridGeneratorOpticalFlow:updateGradInput(transformMap, gradGrids)
AffineGridGeneratorOpticalFlow will take height x width x 2x3 an affine transform map (homogeneous
coordinates) as input, and output a grid, in normalized coordinates* that, once used
with the Bilinear Sampler, will generate the next frame in the sequence according to the optical
flow transform map.
*: normalized coordinates [-1,1] correspond to the boundaries of the input image.
]]
function AGGOF:__init(height, width)
parent.__init(self)
assert(height > 1)
assert(width > 1)
self.height = height
self.width = width
self.baseGrid = torch.Tensor(2, height, width)
for i=1,self.height do
self.baseGrid:select(1,1):select(1,i):fill(-1 + (i-1)/(self.height-1) * 2)
end
for j=1,self.width do
self.baseGrid:select(1,2):select(2,j):fill(-1 + (j-1)/(self.width-1) * 2)
end
--self.baseGrid:select(1,3):fill(1)
end
function AGGOF:updateOutput(transformMap)
assert(transformMap:nDimension()==3
and transformMap:size(1)== 2
, 'please input a valid transform map ')
-- need to scale the transformMap
self.output:resize(2, self.height, self.width):zero()
self.output = torch.add(self.baseGrid,transformMap)
return self.output
end
function AGGOF:updateGradInput(transformMap, gradGrid)
self.gradInput:resizeAs(transformMap):zero()
self.gradInput:copy(gradGrid)
return self.gradInput
end