Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.linalg.LinAlgError: 2-th leading minor of the array is not positive definite Error? #12648

Open
1 of 2 tasks
noinsung opened this issue May 13, 2024 · 9 comments
Open
1 of 2 tasks
Labels
bug Something isn't working non-reproducible Bug is not reproducible

Comments

@noinsung
Copy link

Search before asking

  • I have searched the YOLOv8 issues and found no similar bug report.

YOLOv8 Component

No response

Bug

Error log

Traceback (most recent call last):
File "classcounting.py", line 33, in
tracks = model.track(im0, persist=True, show=False,
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\engine\model.py", line 493, in track
return self.predict(source=source, stream=stream, **kwargs)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\engine\model.py", line 453, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\engine\predictor.py", line 168, in call
return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one
File "E:\Anaconda3_envs\yolov8\lib\site-packages\torch\utils_contextlib.py", line 35, in generator_context
response = gen.send(None)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\engine\predictor.py", line 256, in stream_inference
self.run_callbacks("on_predict_postprocess_end")
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\engine\predictor.py", line 393, in run_callbacks
callback(self)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\track.py", line 70, in on_predict_postprocess_end
tracks = tracker.update(det, im0s[i])
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\byte_tracker.py", line 319, in update
track.update(det, self.frame_id)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\bot_sort.py", line 86, in update
super().update(new_track, frame_id)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\byte_tracker.py", line 144, in update
self.mean, self.covariance = self.kalman_filter.update(
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\utils\kalman_filter.py", line 360, in update
return super().update(mean, covariance, measurement)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\ultralytics\trackers\utils\kalman_filter.py", line 166, in update
chol_factor, lower = scipy.linalg.cho_factor(projected_cov, lower=True, check_finite=False)
File "E:\Anaconda3_envs\yolov8\lib\site-packages\scipy\linalg_decomp_cholesky.py", line 154, in cho_factor
c, lower = _cholesky(a, lower=lower, overwrite_a=overwrite_a, clean=False,
File "E:\Anaconda3_envs\yolov8\lib\site-packages\scipy\linalg_decomp_cholesky.py", line 37, in _cholesky
raise LinAlgError("%d-th leading minor of the array is not positive "
numpy.linalg.LinAlgError: 2-th leading minor of the array is not positive definite

Environment

No response

Minimal Reproducible Example

from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("C:/Users/user/ultralytics-main/KOREA ROAD ASMR 4K 60P.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

line_points = [(0, 720), (1280, 720), (1280, 0), (0, 0)] # line or region points
#[(40, 500), (1220, 500), (1080, 360), (100, 360)]
classes_to_count = [0, 2, 3, 5, 7] # person and car classes for count

Video writer

video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))

Init Object Counter

counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2,view_out_counts=False)

while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False,
classes=classes_to_count)

im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()

============================================

When you run the above code, object detection runs normally for about 10 minutes, but then a bug occurs that causes it to terminate. The same bug occurred on a PC with a window 11 environment and a laptop with an ubuntu 22.04 environment.

List of installed packages and versions

Package Version


absl-py 2.1.0
anyio 4.2.0
argon2-cffi 23.1.0
argon2-cffi-bindings 21.2.0
arrow 1.3.0
asttokens 2.4.1
async-lru 2.0.4
attrs 23.2.0
Babel 2.14.0
backcall 0.2.0
beautifulsoup4 4.12.3
bleach 6.1.0
cachetools 5.3.2
certifi 2022.12.7
cffi 1.16.0
charset-normalizer 2.1.1
colorama 0.4.6
comm 0.2.1
contourpy 1.1.1
cycler 0.12.1
Cython 3.0.10
debugpy 1.8.0
decorator 5.1.1
defusedxml 0.7.1
exceptiongroup 1.2.0
executing 2.0.1
fastjsonschema 2.19.1
filelock 3.9.0
fonttools 4.47.2
fqdn 1.5.1
fsspec 2023.4.0
gitdb 4.0.11
GitPython 3.1.41
google-auth 2.27.0
google-auth-oauthlib 0.4.6
grpcio 1.60.1
idna 3.4
importlib-metadata 7.0.1
importlib-resources 6.1.1
ipykernel 6.29.0
ipython 8.12.3
ipywidgets 8.1.1
isoduration 20.11.0
jedi 0.19.1
Jinja2 3.1.2
joblib 1.3.2
json5 0.9.14
jsonpointer 2.4
jsonschema 4.21.1
jsonschema-specifications 2023.12.1
jupyter 1.0.0
jupyter_client 8.6.0
jupyter-console 6.6.3
jupyter_core 5.7.1
jupyter-events 0.9.0
jupyter-lsp 2.2.2
jupyter_server 2.12.5
jupyter_server_terminals 0.5.2
jupyterlab 4.0.12
jupyterlab_pygments 0.3.0
jupyterlab_server 2.25.2
jupyterlab-widgets 3.0.9
kiwisolver 1.4.5
labelImg 1.8.6
lapx 0.5.7
lxml 5.1.0
Markdown 3.5.2
MarkupSafe 2.1.3
matplotlib 3.7.4
matplotlib-inline 0.1.6
mistune 3.0.2
mpmath 1.3.0
nbclient 0.9.0
nbconvert 7.14.2
nbformat 5.9.2
nest-asyncio 1.6.0
networkx 3.0
notebook 7.0.7
notebook_shim 0.2.3
numpy 1.24.1
oauthlib 3.2.2
opencv-python 4.9.0.80
overrides 7.7.0
packaging 23.2
pafy 0.5.5
pandas 2.0.3
pandocfilters 1.5.1
parso 0.8.3
pickleshare 0.7.5
pillow 10.2.0
pip 23.3.1
pkgutil_resolve_name 1.3.10
platformdirs 4.2.0
prometheus-client 0.19.0
prompt-toolkit 3.0.43
protobuf 3.20.0
psutil 5.9.8
pure-eval 0.2.2
py-cpuinfo 9.0.0
pyasn1 0.5.1
pyasn1-modules 0.3.0
pycparser 2.21
Pygments 2.17.2
pyparsing 3.1.1
PyQt5 5.15.10
PyQt5-Qt5 5.15.2
PyQt5-sip 12.13.0
PySide6 6.4.2
PySide6-Addons 6.4.2
PySide6-Essentials 6.4.2
python-dateutil 2.8.2
python-json-logger 2.0.7
pytz 2023.4
pywin32 306
pywinpty 2.0.12
PyYAML 6.0.1
pyzmq 25.1.2
qtconsole 5.5.1
QtPy 2.4.1
referencing 0.33.0
requests 2.31.0
requests-oauthlib 1.3.1
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rpds-py 0.17.1
rsa 4.9
scikit-learn 1.0.2
scipy 1.10.1
seaborn 0.13.2
Send2Trash 1.8.2
sentry-sdk 1.45.0
setuptools 68.2.2
shapely 2.0.3
shiboken6 6.4.2
six 1.16.0
smmap 5.0.1
sniffio 1.3.0
soupsieve 2.5
stack-data 0.6.3
sympy 1.12
tensorboard 2.8.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.1
terminado 0.18.0
thop 0.1.1.post2209072238
threadpoolctl 3.2.0
tinycss2 1.2.1
tomli 2.0.1
torch 2.2.0+cu121
torchaudio 2.2.0+cu121
torchvision 0.17.0+cu121
tornado 6.4
tqdm 4.66.1
traitlets 5.14.1
types-python-dateutil 2.8.19.20240106
typing_extensions 4.8.0
tzdata 2023.4
ultralytics 8.2.14
uri-template 1.3.0
urllib3 1.26.13
wcwidth 0.2.13
webcolors 1.13
webencodings 0.5.1
websocket-client 1.7.0
Werkzeug 3.0.1
wheel 0.41.2
widgetsnbextension 4.0.9
youtube-dl 2020.12.2
zipp 3.17.0

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@noinsung noinsung added the bug Something isn't working label May 13, 2024
@glenn-jocher
Copy link
Member

@noinsung hi there! It looks like you're encountering the LinAlgError which typically points to an issue with the Kalman filter in the tracking algorithm where it's unable to process the covariance matrix properly.

This may happen if there's an inadequate detection input coming into the tracker. Hence, it's crucial to ensure that the detections fed into the tracker are valid and sufficiently accurate. One way to potentially mitigate this error is to tune the confidence threshold for detections, ensuring less noisy and more stable inputs to the tracker.

Here’s a quick way to adjust the confidence threshold in your code:

model = YOLO("yolov8n.pt", conf=0.4)  # Increase confidence threshold to 0.4

Adjust the conf value to a level that reduces the error but retains adequate detections for your use case.

If the issue persists despite adjusting the confidence levels or other parameters, it could be beneficial to look into the specific scenarios or frames causing these errors. This might involve diving deeper into the tracker code or even modifying the Kalman filter setup for better stability in your specific use case.

Let me know how it goes or if you need further assistance! 😊

@glenn-jocher glenn-jocher added the non-reproducible Bug is not reproducible label May 14, 2024
@noinsung
Copy link
Author

noinsung commented May 16, 2024

An error occurs when editing model = YOLO("yolov8n.pt", conf=0.4).

while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False,
classes=classes_to_count)

  im0 = counter.start_counting(im0, tracks)
  video_writer.write(im0)

