Input shape mismatch: Input 'image_tensor' has shape (-1, -1, -1, 3), but input data is of length 2160000.'
System information
- OS version/distro: Windows 10
- .NET Version (eg., dotnet --info): .NET Core 2.1
Issue
-
What did you do? I created my own TensorFlow object detection model and tried to load it in a C# console application.
-
What happened? When I tried to create a model by using pipeline.fit I get this error System.ArgumentOutOfRangeException: ”System.InvalidOperationException: 'Input shape mismatch: Input 'image_tensor' has shape (-1, -1, -1, 3), but input data is of length 2160000.'
-
What did you expect? I would have been able to create the model and continue to create the prediction engine and test my TensorFlow.
Source code / logs
Here is how I am loading the pipeline
var pipeline = mlContext.Transforms .LoadImages("image_tensor", @"E:\output_original_image", nameof(ImageData.ImagePath)) .Append(mlContext.Transforms.ResizeImages(outputColumnName: "image_tensor", imageWidth: 1200, imageHeight: 600, inputColumnName: "image_tensor")) .Append(mlContext.Transforms.ExtractPixels(outputColumnName: "image_tensor", interleavePixelColors: true, outputAsFloatArray: false)) .Append(mlContext.Model.LoadTensorFlowModel(modelLocation) .ScoreTensorFlowModel(outputColumnNames: new[] { "detection_boxes", "detection_classes", "detection_scores", "num_detections" }, inputColumnNames: new[] { "image_tensor" }, addBatchDimensionInput: true));
When I try to load the model inPython like so it works just fine.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0')
image = cv2.imread(PATH_TO_IMAGE) image_expanded = np.expand_dims(image, axis=0)
print("going to run the model now")
(boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) Any ideas on how to find the input column that ML.NET wants? Please paste or attach the code or logs or traces that would be helpful to diagnose the issue you are reporting.
@VinuthaRaghavendra could you please share your model file so we can investigate further?
This might have been fixed by this commit. Do we have a nightly build nupkg for user to try out? @frank-dong-ms
@wangyems Thanks, we do have nightly build package with latest fix.
@VinuthaRaghavendra this is the latest nuget package with fix Ye mentioned, please have a try: microsoft.ml.vision.1.5.1-29103-1.nupkg.zip
Thanks for the response,
I tried installing nupkg in the project
I get these error:
Install-Package C:\microsoft.ml.vision.1.5.1-29103-1.nupkg The 'Source' parameter is not respected for the transitive package management based project(s) ObjectDetection_Pipeline. The enabled sources in your NuGet configuration will be used. Restoring packages for C:\ObjectDetection_Pipeline.csproj... Install-Package : NU1605: Detected package downgrade: Microsoft.ML.TensorFlow from 1.5.1-29103-1 to 1.5.0. Reference the package directly from the project to select a different version. ObjectDetection_Pipeline -> Microsoft.ML.Vision 1.5.1-29103-1 -> Microsoft.ML.TensorFlow (>= 1.5.1-29103-1) ObjectDetection_Pipeline -> Microsoft.ML.TensorFlow (>= 1.5.0) At line:1 char:1
- Install-Package C:\ObjectDetection_P ...
-
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPac kageCommand
Install-Package : Package restore failed. Rolling back package changes for 'ObjectDetection_Pipeline'. At line:1 char:1
- Install-Package C:\ObjectDetection_P ...
-
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPac kageCommand
@VinuthaRaghavendra I see, which packages do you use in your project? I will give you all the packages of latest version so you won't have package dependency issue.
Ok.. Microsoft.ML.ImageAnalytics 1.5.0, Microsoft.ML.TensorFlow 1.5.0 , SciSharp.TensorFlow.Redist 1.15.0
I was with the same error , when i try this new package i got this error : Severity Code Description Project File Line Suppression State Error NU1102 Unable to find package Microsoft.ML with version (>= 1.5.1-29103-1)
- Found 22 version(s) in nuget.org [ Nearest version: 1.5.0 ]
- Found 1 version(s) in Local [ Nearest version: 1.5.0-preview-28305-3 ]
- Found 0 version(s) in Microsoft Visual Studio Offline Packages TesteApp
I put all dependent packages into the zip, please have a try. @erlonted @VinuthaRaghavendra
I have tried installing nuget packages from the Downloads.zip
I get these error even after running with "x64-bits"

