Skip to content

Commit

Permalink
Support for byte tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
saddam213 committed Jun 12, 2024
1 parent 6ae4eee commit f1b6c58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
12 changes: 12 additions & 0 deletions OnnxStack.Core/Extensions/OrtValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ public static OrtValue ToOrtValue(this DenseTensor<bool> tensor, OnnxNamedMetada
}


/// <summary>
/// Converts DenseTensor<bool> to OrtValue.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <param name="metadata">The metadata.</param>
/// <returns></returns>
public static OrtValue ToOrtValue(this DenseTensor<byte> tensor, OnnxNamedMetadata metadata)
{
return OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, tensor.Dimensions.ToLong());
}


/// <summary>
/// Creates and allocates the output tensors buffer.
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions OnnxStack.Core/Image/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ private static DenseTensor<float> SplitImageTile(DenseTensor<float> source, int
var splitTensor = new DenseTensor<float>(new[] { 1, channels, height, width });
Parallel.For(0, channels, (c) =>
{
Parallel.For(0, height, (i) =>
for (int i = 0; i < height; i++)
{
Parallel.For(0, width, (j) =>
for (int j = 0; j < width; j++)
{
splitTensor[0, c, i, j] = source[0, c, startRow + i, startCol + j];
});
});
}
}
});
return splitTensor;
}
Expand Down Expand Up @@ -135,9 +135,9 @@ private static void JoinImageTile(DenseTensor<float> destination, DenseTensor<fl
int channels = tile.Dimensions[1];
Parallel.For(0, channels, (c) =>
{
Parallel.For(0, height, (i) =>
for (int i = 0; i < height; i++)
{
Parallel.For(0, width, (j) =>
for (int j = 0; j < width; j++)
{
var value = tile[0, c, i, j];
var existing = destination[0, c, startRow + i, startCol + j];
Expand All @@ -147,8 +147,8 @@ private static void JoinImageTile(DenseTensor<float> destination, DenseTensor<fl
value = (existing + value) / 2f;
}
destination[0, c, startRow + i, startCol + j] = value;
});
});
}
}
});
}
}
Expand Down
11 changes: 11 additions & 0 deletions OnnxStack.Core/Model/OnnxInferenceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public void AddInputTensor(DenseTensor<bool> value)
}


/// <summary>
/// Adds the input tensor.
/// </summary>
/// <param name="value">The value.</param>
public void AddInputTensor(DenseTensor<byte> value)
{
var metaData = GetNextInputMetadata();
_inputs.Add(metaData, value.ToOrtValue(metaData));
}


/// <summary>
/// Adds an output parameter with known output size.
/// </summary>
Expand Down

0 comments on commit f1b6c58

Please sign in to comment.