SciSharp / NumSharp

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.
https://github.com/SciSharp
Apache License 2.0
1.34k stars 188 forks source link

NDArray astype conversion is slow #420

Closed Ripidi closed 3 years ago

Ripidi commented 3 years ago

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.

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.

NDArray val_int = output.astype(np.uint8);
NDArray val = val_int[":,:," + i.ToString()];
byte[] byte_val = val.ToByteArray();

Any help is much appreciated.

Ripidi commented 3 years ago

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:

NDArray val = val_int[i];