Skip to content

Instantly share code, notes, and snippets.

@luisquintanilla
Created December 21, 2021 19:06
Show Gist options
  • Save luisquintanilla/dcd4f78d62f1b6f8d7d6fe160230b4b9 to your computer and use it in GitHub Desktop.
Save luisquintanilla/dcd4f78d62f1b6f8d7d6fe160230b4b9 to your computer and use it in GitHub Desktop.
Style Transfer: Image to Mosaic F#, ML.NET, ONNX Sample
#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