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

Commit 1486e7d

Browse files
committed
Update image library
1 parent 92504d5 commit 1486e7d

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

OnnxStack.StableDiffusion/Helpers/ImageHelpers.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,19 @@ public static DenseTensor<float> TensorFromImage(string filename, int width, int
117117
{
118118
Resize(image, width, height);
119119
var imageArray = new DenseTensor<float>(new[] { 1, 3, width, height });
120-
for (int x = 0; x < width; x++)
120+
image.ProcessPixelRows(img =>
121121
{
122-
for (int y = 0; y < height; y++)
122+
for (int x = 0; x < width; x++)
123123
{
124-
var pixelSpan = image.GetPixelRowSpan(y);
125-
imageArray[0, 0, y, x] = (pixelSpan[x].R / 255.0f) * 2.0f - 1.0f;
126-
imageArray[0, 1, y, x] = (pixelSpan[x].G / 255.0f) * 2.0f - 1.0f;
127-
imageArray[0, 2, y, x] = (pixelSpan[x].B / 255.0f) * 2.0f - 1.0f;
124+
for (int y = 0; y < height; y++)
125+
{
126+
var pixelSpan = img.GetRowSpan(y);
127+
imageArray[0, 0, y, x] = (pixelSpan[x].R / 255.0f) * 2.0f - 1.0f;
128+
imageArray[0, 1, y, x] = (pixelSpan[x].G / 255.0f) * 2.0f - 1.0f;
129+
imageArray[0, 2, y, x] = (pixelSpan[x].B / 255.0f) * 2.0f - 1.0f;
130+
}
128131
}
129-
}
132+
});
130133
return imageArray;
131134
}
132135
}

OnnxStack.StableDiffusion/OnnxStack.StableDiffusion.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<ItemGroup>
5252
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
5353
<PackageReference Include="NumSharp" Version="0.30.0" />
54-
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
54+
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
5555
</ItemGroup>
5656

5757
<ItemGroup>

OnnxStack.StableDiffusion/Services/SchedulerService.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,19 @@ private static DenseTensor<float> ClipImageFeatureExtractor(SchedulerOptions opt
210210
var input = new DenseTensor<float>(new[] { 1, 3, 224, 224 });
211211
var mean = new[] { 0.485f, 0.456f, 0.406f };
212212
var stddev = new[] { 0.229f, 0.224f, 0.225f };
213-
for (int y = 0; y < image.Height; y++)
213+
image.ProcessPixelRows(img =>
214214
{
215-
var pixelSpan = image.GetPixelRowSpan(y);
216-
for (int x = 0; x < image.Width; x++)
215+
for (int y = 0; y < image.Height; y++)
217216
{
218-
input[0, 0, y, x] = (pixelSpan[x].R / 255f - mean[0]) / stddev[0];
219-
input[0, 1, y, x] = (pixelSpan[x].G / 255f - mean[1]) / stddev[1];
220-
input[0, 2, y, x] = (pixelSpan[x].B / 255f - mean[2]) / stddev[2];
217+
var pixelSpan = img.GetRowSpan(y);
218+
for (int x = 0; x < image.Width; x++)
219+
{
220+
input[0, 0, y, x] = (pixelSpan[x].R / 255f - mean[0]) / stddev[0];
221+
input[0, 1, y, x] = (pixelSpan[x].G / 255f - mean[1]) / stddev[1];
222+
input[0, 2, y, x] = (pixelSpan[x].B / 255f - mean[2]) / stddev[2];
223+
}
221224
}
222-
}
225+
});
223226
return input;
224227
}
225228
}

0 commit comments

Comments
 (0)