We are working on how to consume the tensorflow model in .Net using ML.NET. We are using below tutorial as reference :
Tutorial Link : https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification
We tested with model that is used in the Tutorial and it worked fine. But, when we replace tutorial model with our tensorflow model (object detection model which we have exported from Azure Custom Vision, .pb file), it is throwing an Exception saying ‘Invalid GraphDef’, while loading the tensor flow model. The same custom vision model works fine when consumed in Python code.
''' Code :
IEstimator<ITransformer> pipeline = mlContext.Transforms.LoadImages(outputColumnName: "input",
imageFolder: _imagesFolder, inputColumnName: nameof(ImageData.ImagePath))
.Append(mlContext.Model.LoadTensorFlowModel(_inceptionTensorFlowModel)
.ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2_pre_activation" }, inputColumnNames: new[]
{ "input" }, addBatchDimensionInput: true))
`
Exception Details :
Exception Message : Tensorflow exception triggered while loading model
Source : Microsoft.ML.TensorFlow
Stacktrace :
at Microsoft.ML.TensorFlow.TensorFlowUtils.LoadTFSessionByModelFilePath(IExceptionContext ectx, String modelFile, Boolean metaGraph)
at Microsoft.ML.TensorFlow.TensorFlowUtils.GetSession(IHostEnvironment env, String modelPath, Boolean metaGraph)
at Microsoft.ML.TensorFlow.TensorFlowUtils.LoadTensorFlowModel(IHostEnvironment env, String modelPath)
at Microsoft.ML.TensorflowCatalog.LoadTensorFlowModel(ModelOperationsCatalog catalog, String modelLocation)
at PredictionModel.Program.GenerateModel(MLContext mlContext) in D:\Cognitive-Samples-VideoFrameAnalysis-master\Windows\PredictionModel\Program.cs:line 35
at PredictionModel.Program.Main(String[] args) in D:\Cognitive-Samples-VideoFrameAnalysis-master\Windows\PredictionModel\Program.cs:line 22
Inner Exception:
Exception Message : Invalid GraphDef
Source : TensorFlow.NET
Stack trace :
at Tensorflow.Status.Check(Boolean throwException)
at Tensorflow.Graph.Import(Byte[] bytes, String prefix)
at Tensorflow.Graph.Import(String file_path, String prefix)
at Microsoft.ML.TensorFlow.TensorFlowUtils.LoadTFSessionByModelFilePath(IExceptionContext ectx, String modelFile, Boolean metaGraph)
'''