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

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

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

  // dims
  int iwidth = input->size[2];
  int iheight = input->size[1];
  int owidth = iwidth * dW;
  int oheight = iheight * dH;
  int channels1 = input->size[0];
  int channels2 = input->size[3];

  // 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
  int k1, k2, x, y;
  for (k1 = 0; k1 < channels1; k1++) {
    for (k2 = 0; k2 < channels2; k2++) {
      // get planes
      real *input_p = input_data + k1*is[0] + k2*is[3];
      real *output_p = output_data + k1*os[0] + k2*os[3];
      
      // for each plane, resample
      for (y=0; y<oheight; y++) {
	for (x=0; x<owidth; x++) {
	  // input positions (floored)
	  int ix = x/dW;
	  int iy = y/dH;
	  
	  // set output
	  output_p[y*os[1] + x*os[2]] = input_p[iy*is[1] + ix*is[2]];
	}
      }
    }
  }
  return 1;
}

static int nn_(SpatialUpSampling_updateGradInput)(lua_State *L)
{
  // get all params
  //THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
  THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
  int dW = luaT_getfieldcheckint(L, 1, "dW");
  int dH = luaT_getfieldcheckint(L, 1, "dH");

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

  // resize gradInput
  THTensor_(zero)(gradInput);

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


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

  // compute gradients for each plane
  int k1, k2, x, y;
  for (k1 = 0; k1 < channels1; k1++) {
    for (k2 = 0; k2 < channels2; k2++) {
      // get planes
      real *gradInput_p = gradInput_data + k1*gis[0] + k2*gis[3];
      real *gradOutput_p = gradOutput_data + k1*gos[0] + k2*gos[3];
      
      // for each plane, resample
      for (y=0; y<oheight; y++) {
	for (x=0; x<owidth; x++) {
	  // input positions (floored)
	  int ix = x/dW;
	  int iy = y/dH;
	  
	  // accumulate gradient
	  gradInput_p[iy*gis[1] + ix*gis[2]] += gradOutput_p[y*gos[1] + x*gos[2]];
	}
      }
    }
  }
  return 1;
}

static const struct luaL_Reg nn_(SpatialUpSampling__) [] = {
  {"SpatialUpSampling_updateOutput", nn_(SpatialUpSampling_updateOutput)},
  {"SpatialUpSampling_updateGradInput", nn_(SpatialUpSampling_updateGradInput)},
  {NULL, NULL}
};

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

#endif