Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw a random coefficient sample in MoMoInstance #94

Merged
merged 6 commits into from
Mar 29, 2018

Conversation

andreas-schneider
Copy link

Currently, it is laborious to draw a random MoMoInstance random coefficient that can be used for example rendering a random model sample.

Functionality:
Draws shape, color and expression vector entries N(0,1) distributed.

This is a possible use case of the proposed functionality:

val model: MoMo = ???
val renderer = MoMoRenderer(model)
val parameter = RenderParameter.defaultSquare.withMoMo(MoMoInstance.sample(model))
val randomRendering = renderer.renderImage(parameter)

Copy link
Member

@Andreas-Forster Andreas-Forster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am somehow not sure if we should provide this functionality in the MoMoInstance or MoMoCoefficients class. Of course, at the moment they have a very strict setting they are applied to in our framework but others might reuse them without the restriction of each coefficient being standard Gaussian distributed. This property is induced by using a specific model and not from the coefficients class.

@@ -17,6 +17,7 @@
package scalismo.faces.momo

import breeze.linalg.DenseVector
import breeze.stats.distributions.Gaussian
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not move the functionality from MoMoInstance to here then this import is not needed.


/** create a random coefficient vector with its elements N(0,1) distributed. */
def sample(model: MoMo, modelURI: URI)(implicit rnd: Random): MoMoInstance = {
val fullShapeCoeffs = for (_ <- 0 until model.neutralModel.shape.rank) yield Gaussian(0, 1).draw()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why creating a Gaussian(0,1) three times? You could also use rng.breezeRandBasis.gaussian.draw() instead.

@@ -141,6 +143,20 @@ object MoMoInstance {
modelURI: URI): MoMoInstance = {
fromCoefficients(MoMoCoefficients.zeros(shapeComponents, colorComponents, expressionComponents), modelURI)
}

/** create a random coefficient vector with its elements N(0,1) distributed. */
def sample(model: MoMo, modelURI: URI)(implicit rnd: Random): MoMoInstance = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first part of this function should go to MoMoCoefficients.sample() and then call this function here.

val fullShapeCoeffs = for (_ <- 0 until model.neutralModel.shape.rank) yield Gaussian(0, 1).draw()
val fullColorCoeffs = for (_ <- 0 until model.neutralModel.color.rank) yield Gaussian(0, 1).draw()
val expressionRank = model.expressionModel.get.expression.rank
val coeff = if(expressionRank > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need to check. The if and else branch do exactly the same for values smaller or equal zero.

@BernhardEgger
Copy link
Member

In this implementation the coefficients are always of full rank of the model. In practice it is often desired to have only the first x parameters. The MoMo itself has already a sample function which gives the VertexColorMesh directly. Without the possibility to use less parameters I see no benefit...

@andreas-schneider
Copy link
Author

@Andreas-Forster
I have incorporated your suggestions.

@BernhardEgger

  • The MoMo.sample() method can not always be used, for example when wanting to render a random sample with the MoMoRenderer. There you need to supply the coefficients.
  • You are right, usefulness will be incresed when we can choose the size of the vectors. I added the functionality. You can now choose the size of MoMoCoefficients and MoMoInstance.

@sschoenborn
Copy link
Contributor

Just a side note: In PancakeDLRGP you have a coefficientsDistribution - this could be a possible concept to capture the distribution (including a sample() method) of the coefficients for a model

@Andreas-Forster
Copy link
Member

Andreas-Forster commented Mar 29, 2018

@sschoenborn Thank you for pointing that out. I think this is maybe the best solution for this. I provided the implementation in addition to the former one.

@BernhardEgger I think it is a rare case where you want to have coefficients with a lot of different length in one application. So given this new solution, you would truncate the model first before sampling the coefficients. This makes also the generation of instances faster afterward.

@andreas-schneider It would now be possible to write:

  val model: MoMo = ???
  val modelURI: URI = ???
  val renderer = MoMoRenderer(model)
  val parameter = RenderParameter.defaultSquare.withMoMo(MoMoInstance.fromCoefficients(model.sampleCoefficients(),modelURI))
  val randomRendering = renderer.renderImage(parameter)

What do you think about this solution?

(Please do not merge as we should clean one of the solutions before.)

@unibas-gravis unibas-gravis deleted a comment Mar 29, 2018
@andreas-schneider
Copy link
Author

Yes, the sampleCoefficients sounds good. I have updated it accordingly.

@Andreas-Forster Andreas-Forster merged commit 58f71fb into master Mar 29, 2018
@Andreas-Forster Andreas-Forster deleted the feature-momoinstance-random-sample branch March 29, 2018 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants