|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.ML.OnnxRuntime.Tensors; |
| 3 | +using OnnxStack.Core; |
| 4 | +using OnnxStack.Core.Config; |
| 5 | +using OnnxStack.Core.Image; |
| 6 | +using OnnxStack.Core.Model; |
| 7 | +using OnnxStack.ImageUpscaler.Common; |
| 8 | +using OnnxStack.ImageUpscaler.Extensions; |
| 9 | +using OnnxStack.ImageUpscaler.Models; |
| 10 | +using SixLabors.ImageSharp; |
| 11 | +using SixLabors.ImageSharp.PixelFormats; |
| 12 | +using System.IO; |
| 13 | +using System.Linq; |
| 14 | +using System.Threading; |
| 15 | +using System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace OnnxStack.FeatureExtractor.Pipelines |
| 18 | +{ |
| 19 | + public class ImageUpscalePipeline |
| 20 | + { |
| 21 | + private readonly string _name; |
| 22 | + private readonly ILogger _logger; |
| 23 | + private readonly UpscaleModel _upscaleModel; |
| 24 | + |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="ImageUpscalePipeline"/> class. |
| 28 | + /// </summary> |
| 29 | + /// <param name="name">The name.</param> |
| 30 | + /// <param name="upscaleModel">The upscale model.</param> |
| 31 | + /// <param name="logger">The logger.</param> |
| 32 | + public ImageUpscalePipeline(string name, UpscaleModel upscaleModel, ILogger logger = default) |
| 33 | + { |
| 34 | + _name = name; |
| 35 | + _logger = logger; |
| 36 | + _upscaleModel = upscaleModel; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets the name. |
| 42 | + /// </summary> |
| 43 | + /// <value> |
| 44 | + public string Name => _name; |
| 45 | + |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Loads the model. |
| 49 | + /// </summary> |
| 50 | + public async Task LoadAsync() |
| 51 | + { |
| 52 | + await _upscaleModel.LoadAsync(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Unloads the models. |
| 58 | + /// </summary> |
| 59 | + public async Task UnloadAsync() |
| 60 | + { |
| 61 | + await Task.Yield(); |
| 62 | + _upscaleModel?.Dispose(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Runs the upscale pipeline. |
| 68 | + /// </summary> |
| 69 | + /// <param name="inputImage">The input image.</param> |
| 70 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 71 | + /// <returns></returns> |
| 72 | + public async Task<DenseTensor<float>> RunAsync(InputImage inputImage, CancellationToken cancellationToken = default) |
| 73 | + { |
| 74 | + using (var image = await inputImage.ToImageAsync()) |
| 75 | + { |
| 76 | + var upscaleInput = CreateInputParams(image, _upscaleModel.SampleSize, _upscaleModel.ScaleFactor); |
| 77 | + var metadata = await _upscaleModel.GetMetadataAsync(); |
| 78 | + |
| 79 | + var outputTensor = new DenseTensor<float>(new[] { 1, _upscaleModel.Channels, upscaleInput.OutputHeight, upscaleInput.OutputWidth }); |
| 80 | + foreach (var imageTile in upscaleInput.ImageTiles) |
| 81 | + { |
| 82 | + cancellationToken.ThrowIfCancellationRequested(); |
| 83 | + |
| 84 | + var outputDimension = new[] { 1, _upscaleModel.Channels, imageTile.Destination.Height, imageTile.Destination.Width }; |
| 85 | + var inputTensor = imageTile.Image.ToDenseTensor(ImageNormalizeType.ZeroToOne, _upscaleModel.Channels); |
| 86 | + using (var inferenceParameters = new OnnxInferenceParameters(metadata)) |
| 87 | + { |
| 88 | + inferenceParameters.AddInputTensor(inputTensor); |
| 89 | + inferenceParameters.AddOutputBuffer(outputDimension); |
| 90 | + |
| 91 | + var results = await _upscaleModel.RunInferenceAsync(inferenceParameters); |
| 92 | + using (var result = results.First()) |
| 93 | + { |
| 94 | + outputTensor.ApplyImageTile(result.ToDenseTensor(), imageTile.Destination); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return outputTensor; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private static UpscaleInput CreateInputParams(Image<Rgba32> imageSource, int maxTileSize, int scaleFactor) |
| 105 | + { |
| 106 | + var tiles = imageSource.GenerateTiles(maxTileSize, scaleFactor); |
| 107 | + var width = imageSource.Width * scaleFactor; |
| 108 | + var height = imageSource.Height * scaleFactor; |
| 109 | + return new UpscaleInput(tiles, width, height); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Creates the pipeline from a UpscaleModelSet. |
| 115 | + /// </summary> |
| 116 | + /// <param name="modelSet">The model set.</param> |
| 117 | + /// <param name="logger">The logger.</param> |
| 118 | + /// <returns></returns> |
| 119 | + public static ImageUpscalePipeline CreatePipeline(UpscaleModelSet modelSet, ILogger logger = default) |
| 120 | + { |
| 121 | + var upscaleModel = new UpscaleModel(modelSet.UpscaleModelConfig.ApplyDefaults(modelSet)); |
| 122 | + return new ImageUpscalePipeline(modelSet.Name, upscaleModel, logger); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Creates the pipeline from the specified folder. |
| 128 | + /// </summary> |
| 129 | + /// <param name="modelFolder">The model folder.</param> |
| 130 | + /// <param name="deviceId">The device identifier.</param> |
| 131 | + /// <param name="executionProvider">The execution provider.</param> |
| 132 | + /// <param name="logger">The logger.</param> |
| 133 | + /// <returns></returns> |
| 134 | + public static ImageUpscalePipeline CreatePipeline(string modelFile, int scaleFactor, int sampleSize = 512, int deviceId = 0, ExecutionProvider executionProvider = ExecutionProvider.DirectML, ILogger logger = default) |
| 135 | + { |
| 136 | + var name = Path.GetFileNameWithoutExtension(modelFile); |
| 137 | + var configuration = new UpscaleModelSet |
| 138 | + { |
| 139 | + Name = name, |
| 140 | + IsEnabled = true, |
| 141 | + DeviceId = deviceId, |
| 142 | + ExecutionProvider = executionProvider, |
| 143 | + UpscaleModelConfig = new UpscaleModelConfig |
| 144 | + { |
| 145 | + Channels = 3, |
| 146 | + SampleSize = sampleSize, |
| 147 | + ScaleFactor = scaleFactor, |
| 148 | + OnnxModelPath = modelFile |
| 149 | + } |
| 150 | + }; |
| 151 | + return CreatePipeline(configuration, logger); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments