-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from juglab/ms/feat/model_downloader
- Loading branch information
Showing
4 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ dependencies: | |
- h5py | ||
- pytorch=2.1.2 | ||
- torchvision | ||
- pooch | ||
- pip | ||
- pip: | ||
- numpy | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ install_requires = | |
magicgui | ||
qtpy | ||
h5py | ||
pooch | ||
; pytorch | ||
; torchvision | ||
timm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pathlib import Path | ||
import pooch | ||
|
||
|
||
MODELS_CACHE_DIR = Path.home().joinpath(".featureforest", "models") | ||
|
||
|
||
def download_model( | ||
model_url: str, model_name: str, | ||
cache_dir: Path = MODELS_CACHE_DIR, is_archived: bool = False | ||
) -> str: | ||
"""Download a model weights from a given url. | ||
Args: | ||
model_url (str): the model weights' url. | ||
model_name (str): model's name that will be saved in cache. | ||
cache_dir (Path, optional): download directory. Defaults to CACHE_DIR. | ||
is_archived (bool, optional): set to True to unzip the downloaded file. | ||
Defaults to False. | ||
Returns: | ||
str: full path of the downloaded file. | ||
""" | ||
try: | ||
downloaded_file = pooch.retrieve( | ||
url=model_url, | ||
fname=model_name, | ||
path=cache_dir, | ||
known_hash=None, | ||
processor=pooch.Unzip() if is_archived else None | ||
) | ||
# for zip files, get the file ending with "pt" or "pth" as model weights file. | ||
if is_archived: | ||
pytorch_files = [ | ||
f for f in downloaded_file | ||
if Path(f).suffix in ["pt", "pth"] | ||
] | ||
downloaded_file = pytorch_files[0] if len(pytorch_files) > 0 else None | ||
|
||
return downloaded_file | ||
|
||
except Exception as err: | ||
print(f"\nError while downloading the model:\n{err}") | ||
return None |