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

SpatialMaxPooling.c « generic - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca21ce5a56a5364b70d618691ce8051ac5cf7d18 (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
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/SpatialMaxPooling.c"
#else

static int nn_(SpatialMaxPooling_updateOutput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  int kW = luaT_getfieldcheckint(L, 1, "kW");
  int kH = luaT_getfieldcheckint(L, 1, "kH");
  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);

  luaL_argcheck(L, input->nDimension == 3 || input->nDimension == 4 , 2, "3D or 4D (batch mode) tensor expected");
  int dimw = 2;
  int dimh = 1;
  long nbatch = 1;
  if (input->nDimension == 4) 
  {
    nbatch = input->size[0];
    dimw++;
    dimh++;
  }
  luaL_argcheck(L, input->size[dimw] >= kW && input->size[dimh] >= kH, 2, "input image smaller than kernel size");

  // sizes
  long nslices = input->size[dimh-1];
  long iheight = input->size[dimh];
  long iwidth = input->size[dimw];
  long oheight = (iheight - kH) / dH + 1;
  long owidth = (iwidth - kW) / dW + 1;

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

  // resize output
  if (input->nDimension == 3)
  {
    THTensor_(resize3d)(output, nslices, oheight, owidth);
    // indices will contain i,j locatyions for each output point
    THTensor_(resize4d)(indices, 2, nslices, oheight, owidth);
  }
  else
  {
    THTensor_(resize4d)(output, nbatch, nslices, oheight, owidth);
    // indices will contain i,j locatyions for each output point
    THTensor_(resize5d)(indices, 2, nbatch, nslices, oheight, owidth);
  }


  // get raw pointers
  real *input_data = THTensor_(data)(input);
  real *output_data = THTensor_(data)(output);
  real *indices_data = THTensor_(data)(indices);

  // compute max pooling for each input slice
  long k;
#pragma omp parallel for private(k)
  for (k = 0; k < nslices; k++)
  {
    long p;
    for (p = 0; p < nbatch; p++)
    {
      // pointers to slices
      real *input_p = input_data + p*nslices*iwidth*iheight + k*iwidth*iheight;
      real *output_p = output_data + p*nslices*owidth*oheight + k*owidth*oheight;
      real *indy_p = indices_data + p*nslices*owidth*oheight + k*owidth*oheight;
      real *indx_p = indices_data + (p+nbatch)*nslices*owidth*oheight + k*owidth*oheight;
      
      // loop over output
      int i,j;
      for(i = 0; i < oheight; i++) {
	for(j = 0; j < owidth; j++) {
	  // local pointers
	  real *ip = input_p + i*iwidth*dH + j*dW;
	  real *op = output_p + i*owidth + j;
	  real *indyp = indy_p + i*owidth + j;
	  real *indxp = indx_p + i*owidth + j;
	  
	  // compute local max:
	  long maxindex = -1;
	  real maxval = -THInf;
	  long tcntr = 0;
	  int x,y;
	  for(y = 0; y < kH; y++) {
	    for(x = 0; x < kW; x++) {
	      real val = *(ip + y*iwidth + x);
	      if (val > maxval) {
		maxval = val;
		maxindex = tcntr;
	      }
	      tcntr++;
	    }
	  }

	  // set output to local max
	  *op = maxval;
	  
	  // store location of max (x,y)
	  *indyp = (int)(maxindex / kW)+1;
	  *indxp = (maxindex % kW) +1;
	}
      }
    }
  }
  // cleanup
  THTensor_(free)(input);

  return 1;
}

static int nn_(SpatialMaxPooling_updateGradInput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
  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);

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

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

  int dimw = 2;
  int dimh = 1;
  long nbatch = 1;
  if (input->nDimension == 4) {
    nbatch = input->size[0];
    dimw++;
    dimh++;
  }


  // sizes
  int nslices = input->size[dimh-1];
  int iheight = input->size[dimh];
  int iwidth = input->size[dimw];
  int oheight = gradOutput->size[dimh];
  int owidth = gradOutput->size[dimw];

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

  // backprop
  long k;
#pragma omp parallel for private(k)
  for (k = 0; k < nslices; k++)
  {
    long p;
    for (p = 0; p < nbatch; p++)
    {
      // pointers to slices
      real *gradOutput_p = gradOutput_data + p*nslices*owidth*oheight + k*owidth*oheight;
      real *gradInput_p = gradInput_data + p*nslices*iwidth*iheight + k*iwidth*iheight;
      real *indy_p = indices_data + p*nslices*owidth*oheight + k*owidth*oheight;
      real *indx_p = indices_data + (p+nbatch)*nslices*owidth*oheight + k*owidth*oheight;
      
      // calculate max points
      int i,j;
      for(i = 0; i < oheight; i++) {
	for(j = 0; j < owidth; j++) {
	  // retrieve position of max
	  long maxi = *(indy_p + i*owidth + j) - 1 + i*dH;
	  long maxj = *(indx_p + i*owidth + j) - 1 + j*dW;
	  
	  // update gradient
	  *(gradInput_p + maxi*iwidth + maxj) += *(gradOutput_p + i*owidth + j);
	}
      }
    }
  }

  // cleanup
  THTensor_(free)(gradOutput);

  return 1;
}

static const struct luaL_Reg nn_(SpatialMaxPooling__) [] = {
  {"SpatialMaxPooling_updateOutput", nn_(SpatialMaxPooling_updateOutput)},
  {"SpatialMaxPooling_updateGradInput", nn_(SpatialMaxPooling_updateGradInput)},
  {NULL, NULL}
};

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

#endif