1
1
local SpatialUniformCrop , parent = torch .class (" nn.SpatialUniformCrop" , " nn.Module" )
2
2
3
3
function SpatialUniformCrop :__init (oheight , owidth , scale )
4
+ dpnn .require (' nnx' )
4
5
parent .__init (self )
5
6
self .scale = scale or nil
6
7
if self .scale ~= nil then
@@ -12,8 +13,9 @@ function SpatialUniformCrop:__init(oheight, owidth, scale)
12
13
end
13
14
14
15
function SpatialUniformCrop :updateOutput (input )
16
+ dpnn .require (' nnx' )
15
17
input = self :toBatch (input , 3 )
16
-
18
+
17
19
self .output :resize (input :size (1 ), input :size (2 ), self .oheight , self .owidth )
18
20
self .coord = self .coord or torch .IntTensor ()
19
21
self .coord :resize (input :size (1 ), 2 )
@@ -22,7 +24,7 @@ function SpatialUniformCrop:updateOutput(input)
22
24
self .scales = self .scales or torch .FloatTensor ()
23
25
self .scales :resize (input :size (1 ))
24
26
end
25
-
27
+
26
28
local iH , iW = input :size (3 ), input :size (4 )
27
29
if self .train ~= false then
28
30
if self .scale ~= nil then
@@ -34,7 +36,7 @@ function SpatialUniformCrop:updateOutput(input)
34
36
35
37
local h = math.ceil (torch .uniform (1e-2 , iH - soheight ))
36
38
local w = math.ceil (torch .uniform (1e-2 , iW - sowidth ))
37
-
39
+
38
40
local ch = math.ceil (iH / 2 - (iH - soheight )/ 2 + h )
39
41
local cw = math.ceil (iW / 2 - (iH - sowidth )/ 2 + w )
40
42
@@ -70,15 +72,15 @@ function SpatialUniformCrop:updateOutput(input)
70
72
local crop = input :narrow (3 ,h1 ,self .oheight ):narrow (4 ,w1 ,self .owidth )
71
73
self .output :copy (crop )
72
74
end
73
-
75
+
74
76
self .output = self :fromBatch (self .output , 1 )
75
77
return self .output
76
78
end
77
79
78
80
function SpatialUniformCrop :updateGradInput (input , gradOutput )
79
81
input = self :toBatch (input , 3 )
80
82
gradOutput = self :toBatch (gradOutput , 3 )
81
-
83
+
82
84
self .gradInput :resizeAs (input ):zero ()
83
85
if self .scale ~= nil then
84
86
local iH , iW = input :size (3 ), input :size (4 )
@@ -88,7 +90,7 @@ function SpatialUniformCrop:updateGradInput(input, gradOutput)
88
90
local sowidth = math.ceil (s * self .owidth )
89
91
90
92
local h , w = self .coord [{i ,1 }], self .coord [{i ,2 }]
91
-
93
+
92
94
local ch = math.ceil (iH / 2 - (iH - soheight )/ 2 + h )
93
95
local cw = math.ceil (iW / 2 - (iH - sowidth )/ 2 + w )
94
96
@@ -108,7 +110,7 @@ function SpatialUniformCrop:updateGradInput(input, gradOutput)
108
110
self .gradInput [i ]:narrow (2 ,h1 ,self .oheight ):narrow (3 ,w1 ,self .owidth ):copy (gradOutput [i ])
109
111
end
110
112
end
111
-
113
+
112
114
self .gradInput = self :fromBatch (self .gradInput , 1 )
113
115
return self .gradInput
114
116
end
0 commit comments