I am just starting to learn AI and I chose to follow a tutorial provided by Microsoft Learn.
In this tutorial, I am learning to create and train a classification model to recognize images of soup, vegetables, or desserts.
I have downloaded this model in ONNX format and I am trying to use it in a small simple program written in VB.NET (sorry for that, it’s the language I am most comfortable with).
I think I am doing something wrong because no matter what image I provide to the model, the predictions are always the same: soup and all probabilities are the same : +- 52% for soup +- 39% for vegetables and +- 13% for dessert
Could you guide me on what I am doing wrong?
Thank you in advance.
I have simplified my code to eliminate the superfluous, and you will also find the properties of my model.
Imports Microsoft.ML.OnnxRuntime
Imports Microsoft.ML.OnnxRuntime.Tensors
Public Class classifierInput
Public Property data As DenseTensor(Of Single)
Public Sub New(ByVal imgpath As String)
Dim img As Image = New Bitmap(Bitmap.FromFile(imgpath), New Size(224, 224))
Me.data = ConvertImageToFloatTensor(img)
End Sub
Public Function ConvertImageToFloatTensor(img As Bitmap) As DenseTensor(Of Single)
Dim data As New DenseTensor(Of Single)(New Integer() {1, 3, img.Width, img.Height})
For y As Integer = 0 To img.Height - 1
For x As Integer = 0 To img.Width - 1
Dim pixel As Color = img.GetPixel(x, y)
data(0, 0, y, x) = pixel.B / 255.0F
data(0, 1, y, x) = pixel.G / 255.0F
data(0, 2, y, x) = pixel.R / 255.0F
Next
Next
Return data
End Function
End Class
Public Class classifierModel
Private session As InferenceSession
Public Shared Async Function CreateFromStreamAsync(modelPath As String) As Task(Of classifierModel)
Dim learningModel As New classifierModel()
learningModel.session = Await Task.Run(Function() New InferenceSession(modelPath))
Return learningModel
End Function
Public Async Function EvaluateAsync(input As classifierInput) As Task(Of List(Of DisposableNamedOnnxValue))
Dim inputs As New List(Of NamedOnnxValue) From {
NamedOnnxValue.CreateFromTensor("data", input.data)
}
Dim results = Await Task.Run(Function() session.Run(inputs))
Return results
End Function
End Class
I have been focusing on converting images to tensors, but I am unsure about what I am doing wrong