@@ -212,9 +212,9 @@ public AIManager(string modelPath)
212212 EnableCpuMemArena = true ,
213213 EnableMemoryPattern = false ,
214214 GraphOptimizationLevel = GraphOptimizationLevel . ORT_ENABLE_ALL ,
215- ExecutionMode = ExecutionMode . ORT_PARALLEL ,
216- InterOpNumThreads = Environment . ProcessorCount ,
217- IntraOpNumThreads = Environment . ProcessorCount
215+ ExecutionMode = ExecutionMode . ORT_SEQUENTIAL ,
216+ InterOpNumThreads = 1 ,
217+ IntraOpNumThreads = 4
218218 } ;
219219
220220 // Attempt to load via DirectML (else fallback to CPU)
@@ -935,70 +935,79 @@ private void HandlePredictions(KalmanPrediction kalmanPrediction, Prediction clo
935935
936936 if ( _onnxModel == null ) return null ;
937937
938+ IDisposableReadOnlyCollection < DisposableNamedOnnxValue > ? results = null ;
938939 Tensor < float > ? outputTensor = null ;
939- using ( Benchmark ( "ModelInference" ) )
940- {
941- using var results = _onnxModel . Run ( _reusableInputs , _outputNames , _modeloptions ) ;
942- outputTensor = results [ 0 ] . AsTensor < float > ( ) ;
943- }
944940
945- if ( outputTensor == null )
941+ try
946942 {
947- Log ( LogLevel . Error , "Model inference returned null output tensor." , true , 2000 ) ;
948- SaveFrame ( frame ) ;
949- return null ;
950- }
943+ using ( Benchmark ( "ModelInference" ) )
944+ {
945+ results = _onnxModel . Run ( _reusableInputs , _outputNames , _modeloptions ) ;
946+ outputTensor = results [ 0 ] . AsTensor < float > ( ) ;
947+ }
951948
952- // Calculate the FOV boundaries
953- float FovSize = ( float ) Dictionary . sliderSettings [ "FOV Size" ] ;
954- float fovMinX = ( IMAGE_SIZE - FovSize ) / 2.0f ;
955- float fovMaxX = ( IMAGE_SIZE + FovSize ) / 2.0f ;
956- float fovMinY = ( IMAGE_SIZE - FovSize ) / 2.0f ;
957- float fovMaxY = ( IMAGE_SIZE + FovSize ) / 2.0f ;
949+ if ( outputTensor == null )
950+ {
951+ Log ( LogLevel . Error , "Model inference returned null output tensor." , true , 2000 ) ;
952+ SaveFrame ( frame ) ;
953+ return null ;
954+ }
958955
959- //List<double[]> KDpoints;
960- List < Prediction > KDPredictions ;
961- using ( Benchmark ( "PrepareKDTreeData" ) )
962- {
963- KDPredictions = PrepareKDTreeData ( outputTensor , detectionBox , fovMinX , fovMaxX , fovMinY , fovMaxY ) ;
964- }
956+ // Calculate the FOV boundaries
957+ float FovSize = ( float ) Dictionary . sliderSettings [ "FOV Size" ] ;
958+ float fovMinX = ( IMAGE_SIZE - FovSize ) / 2.0f ;
959+ float fovMaxX = ( IMAGE_SIZE + FovSize ) / 2.0f ;
960+ float fovMinY = ( IMAGE_SIZE - FovSize ) / 2.0f ;
961+ float fovMaxY = ( IMAGE_SIZE + FovSize ) / 2.0f ;
965962
966- if ( KDPredictions . Count == 0 )
967- {
968- SaveFrame ( frame ) ;
969- return null ;
970- }
963+ //List<double[]> KDpoints;
964+ List < Prediction > KDPredictions ;
965+ using ( Benchmark ( "PrepareKDTreeData" ) )
966+ {
967+ KDPredictions = PrepareKDTreeData ( outputTensor , detectionBox , fovMinX , fovMaxX , fovMinY , fovMaxY ) ;
968+ }
971969
972- //kdtree was replaced with linear search
973- Prediction ? bestCandidate = null ;
974- double bestDistSq = double . MaxValue ;
975- double center = IMAGE_SIZE / 2.0 ;
970+ if ( KDPredictions . Count == 0 )
971+ {
972+ SaveFrame ( frame ) ;
973+ return null ;
974+ }
976975
977- // TODO: Optimize this linear search further if needed
978- // TODO: Consider updating KD-Tree and adding options to switch from linear to kd.
979- // we can honestly replacing linear search by letting sticky aim handle the search
980- using ( Benchmark ( "LinearSearch" ) )
981- {
982- foreach ( var p in KDPredictions )
976+ //kdtree was replaced with linear search
977+ Prediction ? bestCandidate = null ;
978+ double bestDistSq = double . MaxValue ;
979+ double center = IMAGE_SIZE / 2.0 ;
980+
981+ // TODO: Optimize this linear search further if needed
982+ // TODO: Consider updating KD-Tree and adding options to switch from linear to kd.
983+ // we can honestly replacing linear search by letting sticky aim handle the search
984+ using ( Benchmark ( "LinearSearch" ) )
983985 {
984- var dx = p . CenterXTranslated * IMAGE_SIZE - center ;
985- var dy = p . CenterYTranslated * IMAGE_SIZE - center ;
986- double d2 = dx * dx + dy * dy ; // dx^2 + dy^2
986+ foreach ( var p in KDPredictions )
987+ {
988+ var dx = p . CenterXTranslated * IMAGE_SIZE - center ;
989+ var dy = p . CenterYTranslated * IMAGE_SIZE - center ;
990+ double d2 = dx * dx + dy * dy ; // dx^2 + dy^2
991+
992+ if ( d2 < bestDistSq ) { bestDistSq = d2 ; bestCandidate = p ; }
993+ }
994+ }
987995
988- if ( d2 < bestDistSq ) { bestDistSq = d2 ; bestCandidate = p ; }
996+ Prediction ? finalTarget = HandleStickyAim ( bestCandidate , KDPredictions ) ;
997+ if ( finalTarget != null )
998+ {
999+ UpdateDetectionBox ( finalTarget , detectionBox ) ;
1000+ SaveFrame ( frame , finalTarget ) ;
1001+ return finalTarget ;
9891002 }
990- }
9911003
992- Prediction ? finalTarget = HandleStickyAim ( bestCandidate , KDPredictions ) ;
993- if ( finalTarget != null )
1004+ frame . Dispose ( ) ;
1005+ return null ;
1006+ }
1007+ finally
9941008 {
995- UpdateDetectionBox ( finalTarget , detectionBox ) ;
996- SaveFrame ( frame , finalTarget ) ;
997- return finalTarget ;
1009+ results ? . Dispose ( ) ;
9981010 }
999-
1000- frame . Dispose ( ) ; // Dispose the frame to free resources
1001- return null ;
10021011 }
10031012
10041013 // sticky aim needs to be refined
0 commit comments