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

detect.py fails without internet connection, always tries to load pre-trained model weights #2799

Closed
r-blmnr opened this issue Apr 15, 2021 · 5 comments · Fixed by #2890
Closed

Comments

@r-blmnr
Copy link
Contributor

r-blmnr commented Apr 15, 2021

First of all, thanks for this great piece of software!

Problem

Without internet connection, calling detect.py fails. It seems that even when a local copy of weights is available, the code still tries to load a set of weights from an online resource.

Commenting this line prevents the error and the code runs as expected:

attempt_download(w)

However, as I am using the yolov5 version from PyPI, which is dynamically installed into a virtual environment, commenting is not a long-term option.

Request

@glenn-jocher Would it be possible to include a switch, where the code only tries to load weights from an online resource if the weights are not found locally? This would also be in line with the comment at:

# Attempt file download if does not exist

Thanks in advance!

@github-actions
Copy link
Contributor

github-actions bot commented Apr 15, 2021

👋 Hello @r-blmnr, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@r-blmnr hi, thanks for the feature request! Models are already only searched for online if they are not found locally in L23 below. This feature has been in place for several months. Please git pull or git clone to use the latest code.

def attempt_download(file, repo='ultralytics/yolov5'):
# Attempt file download if does not exist
file = Path(str(file).strip().replace("'", '').lower())
if not file.exists():
try:
response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
tag = response['tag_name'] # i.e. 'v1.0'
except: # fallback plan
assets = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']
tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]
name = file.name
if name in assets:
msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
redundant = False # second download option
try: # GitHub
url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
print(f'Downloading {url} to {file}...')
torch.hub.download_url_to_file(url, file)
assert file.exists() and file.stat().st_size > 1E6 # check
except Exception as e: # GCP
print(f'Download error: {e}')
assert redundant, 'No secondary mirror'
url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
print(f'Downloading {url} to {file}...')
os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights)
finally:
if not file.exists() or file.stat().st_size < 1E6: # check
file.unlink(missing_ok=True) # remove partial downloads
print(f'ERROR: Download failure: {msg}')
print('')
return

@r-blmnr
Copy link
Contributor Author

r-blmnr commented Apr 21, 2021

@glenn-jocher thanks for the quick reply. I have been using version 4.0.12 from PyPI, where line 25 from the code excerpt in your reply was still executed before checking whether the weight file exists. However, I just saw that this has been fixed in release 5.0.0 (issue #1890).

While working on this I found another problem with my project structure:
My weight files were stored in a directory that contained uppercase characters. In L21 in the code excerpt above however, the file path is converted to lowercase, after which my model weights "did not exist".

Upgrading to version 5.0.0 and converting directory names to lower case solved my issue. Thanks again.

@r-blmnr r-blmnr closed this as completed Apr 21, 2021
@glenn-jocher
Copy link
Member

@r-blmnr ah! The .lower() must be the root cause of your issue. Can you submit a PR for it's removal please?

@r-blmnr
Copy link
Contributor Author

r-blmnr commented Apr 22, 2021

done.

@r-blmnr r-blmnr reopened this Apr 22, 2021
@glenn-jocher glenn-jocher linked a pull request Apr 22, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants