Skip to content

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

@jam0ss

Description

@jam0ss

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());
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededproduct-questionQuestion about ML.NET core product

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions