Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VolumetricMaxPooling.c « generic - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20f970155471d1757b5a9498ae74cdeae9dcee61 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/VolumetricMaxPooling.c"
#else

static void nn_(VolumetricMaxPooling_updateOutput_frame)(real *input_p, real *output_p,
							 real *indx_p, real *indy_p, real *indz_p,
							 long nslices,
							 long itime, long iwidth, long iheight,
							 long otime, long owidth, long oheight,
							 int kT, int kW, int kH, int dT, int dW, int dH)
{
  long k;
#pragma omp parallel for private(k)
  for (k = 0; k < nslices; k++)
  {
    /* loop over output */
    long i, j, ti;
    for(ti = 0; ti < otime; ti++)
    {
      for(i = 0; i < oheight; i++)
      {
	for(j = 0; j < owidth; j++)
	{
	  /* local pointers */
	  real *ip = input_p   + k*itime*iwidth*iheight + ti*iwidth*iheight*dT +  i*iwidth*dH + j*dW;
	  real *op = output_p  + k*otime*owidth*oheight + ti*owidth*oheight + i*owidth + j;
	  real *indzp = indz_p + k*otime*owidth*oheight + ti*owidth*oheight + i*owidth + j;
	  real *indyp = indy_p + k*otime*owidth*oheight + ti*owidth*oheight + i*owidth + j;
	  real *indxp = indx_p + k*otime*owidth*oheight + ti*owidth*oheight + i*owidth + j;
	  
	  /* compute local max: */
	  real maxval = -THInf;
	  int x,y,z;

	  *indzp = -1;
	  *indyp = -1;
	  *indxp = -1;
	  for(z=0; z < kT; z++)
	  {
	    for(y = 0; y < kH; y++)
	    {
	      for(x = 0; x < kW; x++)
	      {
		real val = *(ip + z*iwidth*iheight + y*iwidth + x);
		if (val > maxval)
		{
		  maxval = val;
		  *indzp = z+1;
		  *indyp = y+1;
		  *indxp = x+1;
		}
	      }
	    }
	  }
	  /* set output to local max */
	  *op = maxval;
	  
	  /* store location of max (x,y) */
	  /**indyp = (int)(maxindex / kW)+1;*/
	  /**indxp = (maxindex % kW) +1;*/
	}
      }
    }
  }
}

static int nn_(VolumetricMaxPooling_updateOutput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  int kT = luaT_getfieldcheckint(L, 1, "kT");
  int kW = luaT_getfieldcheckint(L, 1, "kW");
  int kH = luaT_getfieldcheckint(L, 1, "kH");
  int dT = luaT_getfieldcheckint(L, 1, "dT");
  int dW = luaT_getfieldcheckint(L, 1, "dW");
  int dH = luaT_getfieldcheckint(L, 1, "dH");
  THTensor *indices = luaT_getfieldcheckudata(L, 1, "indices", torch_Tensor);
  THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);
  long nslices;
  long itime;
  long iheight;
  long iwidth;
  long otime;
  long oheight;
  long owidth;
  real *input_data;
  real *output_data;
  real *indices_data;


  luaL_argcheck(L, input->nDimension == 4 , 2, "4D tensor expected");
  luaL_argcheck(L, input->size[3] >= kW && input->size[2] >= kH && input->size[1] >= kT, 2, "input image smaller than kernel size");

  /* sizes */
  nslices = input->size[0];
  itime = input->size[1];
  iheight = input->size[2];
  iwidth = input->size[3];
  otime = (itime - kT) / dT + 1;
  oheight = (iheight - kH) / dH + 1;
  owidth = (iwidth - kW) / dW + 1;

  /* get contiguous input */
  input = THTensor_(newContiguous)(input);

  /* resize output */
  THTensor_(resize4d)(output, nslices, otime, oheight, owidth);
  /* indices will contain ti,i,j locations for each output point */
  THTensor_(resize5d)(indices, 3, nslices, otime, oheight, owidth);
  
  input_data = THTensor_(data)(input);
  output_data = THTensor_(data)(output);
  indices_data = THTensor_(data)(indices);
  
  nn_(VolumetricMaxPooling_updateOutput_frame)(input_data, output_data,
					       indices_data+nslices*otime*owidth*oheight*2, 
					       indices_data+nslices*otime*owidth*oheight, 
					       indices_data,
					       nslices,
					       itime, iwidth, iheight,
					       otime, owidth, oheight,
					       kT, kW, kH, dT, dW, dH);
  /* cleanup */
  THTensor_(free)(input);
  return 1;
}