===================================================
tracks = model.track(im0, persist=True, show=False,
classes=classes_to_count,conf=0.4)

I would like to ask if adding conf=0.4 is the correct way to fix it.

When you modify the above and run the program, it can run, but the same bug still occurs.

@glenn-jocher
Copy link
Member

Hello! It looks like you're on the right track with adjusting the confidence level using conf=0.4 to refine the tracking input. However, setting the conf parameter should be applied when you initialize the model, not during the track method call.

Here's how you should correctly set it up:

model = YOLO("yolov8n.pt", conf=0.4)  # Set your confidence threshold when loading the model

After setting this, you can continue with the tracking as you are doing in your script. Adjusting it directly in track() won't affect the configuration as it’s not an accepted parameter there.

If the issue persists after this correction, it might be beneficial to assess if the error is due to specific frames in your video or consider if other parameters might also need tweaking based on your specific use case.

Let us know how it goes or if there's anything else we can help you with! 😊

@noinsung
Copy link
Author

from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2

model = YOLO("yolov8n.pt", conf=0.4)
cap = cv2.VideoCapture("C:/Users/user/ultralytics-main/4K 주행영상 강원도 강릉시 GANGNEUNG CITY DRIVING DOWN TOWN KOREA ROAD ASMR 4K 60P.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

line_points = [(0, 720), (1280, 720), (1280, 0), (0, 0)] # line or region points
#[(40, 500), (1220, 500), (1080, 360), (100, 360)]
classes_to_count = [0, 2, 3, 5, 7] # person and car classes for count

Video writer

video_writer = cv2.VideoWriter("1212object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))

Init Object Counter

counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2,view_out_counts=False)

while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False,
classes=classes_to_count,conf=0.2)

