machinelearning
machinelearning copied to clipboard
ResNet101 [ShapeInferenceError] Attribute pads has incorrect size
I am using ML.NET in C# to extract features for a reverse image search. It is based on the code sample from Microsoft's documentation at the following page: https://learn.microsoft.com/en-us/dotnet/api/microsoft.ml.onnxcatalog.dnnfeaturizeimage?view=ml-dotnet#microsoft-ml-onnxcatalog-dnnfeaturizeimage(microsoft-ml-transformscatalog-system-string-system-func((microsoft-ml-transforms-onnx-dnnimagefeaturizerinput-microsoft-ml-data-estimatorchain((microsoft-ml-transforms-columncopyingtransformer))))-system-string)
I have been successful at extracting features with ResNet18, TensorFlow, TensorFlowInceptionV3, and ONNX TinyYolo2.
Describe the bug I get the following error when trying to use ResNet50 and ResNet101. ResNet50
System.InvalidOperationException: 'Error initializing model :Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:Fail] Load model from C:\Users\myname\source\repos\ReverseImageSearch\ReverseImageSearch\bin\Debug\net7.0\DnnImageModels\ResNet50Onnx\ResNet50.onnx failed:Node (Pooling1096) Op (AveragePool) [ShapeInferenceError] Attribute pads has incorrect size
ResNet101
System.InvalidOperationException: 'Error initializing model :Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:Fail] Load model from C:\Users\myname\source\repos\ReverseImageSearch\ReverseImageSearch\bin\Debug\net7.0\DnnImageModels\ResNet101Onnx\ResNet101.onnx failed:Node (Pooling2286) Op (AveragePool) [ShapeInferenceError] Attribute pads has incorrect size
This is similar to the "Closed" issue at the following link https://github.com/dotnet/machinelearning/issues/4075
Here is the function
public static float[] GenerateImageFeaturesResNet101(string ImagePath)
{
var myFileName = System.IO.Path.GetFileName(ImagePath);
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();
//
var myImageNetData = new List<ImageNetData>
{
new ImageNetData {ImagePath = ImagePath, Label = myFileName}
};
var data = mlContext.Data.LoadFromEnumerable(myImageNetData);
var imagesFolder = Path.GetDirectoryName(ImagePath);
// Image loading pipeline.
//
var pipeline = mlContext.Transforms.LoadImages(outputColumnName: "ImageObject", imagesFolder, inputColumnName: "ImagePath")
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "ImageObjectResized", imageWidth:
224, imageHeight: 224, inputColumnName: "ImageObject"))
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "Pixels", inputColumnName: "ImageObjectResized"))
.Append(mlContext.Transforms.DnnFeaturizeImage(outputColumnName: "FeaturizedImage",
m => m.ModelSelector.ResNet101(mlContext, m.OutputColumn, m.InputColumn), inputColumnName: "Pixels"));
var transformedData = pipeline.Fit(data).Transform(data);
//Extractor image features
var FeaturizedImageValues = transformedData.GetColumn<float[]>("FeaturizedImage").ToArray();
return FeaturizedImageValues[0];
}
System Information
- OS & Version:Windows 10
- ML.NET Version: 2.0.1
- .NET Version: .NET 7.0
- microsoft.ml.dnnimagefeaturizer.resnet101 (0.20.1)
- microsoft.ml.dnnimagefeaturizer.resnet50 (0.20.1)
- microsoft.ml.dnnimagefeaturizer.resnet18 (0.20.1)
I get the same error when using resnet50, resnet101 and alexnet. All using version 0.21.0.
Using an older version of resnet101 yields the following error:
'Method not found: 'Void Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator..ctor(Microsoft.ML.Runtime.IHostEnvironment, System.String[], System.String[], System.String, System.Nullable`1<Int32>, Boolean, System.Collections.Generic.IDictionary`2<System.String,Int32[]>, .
System Information:
- Os : windows 11
- .NET 8.0
- Microsoft.ML.DnnImageFeaturizer.AlexNet : (0.21.0)
- Microsoft.ML.DnnImageFeaturizer.ResNet18 : (0.21.0) (working)
- Microsoft.ML.DnnImageFeaturizer.ResNet101 : (0.21.0)
- Microsoft.ML (3.0.0)
- Microsoft.ML.OnnxRuntime (1.16.3)
- Microsoft.ML.OnnxTransformer (3.0.0)