@@ -222,7 +222,7 @@ public AIManager(string modelPath)
222222
223223
224224 // Attempt to load via CUDA (else fallback to CPU)
225- Task . Run ( ( ) => InitializeModel ( modelPath ) ) ;
225+ _ = InitializeModel ( modelPath ) ;
226226 }
227227
228228 #region Models
@@ -255,7 +255,7 @@ private async Task InitializeModel(string modelPath)
255255 }
256256 }
257257
258- private Task LoadModelAsync ( string modelPath , bool failure = false ) // default value for failure is false, obviously
258+ private async Task LoadModelAsync ( string modelPath , bool failure = false ) // default value for failure is false, obviously
259259 {
260260 try
261261 {
@@ -303,7 +303,7 @@ private async Task InitializeModel(string modelPath)
303303 }
304304
305305 var cts = new CancellationTokenSource ( TimeSpan . FromMinutes ( 2 ) ) ;
306- _onnxModel = Task . Run ( ( ) => new InferenceSession ( modelPath , sessionOptions ) , cts . Token ) . Result ;
306+ _onnxModel = await Task . Run ( ( ) => new InferenceSession ( modelPath , sessionOptions ) , cts . Token ) ;
307307 //_onnxModel = new InferenceSession(modelPath, sessionOptions);
308308 _outputNames = new List < string > ( _onnxModel . OutputMetadata . Keys ) ;
309309
@@ -913,13 +913,20 @@ private void HandlePredictions(KalmanPrediction kalmanPrediction, Prediction clo
913913
914914 if ( _onnxModel == null ) return null ;
915915
916- IDisposableReadOnlyCollection < DisposableNamedOnnxValue > results ;
916+ //IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
917+ Tensor < float > ? outputTensor = null ;
917918 using ( Benchmark ( "ModelInference" ) )
918919 {
919- results = _onnxModel . Run ( _reusableInputs , _outputNames , _modeloptions ) ;
920+ using var results = _onnxModel . Run ( _reusableInputs , _outputNames , _modeloptions ) ;
921+ outputTensor = results [ 0 ] . AsTensor < float > ( ) ;
920922 }
921923
922- var outputTensor = results [ 0 ] . AsTensor < float > ( ) ;
924+ if ( outputTensor == null )
925+ {
926+ Log ( LogLevel . Error , "Model inference returned null output tensor." , true , 2000 ) ;
927+ SaveFrame ( frame ) ;
928+ return null ;
929+ }
923930
924931 // Calculate the FOV boundaries
925932 float FovSize = ( float ) Dictionary . sliderSettings [ "FOV Size" ] ;
@@ -934,25 +941,13 @@ private void HandlePredictions(KalmanPrediction kalmanPrediction, Prediction clo
934941 {
935942 ( KDpoints , KDPredictions ) = PrepareKDTreeData ( outputTensor , detectionBox , fovMinX , fovMaxX , fovMinY , fovMaxY ) ;
936943 }
937-
938- results . Dispose ( ) ; // fix memory leak
939944
940945 if ( KDpoints . Count == 0 || KDPredictions . Count == 0 )
941946 {
942947 SaveFrame ( frame ) ;
943948 return null ;
944949 }
945-
946- //KDTree<double, Prediction> tree;
947- //Tuple<double[], Prediction>[]? nearest;
948- //using (Benchmark("KDTreeOperations"))
949- //{
950- // tree = new KDTree<double, Prediction>(2, KDpoints.ToArray(), KDPredictions.ToArray(), L2Norm_Squared_Double);
951- // nearest = tree.NearestNeighbors(new double[] { IMAGE_SIZE / 2.0, IMAGE_SIZE / 2.0 }, 1);
952- //}
953-
954- //Prediction? bestCandidate = (nearest.Length > 0) ? nearest[0].Item2 : null;
955-
950+ // i removed kd tree.
956951 Prediction ? bestCandidate = null ;
957952 double bestDistSq = double . MaxValue ;
958953 double center = IMAGE_SIZE / 2.0 ;
@@ -1046,8 +1041,8 @@ private void UpdateDetectionBox(Prediction target, Rectangle detectionBox)
10461041 string selectedClass = Dictionary . dropdownState [ "Target Class" ] ;
10471042 int selectedClassId = selectedClass == "Best Confidence" ? - 1 : _modelClasses . FirstOrDefault ( c => c . Value == selectedClass ) . Key ;
10481043
1049- var KDpoints = new List < double [ ] > ( 100 ) ; // Pre-allocate with estimated capacity
1050- var KDpredictions = new List < Prediction > ( 100 ) ;
1044+ var KDpoints = new List < double [ ] > ( NUM_DETECTIONS ) ; // Pre-allocate with estimated capacity
1045+ var KDpredictions = new List < Prediction > ( NUM_DETECTIONS ) ;
10511046
10521047 for ( int i = 0 ; i < NUM_DETECTIONS ; i ++ )
10531048 {
0 commit comments