im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()

==========================================================

(E:\Anaconda3_envs\yolov8) C:\Users\user\ultralytics-main\ultralytics>python classcounting.py
Traceback (most recent call last):
File "classcounting.py", line 5, in
model = YOLO("yolov8n.pt", conf=0.4)
TypeError: init() got an unexpected keyword argument 'conf'

When running the above code, the following error occurs.

@glenn-jocher
Copy link
Member

Hello! It looks like you're experiencing an issue with the initialization of the YOLO model with a conf parameter. The correct way to apply a confidence threshold is not during initialization, but when you're making predictions or tracking objects.

For your script that involves tracking, if you want to set the confidence threshold, you should pass it to the track method like this:

tracks = model.track(im0, conf=0.4, persist=True, show=False, classes=classes_to_count)

Make sure the conf parameter is within the track method. The YOLO constructor does not accept conf directly during model creation.

Let me know if modifying this resolves the issue, or if there's anything else you need help with! 😊

@noinsung
Copy link
Author

Yolov8 runs for about 10 minutes, but the same error message still occurs.

@glenn-jocher
Copy link
Member

Hello! It sounds like you're still encountering issues even after adjusting the confidence threshold. This might be due to specific frames or scenarios in your video that are causing instability in the tracking algorithm.

Could you try logging the frame number or timestamp when the error occurs? This might help identify if there's a specific part of the video causing the issue. Here's a quick way to modify your loop to include this:

frame_number = 0
while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("End of video or error reading frame.")
        break
    try:
        tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
        im0 = counter.start_counting(im0, tracks)
        video_writer.write(im0)
    except Exception as e:
        print(f"Error on frame {frame_number}: {str(e)}")
        break
    frame_number += 1

This modification will print the frame number where the error occurs, which can be very helpful for debugging. Let us know how it goes! 😊

@noinsung
Copy link
Author

.
.
.
0: 384x640 2 cars, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)

0: 384x640 2 cars, 11.5ms
Speed: 2.5ms preprocess, 11.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)

Error on frame 25203: 2-th leading minor of the array is not positive definite

The following error occurred. However, it does not necessarily end at the 25203rd frame, and the section where it ends is random each time the program is run. Based on the length of the video, sometimes it ends around 12 minutes, and sometimes it ends around 14 minutes.

I can't figure out what debugging to do to resolve this error.

@glenn-jocher
Copy link
Member

Hello! It looks like you're encountering a stability issue with the Kalman filter used in the tracking algorithm, which can be sensitive to the quality of input detections. This error typically arises when the covariance matrix isn't positive definite, often due to numerical instabilities or inadequate detection inputs.

Here are a couple of suggestions to help you troubleshoot and potentially resolve this issue:

  1. Adjust the Confidence Threshold: Increase the confidence threshold to ensure that only high-confidence detections are fed into the tracker. This can sometimes help stabilize the tracking updates.

    tracks = model.track(im0, conf=0.5, persist=True, show=False)
  2. Log More Information: Modify your code to log more detailed information about the detections and the state of the tracker right before the crash. This might help identify if specific detections or scenarios are causing the issue.

    try:
        tracks = model.track(im0, persist=True, show=False)
        # Log detection details here
    except Exception as e:
        print(f"Error on frame {frame_number}: {str(e)}")
        # Optionally log additional state information here
        break
  3. Review Input Data: Check if there are any anomalies in the video frames around the time the error occurs. Corrupted or highly unusual video data might be contributing to the instability.

If these steps don't help, consider providing a minimal reproducible example and the specific configurations you're using, so the community or the development team can further assist you! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working non-reproducible Bug is not reproducible
Projects
None yet
Development

No branches or pull requests

2 participants