Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 564baf5

Browse files
committed
Inpaint Legacy prototype
1 parent e31a85d commit 564baf5

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

OnnxStack.StableDiffusion/Diffusers/InpaintDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private DenseTensor<float> PrepareMask(PromptOptions promptOptions, SchedulerOpt
126126
for (int y = 0; y < height; y++)
127127
{
128128
var pixelSpan = img.GetRowSpan(y);
129-
var value = pixelSpan[x].PackedValue / 255.0f;
129+
var value = pixelSpan[x].A / 255.0f;
130130
if (value < 0.5f)
131131
value = 0f;
132132
else if (value >= 0.5f)

OnnxStack.StableDiffusion/Diffusers/InpaintLegacyDiffuser.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using OnnxStack.StableDiffusion.Helpers;
99
using SixLabors.ImageSharp;
1010
using SixLabors.ImageSharp.Processing;
11-
using SixLabors.ImageSharp.Processing.Processors.Transforms;
1211
using System;
1312
using System.Collections.Generic;
1413
using System.Linq;
@@ -91,8 +90,6 @@ public override async Task<DenseTensor<float>> DiffuseAsync(PromptOptions prompt
9190

9291
// Apply mask and combine
9392
latents = ApplyMaskedLatents(steplatents, initLatentsProper, maskImage);
94-
95-
ImageHelpers.TensorToImageDebug(latents, $@"D:\Debug\Latent{step}.png");
9693
}
9794

9895
progress?.Invoke(++step, timesteps.Count);
@@ -167,9 +164,7 @@ private DenseTensor<float> PrepareMask(PromptOptions promptOptions, SchedulerOpt
167164
{
168165
var pixelSpan = img.GetRowSpan(y);
169166
var value = (float)pixelSpan[x].A / 255.0f;
170-
171-
//TODO: mask = 1 - mask # repaint white, keep black
172-
maskTensor[0, 0, y, x] = 0f;
167+
maskTensor[0, 0, y, x] = 1f - value;
173168
maskTensor[0, 1, y, x] = 0f; // Needed for shape only
174169
maskTensor[0, 2, y, x] = 0f; // Needed for shape only
175170
maskTensor[0, 3, y, x] = 0f; // Needed for shape only
@@ -203,7 +198,7 @@ private DenseTensor<float> ApplyMaskedLatents(DenseTensor<float> latents, DenseT
203198
float latentsValue = latents[batch, channel, height, width];
204199
float initLatentsProperValue = initLatentsProper[batch, channel, height, width];
205200

206-
//TODO: Apply the logic to compute the result based on the mask
201+
//Apply the logic to compute the result based on the mask
207202
float newValue = (initLatentsProperValue * maskValue) + (latentsValue * (1f - maskValue));
208203
result[batch, channel, height, width] = newValue;
209204
}

OnnxStack.StableDiffusion/Helpers/ImageHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public static void TensorToImageDebug2(DenseTensor<float> imageTensor, string fi
173173
{
174174
for (var x = 0; x < width; x++)
175175
{
176-
result[x, y] = new L8(CalculateByte(imageTensor, 0, y, x));
176+
result[x, y] = new L8((byte)(imageTensor[0, 0, y, x] * 255.0f));
177177
}
178178
}
179179
result.SaveAsPng(filename);

OnnxStack.WebUI/Pages/StableDiffusion/ImageInpaint.cshtml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Models;
44
using OnnxStack.Core;
55
using OnnxStack.StableDiffusion.Config;
6+
using OnnxStack.StableDiffusion.Enums;
67
using Services;
78

89
namespace OnnxStack.WebUI.Pages.StableDiffusion
@@ -38,12 +39,12 @@ public async Task OnGet(string img = null, int width = 0, int height = 0)
3839

3940
Prompt = new PromptOptions
4041
{
41-
// Prompt = "A photo of a cat",
42+
SchedulerType = SchedulerType.DDPM
4243
};
4344

4445
Options = new SchedulerOptions
4546
{
46-
// InferenceSteps = 5,
47+
Strength = 1.0f,
4748
};
4849
}
4950
}

0 commit comments

Comments
 (0)