InferNetOnnxInstanceSegYOLOV5.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using AI.Common.Implements;
  5. using AI.Common.Interface;
  6. namespace YOLOInstanceSegProcessLib
  7. {
  8. public class InferNetOnnxInstanceSegYOLOV5 : InferNetOnnxInstanceSegYOLOBasic
  9. {
  10. #region override
  11. protected override string NetworkHashCode => "6a6fd0d2fcc4b280177c15c5c4324c82";
  12. public override string NetworkName => "ThyroidLesionSegYOLOV5.emd";
  13. /// <summary>
  14. /// 复制
  15. /// </summary>
  16. /// <returns></returns>
  17. public override IInferenceNetwork Clone()
  18. {
  19. if (!_modelLoaded)
  20. {
  21. return new InferNetOnnxInstanceSegYOLOV5();
  22. }
  23. return new InferNetOnnxInstanceSegYOLOV5(_trainedModel, _numCPU);
  24. }
  25. #endregion
  26. #region constructor
  27. public InferNetOnnxInstanceSegYOLOV5()
  28. {
  29. ModelParamsAssign();
  30. ConstructIntermediateVariables();
  31. }
  32. public InferNetOnnxInstanceSegYOLOV5(byte[] trainedModel, int numCPU)
  33. {
  34. ModelParamsAssign();
  35. ConstructIntermediateVariables();
  36. ConstructClonedModelRelatedVariables(trainedModel, numCPU);
  37. }
  38. private void ModelParamsAssign()
  39. {
  40. _inputTensorNames = new[] { "images" };
  41. _inputAxisOrder = EnumAxisOrder.CHW;
  42. _outputTensorNames = new[] { "output0", "output1" };
  43. _meanValueType = EnumMeanValueType.None;
  44. _meanR = 0;
  45. _meanG = 0;
  46. _meanB = 0;
  47. _resizeMode = EnumResizeMode.Warp;
  48. _meanValueType = EnumMeanValueType.None;
  49. _scaleType = EnumScaleValueType.ConstantScale;
  50. _scaleR = 255;
  51. _scaleG = 255;
  52. _scaleB = 255;
  53. _normType = EnumNormalizationType.None;
  54. _channelOrder = EnumChannelOrder.RGB;
  55. _yoloType = YoloType.yolov5;
  56. _boxConfThres = 0.5f;
  57. _clsConfThres = 0.5f;
  58. _batchSize = 1;
  59. _maxDet = 100;
  60. _minBoxRatio = 0.001f;
  61. _postProcessTopK = 6400;
  62. _enableIouFilt = true;
  63. _enableIosFilt = true;
  64. _iouFltth = 0.3f;
  65. _iosFltth = 0.3f;
  66. _enableIouFiltDiffCls = true;
  67. _enableIosFiltDiffCls = true;
  68. _iouFltthDiffCls = 0.5f;
  69. _iosFltthDiffCls = 0.5f;
  70. _maskThres = 0.7f;
  71. //模型输入输出的尺寸,netron直接读取即可,此处可以直接赋值,暂时放这边是为了大家理解输出的组成
  72. _inputVariableShapes = new List<int[]> { new int[] { 1, 3, 320, 320 } };
  73. _outputVariableShapes = new List<int[]> { new int[] { 1, 6300, 6 + 5 + 32 },
  74. new int[] { 1, 32, 80, 80 } };
  75. _segPostProcessParam = new Dictionary<int, InstanceSegmentationPostProcessParams>
  76. {
  77. // 1:TIRADS 2
  78. { 1, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f),
  79. new LesionAreaSelectInfo(EnumLesionAreaThresholdType.PixelInOrigImg, 0, 50000)}, 5)},
  80. // 2:TIRADS 3
  81. { 2, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f)}, 5)},
  82. // 3:TIRADS 4a
  83. { 3, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f)}, 5)},
  84. //// 4:TIRADS 4b
  85. { 4, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f)}, 5)},
  86. // 5:TIRADS 4c
  87. { 5, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f)}, 5)},
  88. // 6:TIRADS 5
  89. { 6, new InstanceSegmentationPostProcessParams( new List<LesionAreaSelectInfo>(){new LesionAreaSelectInfo(EnumLesionAreaThresholdType.RatioInMask, 1.0f/1000, 1.0f)}, 5)},
  90. };
  91. ConfidenceMapInfo confidenceMap = new ConfidenceMapInfo(0.5f, 1.0f, 0.5f, 0.9f);
  92. _confidenceMapInfo = new Dictionary<int, ConfidenceMapInfo>
  93. {
  94. { 1, confidenceMap},
  95. { 2, confidenceMap},
  96. { 3, confidenceMap},
  97. { 4, confidenceMap},
  98. { 5, confidenceMap},
  99. };
  100. var validTypes = new List<Type>
  101. {
  102. typeof(LesionMalignantDegreeLabelExtraInfo),
  103. typeof(LesionInvolvedScopeLabelExtraInfo),
  104. };
  105. _labelGroup = new LabelGroup("AI.Vaid.Thyroid.FocalLesion", validTypes);
  106. _labelGroup.RegistLabel(0, "NoObviousLesion");
  107. _labelGroup.RegistLabel(1, "TI-RADS 2", new List<ILabelExtraInfo>
  108. {
  109. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.BenignLesion),
  110. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  111. });
  112. _labelGroup.RegistLabel(2, "TI-RADS 3", new List<ILabelExtraInfo>
  113. {
  114. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.BenignLesion),
  115. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  116. });
  117. _labelGroup.RegistLabel(3, "TI-RADS 4a", new List<ILabelExtraInfo>
  118. {
  119. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.MalignantLesion),
  120. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  121. });
  122. _labelGroup.RegistLabel(4, "TI-RADS 4b", new List<ILabelExtraInfo>
  123. {
  124. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.MalignantLesion),
  125. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  126. });
  127. _labelGroup.RegistLabel(5, "TI-RADS 4c", new List<ILabelExtraInfo>
  128. {
  129. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.MalignantLesion),
  130. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  131. });
  132. _labelGroup.RegistLabel(6, "TI-RADS 5", new List<ILabelExtraInfo>
  133. {
  134. new LesionMalignantDegreeLabelExtraInfo(EnumLesionMalignantDegree.MalignantLesion),
  135. new LesionInvolvedScopeLabelExtraInfo(EnumLesionInvolvedScope.FocalLesion),
  136. });
  137. }
  138. #endregion
  139. }
  140. }