Skip to content

Commit

Permalink
support chainer 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroharu-kato committed Nov 29, 2018
1 parent 310d8b5 commit 85aa98d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
6 changes: 3 additions & 3 deletions neural_renderer/cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def forward_gpu(self, inputs):
'int32 j, raw T a, raw T b',
'raw T c',
'''
float* ap = &a[j * 3];
float* bp = &b[j * 3];
float* cp = &c[j * 3];
float* ap = (float*)&a[j * 3];
float* bp = (float*)&b[j * 3];
float* cp = (float*)&c[j * 3];
cp[0] = ap[1] * bp[2] - ap[2] * bp[1];
cp[1] = ap[2] * bp[0] - ap[0] * bp[2];
cp[2] = ap[0] * bp[1] - ap[1] * bp[0];
Expand Down
4 changes: 2 additions & 2 deletions neural_renderer/load_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def load_textures(filename_obj, filename_mtl, texture_size):
dim1 /= sum;
dim2 /= sum;
const float* face = &faces[fn * 3 * 2];
float* texture = &textures[i * 3];
float* face = (float*)&faces[fn * 3 * 2];
float* texture = (float*)&textures[i * 3];
if (is_update[fn] == 0) return;
const float pos_x = (
Expand Down
73 changes: 36 additions & 37 deletions neural_renderer/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def forward_face_index_map_gpu(self):
const int bn = i / ${num_faces};
const int fn = i % ${num_faces};
const int is = ${image_size};
const float* face = &faces[i * 9];
const float* face = (float*)&faces[i * 9];
/* return if backside */
if ((face[7] - face[1]) * (face[3] - face[0]) < (face[4] - face[1]) * (face[6] - face[0])) return;
Expand Down Expand Up @@ -245,8 +245,8 @@ def forward_face_index_map_gpu(self):
string.Template('''
/* face[v012][RGB] */
const int is = ${image_size};
const float* face = &faces[i * 9];
float* face_inv_g = &faces_inv[i * 9];
const float* face = (float*)&faces[i * 9];
float* face_inv_g = (float*)&faces_inv[i * 9];
/* return if backside */
if ((face[7] - face[1]) * (face[3] - face[0]) < (face[4] - face[1]) * (face[6] - face[0]))
Expand Down Expand Up @@ -279,9 +279,8 @@ def forward_face_index_map_gpu(self):
# for each pixel
loop = self.xp.arange(self.batch_size * self.image_size * self.image_size).astype('int32')
chainer.cuda.elementwise(
'int32 _, raw float32 faces, raw float32 faces_inv, raw int32 face_index_map, ' +
'raw float32 weight_map, raw float32 depth_map, raw float32 face_inv_map',
'',
'int32 _, raw float32 faces, raw float32 faces_inv',
'raw int32 face_index_map, raw float32 weight_map, raw float32 depth_map, raw float32 face_inv_map',
string.Template('''
const int is = ${image_size};
const int nf = ${num_faces};
Expand All @@ -292,8 +291,8 @@ def forward_face_index_map_gpu(self):
const float yp = (2. * yi + 1 - is) / is;
const float xp = (2. * xi + 1 - is) / is;
float* face = &faces[bn * nf * 9] - 9;
float* face_inv = &faces_inv[bn * nf * 9] - 9;
float* face = (float*)&faces[bn * nf * 9] - 9;
float* face_inv = (float*)&faces_inv[bn * nf * 9] - 9;
float depth_min = ${far};
int face_index_min = -1;
float weight_min[3];
Expand Down Expand Up @@ -387,13 +386,13 @@ def forward_texture_sampling(self):
const int bn = i / (${image_size} * ${image_size});
const int nf = ${num_faces};
const int ts = ${texture_size};
const float* face = &faces[face_index * 9];
const float* texture = &textures[(bn * nf + face_index) * ts * ts * ts * 3];
float* pixel = &rgb_map[i * 3];
const float* weight = &weight_map[i * 3];
const float* face = (float*)&faces[face_index * 9];
const float* texture = (float*)&textures[(bn * nf + face_index) * ts * ts * ts * 3];
float* pixel = (float*)&rgb_map[i * 3];
const float* weight = (float*)&weight_map[i * 3];
const float depth = depth_map[i];
int* sampling_indices = &sampling_index_map[i * 8];
float* sampling_weights = &sampling_weight_map[i * 8];
int* sampling_indices = (int*)&sampling_index_map[i * 8];
float* sampling_weights = (float*)&sampling_weight_map[i * 8];
/* get texture index (float) */
float texture_index_float[3];
Expand Down Expand Up @@ -528,13 +527,13 @@ def backward_pixel_map_gpu(self):
loop = self.xp.arange(self.batch_size * self.num_faces).astype('int32')
chainer.cuda.elementwise(
'int32 _, raw float32 faces, raw int32 face_index_map, raw float32 rgb_map, raw float32 alpha_map, ' +
'raw float32 grad_rgb_map, raw float32 grad_alpha_map, raw float32 grad_faces',
'',
'raw float32 grad_rgb_map, raw float32 grad_alpha_map',
'raw float32 grad_faces',
string.Template('''
const int bn = i / ${num_faces};
const int fn = i % ${num_faces};
const int is = ${image_size};
const float* face = &faces[i * 9];
const float* face = (float*)&faces[i * 9];
float grad_face[9] = {};
/* check backside */
Expand Down Expand Up @@ -597,8 +596,8 @@ def backward_pixel_map_gpu(self):
alpha_out = alpha_map[map_index_out];
}
if (${return_rgb}) {
rgb_in = &rgb_map[map_index_in * 3];
rgb_out = &rgb_map[map_index_out * 3];
rgb_in = (float*)&rgb_map[map_index_in * 3];
rgb_out = (float*)&rgb_map[map_index_out * 3];
}
/* out */
Expand All @@ -621,12 +620,12 @@ def backward_pixel_map_gpu(self):
map_index_from = bn * is * is + d0 * is + d1_from;
}
if (${return_alpha}) {
alpha_map_p = &alpha_map[map_index_from];
grad_alpha_map_p = &grad_alpha_map[map_index_from];
alpha_map_p = (float*)&alpha_map[map_index_from];
grad_alpha_map_p = (float*)&grad_alpha_map[map_index_from];
}
if (${return_rgb}) {
rgb_map_p = &rgb_map[map_index_from * 3];
grad_rgb_map_p = &grad_rgb_map[map_index_from * 3];
rgb_map_p = (float*)&rgb_map[map_index_from * 3];
grad_rgb_map_p = (float*)&grad_rgb_map[map_index_from * 3];
}
for (int d1 = d1_from; d1 <= d1_to; d1++) {
float diff_grad = 0;
Expand Down Expand Up @@ -685,14 +684,14 @@ def backward_pixel_map_gpu(self):
} else {
map_index_from = bn * is * is + d0 * is + d1_from;
}
face_index_map_p = &face_index_map[map_index_from] - map_offset;
face_index_map_p = (int*)&face_index_map[map_index_from] - map_offset;
if (${return_alpha}) {
alpha_map_p = &alpha_map[map_index_from] - map_offset;
grad_alpha_map_p = &grad_alpha_map[map_index_from] - map_offset;
alpha_map_p = (float*)&alpha_map[map_index_from] - map_offset;
grad_alpha_map_p = (float*)&grad_alpha_map[map_index_from] - map_offset;
}
if (${return_rgb}) {
rgb_map_p = &rgb_map[map_index_from * 3] - 3 * map_offset;
grad_rgb_map_p = &grad_rgb_map[map_index_from * 3] - 3 * map_offset;
rgb_map_p = (float*)&rgb_map[map_index_from * 3] - 3 * map_offset;
grad_rgb_map_p = (float*)&grad_rgb_map[map_index_from * 3] - 3 * map_offset;
}
for (int d1 = d1_from; d1 <= d1_to; d1++) {
Expand Down Expand Up @@ -770,14 +769,14 @@ def backward_textures_gpu(self):
int ts = ${texture_size};
int bn = i / (is * is); // batch number [0 -> bs]
float* grad_texture = &grad_textures[(bn * nf + face_index) * ts * ts * ts * 3];
float* sampling_weight_map_p = &sampling_weight_map[i * 8];
int* sampling_index_map_p = &sampling_index_map[i * 8];
float* grad_texture = (float*)&grad_textures[(bn * nf + face_index) * ts * ts * ts * 3];
float* sampling_weight_map_p = (float*)&sampling_weight_map[i * 8];
int* sampling_index_map_p = (int*)&sampling_index_map[i * 8];
for (int pn = 0; pn < 8; pn++) {
float w = *sampling_weight_map_p++;
int isc = *sampling_index_map_p++;
float* grad_texture_p = &grad_texture[isc * 3];
float* grad_rgb_map_p = &grad_rgb_map[i * 3];
float* grad_texture_p = (float*)&grad_texture[isc * 3];
float* grad_rgb_map_p = (float*)&grad_rgb_map[i * 3];
for (int k = 0; k < 3; k++) atomicAdd(grad_texture_p++, w * *grad_rgb_map_p++);
}
}
Expand Down Expand Up @@ -813,13 +812,13 @@ def backward_depth_map_gpu(self):
const int nf = ${num_faces};
const int is = ${image_size};
const int bn = i / (is * is);
const float* face = &faces[(bn * nf + fn) * 9];
const float* face = (float*)&faces[(bn * nf + fn) * 9];
const float depth = depth_map[i];
const float depth2 = depth * depth;
const float* face_inv = &face_inv_map[i * 9];
const float* weight = &weight_map[i * 3];
const float* face_inv = (float*)&face_inv_map[i * 9];
const float* weight = (float*)&weight_map[i * 3];
const float grad_depth = grad_depth_map[i];
float* grad_face = &grad_faces[(bn * nf + fn) * 9];
float* grad_face = (float*)&grad_faces[(bn * nf + fn) * 9];
/* derivative wrt z */
for (int k = 0; k < 3; k++) {
Expand Down
Binary file modified tests/data/car.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/display.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 85aa98d

Please sign in to comment.