Example #1
0
    def generate_from_latent(self,
                             z,
                             c_first=None,
                             Rt=None,
                             K=None,
                             stats_dict={},
                             **kwargs):
        ''' Generates mesh from latent.

        Args:
            z (tensor): latent code z
            c (tensor): latent conditioned code c
            stats_dict (dict): stats dictionary
        '''
        threshold = np.log(self.threshold) - np.log(1. - self.threshold)

        t0 = time.time()
        # Compute bounding box size
        box_size = 1 + self.padding

        # Shortcut
        if self.upsampling_steps == 0:
            nx = self.resolution0
            pointsf = box_size * make_3d_grid((-0.5, ) * 3, (0.5, ) * 3,
                                              (nx, ) * 3)
            values = self.eval_points(pointsf, z, c_first,
                                      **kwargs).cpu().numpy()
            value_grid = values.reshape(nx, nx, nx)
        else:
            mesh_extractor = MISE(self.resolution0, self.upsampling_steps,
                                  threshold)

            points = mesh_extractor.query()

            while points.shape[0] != 0:
                # Query points
                pointsf = torch.FloatTensor(points).to(self.device)
                # Normalize to bounding box
                pointsf = pointsf / mesh_extractor.resolution
                pointsf = box_size * (pointsf - 0.5)
                # Evaluate model and update
                values = self.eval_points(pointsf, z, c_first, Rt, K,
                                          **kwargs).cpu().numpy()
                values = values.astype(np.float64)
                mesh_extractor.update(points, values)
                points = mesh_extractor.query()

            value_grid = mesh_extractor.to_dense()

        # Extract mesh
        stats_dict['time (eval points)'] = time.time() - t0

        mesh = self.extract_mesh(value_grid,
                                 z,
                                 c_first,
                                 Rt,
                                 K,
                                 stats_dict=stats_dict)
        return mesh
Example #2
0
    def generate_from_latent(self,
                             z,
                             c=None,
                             stats_dict={},
                             data=None,
                             **kwargs):
        ''' Generates mesh from latent.

        Args:
            z (tensor): latent code z
            c (tensor): latent conditioned code c
            stats_dict (dict): stats dictionary
        '''
        assert data is not None

        if self.is_explicit_mesh:
            normal_faces = data.get('angles.normal_face').to(self.device)
            normal_angles = data.get('angles.normal_angles').to(self.device)

            if self.is_skip_surface_mask_generation_time:
                t0 = time.time()
                output = self.model.decode(None,
                                           z,
                                           c,
                                           angles=normal_angles,
                                           only_return_points=True,
                                           **kwargs)
                stats_dict['time (eval points)'] = time.time() - t0
            t0 = time.time()
            if not self.is_just_measuring_time:
                output = self.model.decode(None,
                                           z,
                                           c,
                                           angles=normal_angles,
                                           **kwargs)
            if not self.is_skip_surface_mask_generation_time:
                stats_dict['time (eval points)'] = time.time() - t0
            normal_vertices, normal_mask, _, _, _ = output

            t0 = time.time()
            B, N, P, D = normal_vertices.shape
            normal_faces_all = torch.cat([(normal_faces + idx * P)
                                          for idx in range(N)],
                                         axis=1)
            assert B == 1
            mem_t = time.time()
            verts = normal_vertices.view(
                N * P, D).to('cpu').detach().numpy() / self.pnet_point_scale

            faces = normal_faces_all.view(-1, 3).to('cpu').detach().numpy()
            if self.is_just_measuring_time:
                visbility = None
            else:
                visbility = (normal_mask > 0.5).view(
                    N * P).to('cpu').detach().numpy()
            skip_t = time.time() - mem_t

            mesh = trimesh.Trimesh(
                verts,
                faces,
                process=False,
                vertex_attributes={'vertex_visibility': visbility})
            stats_dict['time (copy to trimesh)'] = time.time() - t0 - skip_t

        else:
            t0 = time.time()
            threshold = 0.
            #threshold = np.log(self.threshold) - np.log(1. - self.threshold)

            # Compute bounding box size
            box_size = 1 + self.padding

            # Shortcut
            if self.upsampling_steps == 0:
                nx = self.resolution0
                pointsf = box_size * make_3d_grid((-0.5, ) * 3, (0.5, ) * 3,
                                                  (nx, ) * 3)
                values = self.eval_points(pointsf, z, c, data=data,
                                          **kwargs).cpu().numpy()
                value_grid = values.reshape(nx, nx, nx)
            else:
                mesh_extractor = MISE(self.resolution0, self.upsampling_steps,
                                      threshold)

                points = mesh_extractor.query()

                while points.shape[0] != 0:
                    # Query points
                    pointsf = torch.FloatTensor(points).to(self.device)
                    # Normalize to bounding box
                    pointsf = pointsf / mesh_extractor.resolution
                    pointsf = box_size * (pointsf - 0.5)
                    # Evaluate model and update
                    values = self.eval_points(pointsf,
                                              z,
                                              c,
                                              data=data,
                                              **kwargs).cpu().numpy()
                    values = values.astype(np.float64)
                    mesh_extractor.update(points, values)
                    points = mesh_extractor.query()

                value_grid = mesh_extractor.to_dense()

            # Extract mesh
            stats_dict['time (eval points)'] = time.time() - t0

            mesh = self.extract_mesh(value_grid, z, c, stats_dict=stats_dict)
        return mesh