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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/VolumetricConvolution.c"
#else
static int nn_(VolumetricConvolution_updateOutput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
int dT = luaT_getfieldcheckint(L, 1, "dT");
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
THTensor *weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
THTensor *bias = luaT_getfieldcheckudata(L, 1, "bias", torch_Tensor);
THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);
luaL_argcheck(L, input->nDimension == 4 || input->nDimension == 5,
2, "4D or 5D (batch-mode) tensor expected");
int dimt = 1;
int dimh = 2;
int dimw = 3;
if (input->nDimension == 5) {
dimt++;
dimh++;
dimw++;
}
long nOutputPlane = weight->size[0];
long kT = weight->size[2];
long kH = weight->size[3];
long kW = weight->size[4];
long inputDepth = input->size[dimt];
long inputHeight = input->size[dimh];
long inputWidth = input->size[dimw];
long outputDepth = (inputDepth - kT) / dT + 1;
long outputWidth = (inputWidth - kW) / dW + 1;
long outputHeight = (inputHeight - kH) / dH + 1;
THTensor *outn = THTensor_(new)();
long i,j;
if (input->nDimension == 4) { /* non-batch mode */
THTensor_(resize4d)(output, nOutputPlane, outputDepth, outputHeight, outputWidth);
/* add bias */
for (i=0; i<bias->size[0]; i++) {
THTensor_(select)(outn,output,0,i);
THTensor_(fill)(outn, THTensor_(get1d)(bias, i));
}
/* do convolutions */
THTensor_(conv3Dmv)(output, 1.0, 1.0, input, weight, dT, dH, dW, "V", "X");
} else { /* batch mode */
long nBatch = input->size[0];
THTensor_(resize5d)(output, nBatch, nOutputPlane,
outputDepth, outputHeight, outputWidth);
THTensor *inb = THTensor_(new)();
THTensor *outb = THTensor_(new)();
for (j=0; j<nBatch; j++) { /* loop over batches */
THTensor_(select)(inb,input,0,j);
THTensor_(select)(outb,output,0,j);
/* add bias */
for (i=0; i<bias->size[0]; i++) {
THTensor_(select)(outn,outb,0,i);
THTensor_(fill)(outn, THTensor_(get1d)(bias, i));
}
/* do convolutions */
THTensor_(conv3Dmv)(outb, 1.0, 1.0, inb, weight, dT, dH, dW, "V", "X");
}
THTensor_(free)(inb);
THTensor_(free)(outb);
}
THTensor_(free)(outn);
return 1;
}
static int nn_(VolumetricConvolution_updateGradInput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
int dT = luaT_getfieldcheckint(L, 1, "dT");
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane");
THTensor *weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
THTensor *tweight;
luaL_argcheck(L, gradOutput->nDimension == 4 || gradOutput->nDimension == 5,
3, "4D or 5D (batch-mode) tensor expected");
int dimPlane = 0;
if (gradOutput->nDimension == 5) {
dimPlane++;
}
THArgCheck( nOutputPlane == gradOutput->size[dimPlane], 1,
"Number of output features is not equal to nOutputPlane" );
/* gradient to input */
tweight = THTensor_(newTranspose)(weight,0,1);
if (gradOutput->nDimension == 4) { /* non-batch mode */
THTensor_(conv3Dmv)(gradInput, 0.0, 1.0, gradOutput, tweight, dT, dH, dW, "F", "C");
} else { /* batch mode */
long nBatch = gradOutput->size[0];
THTensor *ginpb = THTensor_(new)();
THTensor *goutb = THTensor_(new)();
long j;
THTensor_(resize5d)(gradInput, input->size[0], input->size[1], input->size[2],
input->size[3], input->size[4]);
for (j=0; j<nBatch; j++) { /* loop over batches */
THTensor_(select)(ginpb,gradInput,0,j);
THTensor_(select)(goutb,gradOutput,0,j);
THTensor_(conv3Dmv)(ginpb, 0.0, 1.0, goutb, tweight, dT, dH, dW, "F", "C");
}
THTensor_(free)(ginpb);
THTensor_(free)(goutb);
}
THTensor_(free)(tweight);
return 1;
}
static int nn_(VolumetricConvolution_accGradParameters)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
real scale = luaL_optnumber(L, 4, 1);
int dT = luaT_getfieldcheckint(L, 1, "dT");
int dW = luaT_getfieldcheckint(L, 1, "dW");
int dH = luaT_getfieldcheckint(L, 1, "dH");
int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane");
THTensor *gradWeight = luaT_getfieldcheckudata(L, 1, "gradWeight", torch_Tensor);
THTensor *gradBias = luaT_getfieldcheckudata(L, 1, "gradBias", torch_Tensor);
long k;
real *gradBias_data;
THTensor* gradOutSlice;
int dimPlane = 0;
if (gradOutput->nDimension == 5) {
dimPlane++;
}
THArgCheck( nOutputPlane == gradOutput->size[dimPlane], 1,
"Number of output features is not equal to nOutputPlane" );
if (gradOutput->nDimension == 4) { /* non-batch mode */
/* gradient to bias */
gradBias_data = THTensor_(data)(gradBias);
gradOutSlice = THTensor_(new)();
for(k = 0; k < nOutputPlane; k++)
{
THTensor_(select)(gradOutSlice, gradOutput, 0, k);
gradBias_data[k] += scale*THTensor_(sumall)(gradOutSlice);
}
THTensor_(free)(gradOutSlice);
/* gradient to kernels */
THTensor_(conv3DRevger)(gradWeight, 1.0, scale, input, gradOutput, dT, dH, dW);
} else { /* batch mode */
long nBatch = gradOutput->size[0];
THTensor *inpb = THTensor_(new)();
THTensor *goutb = THTensor_(new)();
long j;
for (j=0; j<nBatch; j++) { /* loop over batches */
THTensor_(select)(inpb,input,0,j);
THTensor_(select)(goutb,gradOutput,0,j);
/* gradient to bias */
gradBias_data = THTensor_(data)(gradBias);
gradOutSlice = THTensor_(new)();
for(k = 0; k < nOutputPlane; k++)
{
THTensor_(select)(gradOutSlice, goutb, 0, k);
gradBias_data[k] += scale*THTensor_(sumall)(gradOutSlice);
}
THTensor_(free)(gradOutSlice);
/* gradient to kernels */
THTensor_(conv3DRevger)(gradWeight, 1.0, scale, inpb, goutb, dT, dH, dW);
}
THTensor_(free)(inpb);
THTensor_(free)(goutb);
}
return 0;
}
static const struct luaL_Reg nn_(VolumetricConvolution__) [] = {
{"VolumetricConvolution_updateOutput", nn_(VolumetricConvolution_updateOutput)},
{"VolumetricConvolution_updateGradInput", nn_(VolumetricConvolution_updateGradInput)},
{"VolumetricConvolution_accGradParameters", nn_(VolumetricConvolution_accGradParameters)},
{NULL, NULL}
};
static void nn_(VolumetricConvolution_init)(lua_State *L)
{
luaT_pushmetatable(L, torch_Tensor);
luaT_registeratname(L, nn_(VolumetricConvolution__), "nn");
lua_pop(L,1);
}
#endif
|