dotnet / dotnet/machinelearning-samples

How to convert float[] 1x3x512x152 to bitmap, by using ONNX model

Open
#860 1 comment 0 reactions 0 assignees View on GitHub
help wanted product-question
Dominant language
PowerShell
Stars
4.7k
Forks
2.7k
Avg merge
2d 22h
Merged PRs (30d)
1

Description

Hello,

I have some trouble trying to do pix2pix by modifying this model template. I wonder why all numbers in my vector float[] 1x3x512x152 are all 1? how could I get the predicted result (image)?

Thank you so much!

MLContext context = new MLContext();

// EmptyData
var emptyImageInput = new List();

// DataView
var data = context.Data.LoadFromEnumerable(emptyImageInput);

// Pipeline
var pipeline = context.Transforms.ResizeImages(resizing: Microsoft.ML.Transforms.Image.ImageResizingEstimator.ResizingKind.Fill,
outputColumnName: P2PSettings.ModelInput,
imageWidth: ImageSettings.imageWidth,
imageHeight: ImageSettings.imageHeight,
inputColumnName: nameof(LabelImage.image))
.Append(context.Transforms.ExtractPixels(outputColumnName: P2PSettings.ModelInput))
.Append(context.Transforms.ApplyOnnxModel(modelFile: modelFilePath,
outputColumnName: P2PSettings.ModelOutput,
inputColumnName: P2PSettings.ModelInput));

var model = pipeline.Fit(data);

// Prediction Engine
var predictionEngine = context.Model.CreatePredictionEngine(model);

Console.WriteLine("===Prediction Engine set up successfully====");
Console.WriteLine("");

Console.WriteLine("==================Load images===============");
var imagePaths = Directory.GetFiles(imagesFolder, "*.png");
var lenPath = imagePaths[0].Split("\\").Length;
foreach (var imagePath in imagePaths)
{
Console.WriteLine("Image Name: ", imagePath.Split("\\")[lenPath-1]);
Bitmap testImage;
using (var stream = new FileStream(imagePath, FileMode.Open))
{
testImage = (Bitmap)Image.FromStream(stream);
}

var prediction = predictionEngine.Predict(new LabelImage { image = testImage });
var predictedLabels = prediction.predictedLabels;

foreach (float n in predictedLabels)
{
Console.WriteLine(n.ToString());
}
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.