|
| 1 | +using Microsoft.ML.OnnxRuntime; |
| 2 | +using Microsoft.ML.OnnxRuntime.Tensors; |
| 3 | +using OnnxStack.Core.Config; |
| 4 | +using OnnxStack.Core.Services; |
| 5 | +using OnnxStack.StableDiffusion.Common; |
| 6 | +using OnnxStack.StableDiffusion.Config; |
| 7 | +using OnnxStack.StableDiffusion.Helpers; |
| 8 | +using SixLabors.ImageSharp; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
| 12 | + |
| 13 | + |
| 14 | +namespace OnnxStack.StableDiffusion.Diffusers.LatentConsistency |
| 15 | +{ |
| 16 | + public sealed class ImageDiffuser : TextDiffuser |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="ImageDiffuser"/> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="configuration">The configuration.</param> |
| 22 | + /// <param name="onnxModelService">The onnx model service.</param> |
| 23 | + public ImageDiffuser(IOnnxModelService onnxModelService, IPromptService promptService) |
| 24 | + : base(onnxModelService, promptService) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets the timesteps. |
| 31 | + /// </summary> |
| 32 | + /// <param name="prompt">The prompt.</param> |
| 33 | + /// <param name="options">The options.</param> |
| 34 | + /// <param name="scheduler">The scheduler.</param> |
| 35 | + /// <returns></returns> |
| 36 | + protected override IReadOnlyList<int> GetTimesteps(PromptOptions prompt, SchedulerOptions options, IScheduler scheduler) |
| 37 | + { |
| 38 | + // Image2Image we narrow step the range by the Strength |
| 39 | + var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps); |
| 40 | + var start = Math.Max(options.InferenceSteps - inittimestep, 0); |
| 41 | + return scheduler.Timesteps.Skip(start).ToList(); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Prepares the latents for inference. |
| 47 | + /// </summary> |
| 48 | + /// <param name="prompt">The prompt.</param> |
| 49 | + /// <param name="options">The options.</param> |
| 50 | + /// <param name="scheduler">The scheduler.</param> |
| 51 | + /// <returns></returns> |
| 52 | + protected override DenseTensor<float> PrepareLatents(IModelOptions model, PromptOptions prompt, SchedulerOptions options, IScheduler scheduler, IReadOnlyList<int> timesteps) |
| 53 | + { |
| 54 | + // Image input, decode, add noise, return as latent 0 |
| 55 | + var imageTensor = prompt.InputImage.ToDenseTensor(new[] { 1, 3, options.Width, options.Height }); |
| 56 | + var inputNames = _onnxModelService.GetInputNames(model, OnnxModelType.VaeEncoder); |
| 57 | + var inputParameters = CreateInputParameters(NamedOnnxValue.CreateFromTensor(inputNames[0], imageTensor)); |
| 58 | + using (var inferResult = _onnxModelService.RunInference(model, OnnxModelType.VaeEncoder, inputParameters)) |
| 59 | + { |
| 60 | + var sample = inferResult.FirstElementAs<DenseTensor<float>>(); |
| 61 | + var scaledSample = sample |
| 62 | + .Add(scheduler.CreateRandomSample(sample.Dimensions, options.InitialNoiseLevel)) |
| 63 | + .MultiplyBy(model.ScaleFactor); |
| 64 | + |
| 65 | + var noisySample = scheduler.AddNoise(scaledSample, scheduler.CreateRandomSample(scaledSample.Dimensions), timesteps); |
| 66 | + if (prompt.BatchCount > 1) |
| 67 | + return noisySample.Repeat(prompt.BatchCount); |
| 68 | + |
| 69 | + return noisySample; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | +} |
0 commit comments