i'm trying to transfer a working semantic segmentation model from Python to C# with Tensorflow.NET.
For further processing, the Tensorflow.NET NDArray output needs to be converted to a OpenCvSharp Mat.
The results are correct, but the type conversion to np.uint8 is very slow.
In Python the simliar code takes ~20ms and in C# ~200ms.
public Mat[] GetClasses(NDArray output)
{
Mat[] classes = new Mat[labels.Length];
for (int i = 0; i < labels.Length; i++)
{
NDArray val = output[":,:," + i.ToString()].astype(np.uint8); <--- this line is slow
byte[] byte_val = val.ToByteArray();
OpenCvSharp.Mat mat = new OpenCvSharp.Mat(rows: 512, cols: 768, type: OpenCvSharp.MatType.CV_8UC1, data: byte_val);
Cv2.Threshold(mat, mat, 128, 255, ThresholdTypes.Binary);
classes[i] = mat;
}
return classes;
}
The NDArray output is a squeezed Tensorflow.NET output (shape: 768, 512, 6).
The values are float32 and between 0.0 to 255.0.
If i'm converting the complete array and access a matrix after that, then the runtime is ~20ms. But the OpenCV mat result is completely wrong.
I was able to solve my problem with a workaround.
After converting the shape of my tensorflow output to (6, 768, 512), the matrix is accessible in desired time with:
Hello,
i'm trying to transfer a working semantic segmentation model from Python to C# with Tensorflow.NET. For further processing, the Tensorflow.NET NDArray output needs to be converted to a OpenCvSharp Mat. The results are correct, but the type conversion to np.uint8 is very slow. In Python the simliar code takes ~20ms and in C# ~200ms.
The NDArray output is a squeezed Tensorflow.NET output (shape: 768, 512, 6). The values are float32 and between 0.0 to 255.0.
If i'm converting the complete array and access a matrix after that, then the runtime is ~20ms. But the OpenCV mat result is completely wrong.
Any help is much appreciated.