Skip to content

Commit

Permalink
ver 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroharu-kato committed Feb 6, 2018
1 parent a3e09cf commit c86f9bb
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ python ./examples/example4.py

## Example 2: Optimizing vertices

Transforming the silhouette of a teapot into a rectangle. The loss function is the difference bettween the rendered image and the reference image.
Transforming the silhouette of a teapot into a rectangle. The loss function is the difference between the rendered image and the reference image.

Reference image, optimization, and the result.

Expand Down
Binary file modified examples/data/example2_optimization.gif
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 examples/data/example2_result.gif
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 examples/data/example3_result.gif
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 examples/data/example4_result.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def __init__(self, filename_obj, filename_ref):
renderer = neural_renderer.Renderer()
self.renderer = renderer

def to_gpu(self):
super(Model, self).to_gpu()
self.faces = chainer.cuda.to_gpu(self.faces)
self.textures = chainer.cuda.to_gpu(self.textures)
self.image_ref = chainer.cuda.to_gpu(self.image_ref)
def to_gpu(self, device=None):
super(Model, self).to_gpu(device)
self.faces = chainer.cuda.to_gpu(self.faces, device)
self.textures = chainer.cuda.to_gpu(self.textures, device)
self.image_ref = chainer.cuda.to_gpu(self.image_ref, device)

def __call__(self):
self.renderer.eye = neural_renderer.get_points_from_angles(2.732, 0, 90)
Expand Down
10 changes: 5 additions & 5 deletions examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self, filename_obj, filename_ref):
renderer.light_intensity_ambient = 1.0
self.renderer = renderer

def to_gpu(self):
super(Model, self).to_gpu()
self.faces = chainer.cuda.to_gpu(self.faces)
self.vertices = chainer.cuda.to_gpu(self.vertices)
self.image_ref = chainer.cuda.to_gpu(self.image_ref)
def to_gpu(self, device=None):
super(Model, self).to_gpu(device)
self.faces = chainer.cuda.to_gpu(self.faces, device)
self.vertices = chainer.cuda.to_gpu(self.vertices, device)
self.image_ref = chainer.cuda.to_gpu(self.image_ref, device)

def __call__(self):
self.renderer.eye = neural_renderer.get_points_from_angles(2.732, 0, np.random.uniform(0, 360))
Expand Down
10 changes: 5 additions & 5 deletions examples/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def __init__(self, filename_obj, filename_ref=None):
renderer.eye = self.camera_position
self.renderer = renderer

def to_gpu(self):
super(Model, self).to_gpu()
self.faces = chainer.cuda.to_gpu(self.faces)
self.vertices = chainer.cuda.to_gpu(self.vertices)
self.textures = chainer.cuda.to_gpu(self.textures)
def to_gpu(self, device=None):
super(Model, self).to_gpu(device)
self.faces = chainer.cuda.to_gpu(self.faces, device)
self.vertices = chainer.cuda.to_gpu(self.vertices, device)
self.textures = chainer.cuda.to_gpu(self.textures, device)
if self.image_ref is not None:
self.image_ref = chainer.cuda.to_gpu(self.image_ref)

Expand Down
1 change: 1 addition & 0 deletions misc/measure_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def run():
parser.add_argument('-i', '--filename_input', type=str, default='./examples/data/teapot.obj')
parser.add_argument('-bs', '--batch_size', type=int, default=1)
parser.add_argument('-is', '--image_size', type=int, default=256)
parser.add_argument('-us', '--unsafe', type=int, default=0)
parser.add_argument('-g', '--gpu', type=int, default=0)
args = parser.parse_args()

Expand Down
4 changes: 2 additions & 2 deletions neural_renderer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from mesh import Mesh
from optimizers import Adam
from perspective import perspective
from rasterize import rasterize_rgbad, rasterize, rasterize_silhouettes, rasterize_depth, use_unsafe_rasterizer
from renderer import Renderer
from vertices_to_faces import vertices_to_faces
from rasterize import rasterize_rgbad, rasterize, rasterize_silhouettes, rasterize_depth

__version__ = '1.0.0'
__version__ = '1.1.0'
6 changes: 3 additions & 3 deletions neural_renderer/get_points_from_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import chainer


def get_points_from_angles(distance, elevation, azimuth, digrees=True):
def get_points_from_angles(distance, elevation, azimuth, degrees=True):
if isinstance(distance, float) or isinstance(distance, int):
if digrees:
if degrees:
elevation = math.radians(elevation)
azimuth = math.radians(azimuth)
return (
Expand All @@ -14,7 +14,7 @@ def get_points_from_angles(distance, elevation, azimuth, digrees=True):
-distance * math.cos(elevation) * math.cos(azimuth))
else:
xp = chainer.cuda.get_array_module(distance)
if digrees:
if degrees:
elevation = xp.radians(elevation)
azimuth = xp.radians(azimuth)
return xp.stack([
Expand Down
2 changes: 1 addition & 1 deletion neural_renderer/look.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def look(vertices, eye, direction=None, up=None):
if vertices.shape != eye.shape:
eye = cf.broadcast_to(eye[:, None, :], vertices.shape)
vertices = vertices - eye
vertices = cf.batch_matmul(vertices, r, transb=True)
vertices = cf.matmul(vertices, r, transb=True)

return vertices
2 changes: 1 addition & 1 deletion neural_renderer/look_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def look_at(vertices, eye, at=None, up=None):
if vertices.shape != eye.shape:
eye = cf.broadcast_to(eye[:, None, :], vertices.shape)
vertices = vertices - eye
vertices = cf.batch_matmul(vertices, r, transb=True)
vertices = cf.matmul(vertices, r, transb=True)

return vertices
6 changes: 3 additions & 3 deletions neural_renderer/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def __init__(self, filename_obj, texture_size=4):
self.textures = chainer.Parameter(init, shape)
self.texture_size = texture_size

def to_gpu(self):
super(Mesh, self).to_gpu()
self.faces = chainer.cuda.to_gpu(self.faces)
def to_gpu(self, device=None):
super(Mesh, self).to_gpu(device)
self.faces = chainer.cuda.to_gpu(self.faces, device)

def get_batch(self, batch_size):
# broadcast for minibatch
Expand Down
32 changes: 24 additions & 8 deletions neural_renderer/rasterize.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import string

import chainer
Expand All @@ -9,6 +10,10 @@
DEFAULT_FAR = 100
DEFAULT_EPS = 1e-4
DEFAULT_BACKGROUND_COLOR = (0, 0, 0)
USE_UNSAFE_IMPLEMENTATION = False

if 'NEURAL_RENDERER_UNSAFE' in os.environ and int(os.environ['NEURAL_RENDERER_UNSAFE']):
USE_UNSAFE_IMPLEMENTATION = True


class Rasterize(chainer.Function):
Expand Down Expand Up @@ -94,8 +99,8 @@ def forward_face_index_map_gpu(self):
# if alpha: face_index_map, weight_map, depth_map
# if depth: face_index_map, weight_map, depth_map, face_inv_map

if False:
# very fast, but unable to run on Pascal GPUs
if USE_UNSAFE_IMPLEMENTATION:
# very fast, but unable to run on some environments

# for each face
loop = self.xp.arange(self.batch_size * self.num_faces).astype('int32')
Expand Down Expand Up @@ -198,17 +203,23 @@ def forward_face_index_map_gpu(self):
bool locked = false;
do {
if (locked = atomicCAS(&lock[index], 0, 1) == 0) {
if (zp < depth_map[index]) {
depth_map[index] = zp;
face_index_map[index] = fn;
for (int k = 0; k < 3; k++) weight_map[3 * index + pi[k]] = w[k];
if (zp < atomicAdd(&depth_map[index], 0)) {
float record = 0;
atomicExch(&depth_map[index], zp);
atomicExch(&face_index_map[index], fn);
for (int k = 0; k < 3; k++) atomicExch(&weight_map[3 * index + pi[k]], w[k]);
if (${return_depth}) {
for (int k = 0; k < 3; k++) for (int l = 0; l < 3; l++)
face_inv_map[9 * index + 3 * pi[l] + k] = face_inv[3 * l + k];
atomicExch(
&face_inv_map[9 * index + 3 * pi[l] + k], face_inv[3 * l + k]);
}
record += atomicAdd(&depth_map[index], 0.);
record += atomicAdd(&face_index_map[index], 0.);
if (0 < record) atomicExch(&lock[index], 0);
} else {
atomicExch(&lock[index], 0);
}
}
if (locked) atomicExch(&lock[index], 0);
} while (!locked);
}
}
Expand Down Expand Up @@ -1041,3 +1052,8 @@ def rasterize_depth(
"""
return rasterize_rgbad(faces, None, image_size, anti_aliasing, near, far, eps, None, False, False, True)['depth']


def use_unsafe_rasterizer(flag):
global USE_UNSAFE_IMPLEMENTATION
USE_UNSAFE_IMPLEMENTATION = flag
5 changes: 3 additions & 2 deletions tests/test_rasterize_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_forward_case1(self):

chainer.testing.assert_allclose(ref, image)

def tesst_forward_case2(self):
def test_forward_case2(self):
# load teapot
vertices, faces, _ = utils.load_teapot_batch()

Expand All @@ -53,10 +53,11 @@ def tesst_forward_case2(self):

ref = scipy.misc.imread('./tests/data/test_depth.png')
ref = ref.astype('float32') / 255.
scipy.misc.toimage(image).save('../tmp/test_depth.png')

chainer.testing.assert_allclose(image, ref, atol=1e-2)

def tesst_backward_case1(self):
def test_backward_case1(self):
vertices = [
[-0.9, -0.9, 2.],
[-0.8, 0.8, 1.],
Expand Down

0 comments on commit c86f9bb

Please sign in to comment.