Saving Model (Tutorial 06)

65 views
Skip to first unread message

Paul-Emmanuel Edeline

unread,
Dec 9, 2024, 8:52:31 AM12/9/24
to scalismo

Dear all,

I am new to Scalismo and currently trying to familiarize myself with its processes. I’m facing an issue saving a model into a .h5 file. Despite exploring various functions, I haven’t been able to find a way to achieve this.

I started with the code from Tutorial 06, which works well and allows me to create a decent model from my meshes. However, I can’t seem to find any function to save a PointDistributionModel. The methods I’ve come across seem to be tailored for StatisticalMeshModel objects, but since PointDistributionModel is a more general concept, I’m unsure how to handle it.

Apologies if this is a basic question, but I’ve consulted several Scalismo users who couldn’t provide a clear solution. Any guidance would be greatly appreciated!

Thank you,
Paul

Andreas Morel-Forster

unread,
Dec 9, 2024, 9:17:25 AM12/9/24
to scalismo
Hi Paul

Welcome in the community and I hope that, aside of the IO issue, you had a smooth start.

You are right that the PointDistributionModel (PDM) is more general and we do offer a general IO function. When we serialize a model, we always want to store also the domain information. Based on the different domains of e.g. a TriangleMesh, a LineMesh or for points only, an UnstructuredPointsDomain, we offer different IO functions. In case you have only points in your model, and do not have anything more that you know about your domain, then you can use the following to read and write your model:

  val pdm: PointDistributionModel[_3D, UnstructuredPointsDomain] = ???
  StatisticalModelIO.writeStatisticalPointModel3D(pdm, file).get
  val model = StatisticalModelIO.readStatisticalPointModel3D(file).get

In case this is not what you want, maybe you can provide us more details about the domain you are using.

Best regards
Andreas

--

Andreas Morel-Forster, PhD

Shapemeans GmbH
Aeschenplatz 6
4052 Basel, Switzerland

Paul-Emmanuel Edeline

unread,
Dec 9, 2024, 10:00:11 AM12/9/24
to scalismo
Hi Andreas,

Thank you very much for your quick and helpful response.

I now understand that the function StatisticalModelIO.writeStatisticalPointModel3D is specifically designed for models with an UnstructuredPointsDomain. However, in my case, the domain of my PointDistributionModel is a TriangleMesh.

After exploring the StatisticalModelIO functions, I came across writeStatisticalTriangleMeshModel3D, which seems more appropriate for my issue. However, when I attempt to write the model using this function, the output file appears to be incorrect. Upon checking the file format, I get the following: 

"file ModelDC.h5; ModelDC.h5: ASCII text, with very long lines (65536), with no line terminators"

I was expecting the file to be saved in the proper HDF5 format (Hierarchical Data Format (version 5) data). It seems something went wrong during the saving process, but I’m not sure what.

If more details are needed, the specific model I want to save is modelFromDataCollection from Tutorial06, created with the following lines:

// Create a DataCollection from the aligned meshes
val dc = DataCollection.fromTriangleMesh3DSequence(reference, meshes)

// Create a statistical shape model using PCA on the DataCollection
val modelFromDataCollection = PointDistributionModel.createUsingPCA(dc)

val modelGroup2 = ui.createGroup("modelGroup2")
ui.show(modelGroup2, modelFromDataCollection, "ModelDC")

// Specify the file path for saving the model
val modelFile = new File("ModelDC.h5")
// Save the model to the file
StatisticalModelIO.writeStatisticalTriangleMeshModel3D(modelFromDataCollection, modelFile).get

I also realize that I might have been unclear in my previous message. My goal is to save the Statistical Shape Model created from the PointDistributionModel using PCA, not the PointDistributionModel itself.

Do you have any guess about what am I doing wrong ? 

Thank you again for your assistance,

Best regards,

Paul

Andreas Morel-Forster

unread,
Dec 9, 2024, 11:11:08 AM12/9/24
to scal...@googlegroups.com

Hi Paul

You are absolutely right about the writing of a triangle mesh model.

You get the error because you write a ".h5.json" file and not a ".h5" file. Our reader then tries to read ".h5" file and fails. We switched to ".h5.json" (see here) due to vulnerabilities (see here) in the hdf5 lib we used before. Our writer just does not warn at the moment if the file ending you provide is .h5, which something we should fix.

Current situation:

  • The reader can read ".h5.json" and ".h5" files, but does it based on the file extension.
  • Writing is currently only ".json.h5". We could in the future maybe again support ".h5" as some libraries evolved further.
  • If you need ".h5" files, there exists a python tool that can do the transcoding.

So you could do the following:

val file = new File("out.h5.json")
val fileIn = new File("in.h5")

val pdm: PointDistributionModel[_3D, TriangleMesh] = bfm
StatisticalModelIO.writeStatisticalTriangleMeshModel3D(pdm, file).get
// jsontoh5.py in.h5.json out.h5
val model = StatisticalModelIO.readStatisticalTriangleMeshModel3D(fileIn).get

Best, Andreas
--
You received this message because you are subscribed to a topic in the Google Groups "scalismo" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scalismo/x_d7IN2e45o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scalismo+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/scalismo/0f26a418-7112-4811-9e12-b307b8a1e743n%40googlegroups.com.
-- 
Andreas Morel-Forster, PhD

Shapemeans GmbH
Aeschenplatz 6, 4052 Basel, Switzerland
Email: andrea...@shapemeans.com
www.shapemeans.com

Paul-Emmanuel Edeline

unread,
Dec 9, 2024, 11:45:19 AM12/9/24
to scalismo
Hi Andreas,

Thanks for pointing out the change with .h5 and .h5.json - I'd missed it completely! Your code works perfectly now, and the Python tool allowed me to handle the transcoding without any problems.

I really appreciate your help - I couldn't have wished for anything better!

Sincerely,
Paul
Reply all
Reply to author
Forward
0 new messages