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

SpatialDownSampling.c « generic - github.com/clementfarabet/lua---nnx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e1fc5a9c2479e0725d88331b15d405318920cd0 (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
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/SpatialDownSampling.c"
#else

static int nn_(SpatialDownSampling_updateOutput)(lua_State *L) {
  // get all params
  THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  int rW = luaT_getfieldcheckint(L, 1, "rW");
  int rH = luaT_getfieldcheckint(L, 1, "rH");
  THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);

  // dims
  int iwidth = input->size[2];
  int iheight = input->size[1];
  int ichannels = input->size[0];
  int owidth = floor(iwidth / rW);
  int oheight = floor(iheight / rH);

  // get strides
  long *is = input->stride;
  long *os = output->stride;

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

  // resample each plane
  real avg;
  real *input_p = input_data, *output_p = output_data;
  int k, x, y, i, j;
  for (k = 0; k < ichannels; ++k, input_p += is[0], output_p += os[0])
    for (y = 0; y < oheight; ++y)
      for (x = 0; x < owidth; ++x) {
	avg = 0.0;
	for (i = y*rH; i < (y+1)*rH; ++i)
	  for (j = x*rW; j < (x+1)*rW; ++j)
	    avg += input_p[i*is[1]+j*is[2]];
        output_p[y*os[1] + x*os[2]] = avg;
      }
  THTensor_(mul)(output, output, 1.0f/(rH*rW));
  return 1;
}

static int nn_(SpatialDownSampling_updateGradInput)(lua_State *L) {
  // get all params
  THTensor *gradOutput = luaT_checkudata(L, 2, torch_Tensor);
  THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
  int rW = luaT_getfieldcheckint(L, 1, "rW");
  int rH = luaT_getfieldcheckint(L, 1, "rH");

  THArgCheck(gradOutput->nDimension == 3, 2, "gradOutput must be 3D Tensor");

  // dims
  int owidth = gradOutput->size[2];
  int oheight = gradOutput->size[1];
  int ochannels = gradOutput->size[0];

  // get strides
  long *gis = gradInput->stride;
  long *gos = gradOutput->stride;

  THTensor_(zero)(gradInput);

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

  // compute gradients for each plane
  real *gradInput_p = gradInput_data, *gradOutput_p = gradOutput_data;
  int k, x, y, i, j;
  for (k = 0; k < ochannels; ++k, gradInput_p += gis[0], gradOutput_p += gos[0])
    for (y = 0; y < oheight; ++y)
      for (x = 0; x < owidth; ++x)
	for (i = y*rH; i < (y+1)*rH; ++i)
	  for (j = x*rW; j < (x+1)*rW; ++j)
	    gradInput_p[i*gis[1]+j*gis[2]] += gradOutput_p[y*gos[1]+x*gos[2]];
  THTensor_(mul)(gradInput, gradInput, 1.0f/(rH*rW));
  
  return 1;
}

static const struct luaL_Reg nn_(SpatialDownSampling__) [] = {
  {"SpatialDownSampling_updateOutput", nn_(SpatialDownSampling_updateOutput)},
  {"SpatialDownSampling_updateGradInput", nn_(SpatialDownSampling_updateGradInput)},
  {NULL, NULL}
};

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

#endif