static void nn_(VolumetricMaxPooling_updateGradInput_frame)(real *gradInput_p, real *gradOutput_p,
							    real *indx_p, real *indy_p, real *indz_p,
							    long nslices,
							    long itime, long iwidth, long iheight,
							    long otime, long owidth, long oheight,
							    int dT, int dW, int dH)
{
  long k;
#pragma omp parallel for private(k)
  for (k = 0; k < nslices; k++)
  {
    real *gradInput_p_k = gradInput_p + k*itime*iwidth*iheight;
    real *gradOutput_p_k = gradOutput_p + k*otime*owidth*oheight;
    real *indx_p_k = indx_p + k*otime*owidth*oheight;
    real *indy_p_k = indy_p + k*otime*owidth*oheight;
    real *indz_p_k = indz_p + k*otime*owidth*oheight;

    /* calculate max points */
    long ti, i, j;
    for(ti = 0; ti < otime; ti++)
    {
      for(i = 0; i < oheight; i++)
      {
	for(j = 0; j < owidth; j++)
	{
	  /* retrieve position of max */
	  long maxti = indz_p_k[ti*oheight*owidth + i*owidth + j] - 1 + ti*dT;
	  long maxi  = indy_p_k[ti*oheight*owidth + i*owidth + j] - 1 + i*dH;
	  long maxj  = indx_p_k[ti*oheight*owidth + i*owidth + j] - 1 + j*dW;
	  
	  /* update gradient */
	  gradInput_p_k[maxti*iheight*iwidth + maxi*iwidth + maxj] += gradOutput_p_k[ti*oheight*owidth + i*owidth + j];
	}
      }
    }
  }
}

static int nn_(VolumetricMaxPooling_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");
  THTensor *indices = luaT_getfieldcheckudata(L, 1, "indices", torch_Tensor);
  THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
  int nslices;
  int itime;
  int iheight;
  int iwidth;
  int otime;
  int oheight;
  int owidth;
  real *gradInput_data;
  real *gradOutput_data;
  real *indices_data;

  /* get contiguous gradOutput */
  gradOutput = THTensor_(newContiguous)(gradOutput);

  /* resize */
  THTensor_(resizeAs)(gradInput, input);
  THTensor_(zero)(gradInput);

  /* sizes */
  nslices = input->size[0];
  itime = input->size[1];
  iheight = input->size[2];
  iwidth = input->size[3];
  otime = gradOutput->size[1];
  oheight = gradOutput->size[2];
  owidth = gradOutput->size[3];

  /* get raw pointers */
  gradInput_data = THTensor_(data)(gradInput);
  gradOutput_data = THTensor_(data)(gradOutput);
  indices_data = THTensor_(data)(indices);

  /* backprop */
  nn_(VolumetricMaxPooling_updateGradInput_frame)(gradInput_data, gradOutput_data,
						  indices_data+nslices*otime*owidth*oheight*2, 
						  indices_data+nslices*otime*owidth*oheight, 
						  indices_data,
						  nslices,
						  itime, iwidth, iheight,
						  otime, owidth, oheight,
						  dT, dW, dH);

  /* cleanup */
  THTensor_(free)(gradOutput);
  return 1;
}

static const struct luaL_Reg nn_(VolumetricMaxPooling__) [] = {
  {"VolumetricMaxPooling_updateOutput", nn_(VolumetricMaxPooling_updateOutput)},
  {"VolumetricMaxPooling_updateGradInput", nn_(VolumetricMaxPooling_updateGradInput)},
  {NULL, NULL}
};

static void nn_(VolumetricMaxPooling_init)(lua_State *L)
{
  luaT_pushmetatable(L, torch_Tensor);
  luaT_registeratname(L, nn_(VolumetricMaxPooling__), "nn");
  lua_pop(L,1);
}

#endif