Skip to content

Commit

Permalink
Improve preprocess, try first get continuous memory block of the enti…
Browse files Browse the repository at this point in the history
…re image data
  • Loading branch information
dme-compunet committed Sep 26, 2023
1 parent 69435c6 commit d1c40fd
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions Source/YoloV8/Utilities/PreprocessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,39 @@ public static void ProcessToTensor(Image<Rgb24> image, Size modelSize, bool orig
var xPadding = (modelSize.Width - image.Width) / 2;
var yPadding = (modelSize.Height - image.Height) / 2;

Parallel.For(0, image.Height, y =>
{
var row = image.DangerousGetPixelRowMemory(y).Span;
var width = image.Width;
var height = image.Height;

for (int x = 0; x < image.Width; x++)
// Try get continuous memory block of the entire image data
if (image.DangerousTryGetSinglePixelMemory(out var memory))
{
Parallel.For(0, width * height, index =>
{
var pixel = row[x];
int x = index % width;
int y = index / width;

var pixel = memory.Span[index];

target[batch, 0, y + yPadding, x + xPadding] = pixel.R / 255f;
target[batch, 1, y + yPadding, x + xPadding] = pixel.G / 255f;
target[batch, 2, y + yPadding, x + xPadding] = pixel.B / 255f;
}
});
});
}
else
{
Parallel.For(0, height, y =>
{
var row = image.DangerousGetPixelRowMemory(y).Span;

for (int x = 0; x < width; x++)
{
var pixel = row[x];

target[batch, 0, y + yPadding, x + xPadding] = pixel.R / 255f;
target[batch, 1, y + yPadding, x + xPadding] = pixel.G / 255f;
target[batch, 2, y + yPadding, x + xPadding] = pixel.B / 255f;
}
});
}
}
}

0 comments on commit d1c40fd

Please sign in to comment.