{System.FormatException: Tensorflow exception triggered while loading model. ---> System.DllNotFoundException: Unable to load DLL 'tensorflow' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
@VinuthaRaghavendra I see, in that case could you please share your model file and project so I can have a try? If this is already fix in Ye's PR then you can get work version from next release or I can give you workable nuget packages if necessary(although these packages are internal), if not we can have a further look.
I am having exactly same problem. I am trying to run retrained ssd_inception_v2_coco model from TFOD model zoo. Here is a link to my retrained model. It throws the same exception for both models.
@frank-dong-ms Here is my dummy repo with that problem. To run it you have to download assets and paste it into the root folder
I put all dependent packages into the zip, please have a try. @erlonted @VinuthaRaghavendra
Now it´s working , finally i can retrive the value of 4 tensors : detection_boxes , detection_scores , num_detections , detection_classes
I have tried installing nuget packages from the Downloads.zip
I get these error even after running with "x64-bits"
{System.FormatException: Tensorflow exception triggered while loading model. ---> System.DllNotFoundException: Unable to load DLL 'tensorflow' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Try to add reference to SciSharp.TensorFlow.Redist this package contains the versions of tensorflow dll´s
@erlonted @frank-dong-ms I can confirm that provided packages in Downloads.zip + adding SciSharp.TensorFlow.Redist solves the problem. Thank you!

I am loading the data in the below shown format.. Can you please tell how to get the detection_score from model.Transform(data);
var data = mlContext.Data.LoadFromEnumerable(new List<ImageData>());
var pipeline = mlContext.Transforms
.LoadImages("image_tensor", @"E:\output_original_image\", nameof(ImageData.ImagePath))
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "image_tensor", imageWidth: 1200, imageHeight: 600, inputColumnName: "image_tensor"))
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "image_tensor", interleavePixelColors: true, outputAsFloatArray: false))
.Append(mlContext.Model.LoadTensorFlowModel(modelLocation)
.ScoreTensorFlowModel(outputColumnNames: new[] { "detection_boxes", "detection_classes", "detection_scores", "num_detections" }, inputColumnNames: new[] { "image_tensor" }, addBatchDimensionInput: false));
var model = pipeline.Fit(data);
var probablities = model.Transform(data);
I am loading the data in the below shown format.. Can you please tell how to get the detection_score from model.Transform(data);
var data = mlContext.Data.LoadFromEnumerable(new List<ImageData>()); var pipeline = mlContext.Transforms .LoadImages("image_tensor", @"E:\output_original_image\", nameof(ImageData.ImagePath)) .Append(mlContext.Transforms.ResizeImages(outputColumnName: "image_tensor", imageWidth: 1200, imageHeight: 600, inputColumnName: "image_tensor")) .Append(mlContext.Transforms.ExtractPixels(outputColumnName: "image_tensor", interleavePixelColors: true, outputAsFloatArray: false)) .Append(mlContext.Model.LoadTensorFlowModel(modelLocation) .ScoreTensorFlowModel(outputColumnNames: new[] { "detection_boxes", "detection_classes", "detection_scores", "num_detections" }, inputColumnNames: new[] { "image_tensor" }, addBatchDimensionInput: false)); var model = pipeline.Fit(data); var probablities = model.Transform(data);
I used this way : 1 - Create a class to receive output values public class Output { [ColumnName("detection_boxes")] public float[] Boxes { get; set; }
[ColumnName("detection_classes")]
public float[] Label { get; set; }
[ColumnName("detection_scores")]
public float[] Score { get; set; }
[ColumnName("num_detections")]
public float[] Detections { get; set; }
} 2 - Transform the result values var transformedDataPoints = mlContext.Data.CreateEnumerable(probablities , false); foreach (var item in transformedDataPoints) {
} 3 - The values in the foreach item was in tensorflow format and must be converted you can see an example at the end of this file https://github.com/migueldeicaza/TensorFlowSharp/blob/master/Examples/ExampleObjectDetection/Program.cs
Thanks everyone, we are about to ship new version of ML.NET with fix to this issue so you can expect to get new packages from public nuget source soon. Close this issue now, feel free to reopen if any necessary, thanks.
duplicate of below issue: https://github.com/dotnet/machinelearning/issues/4364