-
Notifications
You must be signed in to change notification settings - Fork 30
[WIP] Benchmark downloader #449
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
Open
JulienT01
wants to merge
21
commits into
rlberry-py:main
Choose a base branch
from
JulienT01:benchmark_download_process
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b095f36
split 'compare_agents' into 2 methods : preprocess_agent_data and com…
JulienT01 5320711
download sb3_zoo benchmark
JulienT01 ca18cf6
add benchmark folder to .gitignore
JulienT01 3f442aa
Merge remote-tracking branch 'upstream/main' into benchmark_download_…
JulienT01 819ada6
empty commit
JulienT01 06957bc
move benchmark_utils
JulienT01 8de3580
add tests on benchmark
JulienT01 a9cdb48
add github dependencies
JulienT01 56ae643
add github dependencies
JulienT01 b5258d6
update version github toml
JulienT01 9aef40d
update version github toml
JulienT01 461ee1f
update benchmark utils to avoid github api (limit call number)
JulienT01 6cbc9b5
add option to overwrite or not
JulienT01 8b51cfd
update test to windows compatibility
JulienT01 316004f
update changelog
JulienT01 0aef1c0
add doc and userguide
JulienT01 a8765f4
Merge branch 'main' into benchmark_download_process
JulienT01 1f9d63e
resolve merge problem in comparaison
JulienT01 9598255
update docs
JulienT01 433590c
rename download_path to output_dir
JulienT01 856b540
resolve merge problem in comparison.py
JulienT01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| # tensorboard runs | ||
| runs/ | ||
|
|
||
| #benchmark folder | ||
| download_benchmark/ | ||
|
|
||
| # videos | ||
| #*.mp4 | ||
| notebooks/videos/* | ||
|
|
||
This file contains hidden or 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 hidden or 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,151 @@ | ||
| import requests | ||
| from tempfile import mkdtemp | ||
| import os | ||
| import shutil | ||
|
|
||
|
|
||
| # TODO : convert external benchmark to DataFrame that match the input of rlberry.manager.comparaison.py -> compare_agents_data() | ||
| # TODO : Download the external benchmark to a specific folder (or new rlberrygithub?), except if they are stable (huggingface/github) | ||
|
|
||
| benchmark_list = { | ||
| "Google Atari bucket": "https://console.cloud.google.com/storage/brow", | ||
| "SB3 zoo": "https://github.com/DLR-RM/rl-baselines3-zoo/tree/master/logs/benchmark", | ||
| "cleanrl": "https://wandb.ai/openrlbenchmark/openrlbenchmark/reportlist", | ||
| } | ||
|
|
||
|
|
||
| def import_from_google_atari_bucket(): | ||
| """import benchmark from Google Atari bucket | ||
|
|
||
| Parameters | ||
| ----------- | ||
| x_vec : numpy.ndarray | ||
| numpy 1d array to be searched in the bins | ||
| bins : list | ||
| list of numpy 1d array, bins[d] = bins of the d-th dimension | ||
|
|
||
|
|
||
| Returns | ||
| -------- | ||
| index (int) corresponding to the position of x in the partition | ||
| defined by the bins. | ||
| """ | ||
| print("TODO") | ||
|
|
||
|
|
||
| def import_from_cleanrl(): | ||
| print("TODO") | ||
|
|
||
|
|
||
| def import_from_hugingface(): | ||
| print("TODO") | ||
|
|
||
|
|
||
| def download_benchmark_from_SB3_zoo( | ||
| agent_name, environment_name, overwrite, download_path=None | ||
| ): | ||
| """ | ||
| Download folder from pre-trained Reinforcement Learning agents using the rl-baselines3-zoo and Stable Baselines3. | ||
| https://github.com/DLR-RM/rl-trained-agents | ||
|
|
||
| Parameters | ||
| ----------- | ||
| agent_name : str | ||
| agent name for benchmark to download | ||
| environment_name : list | ||
| environment name for benchmark to download | ||
| overwrite : bool | ||
| how to manage if the combination agent_name/environment_name exist : | ||
| True : delete the previous folder, then download | ||
| False : raise an error | ||
| download_path : str | ||
| root path where to download files. (default=None : create temp folder) | ||
|
|
||
| Returns | ||
| -------- | ||
| Return the path containing the downloaded files (download_path/agent_name/environment_name) | ||
| """ | ||
| if not download_path: | ||
| download_path = mkdtemp() | ||
|
|
||
| GITHUB_URL = "https://raw.githubusercontent.com/DLR-RM/rl-trained-agents/master/" | ||
| base_url = GITHUB_URL + agent_name + "/" + environment_name + "/" | ||
|
|
||
| output_folder = os.path.join(download_path, agent_name, environment_name) | ||
| environment_base_name = environment_name.split("_")[0] | ||
|
|
||
| if os.path.exists(output_folder): | ||
| if not overwrite: | ||
| raise FileExistsError( | ||
| "The 'overwrite' bool is false, and the combination %s / %s already exist" | ||
| % (agent_name, environment_name) | ||
| ) | ||
| shutil.rmtree(output_folder) | ||
| os.makedirs(output_folder) | ||
|
|
||
| # download CSVs | ||
| url_content = None | ||
| i = 0 | ||
| while url_content != b"404: Not Found": | ||
| file_name_to_download = str(i) + ".monitor.csv" | ||
| url_csv_to_download = base_url + file_name_to_download | ||
|
|
||
| req = requests.get(url_csv_to_download) | ||
| url_content = req.content | ||
|
|
||
| if url_content != b"404: Not Found": | ||
| csv_file = open(os.path.join(output_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
| else: | ||
| break | ||
| i = i + 1 | ||
|
|
||
| # download zip | ||
| file_name_to_download = environment_base_name + ".zip" | ||
| url_zip_to_download = base_url + file_name_to_download | ||
| req = requests.get(url_zip_to_download) | ||
| url_content = req.content | ||
| csv_file = open(os.path.join(output_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
|
|
||
| # download evaluations.npz | ||
| file_name_to_download = "evaluations.npz" | ||
| url_zip_to_download = base_url + file_name_to_download | ||
| req = requests.get(url_zip_to_download) | ||
| url_content = req.content | ||
| csv_file = open(os.path.join(output_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
|
|
||
| # hyperparameter and config | ||
| config_folder = output_folder + "/" + environment_base_name | ||
| base_url_config = base_url + environment_base_name + "/" | ||
|
|
||
| os.makedirs(config_folder) | ||
| file_name_to_download = "args.yml" | ||
| url_zip_to_download = base_url_config + file_name_to_download | ||
| req = requests.get(url_zip_to_download) | ||
| url_content = req.content | ||
| csv_file = open(os.path.join(config_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
|
|
||
| file_name_to_download = "config.yml" | ||
| url_zip_to_download = base_url_config + file_name_to_download | ||
| req = requests.get(url_zip_to_download) | ||
| url_content = req.content | ||
| csv_file = open(os.path.join(config_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
|
|
||
| file_name_to_download = "vecnormalize.pkl" | ||
| url_zip_to_download = base_url_config + file_name_to_download | ||
| req = requests.get(url_zip_to_download) | ||
| url_content = req.content | ||
| csv_file = open(os.path.join(config_folder, file_name_to_download), "wb") | ||
| csv_file.write(url_content) | ||
| csv_file.close() | ||
|
|
||
| return output_folder | ||
This file contains hidden or 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,88 @@ | ||
| import os | ||
| import shutil | ||
| from rlberry.benchmarks.benchmark_utils import download_benchmark_from_SB3_zoo | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("agent_class", ["dqn"]) | ||
| @pytest.mark.parametrize("env", ["PongNoFrameskip-v4_1"]) | ||
| def test_download_benchmark_from_SB3_zoo_(agent_class, env): | ||
| # remove previous test if existing | ||
| test_folder_path = "./tests_dl" | ||
| if os.path.exists(test_folder_path): | ||
| shutil.rmtree(test_folder_path) | ||
| os.makedirs(test_folder_path) | ||
|
|
||
| # download benchmark | ||
| ret_value = download_benchmark_from_SB3_zoo( | ||
| agent_class, env, overwrite=True, download_path=test_folder_path | ||
| ) | ||
|
|
||
| # tests expected result | ||
| environment_base_name = env.split("_")[0] | ||
| assert str(os.path.join(test_folder_path, agent_class, env)) == ret_value | ||
| assert os.path.exists(os.path.join(test_folder_path, agent_class, env)) | ||
| assert os.path.exists( | ||
| os.path.join(test_folder_path, agent_class, env, "0.monitor.csv") | ||
| ) | ||
| assert os.path.exists( | ||
| os.path.join(test_folder_path, agent_class, env, environment_base_name + ".zip") | ||
| ) | ||
| assert os.path.exists( | ||
| os.path.join(test_folder_path, agent_class, env, "evaluations.npz") | ||
| ) | ||
| assert os.path.exists( | ||
| os.path.join( | ||
| test_folder_path, agent_class, env, environment_base_name, "args.yml" | ||
| ) | ||
| ) | ||
| assert os.path.exists( | ||
| os.path.join( | ||
| test_folder_path, agent_class, env, environment_base_name, "config.yml" | ||
| ) | ||
| ) | ||
| assert os.path.exists( | ||
| os.path.join( | ||
| test_folder_path, | ||
| agent_class, | ||
| env, | ||
| environment_base_name, | ||
| "vecnormalize.pkl", | ||
| ) | ||
| ) | ||
|
|
||
| if os.path.exists(test_folder_path): | ||
| shutil.rmtree(test_folder_path) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("agent_class", ["dqn"]) | ||
| @pytest.mark.parametrize("env", ["PongNoFrameskip-v4_1"]) | ||
| @pytest.mark.parametrize("overwrite", [True, False]) | ||
| def test_download_benchmark_from_SB3_zoo_overwrite_True(agent_class, env, overwrite): | ||
| # remove previous test if existing | ||
| test_folder_path = "./tests_dl" | ||
| if os.path.exists(test_folder_path): | ||
| shutil.rmtree(test_folder_path) | ||
| os.makedirs(test_folder_path) | ||
|
|
||
| # first call | ||
| ret_value = download_benchmark_from_SB3_zoo( | ||
| agent_class, env, overwrite=overwrite, download_path=test_folder_path | ||
| ) | ||
|
|
||
| #'overwrite' test | ||
| error_was_raised = False | ||
| try: | ||
| ret_value = download_benchmark_from_SB3_zoo( | ||
| agent_class, env, overwrite=overwrite, download_path=test_folder_path | ||
| ) | ||
| except FileExistsError: | ||
| error_was_raised = True | ||
|
|
||
| if overwrite: | ||
| assert not error_was_raised | ||
| else: | ||
| assert error_was_raised | ||
|
|
||
| if os.path.exists(test_folder_path): | ||
| shutil.rmtree(test_folder_path) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.