Skip to content
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

可以test,无法训练,报错 #2

Open
aijunzhao opened this issue May 20, 2023 · 18 comments
Open

可以test,无法训练,报错 #2

aijunzhao opened this issue May 20, 2023 · 18 comments

Comments

@aijunzhao
Copy link

(SnakeAI) E:\snake-ai-master\main>python train_cnn.py
Using cuda device
Wrapping the env in a VecTransposeImage.
Process SpawnProcess-5:
Traceback (most recent call last):
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\vec_env\subproc_vec_env.py", line 30, in _worker
observation, reward, done, info = env.step(data)
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\monitor.py", line 95, in step
observation, reward, done, info = self.env.step(action)
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\gym\core.py", line 289, in step
return self.env.step(action)
File "E:\snake-ai-master\main\snake_game_custom_wrapper_cnn.py", line 47, in step
self.done, info = self.game.step(action) # info = {"snake_size": int, "snake_head_pos": np.array, "prev_snake_head_pos": np.array, "food_pos": np.array, "food_obtained": bool}
File "E:\snake-ai-master\main\snake_game.py", line 96, in step
self.sound_game_over.play()
AttributeError: 'SnakeGame' object has no attribute 'sound_game_over'
Traceback (most recent call last):
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\multiprocessing\connection.py", line 312, in _recv_bytes
nread, err = ov.GetOverlappedResult(True)
BrokenPipeError: [WinError 109] 管道已结束。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "train_cnn.py", line 95, in
main()
File "train_cnn.py", line 82, in main
model.learn(
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\sb3_contrib\ppo_mask\ppo_mask.py", line 525, in learn
continue_training = self.collect_rollouts(self.env, callback, self.rollout_buffer, self.n_steps, use_masking)
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\sb3_contrib\ppo_mask\ppo_mask.py", line 305, in collect_rollouts
new_obs, rewards, dones, infos = env.step(actions)
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\vec_env\base_vec_env.py", line 163, in step
return self.step_wait()
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\vec_env\vec_transpose.py", line 95, in step_wait
observations, rewards, dones, infos = self.venv.step_wait()
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\vec_env\subproc_vec_env.py", line 121, in step_wait
results = [remote.recv() for remote in self.remotes]
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\site-packages\stable_baselines3\common\vec_env\subproc_vec_env.py", line 121, in
results = [remote.recv() for remote in self.remotes]
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\multiprocessing\connection.py", line 250, in recv
buf = self._recv_bytes()
File "C:\Users\KEN2020.conda\envs\SnakeAI\lib\multiprocessing\connection.py", line 321, in _recv_bytes
raise EOFError
EOFError

@Han-duoduo
Copy link

+1 求解决

@Chapoii
Copy link

Chapoii commented May 20, 2023

image
播放声音前面加一个判断是否是silent_mode,训练的时候不需要播放声音

@BeiYining
Copy link

具体位置是:snake_game.py -- line 95 左右的位置

@shironghe
Copy link

sound_game_over 不会影响我们训练模型,可以注释掉self.sound_game_over.play()再添加pass,等玩test时再打开
image

@aijunzhao
Copy link
Author

sound_game_over 不会影响我们训练模型,可以注释掉self.sound_game_over.play()再添加pass,等玩test时再打开 image

可以了,感谢兄弟

@1816705
Copy link

1816705 commented May 26, 2023

image
为什么我卡在这

@shironghe
Copy link

shironghe commented May 26, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 30, 2023

mlp模型训练时没有在命令行给提示,你可以在windows下ctrl+shift+Esc查看CPU、GPU的使用情况,在ubuntu下使用htop命令查看那CPU的使用情况,在watch -n 1 nvidia-smi下查看GPU的使用情况,程序占用率高就表明在训练了,并不是卡住了 在 2023-05-26 13:09:31,"wave" @.> 写道: 为什么我卡在这 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

请问怎么让训练过程可视化呢

@shironghe
Copy link

shironghe commented May 31, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 31, 2023

训练后注意看logs下会生成新的文件夹,里面的文件可以用TensorBoard查看进行可视化 在 2023-05-31 02:01:11,"zjhcwjb" @.> 写道: mlp模型训练时没有在命令行给提示,你可以在windows下ctrl+shift+Esc查看CPU、GPU的使用情况,在ubuntu下使用htop命令查看那CPU的使用情况,在watch -n 1 nvidia-smi下查看GPU的使用情况,程序占用率高就表明在训练了,并不是卡住了 在 2023-05-26 13:09:31,"wave" @.> 写道: 为什么我卡在这 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 请问怎么让训练过程可视化呢 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

感谢回复 但是如果想看到ai每一局游戏的画面应该怎么做啊

@shironghe
Copy link

shironghe commented May 31, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 31, 2023

你可以借鉴test_mlp的代码,借助env.render()函数对游戏画面进行渲染 在 2023-05-31 13:33:44,"zjhcwjb" @.> 写道: 训练后注意看logs下会生成新的文件夹,里面的文件可以用TensorBoard查看进行可视化 在 2023-05-31 02:01:11,"zjhcwjb" @.> 写道: mlp模型训练时没有在命令行给提示,你可以在windows下ctrl+shift+Esc查看CPU、GPU的使用情况,在ubuntu下使用htop命令查看那CPU的使用情况,在watch -n 1 nvidia-smi下查看GPU的使用情况,程序占用率高就表明在训练了,并不是卡住了 在 2023-05-26 13:09:31,"wave" @.> 写道: 为什么我卡在这 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 请问怎么让训练过程可视化呢 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 感谢回复 但是如果想看到ai每一局游戏的画面应该怎么做啊 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>
已经加上渲染画面的代码了 运行起来gpu占用也很高应该是在训练 但还是看不到画面啊

@shironghe
Copy link

shironghe commented May 31, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 31, 2023

可以的话,共享你的代码 在 2023-05-31 14:34:13,"zjhcwjb" @.> 写道: 你可以借鉴test_mlp的代码,借助env.render()函数对游戏画面进行渲染 在 2023-05-31 13:33:44,"zjhcwjb" @.> 写道: 训练后注意看logs下会生成新的文件夹,里面的文件可以用TensorBoard查看进行可视化 在 2023-05-31 02:01:11,"zjhcwjb" @.> 写道: mlp模型训练时没有在命令行给提示,你可以在windows下ctrl+shift+Esc查看CPU、GPU的使用情况,在ubuntu下使用htop命令查看那CPU的使用情况,在watch -n 1 nvidia-smi下查看GPU的使用情况,程序占用率高就表明在训练了,并不是卡住了 在 2023-05-26 13:09:31,"wave" @.> 写道: 为什么我卡在这 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 请问怎么让训练过程可视化呢 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 感谢回复 但是如果想看到ai每一局游戏的画面应该怎么做啊 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.> 已经加上渲染画面的代码了 运行起来gpu占用也很高应该是在训练 但还是看不到画面啊 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

import os
import sys
import random
import time

from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env import SubprocVecEnv
from stable_baselines3.common.callbacks import CheckpointCallback
from sb3_contrib import MaskablePPO
from sb3_contrib.common.wrappers import ActionMasker

from snake_game_custom_wrapper_mlp import SnakeEnv

NUM_ENV = 32
LOG_DIR = "logs"
os.makedirs(LOG_DIR, exist_ok=True)

Linear scheduler

def linear_schedule(initial_value, final_value=0.0):

if isinstance(initial_value, str):
    initial_value = float(initial_value)
    final_value = float(final_value)
    assert (initial_value > 0.0)

def scheduler(progress):
    return final_value + progress * (initial_value - final_value)

return scheduler

def make_env(seed=0):
def _init():
env = SnakeEnv(seed=seed)
env = ActionMasker(env, SnakeEnv.get_action_mask)
env = Monitor(env)
env.seed(seed)
return env
return _init

def main():

# Generate a list of random seeds for each environment.
seed_set = set()
while len(seed_set) < NUM_ENV:
    seed_set.add(random.randint(0, 1e9))

# Create the Snake environment.
env = SubprocVecEnv([make_env(seed=s) for s in seed_set])

lr_schedule = linear_schedule(2.5e-4, 2.5e-6)
clip_range_schedule = linear_schedule(0.15, 0.025)

# # Instantiate a PPO agent
model = MaskablePPO(
    "MlpPolicy",
    env,
    device="cuda",
    verbose=1,
    n_steps=2048,
    batch_size=512,
    n_epochs=4,
    gamma=0.94,
    learning_rate=lr_schedule,
    clip_range=clip_range_schedule,
    tensorboard_log=LOG_DIR
)

# Set the save directory
save_dir = "trained_models_mlp"
os.makedirs(save_dir, exist_ok=True)

checkpoint_interval = 15625 # checkpoint_interval * num_envs = total_steps_per_checkpoint
checkpoint_callback = CheckpointCallback(save_freq=checkpoint_interval, save_path=save_dir, name_prefix="ppo_snake")

# Writing the training logs from stdout to a file
original_stdout = sys.stdout
log_file_path = os.path.join(save_dir, "training_log.txt")
with open(log_file_path, 'w') as log_file:
    sys.stdout = log_file

    model.learn(
        total_timesteps=int(100000000),
        callback=[checkpoint_callback]
    )
    env.close()

# Restore stdout
sys.stdout = original_stdout

# Save the final model
model.save(os.path.join(save_dir, "ppo_snake_final.zip"))

demo_env = make_env()()

with open(log_file_path, 'w') as log_file:
    sys.stdout = log_file

    for i in range(100):
        model.learn(
            total_timesteps=int(1000000),
            callback=[checkpoint_callback]
        )

        obs = demo_env.reset()
        demo_env.render()
        time.sleep(0.5)
        done = False
        while not done:
            action, _ = model.predict(obs)
            obs, _, done, _ = demo_env.step(action)
            demo_env.render()
            time.sleep(0.5)

if name == "main":
main()
嗯嗯 就是在train_mlp的基础上加上了渲染的部分 完全没有报错信息 但是画面完全不出来

@shironghe
Copy link

shironghe commented May 31, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 31, 2023 via email

@shironghe
Copy link

shironghe commented May 31, 2023 via email

@zjhcwjb
Copy link

zjhcwjb commented May 31, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants