Views
No views yet
1# install huggingface_ding
2git clone https://github.com/opendilab/huggingface_ding.git
3pip3 install -e ./huggingface_ding/
4# install environment dependencies if needed
5pip3 install DI-engine[common_env,video]1# running with trained model
2python3 -u run.py1from ding.bonus import DQNAgent
2from ding.config import Config
3from easydict import EasyDict
4import torch
5
6# Pull model from files which are git cloned from huggingface
7policy_state_dict = torch.load("pytorch_model.bin", map_location=torch.device("cpu"))
8cfg = EasyDict(Config.file_to_dict("policy_config.py").cfg_dict)
9# Instantiate the agent
10agent = DQNAgent(
11 env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-DQN", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
12)
13# Continue training
14agent.train(step=5000)
15# Render the new agent performance
16agent.deploy(enable_save_replay=True)
171# running with trained model
2python3 -u run.py1from ding.bonus import DQNAgent
2from huggingface_ding import pull_model_from_hub
3
4# Pull model from Hugggingface hub
5policy_state_dict, cfg = pull_model_from_hub(repo_id="OpenDILabCommunity/QbertNoFrameskip-v4-DQN")
6# Instantiate the agent
7agent = DQNAgent(
8 env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-DQN", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
9)
10# Continue training
11agent.train(step=5000)
12# Render the new agent performance
13agent.deploy(enable_save_replay=True)
141#Training Your Own Agent
2python3 -u train.py1from ding.bonus import DQNAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = DQNAgent(env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-DQN")
6# Train the agent
7return_ = agent.train(step=int(20000000), collector_env_num=8, evaluator_env_num=8, debug=False)
8print("-----wandb url is----:", return_.wandb_url)
9# Push model to huggingface hub
10push_model_to_hub(
11 agent=agent.best,
12 env_name="OpenAI/Gym/Atari",
13 task_name="QbertNoFrameskip-v4",
14 algo_name="DQN",
15 wandb_url=return_.wandb_url,
16 github_repo_url="https://github.com/opendilab/DI-engine",
17 github_doc_model_url="https://di-engine-docs.readthedocs.io/en/latest/12_policies/dqn.html",
18 github_doc_env_url="https://di-engine-docs.readthedocs.io/en/latest/13_envs/atari.html",
19 installation_guide="pip3 install DI-engine[common_env,video]",
20 usage_file_by_git_clone="./dqn/qbert_dqn_deploy.py",
21 usage_file_by_huggingface_ding="./dqn/qbert_dqn_download.py",
22 train_file="./dqn/qbert_dqn.py",
23 repo_id="OpenDILabCommunity/QbertNoFrameskip-v4-DQN",
24 create_repo=False
25)
261exp_config = {
2 'env': {
3 'manager': {
4 'episode_num': float("inf"),
5 'max_retry': 1,
6 'retry_type': 'reset',
7 'auto_reset': True,
8 'step_timeout': None,
9 'reset_timeout': None,
10 'retry_waiting_time': 0.1,
11 'cfg_type': 'BaseEnvManagerDict'
12 },
13 'stop_value': 30000,
14 'n_evaluator_episode': 8,
15 'env_id': 'QbertNoFrameskip-v4',
16 'collector_env_num': 8,
17 'evaluator_env_num': 8,
18 'fram_stack': 4,
19 'env_wrapper': 'atari_default'
20 },
21 'policy': {
22 'model': {
23 'encoder_hidden_size_list': [128, 128, 512],
24 'obs_shape': [4, 84, 84],
25 'action_shape': 6
26 },
27 'learn': {
28 'learner': {
29 'train_iterations': 1000000000,
30 'dataloader': {
31 'num_workers': 0
32 },
33 'log_policy': True,
34 'hook': {
35 'load_ckpt_before_run': '',
36 'log_show_after_iter': 100,
37 'save_ckpt_after_iter': 10000,
38 'save_ckpt_after_run': True
39 },
40 'cfg_type': 'BaseLearnerDict'
41 },
42 'update_per_collect': 10,
43 'batch_size': 32,
44 'learning_rate': 0.0001,
45 'target_update_freq': 500,
46 'target_theta': 0.005,
47 'ignore_done': False
48 },
49 'collect': {
50 'collector': {},
51 'n_sample': 100,
52 'unroll_len': 1
53 },
54 'eval': {
55 'evaluator': {
56 'eval_freq': 1000,
57 'render': {
58 'render_freq': -1,
59 'mode': 'train_iter'
60 },
61 'figure_path': None,
62 'cfg_type': 'InteractionSerialEvaluatorDict',
63 'stop_value': 30000,
64 'n_episode': 8
65 }
66 },
67 'other': {
68 'replay_buffer': {
69 'replay_buffer_size': 400000
70 },
71 'eps': {
72 'type': 'exp',
73 'start': 1.0,
74 'end': 0.05,
75 'decay': 1000000
76 }
77 },
78 'on_policy': False,
79 'cuda': True,
80 'multi_gpu': False,
81 'bp_update_sync': True,
82 'traj_len_inf': False,
83 'type': 'dqn',
84 'priority': False,
85 'priority_IS_weight': False,
86 'discount_factor': 0.99,
87 'nstep': 3,
88 'cfg_type': 'DQNPolicyDict'
89 },
90 'exp_name': 'QbertNoFrameskip-v4-DQN',
91 'seed': 0,
92 'wandb_logger': {
93 'gradient_logger': True,
94 'video_logger': True,
95 'plot_logger': True,
96 'action_logger': True,
97 'return_logger': False
98 }
99}
100