def render(self, vertices, faces, textures): # fill back if self.fill_back: faces = cf.concat((faces, faces[:, :, ::-1]), axis=1).data textures = cf.concat( (textures, textures.transpose((0, 1, 4, 3, 2, 5))), axis=1) # lighting faces_lighting = neural_renderer.vertices_to_faces(vertices, faces) textures = neural_renderer.lighting(faces_lighting, textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # viewpoint transformation if self.camera_mode == 'look_at': vertices = neural_renderer.look_at(vertices, self.eye) elif self.camera_mode == 'look': vertices = neural_renderer.look(vertices, self.eye, self.camera_direction) # perspective transformation if self.perspective: vertices = neural_renderer.perspective(vertices, angle=self.viewing_angle) # rasterization faces = neural_renderer.vertices_to_faces(vertices, faces) images = neural_renderer.rasterize(faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return images
def render(self, cam, vertices, textures, faces=None, get_fim=False): if faces is None: bs = cam.shape[0] faces = self.faces.repeat(bs, 1, 1) # lighting is inplace operation textures = textures.clone() # lighting faces_lighting = nr.vertices_to_faces(vertices, faces) textures = nr.lighting( faces_lighting, textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # set offset_z for persp proj proj_verts = self.proj_func(vertices, cam) # flipping the y-axis here to make it align with the image coordinate system! proj_verts[:, :, 1] *= -1 # calculate the look_at vertices. vertices = nr.look_at(proj_verts, self.eye) # rasterization faces = nr.vertices_to_faces(vertices, faces) images = nr.rasterize(faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) fim = None if get_fim: fim = nr.rasterize_face_index_map(faces, image_size=self.image_size, anti_aliasing=False, near=self.near, far=self.far, eps=self.rasterizer_eps) return images, fim
def render_normal(self, vertices, faces): # fill back if self.fill_back: faces = cf.concat((faces, faces[:, :, ::-1]), axis=1).data # normal faces_normal = nr.vertices_to_faces(vertices, faces) (bs, nf) = faces_normal.shape[:2] faces_normal = faces_normal.reshape((bs * nf, 3, 3)) v10 = faces_normal[:, 0] - faces_normal[:, 1] v12 = faces_normal[:, 2] - faces_normal[:, 1] normals = cf.normalize(nr.cross(v10, v12)) normals = normals.reshape((bs, nf, 3)) textures = normals[:, :, None, None, None, :] textures = cf.tile(textures, (1, 1, 2, 2, 2, 1)) # viewpoint transformation if self.camera_mode == 'look_at': vertices = nr.look_at(vertices, self.eye) elif self.camera_mode == 'look': vertices = nr.look(vertices, self.eye, self.camera_direction, self.up) # perspective transformation if self.perspective: vertices = nr.perspective(vertices, angle=self.viewing_angle) # rasterization faces = nr.vertices_to_faces(vertices, faces) images = nr.rasterize( faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return images
def render(self, vertices, texture=None, faces=None): if faces is None: bs = vertices.shape[0] faces = self.faces.repeat(bs, 1, 1) if texture is None: texture = self.debug_textures().to(vertices.device) texture = texture.unsqueeze(0).repeat(bs, 1, 1, 1, 1, 1) # lighting is inplace operation texture = texture.clone() # lighting faces_lighting = nr.vertices_to_faces(vertices, faces) texture = nr.lighting( faces_lighting, texture, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # set offset_z for persp proj #proj_verts = self.proj_func(vertices, cam) # flipping the y-axis here to make it align with the image coordinate system! #proj_verts[:, :, 1] *= -1 # calculate the look_at vertices. vertices = nr.look_at(vertices, self.eye) # rasterization faces = nr.vertices_to_faces(vertices, faces) image = nr.rasterize(faces, texture, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return image
def render(self, vertices, faces, textures): # fill back if self.fill_back: faces = cf.concat((faces, faces[:, :, ::-1]), axis=1).data textures = cf.concat((textures, textures.transpose((0, 1, 4, 3, 2, 5))), axis=1) # lighting faces_lighting = neural_renderer.vertices_to_faces(vertices, faces) textures = neural_renderer.lighting( faces_lighting, textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # viewpoint transformation if self.camera_mode == 'look_at': vertices = neural_renderer.look_at(vertices, self.eye) elif self.camera_mode == 'look': vertices = neural_renderer.look(vertices, self.eye, self.camera_direction) # perspective transformation if self.perspective: vertices = neural_renderer.perspective(vertices, angle=self.viewing_angle) # rasterization faces = neural_renderer.vertices_to_faces(vertices, faces) images = neural_renderer.rasterize( faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return images
def render(self, vertices, faces, textures, K=None, R=None, t=None, dist_coeffs=None, orig_size=None): # fill back if self.fill_back: faces = torch.cat( (faces, faces[:, :, list(reversed(range(faces.shape[-1])))]), dim=1).detach() textures = torch.cat((textures, textures.permute( (0, 1, 4, 3, 2, 5))), dim=1) # lighting faces_lighting = nr.vertices_to_faces(vertices, faces) textures = nr.lighting(faces_lighting, textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # viewpoint transformation if self.camera_mode == 'look_at': vertices = nr.look_at(vertices, self.eye) # perspective transformation if self.perspective: vertices = nr.perspective(vertices, angle=self.viewing_angle) elif self.camera_mode == 'look': vertices = nr.look(vertices, self.eye, self.camera_direction) # perspective transformation if self.perspective: vertices = nr.perspective(vertices, angle=self.viewing_angle) elif self.camera_mode == 'projection': if K is None: K = self.K if R is None: R = self.R if t is None: t = self.t if dist_coeffs is None: dist_coeffs = self.dist_coeffs if orig_size is None: orig_size = self.orig_size vertices = nr.projection(vertices, K, R, t, dist_coeffs, orig_size) # rasterization faces = nr.vertices_to_faces(vertices, faces) images = nr.rasterize(faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return images
def render(self, vertices, faces, textures): # fill back if self.fill_back: faces = cf.concat((faces, faces[:, :, ::-1]), axis=1).data textures = cf.concat( (textures, textures.transpose((0, 1, 4, 3, 2, 5))), axis=1) # lighting faces_lighting = neural_renderer.vertices_to_faces(vertices, faces) textures = neural_renderer.lighting(faces_lighting, textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # viewpoint transformation if self.camera_mode == 'look_at': vertices = neural_renderer.look_at(vertices, self.eye) elif self.camera_mode == 'look': vertices = neural_renderer.look(vertices, self.eye, self.camera_direction) # perspective transformation if self.perspective: vertices = neural_renderer.perspective(vertices, angle=self.viewing_angle) # rasterization faces = neural_renderer.vertices_to_faces(vertices, faces) # ==== TM changes ==== results_dict = neural_renderer.rasterize(faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) images = results_dict['rgb'] face_index_map = results_dict['face_index_map'] weight_map = results_dict['weight_map'] sampling_weight_map = results_dict['sampling_weight_map'] # ==== Making another dictionary (just for clarity) ==== return_dict = dict() return_dict['images'] = images return_dict['face_index_map'] = face_index_map return_dict['weight_map'] = weight_map return_dict['sampling_weight_map'] = sampling_weight_map return return_dict
def forward(self, vertices, faces, textures=None, mode=None): ''' Implementation of forward rendering method The old API is preserved for back-compatibility with the Chainer implementation ''' _textures = textures if mode not in [None, 'silhouettes', 'depth']: raise ValueError("mode should be one of None, 'silhouettes' or 'depth'") # fill back if self.fill_back: faces = torch.cat((faces, faces[:, :, list(reversed(range(faces.shape[-1])))]), dim=1).detach() if _textures is not None: _textures = torch.cat((_textures, textures.permute((0, 1, 4, 3, 2, 5))), dim=1) if textures is not None: # lighting faces_lighting = nr.vertices_to_faces(vertices, faces) _textures = nr.lighting( faces_lighting, _textures, self.light_intensity_ambient, self.light_intensity_directional, self.light_color_ambient, self.light_color_directional, self.light_direction) # projection vertices = nr.projection(vertices, self.camera) if self.camera.perspective: vertices = nr.perspective(vertices, angle=self.camera.viewing_angle) # rasterization faces = nr.vertices_to_faces(vertices, faces) if mode is None: images = nr.rasterize( faces, _textures, self.camera.image_size, self.anti_aliasing, self.camera.near, self.camera.far, self.rasterizer_eps, self.background_color) elif mode == 'silhouettes': images = nr.rasterize_silhouettes(faces, self.camera.image_size, self.anti_aliasing) elif mode == 'depth': images = nr.rasterize_depth(faces, self.camera.image_size, self.anti_aliasing) return images
def render(self, vertices, faces, textures): if self.camera_mode == 'look_at': vertices = neural_renderer.look_at(vertices, self.eye) elif self.camera_mode == 'look': vertices = neural_renderer.look(vertices, self.eye, self.camera_direction) if self.perspective: vertices = neural_renderer.perspective(vertices) faces = neural_renderer.vertices_to_faces(vertices, faces) if self.fill_back: faces = cf.concat((faces, faces[:, :, ::-1]), axis=1) textures = cf.concat((textures, textures.transpose((0, 1, 4, 3, 2, 5))), axis=1) textures = neural_renderer.lighting( faces, textures, self.light_intensity_ambient, self.light_intensity_directional) images = neural_renderer.rasterize( faces, textures, self.image_size, self.anti_aliasing, self.near, self.far, self.rasterizer_eps, self.background_color) return images