Ejemplo n.º 1
0
 def _build_shape_model(self, shapes, shape_graph, max_shape_components,
                        verbose=False):
     # if the provided graph is None, then apply PCA, else use the GMRF
     if shape_graph is not None:
         pca_model = GMRFModel(
             shapes, shape_graph, mode='concatenation', n_components=None,
             dtype=np.float64, sparse=False, incremental=self.is_incremental,
             verbose=verbose).principal_components_analysis()
         return OrthoPDM(pca_model, max_n_components=max_shape_components)
     else:
         return OrthoPDM(shapes, max_n_components=max_shape_components)
Ejemplo n.º 2
0
def PointDistributionModel(imgFolder):
    '''LOAD IMAGES'''
    path_to_lfpw = Path(imgFolder)
    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path_to_lfpw / '*.pts', verbose=True)):
        training_shapes.append(lg['all'])
    '''TRAIN PDM MODEL'''
    shape_model = OrthoPDM(training_shapes, max_n_components=None)
    '''MODIFY PARAMETERS'''
    shape_model.n_active_components = 20
    shape_model.n_active_components = 0.95
    return shape_model
Ejemplo n.º 3
0
    def set_target(self, target):
        if target.n_points < self.target.n_points:

            if target.n_points < self.n_landmarks:
                target = PointCloud(target.points[:self.n_align_lms])
                target = np.dot(np.dot(target.as_vector(), self.pinv_va), self.W)
            else:
                target = PointCloud(target.points[:self.n_landmarks])
                target = np.dot(np.dot(target.as_vector(), self.pinv_v), self.W)

            target = PointCloud(np.reshape(target, (-1, self.n_dims)))

        OrthoPDM.set_target(self, target)
Ejemplo n.º 4
0
    def set_target(self, target):
        r"""
        Update this object so that it attempts to recreate the ``new_target``.

        Parameters
        ----------
        new_target : `menpo.shape.PointCloud`
            The new target that this object should try and regenerate.
        """
        if target.n_points == self.n_landmarks:
            # densify target
            target = np.dot(np.dot(target.as_vector(), self.pinv_V), self.W)
            target = PointCloud(np.reshape(target, (-1, self.n_dims)))
        OrthoPDM.set_target(self, target)
def PDMModel(path, max_components=None):
    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path / '*.pts', verbose=True)):
        training_shapes.append(lg['all'])
    # train source PDM model
    shape_model = OrthoPDM(training_shapes, max_n_components=max_components)
    return shape_model, training_shapes
Ejemplo n.º 6
0
    def __init__(self, model, n_landmarks=0, n_align_lms=0):
        super(LinearWarp, self).__init__(model)
        self.pdm = OrthoPDM(model)
        self.n_landmarks = n_landmarks
        self.n_align_lms = n_align_lms
        self.W = np.vstack((self.similarity_model.components,
                            self.model.components))
        v = self.W[:, :self.n_dims*self.n_landmarks]
        self.pinv_v = np.linalg.pinv(v)

        va = self.W[:, :self.n_dims*self.n_align_lms]
        self.pinv_va = np.linalg.pinv(va)
Ejemplo n.º 7
0
def pca_image(images, max_n_components=None):

    training_shapes = []
    for image in images:
        training_shapes.append(image.landmarks['PTS'])  # lg['all']

    shape_model = OrthoPDM(training_shapes, max_n_components=max_n_components)
    print(shape_model)
    # visualize_pointclouds(training_shapes)
    # instance = shape_model.similarity_model.instance([100., -300., 0., 0.])
    # instance.view(render_axes=False)
    return shape_model
Ejemplo n.º 8
0
def pca(path_to_images, max_n_components=None):
    path_to_lfpw = Path(path_to_images)

    training_shapes = []
    for lg in print_progress(
            mio.import_landmark_files(path_to_lfpw / '*.pts', verbose=True)):
        training_shapes.append(lg)  # lg['all']

    shape_model = OrthoPDM(training_shapes, max_n_components=max_n_components)
    print(shape_model)
    # visualize_pointclouds(training_shapes)
    # instance = shape_model.similarity_model.instance([100., -300., 0., 0.])
    # instance.view(render_axes=False)
    return shape_model
Ejemplo n.º 9
0
    def d_dp(self, _):
        r"""
        The derivative with respect to the parametrisation changes evaluated at
        points.

        Parameters
        ----------
        points : ``(n_points, n_dims)`` `ndarray`
            The spatial points at which the derivative should be evaluated.

        Returns
        -------
        d_dp : ``(n_points, n_parameters, n_dims)`` `ndarray`
            The Jacobian with respect to the parametrisation.
        """
        return OrthoPDM.d_dp(self, _)[self.n_landmarks:, ...]
Ejemplo n.º 10
0
 def d_dp(self, _):
     return OrthoPDM.d_dp(self, _)[self.n_landmarks:, ...]
Ejemplo n.º 11
0
 def set_target(self, target):
     if target.n_points == self.n_landmarks:
         # densify target
         target = np.dot(np.dot(target.as_vector(), self.pinv_V), self.W)
         target = PointCloud(np.reshape(target, (-1, self.n_dims)))
     OrthoPDM.set_target(self, target)
Ejemplo n.º 12
0
 def d_dp(self, _):
     return OrthoPDM.d_dp(self, _)[self.n_landmarks:, ...]
Ejemplo n.º 13
0
 def set_target(self, target):
     if target.n_points == self.n_landmarks:
         # densify target
         target = np.dot(np.dot(target.as_vector(), self.pinv_V), self.W)
         target = PointCloud(np.reshape(target, (-1, self.n_dims)))
     OrthoPDM.set_target(self, target)
Ejemplo n.º 14
0
    for i in range(len(labels)):
        landmarks_dataset[i] = string_array_to_np_array(labels.iloc[i])

    return landmarks_dataset


# input
dataset_csv_path = "/disk1/ofirbartal/Projects/Dataset/GANeratedHands_Release/dataset_csv/test_dataset.csv"

landmarks = process_csv_input(dataset_csv_path)

start = time.time()
# PDM
training_shapes = [PointCloud(l) for l in landmarks]
shape_model = OrthoPDM(training_shapes, max_n_components=None)
shape_model.n_active_components = 0.95
end = time.time()
# shape_model = import_pickle(Path('pdm_weights.pkl'))
print(shape_model)
print("PDM Training Time: {}s".format(end - start))
export_pickle(shape_model, Path('test_pdm_weights.pkl'), overwrite=True)

# shape_model.set_target(result_landmarks) # project the target
# shape_model.target # the projected target

# draw
#create canvas on which the triangles will be visualized
# canvas = np.full([400,400], 255).astype('uint8')

#convert to 3 channel RGB for fun colors!
Ejemplo n.º 15
0
 def __init__(self, model, transform_cls, global_transform, source=None):
     self.pdm = OrthoPDM(model, global_transform)
     self._cached_points = None
     self.transform = transform_cls(source, self.target)