Skip to content

Commit

Permalink
[C#] Supplement model and result api for c sharp (PaddlePaddle#1322)
Browse files Browse the repository at this point in the history
* add c sharp api for fastdeploy

* update accroding to c apis

* add cmakelist for c sharp api

* add cmakelists for c sharp

* fix cmakelists

* fix cmakelists

* add c sharp api for fastdeploy

* add ppyoloe demo

* add ppyoloe demo

* modify demo namespace code

* add readme

* fix format

* format code

* fix doc

* add model api

* add batch_predict and string result for c sharp

* add ppdet models

* update api

* fix
  • Loading branch information
rainyfly authored Feb 17, 2023
1 parent db9739a commit ea548ab
Show file tree
Hide file tree
Showing 5 changed files with 2,216 additions and 31 deletions.
20 changes: 20 additions & 0 deletions csharp/fastdeploy/types_internal_c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public struct FD_ClassifyResult {
public FD_ResultType type;
}

[StructLayout(LayoutKind.Sequential)]
public struct FD_OneDimClassifyResult {
public nuint size;
public IntPtr data; // FD_ClassifyResult[]
}


[StructLayout(LayoutKind.Sequential)]
public struct FD_Mask {
public FD_OneDimArrayUint8 data;
Expand All @@ -121,5 +128,18 @@ public struct FD_DetectionResult {
public FD_ResultType type;
}


[StructLayout(LayoutKind.Sequential)]
public struct FD_OneDimDetectionResult {
public nuint size;
public IntPtr data; // FD_DetectionResult[]
}

[StructLayout(LayoutKind.Sequential)]
public struct FD_OneDimMat {
public nuint size;
public IntPtr data; // Mat[]
}

}
}
69 changes: 55 additions & 14 deletions csharp/fastdeploy/vision/classification/ppcls/model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace fastdeploy {
namespace vision {
namespace classification {

class PaddleClasModel {
public class PaddleClasModel {

public PaddleClasModel(string model_file, string params_file,
string config_file, RuntimeOption custom_option = null,
Expand All @@ -40,25 +40,54 @@ public PaddleClasModel(string model_file, string params_file,
FD_C_DestroyPaddleClasModelWrapper(fd_paddleclas_model_wrapper);
}


public string ModelName() {
return "PaddleClas/Model";
}

public ClassifyResult Predict(Mat img) {
IntPtr fd_classify_result_wrapper_ptr = FD_C_CreateClassifyResultWrapper();
FD_C_PaddleClasModelWrapperPredict(
FD_ClassifyResult fd_classify_result = new FD_ClassifyResult();
if(! FD_C_PaddleClasModelWrapperPredict(
fd_paddleclas_model_wrapper, img.CvPtr,
fd_classify_result_wrapper_ptr); // predict
IntPtr fd_classify_result_ptr = FD_C_ClassifyResultWrapperGetData(
fd_classify_result_wrapper_ptr); // get result from wrapper
FD_ClassifyResult fd_classify_result =
(FD_ClassifyResult)Marshal.PtrToStructure(fd_classify_result_ptr,
typeof(FD_ClassifyResult));
ref fd_classify_result))
{
return null;
} // predict
ClassifyResult classify_result =
ConvertResult.ConvertCResultToClassifyResult(fd_classify_result);
FD_C_DestroyClassifyResultWrapper(
fd_classify_result_wrapper_ptr); // free fd_classify_result_wrapper_ptr
FD_C_DestroyClassifyResult(
fd_classify_result_ptr); // free fd_classify_result_ptr
return classify_result;
}

public List<ClassifyResult> BatchPredict(List<Mat> imgs){
FD_OneDimMat imgs_in = new FD_OneDimMat();
imgs_in.size = (nuint)imgs.Count;
// Copy data to unmanaged memory
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
for(int i=0;i < (int)imgs.Count; i++){
mat_ptrs[i] = imgs[i].CvPtr;
}
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
imgs_in.data = Marshal.AllocHGlobal(size);
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
mat_ptrs.Length);
FD_OneDimClassifyResult fd_classify_result_array = new FD_OneDimClassifyResult();
if (!FD_C_PaddleClasModelWrapperBatchPredict(fd_paddleclas_model_wrapper, ref imgs_in, ref fd_classify_result_array)){
return null;
}
List<ClassifyResult> results_out = new List<ClassifyResult>();
for(int i=0;i < (int)imgs.Count; i++){
FD_ClassifyResult fd_classify_result = (FD_ClassifyResult)Marshal.PtrToStructure(
fd_classify_result_array.data + i * Marshal.SizeOf(new FD_ClassifyResult()),
typeof(FD_ClassifyResult));
results_out.Add(ConvertResult.ConvertCResultToClassifyResult(fd_classify_result));
}
return results_out;
}

public bool Initialized() {
return FD_C_PaddleClasModelWrapperInitialized(fd_paddleclas_model_wrapper);
}

// below are underlying C api
private IntPtr fd_paddleclas_model_wrapper;
[DllImport("fastdeploy.dll",
Expand All @@ -75,7 +104,7 @@ private static extern void
private static extern bool
FD_C_PaddleClasModelWrapperPredict(IntPtr fd_paddleclas_model_wrapper,
IntPtr img,
IntPtr fd_classify_result_wrapper);
ref FD_ClassifyResult fd_classify_result);
[DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreateClassifyResultWrapper")]
private static extern IntPtr FD_C_CreateClassifyResultWrapper();
[DllImport("fastdeploy.dll",
Expand All @@ -93,6 +122,18 @@ private static extern IntPtr
EntryPoint = "FD_C_CreateClassifyResultWrapperFromData")]
private static extern IntPtr
FD_C_CreateClassifyResultWrapperFromData(IntPtr fd_classify_result);

[DllImport("fastdeploy.dll",
EntryPoint = "FD_C_PaddleClasModelWrapperInitialized")]
private static extern bool
FD_C_PaddleClasModelWrapperInitialized(IntPtr fd_paddleclas_model_wrapper);
[DllImport("fastdeploy.dll",
EntryPoint = "FD_C_PaddleClasModelWrapperBatchPredict")]
private static extern bool
FD_C_PaddleClasModelWrapperBatchPredict(IntPtr fd_paddleclas_model_wrapper,
ref FD_OneDimMat imgs,
ref FD_OneDimClassifyResult results);

}

}
Expand Down
Loading

0 comments on commit ea548ab

Please sign in to comment.