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 IMPALAAgent
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 = IMPALAAgent(
11 env_id="SpaceInvadersNoFrameskip-v4", exp_name="SpaceInvadersNoFrameskip-v4-IMPALA", 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 IMPALAAgent
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/SpaceInvadersNoFrameskip-v4-IMPALA")
6# Instantiate the agent
7agent = IMPALAAgent(
8 env_id="SpaceInvadersNoFrameskip-v4", exp_name="SpaceInvadersNoFrameskip-v4-IMPALA", 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 IMPALAAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = IMPALAAgent(env_id="SpaceInvadersNoFrameskip-v4", exp_name="SpaceInvadersNoFrameskip-v4-IMPALA")
6# Train the agent
7return_ = agent.train(step=int(20000000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Atari",
12 task_name="SpaceInvadersNoFrameskip-v4",
13 algo_name="IMPALA",
14 wandb_url=return_.wandb_url,
15 github_repo_url="https://github.com/opendilab/DI-engine",
16 github_doc_model_url="https://di-engine-docs.readthedocs.io/en/latest/12_policies/impala.html",
17 github_doc_env_url="https://di-engine-docs.readthedocs.io/en/latest/13_envs/atari.html",
18 installation_guide="pip3 install DI-engine[common_env,video]",
19 usage_file_by_git_clone="./impala/spaceinvaders_impala_deploy.py",
20 usage_file_by_huggingface_ding="./impala/spaceinvaders_impala_download.py",
21 train_file="./impala/spaceinvaders_impala.py",
22 repo_id="OpenDILabCommunity/SpaceInvadersNoFrameskip-v4-IMPALA",
23 create_repo=True
24)
251exp_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': 2000,
14 'n_evaluator_episode': 8,
15 'env_id': 'SpaceInvadersNoFrameskip-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 'obs_shape': [4, 84, 84],
24 'action_shape': 6,
25 'encoder_hidden_size_list': [128, 128, 256, 256],
26 'critic_head_hidden_size': 256,
27 'critic_head_layer_num': 3,
28 'actor_head_hidden_size': 256,
29 'actor_head_layer_num': 3
30 },
31 'learn': {
32 'learner': {
33 'train_iterations': 1000000000,
34 'dataloader': {
35 'num_workers': 0
36 },
37 'log_policy': True,
38 'hook': {
39 'load_ckpt_before_run': '',
40 'log_show_after_iter': 100,
41 'save_ckpt_after_iter': 10000,
42 'save_ckpt_after_run': True
43 },
44 'cfg_type': 'BaseLearnerDict'
45 },
46 'update_per_collect': 2,
47 'batch_size': 128,
48 'learning_rate': 0.0006,
49 'value_weight': 0.5,
50 'entropy_weight': 0.01,
51 'discount_factor': 0.99,
52 'lambda_': 0.95,
53 'rho_clip_ratio': 1.0,
54 'c_clip_ratio': 1.0,
55 'rho_pg_clip_ratio': 1.0,
56 'grad_clip_type': 'clip_norm',
57 'clip_value': 5
58 },
59 'collect': {
60 'collector': {
61 'collect_print_freq': 1000
62 },
63 'n_sample': 16,
64 'unroll_len': 64
65 },
66 'eval': {
67 'evaluator': {
68 'eval_freq': 500,
69 'render': {
70 'render_freq': -1,
71 'mode': 'train_iter'
72 },
73 'figure_path': None,
74 'cfg_type': 'InteractionSerialEvaluatorDict',
75 'stop_value': 2000,
76 'n_episode': 8
77 }
78 },
79 'other': {
80 'replay_buffer': {
81 'replay_buffer_size': 64000,
82 'max_use': 16,
83 'sliced': True
84 }
85 },
86 'on_policy': False,
87 'cuda': True,
88 'multi_gpu': False,
89 'bp_update_sync': True,
90 'traj_len_inf': False,
91 'type': 'impala',
92 'priority': False,
93 'priority_IS_weight': False,
94 'action_space': 'discrete',
95 'unroll_len': 64,
96 'transition_with_policy_data': True,
97 'cfg_type': 'IMPALAPolicyDict',
98 'random_collect_size': 5000
99 },
100 'exp_name': 'SpaceInvadersNoFrameskip-v4-IMPALA',
101 'seed': 0,
102 'wandb_logger': {
103 'gradient_logger': True,
104 'video_logger': True,
105 'plot_logger': True,
106 'action_logger': True,
107 'return_logger': False
108 }
109}
110