# Shape modelling with Gaussian processes and kernels

In this tutorial we learn how to define our own Gaussian processes using analytically defined kernels. Further, we experiment with different kernels that are useful in shape modelling.

##### Related resources

The following resources from our online course may provide some helpful context for this tutorial:

- Covariance functions (Video)
- Constructing kernels for shape modelling (Article)
- Enlarging the flexibility of statistical shape models (Article)

##### Preparation

As in the previous tutorials, we start by importing some commonly used objects and initializing the system.

In the following we will always visualize the effect of different Gaussian process models, by applying the deformations to a reference mesh. We therefore start by loading the mesh and visualizing it in a separate group.

## Modelling deformations using Gaussian processes:

A Gaussian Process is defined by two components: the **mean function** and the **covariance function**.

##### The mean:

As we are modelling deformation fields, the mean of the Gaussian process will, of course, itself be a deformation field. In terms of shape models, we can think of the mean function as the deformation field that deforms our reference mesh into the mean shape.

If the reference shape that we choose corresponds approximately to an average shape, and we do not have any further knowledge about our shape space, it is entirely reasonable to use a zero mean; I.e. a deformation field which applies to every point a zero deformation.

##### The covariance function:

The covariance function, which is also referred to as a *kernel*,
defines the properties that characterize likely deformations in our model.
Formally, it is a symmetric, positive semi-definite function,
$k: X \times X \to R^{ d \times d}$, which defines the covariance between the
values at any pair of points $x, x'$ of the domain. Since we are modelling deformation
fields (I.e. vector-valued functions), the covariance function is matrix-valued.

To define a kernel in Scalismo, we need to implement the following methods of the abstract class `MatrixValuedPDKernel`

:

The field `outputDim`

determines the dimensionality of the values we model. In our case, we model 3D vectors, and hence `outputDim`

should be 3.
The `domain`

indicates the set of points on which our kernel is defined. Most often, we set this to the entire Euclidean space `RealSpace3D`

.
Finally `k`

denotes the covariance function.

The most often used kernel is the Gaussian kernel. Recall that the scalar-valued Gaussian kernel, is defined by the following formula:

A corresponding matrix-valued kernel can be obtained by multiplying the value with an identity matrix (which implies, that we treat each space dimension as independent). In Scalismo, this is defined as follows:

This constructions allows us to define any kernel. For the most commonly used ones, such as the Gaussian kernel, there is, however, an easier way in Scalismo. First, the scalar-valued Gaussian kernel is already implemented in Scalismo:

Further, the class `DiagonalKernel`

allows us to turn any scalar-valued kernel into a matrix-valued kernel,
by specifying for each dimension of the output-space a kernel and assuming them to be independent. To obtain the same kernel as defined above, we can write:

In this case, since we are using the same kernel in every space dimension, we can write this even more succinct as:

##### Building the GP :

Now that we have our mean and covariance functions, we can build a Gaussian process as follows:

We can now sample deformations from our Gaussian process **at any desired set of points**. Below we choose the points to be those of the reference mesh:

The result is an instance from the Gaussian Process evaluated at the points we indicated; in this case on the points of the reference mesh.

We can visualize its effect by interpolating the deformation field, which we then use to deform the reference mesh:

#### Low-rank approximation

Whenever we create a sample using the `sampleAtPoints`

method of the Gaussian process, internally a matrix of dimensionality $nd \times nd$,where $n$ denotes the number of points and $d$ the dimensionality of the output space, is created. Hence if we want to sample
from many points we quickly run out of memory.

We can get around this problem by computing a low-rank approximation of the Gaussian process.
To obtain such a representation in Scalismo, we can use the method
``approximateGPCholesky`

of the *LowRankGaussianProcess* object.

This call computes a finite-rank approximation of the Gaussian Process using a Pivoted Cholesky approximation. The procedure automatically chooses the rank (I.e. the number of basis functions of the Gaussian process), such that the given relative error is achieved. (The error is measures in terms of the variance of the Gaussian process, approximated on the points of the reference Mesh). Using this low rank Gaussian process, we can now directly sample continuous deformation fields:

These in turn, can be used to warp a reference mesh, as discussed above:

More conveniently, we can visualize the sampled meshes by building again a `StatisticalMeshModel`

,

which we can directly visualize

### Building more interesting kernels

In the following we show a few more examples of kernels, which are interesting for shape modelling.

#### Kernels from Statistical Shape Models

As discussed previously, a Statistical Shape Model (SSM) in Scalismo is a discrete Gaussian process. We have seen how to interpolate it to obtain a continuously defined Gaussian Process. As any Gaussian process is completely defined by its mean and covariance function, it follows that this is also true for the GP in our statistical shape model.

This allows us to use this *sample covariance kernel* in combination with other kernels.
For example, we often want to slightly enlarge the flexibility of our statistical models.
In the following, we show how this can be achieved.

In a first step, we get the Gaussian process from the model an interpolate it.

We can then access its covariance function, which is a kernel:

In the next step, we model the additional variance using a Gaussian kernel and add it to the
*sample covariance kernel*.

Finally, we build the Gaussian process with the new kernel.

From here on, we follow the steps outlined above to obtain the *augmented* SSM.

#### Changepoint kernel:

Another very useful kernel is the *changepoint kernel*.
A changepoint kernel is a combination of different kernels, where each kernel is active only in
a certain region of the space.

Here we show how we can define a kernel, which has different behavior in two different regions.

Let's visualize its effect with two different Gaussian Kernels

As you can see each kernel is now active only on one half of the face.

#### Symmetrizing a kernel

Quite often, the shapes that we aim to model exhibit a symmetry. This is particularly valid in the case of faces. Therefore when modelling over such shapes, one would want deformation fields that yield symmetric shapes.

Once we obtained a kernel yielding the type of deformations we desire, it is possible to symmetrize the resulting deformation fields by applying the formula below:

where *x _{m}* is the symmetric point to

*x*around the YZ plane.

The resulting kernel will preserve the same smoothness properties of the deformation fields while adding the symmetry around the YZ plane.

Let's turn it into code: