/// <summary> /// forward net with image file, with preprocess /// </summary> /// <param name="imagePath">image file path</param> /// <param name="predDataType">process function</param> /// <param name="runGPU">run wit GPU</param> /// <param name="mean"> if normalize mean val </param> /// <param name="std">if normalize std val</param> /// <returns></returns> public int RunClassifyFile(string imagePath, PredDataType predDataType, bool runGPU, float[] mean = null, float[] std = null) { if (!netBuilt) { throw new Exception("Net wasn't built yet"); } IntPtr msg = new IntPtr(); int bestIndex = 0; unsafe { if (predDataType == PredDataType.PRE_DATA_TRANSFORMED_FC3) { fixed(float *meanPtr = mean) { fixed(float *stdPtr = std) { if (_runClassifyFile(ref msg, imagePath, ref bestIndex, predDataType, runGPU ? 1 : 0, meanPtr, stdPtr) != 1) { string mstr = Marshal.PtrToStringAnsi(msg); throw new Exception(mstr); } } } } else { if (_runClassifyFile(ref msg, imagePath, ref bestIndex, predDataType, runGPU ? 1 : 0, null, null) != 1) { string mstr = Marshal.PtrToStringAnsi(msg); throw new Exception(mstr); } } } return(bestIndex); }
/// <summary> /// forward net with image BitmapData, with preprocess /// </summary> /// <param name="bitmap">data</param> /// <param name="predDataType">process function</param> /// <param name="swapRGB">net swap RGB or not</param> /// <param name="runGPU">run wit GPU</param> /// <param name="mean"> if normalize mean val </param> /// <param name="std">if normalize std val</param> /// <returns></returns> public int RunClassifyList(BitmapData bitmap, PredDataType predDataType, bool swapRGB, bool runGPU, float[] mean = null, float[] std = null) { if (!netBuilt) { throw new Exception("Net wasn't built yet"); } IntPtr msg = new IntPtr(); int bestIndex = 0; unsafe { if (predDataType == PredDataType.PRE_DATA_TRANSFORMED_FC3) { fixed(float *meanPtr = mean) { fixed(float *stdPtr = std) { if (_runClassifyList(ref msg, (byte *)bitmap.Scan0, bitmap.Width, bitmap.Height, bitmap.Stride / bitmap.Width, ref bestIndex, predDataType, swapRGB ? 1 : 0, runGPU ? 1 : 0, meanPtr, stdPtr) != 1) { string mstr = Marshal.PtrToStringAnsi(msg); throw new Exception(mstr); } } } } else { if (_runClassifyList(ref msg, (byte *)bitmap.Scan0, bitmap.Width, bitmap.Height, bitmap.Stride / bitmap.Width, ref bestIndex, predDataType, swapRGB ? 1 : 0, runGPU ? 1 : 0, null, null) != 1) { string mstr = Marshal.PtrToStringAnsi(msg); throw new Exception(mstr); } } } return(bestIndex); }
static unsafe extern int _runClassifyList(ref IntPtr msg, byte *data, int width, int height, int channel, ref int bestIndex, PredDataType predDataType, int swapRGB, int runGPU, float *mean, float *std);
static unsafe extern int _runClassifyFile(ref IntPtr msg, string imagePath, ref int bestIndex, PredDataType predDataType, int runGPU, float *mean, float *std);