dotnet/machinelearning-samples
在 GitHub 查看How to convert float[] 1x3x512x152 to bitmap, by using ONNX model
Open
#860 创建于 2020年11月20日
help wantedproduct-question
描述
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<LabelImage>();
// 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<LabelImage, PredictedImage>(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());
}
}