Version problem between ML.NET and tensorflow models

uygur_yuzereroglu

New member
Joined
Apr 26, 2024
Messages
4
Programming Experience
3-5
I develop a face recognizer(as an image classification model) software.i trained several ml.net models via image classification catalog and tensorflow models via colab.all models works satisfactory but i have a big problem; in order to use ml.net trained models i have to install nuget SciSharp.TensorFlow.Redist 2.3.1 but some of others(colab trained ones) gives this error when i try to use;

C#:
Tensorflow.InvalidArgumentError: Converting GraphDef to Graph has failed. The binary trying to import the GraphDef was built when GraphDef version was 440. The GraphDef was produced by a binary built when GraphDef version was 1645.

when i update nuget SciSharp.TensorFlow.Redist 2.16.0 colab trained models works fine but ml.net trained models gives this error;

C#:
System.EntryPointNotFoundException: Unable to find an entry point named 'TF_StringEncodedSize' in DLL 'tensorflow'.
 at Tensorflow.c_api.TF_StringEncodedSize(UInt64 len) at Microsoft.ML.Vision.ImageClassificationTrainer.EncodeByteAsString(VBuffer1 buffer)
    at Microsoft.ML.Vision.ImageClassificationTrainer.ImageProcessor.ProcessImage(VBuffer1& imageBuffer)
 at Microsoft.ML.Vision.ImageClassificationModelParameters.Classifier.Score(VBuffer1& image, Span1 classProbabilities)
 at Microsoft.ML.Vision.ImageClassificationModelParameters.<>c__DisplayClass22_02.<Microsoft.ML.Data.IValueMapper.GetMapper>b__0(VBuffer1& src, VBuffer1& dst)
    at Microsoft.ML.Data.SchemaBindablePredictorWrapperBase.<>c__DisplayClass19_02.b__0(TDst& dst)
 at Microsoft.ML.Data.PredictedLabelScorerBase.EnsureCachedPosition[TScore](Int64& cachedPosition, TScore& score, DataViewRow boundRow, ValueGetter1 scoreGetter)
    at Microsoft.ML.Data.MulticlassClassificationScorer.<>c__DisplayClass16_0.<GetPredictedLabelGetter>b__1(VBuffer1& dst)
 at Microsoft.ML.Data.TypedCursorable1.TypedRowBase.<>c__DisplayClass8_01.b__0(TRow row)
 at Microsoft.ML.Data.TypedCursorable1.TypedRowBase.FillValues(TRow row)
  at Microsoft.ML.Data.TypedCursorable1.RowImplementation.FillValues(TRow row)
 at Microsoft.ML.PredictionEngineBase2.FillValues(TDst prediction)
    at Microsoft.ML.PredictionEngine2.Predict(TSrc example, TDst& prediction)
 at Microsoft.ML.PredictionEngineBase`2.Predict(TSrc example) at FaceRecognizer.MyTrainer.Predictor.Predict(GlobalImageInput inp) in E:\source\repos\Setup_Projects\FaceRecognizer\FaceRecognizer\MyTrainer\Predictor.cs:line 52

i found that TF_StringEncodedSize function no longer exists after tensorflow 2.4.0 . Is there a way to use all models in my app?for example, can i use different versions of this SciSharp.TensorFlow.Redist nuget while runtime,i mean is there any way of changing the version of this nuget at runtime? if no, how can i handle this situation?
 
Last edited by a moderator:
What version of ML.NET do you have installed?
 
According to NuGet, It should already be at 3.0.1. But regardless, I only see a dependency on TensorFlow.NET, not SciSharp.TensorFlow.Redist. Does the docs tell you that you need to install this extra component because the NuGet package doesn't have the correct set of transitive dependencies?
 
According to NuGet, It should already be at 3.0.1. But regardless, I only see a dependency on TensorFlow.NET, not SciSharp.TensorFlow.Redist. Does the docs tell you that you need to install this extra component because the NuGet package doesn't have the correct set of transitive dependencies?

I couldn't find a solution.ML.Net image classification catalog models strictly requires SciSharp.TensorFlow.Redist 2.3.1,otherwise(using this configuration with tensorflow models) "Converting GraphDef to Graph has failed" error occures.Pretrained tensorflow models requires SciSharp.TensorFlow.Redist 2.16.0,otherwise(using this configuration with ML.Net models) "TF_StringEncodedSize" error occures.I tested both my prediction and training module with several nugget upgrades but the result was the same.I'll try to build tensorflow.dll from source code with adding missing functions to the code(good luck to me ;)).is there another option that i can change the SciSharp.TensorFlow.Redist version on runtime?
 
This might help if you attack the problem from Python/Colab side rather than the ML.NET side:

But to answer your question, in general there is no way to have two version of the same assembly loaded into the same .NET appdomain. I'm not sure if you can have different versions of the same assembly loaded in the different appdomains, but within the same process.

Personally, I would just go for the simplest approach would be to have two different child executables with the appropriate versions, and then have a main driver executable that communicates with the two children using some kind of transport (e.g. pipes, shared memory, TCP, HTTP, message queue, etc.)

The other thing I would explore is if exporting and importing ONNX models would help. Perhaps the export to an open standard for models might get rid of the dependencies for specific versions of TensorFlow.
 
This might help if you attack the problem from Python/Colab side rather than the ML.NET side:

But to answer your question, in general there is no way to have two version of the same assembly loaded into the same .NET appdomain. I'm not sure if you can have different versions of the same assembly loaded in the different appdomains, but within the same process.

Personally, I would just go for the simplest approach would be to have two different child executables with the appropriate versions, and then have a main driver executable that communicates with the two children using some kind of transport (e.g. pipes, shared memory, TCP, HTTP, message queue, etc.)

The other thing I would explore is if exporting and importing ONNX models would help. Perhaps the export to an open standard for models might get rid of the dependencies for specific versions of TensorFlow.

Thanks a lot for your help.I achieved to freeze and train no-top tensorflow models and convert them to ONNX on python/colab side already.then i trained them by adding LbfgsMaximumEntropy classifier layer on the ml.net side in order to get the best accuracies.but as far as i know training a simple ONNX model isn't supported in ml.net.
 
Back
Top Bottom