Created
December 21, 2021 19:06
-
-
Save luisquintanilla/dcd4f78d62f1b6f8d7d6fe160230b4b9 to your computer and use it in GitHub Desktop.
Style Transfer: Image to Mosaic F#, ML.NET, ONNX Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget:Microsoft.ML" | |
#r "nuget:Microsoft.ML.OnnxRuntime" | |
#r "nuget:Microsoft.ML.OnnxTransformer" | |
#r "nuget:Microsoft.ML.ImageAnalytics" | |
#r "nuget:System.Drawing.Common" | |
open System.IO | |
open System.Drawing | |
open Microsoft.ML | |
open Microsoft.ML.Data | |
// Source: https://github.com/onnx/models/blob/master/vision/style_transfer/fast_neural_style/model/mosaic-8.onnx | |
let onnxFilePath = Path.Join(__SOURCE_DIRECTORY__,"mosaic-8.onnx") | |
let ctx = new MLContext() | |
let data = | |
seq { | |
{| ImagePath = Path.Join(__SOURCE_DIRECTORY__,"forest.JPG") |} | |
} | |
let idv = ctx.Data.LoadFromEnumerable(data) | |
let pipeline = | |
EstimatorChain() | |
.Append(ctx.Transforms.LoadImages("Image",null,"ImagePath")) | |
.Append(ctx.Transforms.ResizeImages("ResizedImage",224,224,"Image")) | |
.Append(ctx.Transforms.ExtractPixels("input1","ResizedImage")) | |
.Append(ctx.Transforms.ApplyOnnxModel("output1","input1",onnxFilePath)) | |
.Append(ctx.Transforms.ConvertToImage(224,224,"TransformedImage","output1")) | |
let transformedData = pipeline.Fit(idv).Transform(idv) | |
let images = transformedData.GetColumn<Bitmap>("TransformedImage") | |
let firstImage = images |> Seq.head | |
firstImage.Save("transformed.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment