https://github.com/huggingface/huggingface_sb3

Additional code for Stable-baselines3 to load and upload models from the Hub.

https://github.com/huggingface/huggingface_sb3

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 8 committers (12.5%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.5%) to scientific vocabulary

Keywords from Contributors

transformer reinforcement-learning diffusion model-hub pytorch-transformers qwen speech-recognition vlm flux image-generation
Last synced: 6 months ago · JSON representation

Repository

Additional code for Stable-baselines3 to load and upload models from the Hub.

Basic Info
  • Host: GitHub
  • Owner: huggingface
  • Language: Jupyter Notebook
  • Default Branch: main
  • Size: 2.53 MB
Statistics
  • Stars: 88
  • Watchers: 31
  • Forks: 28
  • Open Issues: 11
  • Releases: 9
Created over 4 years ago · Last pushed almost 2 years ago
Metadata Files
Readme

README.md

Hugging Face 🤗 x Stable-baselines3 v3.0

A library to load and upload Stable-baselines3 models from the Hub with Gymnasium and Gymnasium compatible environments.

⚠️ If you use Gym, you need to install huggingface_sb3==2.3.1

Installation

With pip

pip install huggingface-sb3

Examples

We wrote a tutorial on how to use 🤗 Hub and Stable-Baselines3 here

If you use Colab or a Virtual/Screenless Machine, you can check Case 3 and Case 4.

Case 1: I want to download a model from the Hub

```python import gymnasium as gym

from huggingfacesb3 import loadfromhub from stablebaselines3 import PPO from stablebaselines3.common.evaluation import evaluatepolicy

Retrieve the model from the hub

repoid = id of the model repository from the Hugging Face Hub (repoid = {organization}/{repo_name})

filename = name of the model zip file from the repository

checkpoint = loadfromhub( repo_id="sb3/demo-hf-CartPole-v1", filename="ppo-CartPole-v1.zip", ) model = PPO.load(checkpoint)

Evaluate the agent and watch it

evalenv = gym.make("CartPole-v1") meanreward, stdreward = evaluatepolicy( model, evalenv, render=False, nevalepisodes=5, deterministic=True, warn=False ) print(f"meanreward={meanreward:.2f} +/- {stdreward}") ```

Case 2: I trained an agent and want to upload it to the Hub

With package_to_hub() we'll save, evaluate, generate a model card and record a replay video of your agent before pushing the repo to the hub. It currently works for Gym and Atari environments. If you use another environment, you should use push_to_hub() instead.

First you need to be logged in to Hugging Face: - If you're using Colab/Jupyter Notebooks: ```python from huggingface_hub import login

login() - Else: huggingface-cli login ```

For more details about authentication, check out this guide.

Then

With package_to_hub():

```python import gymnasium as gym

from stablebaselines3 import PPO from stablebaselines3.common.envutil import makevecenv from huggingfacesb3 import packagetohub

Create the environment

envid = "LunarLander-v2" env = makevecenv(envid, n_envs=1)

Create the evaluation env

evalenv = makevecenv(envid, n_envs=1)

Instantiate the agent

model = PPO("MlpPolicy", env, verbose=1)

Train the agent

model.learn(total_timesteps=int(5000))

This method save, evaluate, generate a model card and record a replay video of your agent before pushing the repo to the hub

packagetohub(model=model, modelname="ppo-LunarLander-v2", modelarchitecture="PPO", envid=envid, evalenv=evalenv, repoid="ThomasSimonini/ppo-LunarLander-v2", commitmessage="Test commit") ```

With push_to_hub(): Push to hub only push a file to the Hub, if you want to save, evaluate, generate a model card and record a replay video of your agent before pushing the repo to the hub, use package_to_hub()

```python import gymnasium as gym

from stablebaselines3 import PPO from stablebaselines3.common.envutil import makevecenv from huggingfacesb3 import pushtohub

Create the environment

envid = "LunarLander-v2" env = makevecenv(envid, n_envs=1)

Instantiate the agent

model = PPO("MlpPolicy", env, verbose=1)

Train it for 10000 timesteps

model.learn(totaltimesteps=10000)

Save the model

model.save("ppo-LunarLander-v2")

Push this saved model .zip file to the hf repo

If this repo does not exists it will be created

repoid = id of the model repository from the Hugging Face Hub (repoid = {organization}/{repo_name})

filename: the name of the file == "name" inside model.save("ppo-LunarLander-v2")

pushtohub( repoid="ThomasSimonini/ppo-LunarLander-v2", filename="ppo-LunarLander-v2.zip", commitmessage="Added LunarLander-v2 model trained with PPO", ) ```

Case 3: I use Google Colab with Classic Control/Box2D Gym Environments

  • You can use xvbf (virtual screen) !apt-get install -y xvfb python-opengl > /dev/null 2>&1
  • Just put your code inside a python file and run !xvfb-run -s "-screen 0 1400x900x24" <your_python_file>

Case 4: I use a Virtual/Remote Machine

  • You can use xvbf (virtual screen)

xvfb-run -s "-screen 0 1400x900x24" <your_python_file>

Case 5: I want to automate upload/download from the Hub

If you want to upload or download models for many environments, you might want to automate this process. It makes sense to adhere to a fixed naming scheme for models and repositories. You will run into trouble when your environment names contain slashes. Therefore, we provide some helper classes:

```python import gymnasium as gym from huggingfacesb3.namingschemes import EnvironmentName, ModelName, ModelRepoId

envname = EnvironmentName("seals/Walker2d-v0") modelname = ModelName("ppo", envname) repoid = ModelRepoId("YourOrganization", model_name)

prints 'seals-Walker2d-v0'. Notice how the slash is removed so you can use it to

construct file paths if you like.

print(env_name)

you can still access the original gym id if needed

env = gym.make(envname.gymid)

prints ppo-seals-Walker2d-v0

print(model_name)

prints: ppo-seals-Walker2d-v0.zip.

This is where model.save(model_name) will place the model file

print(model_name.filename)

prints: YourOrganization/ppo-seals-Walker2d-v0

print(repo_id) ```

Owner

  • Name: Hugging Face
  • Login: huggingface
  • Kind: organization
  • Location: NYC + Paris

The AI community building the future.

GitHub Events

Total
  • Issues event: 9
  • Watch event: 13
  • Issue comment event: 19
  • Pull request review event: 1
  • Fork event: 6
Last Year
  • Issues event: 9
  • Watch event: 13
  • Issue comment event: 19
  • Pull request review event: 1
  • Fork event: 6

Committers

Last synced: 6 months ago

All Time
  • Total Commits: 137
  • Total Committers: 8
  • Avg Commits per committer: 17.125
  • Development Distribution Score (DDS): 0.54
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Thomas Simonini s****o@g****m 63
simoninithomas s****s@o****r 33
Maximilian Ernestus m****n@e****e 19
Antonin Raffin a****n@e****g 10
Quentin Gallouédec 4****c@u****m 4
osanseviero o****o@g****m 4
nateraw n****6@g****u 3
Lucain l****p@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 27
  • Total pull requests: 25
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 11 days
  • Total issue authors: 22
  • Total pull request authors: 11
  • Average comments per issue: 2.19
  • Average comments per pull request: 1.68
  • Merged pull requests: 19
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 8
  • Pull requests: 1
  • Average time to close issues: 25 days
  • Average time to close pull requests: N/A
  • Issue authors: 7
  • Pull request authors: 1
  • Average comments per issue: 1.38
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • osanseviero (3)
  • ernestum (3)
  • Fouad-Smaoui (2)
  • zhangjian94cn (1)
  • Yatsrib-star (1)
  • LoryPack (1)
  • zhangpaipai (1)
  • zhang705 (1)
  • markLaiunimelb (1)
  • HusseinEid101 (1)
  • meenusinha (1)
  • GEMCorp (1)
  • araffin (1)
  • zoctipus (1)
  • damiano00 (1)
Pull Request Authors
  • simoninithomas (5)
  • araffin (5)
  • ernestum (3)
  • qgallouedec (3)
  • osanseviero (2)
  • nateraw (2)
  • Wauplin (1)
  • EdoardoPona (1)
  • Benjy-D (1)
  • andrewtheiss (1)
  • robertoschiavone (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 28,715 last-month
  • Total docker downloads: 52
  • Total dependent packages: 8
  • Total dependent repositories: 3
  • Total versions: 18
  • Total maintainers: 1
pypi.org: huggingface-sb3

Additional code for Stable-baselines3 to load and upload models from the Hub.

  • Versions: 18
  • Dependent Packages: 8
  • Dependent Repositories: 3
  • Downloads: 28,715 Last month
  • Docker Downloads: 52
Rankings
Dependent packages count: 1.3%
Docker downloads count: 3.1%
Downloads: 3.6%
Average: 5.7%
Forks count: 8.4%
Stargazers count: 8.7%
Dependent repos count: 9.0%
Maintainers (1)
Last synced: 6 months ago

Dependencies

setup.py pypi
  • cloudpickle ==1.6
  • huggingface_hub ==0.8.1
  • numpy *
  • pyyaml ==6.0
  • wasabi *
.github/workflows/python-release.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite