Skip to content

Commit

Permalink
ultralytics 8.1.37 fix empty sys.argv bug (ultralytics#9390)
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Jocher <[email protected]>
Co-authored-by: Joshua Harrison <[email protected]>
Co-authored-by: Joshua Harrison <[email protected]>
Co-authored-by: UltralyticsAssistant <[email protected]>
  • Loading branch information
4 people authored Mar 29, 2024
1 parent ed2250c commit 4a7ccba
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
8 changes: 6 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

import sys
from unittest import mock
from ultralytics import YOLO
from ultralytics.cfg import get_cfg
from ultralytics.engine.exporter import Exporter
Expand Down Expand Up @@ -49,8 +51,10 @@ def test_detect():
pred = detect.DetectionPredictor(overrides={"imgsz": [64, 64]})
pred.add_callback("on_predict_start", test_func)
assert test_func in pred.callbacks["on_predict_start"], "callback test failed"
result = pred(source=ASSETS, model=f"{MODEL}.pt")
assert len(result), "predictor test failed"
# Confirm there is no issue with sys.argv being empty.
with mock.patch.object(sys, 'argv', []):
result = pred(source=ASSETS, model=f"{MODEL}.pt")
assert len(result), "predictor test failed"

overrides["resume"] = trainer.last
trainer = detect.DetectionTrainer(overrides=overrides)
Expand Down
2 changes: 1 addition & 1 deletion ultralytics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

__version__ = "8.1.36"
__version__ = "8.1.37"

from ultralytics.data.explorer.explorer import Explorer
from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld
Expand Down
5 changes: 3 additions & 2 deletions ultralytics/cfg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
"obb": "metrics/mAP50-95(B)",
}

ARGV = sys.argv or ["", ""] # sometimes sys.argv = []
CLI_HELP_MSG = f"""
Arguments received: {str(['yolo'] + sys.argv[1:])}. Ultralytics 'yolo' commands use the following syntax:
Arguments received: {str(['yolo'] + ARGV[1:])}. Ultralytics 'yolo' commands use the following syntax:
yolo TASK MODE ARGS
Expand Down Expand Up @@ -452,7 +453,7 @@ def entrypoint(debug=""):
It uses the package's default cfg and initializes it using the passed overrides.
Then it calls the CLI function with the composed cfg
"""
args = (debug.split(" ") if debug else sys.argv)[1:]
args = (debug.split(" ") if debug else ARGV)[1:]
if not args: # no arguments passed
LOGGER.info(CLI_HELP_MSG)
return
Expand Down
18 changes: 14 additions & 4 deletions ultralytics/engine/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

import inspect
import sys
from pathlib import Path
from typing import Union

Expand All @@ -11,7 +10,18 @@
from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir
from ultralytics.hub.utils import HUB_WEB_ROOT
from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, nn, yaml_model_load
from ultralytics.utils import ASSETS, DEFAULT_CFG_DICT, LOGGER, RANK, SETTINGS, callbacks, checks, emojis, yaml_load
from ultralytics.utils import (
ARGV,
ASSETS,
DEFAULT_CFG_DICT,
LOGGER,
RANK,
SETTINGS,
callbacks,
checks,
emojis,
yaml_load,
)


class Model(nn.Module):
Expand Down Expand Up @@ -421,8 +431,8 @@ def predict(
source = ASSETS
LOGGER.warning(f"WARNING ⚠️ 'source' is missing. Using 'source={source}'.")

is_cli = (sys.argv[0].endswith("yolo") or sys.argv[0].endswith("ultralytics")) and any(
x in sys.argv for x in ("predict", "track", "mode=predict", "mode=track")
is_cli = (ARGV[0].endswith("yolo") or ARGV[0].endswith("ultralytics")) and any(
x in ARGV for x in ("predict", "track", "mode=predict", "mode=track")
)

custom = {"conf": 0.25, "batch": 1, "save": is_cli, "mode": "predict"} # method defaults
Expand Down
4 changes: 2 additions & 2 deletions ultralytics/hub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import os
import platform
import random
import sys
import threading
import time
from pathlib import Path

import requests

from ultralytics.utils import (
ARGV,
ENVIRONMENT,
LOGGER,
ONLINE,
Expand Down Expand Up @@ -188,7 +188,7 @@ def __init__(self):
self.rate_limit = 60.0 # rate limit (seconds)
self.t = 0.0 # rate limit timer (seconds)
self.metadata = {
"cli": Path(sys.argv[0]).name == "yolo",
"cli": Path(ARGV[0]).name == "yolo",
"install": "git" if is_git_dir() else "pip" if is_pip_package() else "other",
"python": ".".join(platform.python_version_tuple()[:2]), # i.e. 3.10
"version": __version__,
Expand Down
9 changes: 5 additions & 4 deletions ultralytics/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
LOCAL_RANK = int(os.getenv("LOCAL_RANK", -1)) # https://pytorch.org/docs/stable/elastic/run.html

# Other Constants
ARGV = sys.argv or ["", ""] # sometimes sys.argv = []
FILE = Path(__file__).resolve()
ROOT = FILE.parents[1] # YOLO
ASSETS = ROOT / "assets" # default images
Expand Down Expand Up @@ -522,7 +523,7 @@ def is_pytest_running():
Returns:
(bool): True if pytest is running, False otherwise.
"""
return ("PYTEST_CURRENT_TEST" in os.environ) or ("pytest" in sys.modules) or ("pytest" in Path(sys.argv[0]).stem)
return ("PYTEST_CURRENT_TEST" in os.environ) or ("pytest" in sys.modules) or ("pytest" in Path(ARGV[0]).stem)


def is_github_action_running() -> bool:
Expand Down Expand Up @@ -869,8 +870,8 @@ def before_send(event, hint):
return None # do not send event

event["tags"] = {
"sys_argv": sys.argv[0],
"sys_argv_name": Path(sys.argv[0]).name,
"sys_argv": ARGV[0],
"sys_argv_name": Path(ARGV[0]).name,
"install": "git" if is_git_dir() else "pip" if is_pip_package() else "other",
"os": ENVIRONMENT,
}
Expand All @@ -879,7 +880,7 @@ def before_send(event, hint):
if (
SETTINGS["sync"]
and RANK in (-1, 0)
and Path(sys.argv[0]).name == "yolo"
and Path(ARGV[0]).name == "yolo"
and not TESTS_RUNNING
and ONLINE
and is_pip_package()
Expand Down

0 comments on commit 4a7ccba

Please sign in to comment.