Open fengbozs opened 11 months ago
输出信息: root@EASY-EAI-NANO:/userdata/apps/test# dotnet TestConsoleApp.dll librknn_runtime version 1.7.3 (2e55827 build: 2022-08-25 10:45:32 base: 1131) rknn init result:0---4978904 rknn_query_sdk_version success! result:0 drv_version:System.Char[] api_version:System.Char[] rknn_query_in_out_num success! result:0 input_num:1 output_num:3 0Input------ images_283 RKNN_TENSOR_UINT8 RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC 0Output------ Conv_Conv_279/out0_0 RKNN_TENSOR_UINT8 RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC 1Output------ Conv_Conv_280/out0_1 RKNN_TENSOR_UINT8 RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC 2Output------ Conv_Conv_281/out0_2 RKNN_TENSOR_UINT8 RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC model is NCHW input fmt model input height=640, width=640, channel=3 rknn_inputs_set result:0 rknn_run success! 111111111111111111 Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at TestConsoleApp.Program.Main(String[] args) in D:\BRCodes\AvaloniaApplication\TestConsoleApp\Program.cs:line 174 Aborted root@EASY-EAI-NANO:/userdata/apps/test#
没有这样的计划
是否可以考虑提供C#的P/Invoke版本,我在进行P/Invoke封装时,经过rknn_outputs_get以后,outputs变成了空,也没有额外的输出,不知道怎么解决,以下是我的测试代码: IntPtr rknn_context = IntPtr.Zero; var modelBuffer = File.ReadAllBytes("yolov5_pre.rknn"); var result = RKNNAPI.rknn_init(ref rknn_context, modelBuffer, modelBuffer.Length, 0); Console.WriteLine($"rknn init result:{result}---{rknn_context.ToInt64()}");
region query
rknn_sdk_version rknn_Sdk_Version = new rknn_sdk_version() { api_version = new char[256], drv_version = new char[256] }; uint dwSize = (uint)Marshal.SizeOf(rknn_Sdk_Version); IntPtr sdkVersionIntPtr = Marshal.AllocHGlobal((int)dwSize); Marshal.StructureToPtr(rknn_Sdk_Version, sdkVersionIntPtr, false); var cmd = rknn_query_cmd.RKNN_QUERY_SDK_VERSION; result = RKNNAPI.rknn_query(rknn_context, cmd, sdkVersionIntPtr, dwSize); if (result < 0) { Console.WriteLine($"rknn_query_sdk_version failed! result:{result}"); //return; } else { rknn_Sdk_Version = Marshal.PtrToStructure(sdkVersionIntPtr);
Console.WriteLine($"rknn_query_sdk_version success! result:{result}");
Console.WriteLine($"drv_version:{rknn_Sdk_Version.drv_version} api_version:{rknn_Sdk_Version.api_version}");
}
Marshal.FreeHGlobal(sdkVersionIntPtr);
rknn_input_output_num io_num = new rknn_input_output_num(); dwSize = (uint)Marshal.SizeOf(io_num); IntPtr inputOutputNumIntPtr = Marshal.AllocHGlobal((int)dwSize); Marshal.StructureToPtr(io_num, inputOutputNumIntPtr, false); result = RKNNAPI.rknn_query(rknn_context, rknn_query_cmd.RKNN_QUERY_IN_OUT_NUM, inputOutputNumIntPtr, dwSize); if (result < 0) { Console.WriteLine($"rknn_query_in_out_num failed! result:{result}"); //return; } else { io_num = Marshal.PtrToStructure(inputOutputNumIntPtr);
Console.WriteLine($"rknn_query_in_out_num success! result:{result}");
Console.WriteLine($"input_num:{io_num.n_input} output_num:{io_num.n_output}");
}
Marshal.FreeHGlobal(inputOutputNumIntPtr);
rknn_tensor_attr[] input_attrs = new rknn_tensor_attr[io_num.n_input]; for (int i = 0; i < io_num.n_input; i++) { input_attrs[i] = new rknn_tensor_attr() { index = (uint)i }; dwSize = (uint)Marshal.SizeOf(input_attrs[i]); IntPtr tensorAttrIntPtr = Marshal.AllocHGlobal((int)dwSize); Marshal.StructureToPtr(input_attrs[i], tensorAttrIntPtr, false); result = RKNNAPI.rknn_query(rknn_context, rknn_query_cmd.RKNN_QUERY_INPUT_ATTR, tensorAttrIntPtr, dwSize); if (result < 0) { Console.WriteLine(i + $"Input------ rknn_query_input_attr failed! result:{result}"); } else { input_attrs[i] = Marshal.PtrToStructure(tensorAttrIntPtr);
Console.WriteLine(i + $"Input------ {input_attrs[i].name} {input_attrs[i].type} {input_attrs[i].qnt_type}");
}
}
rknn_tensor_attr[] output_attrs = new rknn_tensor_attr[io_num.n_output]; for (int i = 0; i < io_num.n_output; i++) { output_attrs[i] = new rknn_tensor_attr() { index = (uint)i }; dwSize = (uint)Marshal.SizeOf(output_attrs[i]); IntPtr tensorAttrIntPtr = Marshal.AllocHGlobal((int)dwSize); Marshal.StructureToPtr(output_attrs[i], tensorAttrIntPtr, false); result = RKNNAPI.rknn_query(rknn_context, rknn_query_cmd.RKNN_QUERY_OUTPUT_ATTR, tensorAttrIntPtr, dwSize); if (result < 0) { Console.WriteLine(i + $"Output------ rknn_query_output_attr failed! result:{result}"); } else { output_attrs[i] = Marshal.PtrToStructure(tensorAttrIntPtr);
Console.WriteLine(i + $"Output------ {output_attrs[i].name} {output_attrs[i].type} {output_attrs[i].qnt_type}");
}
}
endregion
int channel = 3; int width = 640; int height = 480; if (input_attrs[0].fmt == rknn_tensor_format.RKNN_TENSOR_NCHW) { Console.WriteLine("model is NCHW input fmt"); width = (int)input_attrs[0].dims[0]; height = (int)input_attrs[0].dims[1]; } else { Console.WriteLine("model is NHWC input fmt\n"); width = (int)input_attrs[0].dims[1]; height = (int)input_attrs[0].dims[2]; } Console.WriteLine($"model input height={height}, width={width}, channel={channel}");
var files = Directory.GetFiles("Images"); foreach (var file in files) { rknn_input[] inputs = [new rknn_input()]; rknn_output[] outputs = new rknn_output[io_num.n_output]; for (int i = 0; i < io_num.n_output; i++) { outputs[i].is_prealloc = 0; outputs[i].want_float = 0; } var image = SixLabors.ImageSharp.Image.Load(file); int img_width = image.Width; int img_height = image.Height; if (image == null) return; image.Mutate(x => x.Resize(width, height));