Giter VIP home page Giter VIP logo

street-fighter-ai's Introduction

SFighterAI

简体中文 | English | Español

This project is an AI agent trained using deep reinforcement learning to beat the final boss in the game "Street Fighter II: Special Champion Edition". The AI agent makes decisions based solely on the game screen's RGB pixel values. In the provided save state, the agent achieves a 100% win rate in the first round of the final level (overfitting occurs, see the Running Tests section for discussion).

File Structure

├───data
├───main
│   ├───logs
│   ├───trained_models
│   └───scripts
├───utils
│   └───scripts

The game configuration files are stored in the data/ folder, and the main project code is in the main/ folder. Within main/, the logs/ folder contains terminal/console outputs and data curves recording the training process (viewable with Tensorboard), while the trained_models/ folder contains model weights from different stages. These weights can be used for running tests in test.py to observe the performance of the AI agent's learned strategies at different training stages.

Running Guide

This project is based on the Python programming language and primarily utilizes standard libraries like OpenAI Gym Retro and Stable-Baselines3. The Python version used is 3.8.10, and it is recommended to use Anaconda to configure the Python environment. The following setup process has been tested on Windows 11. Below are console/terminal/shell commands.

Environment Setup

# Create a conda environment named StreetFighterAI with Python version 3.8.10
conda create -n StreetFighterAI python=3.8.10
conda activate StreetFighterAI

# Install Python libraries
cd [parent_directory_of_project]/street-fighter-ai/main
pip install -r requirements.txt

# Run script to locate gym-retro game folder
cd ..
python .\utils\print_game_lib_folder.py

After the console outputs the folder path, copy it to the file explorer and navigate to the corresponding path. This folder contains the game data files for "Street Fighter II: Special Champion Edition" within gym-retro, including the game ROM file and data configuration files. Copy the Champion.Level12.RyuVsBison.state, data.json, metadata.json, and scenario.json files from the data/ folder of this project into the game data folder, replacing the original files (administrator privileges may be required). The .state file is a save state for the game's highest difficulty level, while the three .json files are gym-retro configuration files storing game information memory addresses (this project only uses [agent_hp] and [enemy_hp] for reading character health values in real-time).

To run the program, you will also need the game ROM file for "Street Fighter II: Special Champion Edition", which is not provided by gym-retro and must be obtained legally through other means. You can refer to this link.

Once you have legally obtained the game ROM file, copy it to the aforementioned gym-retro game data folder and rename it to rom.md. At this point, the environment setup is complete.

Note 1: If you want to manually capture save states and find memory variables in the game, you can use the gym-retro integration ui. Copy data/Gym Retro Integration.exe to the parent menu (two levels up, retro/ folder) of the aforementioned gym-retro game data folder.

Note 2: If you want to record videos of the AI agent's gameplay, you will need to install ffmpeg.

conda install ffmpeg

Running Tests

Once the environment is set up, you can run test.py in the main/ folder to test and experience the AI agent's performance at different stages of training.

cd [parent_directory_of_project]/street-fighter-ai/main
python test.py

Model weight files are stored in the main/trained_models/ folder. The default model used in test.py is ppo_ryu_2500000_steps_updated.zip, which has good generalization and is capable of beating the final level of Street Fighter II: Special Champion Edition. If you want to see the performance of other models, you can change the model_path variable in test.py to the path of another model file. The observed performance of the models at various training stages is as follows:

  • ppo_ryu_2000000_steps_updated: Just beginning to overfit state, generalizable but not quite capable.
  • ppo_ryu_2500000_steps_updated: Approaching the final overfitted state, cannot dominate first round but partially generalizable. High chance of beating the final stage.
  • ppo_ryu_3000000_steps_updated: Near the final overfitted state, almost dominate first round but barely generalizable.
  • ppo_ryu_7000000_steps_updated: Overfitted, dominates first round but not generalizable.

Training the Model

If you want to train your own model, you can run train.py in the main/ folder.

cd [parent_directory_of_project]/street-fighter-ai/main
python train.py

Viewing Training Curves

The project includes Tensorboard graphs of the training process. You can use Tensorboard to view detailed data. It is recommended to use the integrated Tensorboard plugin in VSCode to view the data directly. The traditional viewing method is listed below:

cd [parent_directory_of_project]/street-fighter-ai/main
tensorboard --logdir=logs/

Open the default Tensorboard service address http://localhost:6006/ in your browser to view interactive graphs of the training process.

Acknowledgements

This project uses open-source libraries such as OpenAI Gym Retro, Stable-Baselines3. The contributions of all the developers to the open-source community are appreciated!

Two papers that had a significant impact on this project:

[1] DIAMBRA Arena A New Reinforcement Learning Platform for Research and Experimentation The valuable summary of the experience in setting hyperparameters for deep reinforcement learning models in fighting games in this paper was of great help to the training process of this project.

[2] Mitigating Cowardice for Reinforcement Learning The "penalty decay" mechanism proposed in this paper effectively solved the "cowardice" problem (always avoiding opponents and not daring to even try attacking moves).

street-fighter-ai's People

Contributors

arch-fan avatar kianmeng avatar linyilyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

street-fighter-ai's Issues

看了一下视频和代码简单说以下原理和感觉还有疑问

刚看到视频时感觉我CAO这么牛B,之后看了代码竟然这么小!
打开一看。。这AI原来是乱按的啊,根本没有抓画面,所谓的训练只是靠读取双方血条和胜负的内存地址来判断的。
也就是说AI完全靠乱按试出了一个套路可以在这特定存档中打死拜森/贝加,毫无通用性。
不过CHATGPT能写出这段代码感觉已经非常强了。。
另外 有人能告诉我data/data.json里那些内存地址哪来的?
PS 另外这个感觉用在训练马里奥这种没什么随机性的游戏更适合。。

按照README.MD文件描述步骤进行了,可是还是卡在了运行的最后一步,找了很多方法,依然不行。

C:\Users\my.conda\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\save_util.py:166: UserWarning: Could not deserialize object observation_space. Consider using custom_objects argument to replace this object.
Exception: __randomstate_ctor() takes from 0 to 1 positional arguments but 2 were given
warnings.warn(
Traceback (most recent call last):
File "test.py", line 53, in
model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env)
File "C:\Users\my.conda\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\base_class.py", line 678, in load
raise KeyError("The observation_space and action_space were not given, can't verify new environments")
KeyError: "The observation_space and action_space were not given, can't verify new environments"
以上是报错信息,有大佬可以解答问题在哪里吗?谢谢!

Hardware requirements

林哥,

Will the 3070's 8GB of RAM meet the minimum training requirements?

I would like to know some hardware requirements when running parallel training in your video.

在运行项目之后出现了一些问题

Wrapping the env with a Monitor` wrapper
Wrapping the env in a DummyVecEnv.
Wrapping the env in a VecTransposeImage.

Fighting Begins!

Traceback (most recent call last):
File "test.py", line 79, in
obs, reward, done, info = env.step(action)
File "D:\Last\street-fighter-ai\main\street_fighter_custom_wrapper.py", line 83, in step
curr_player_health = info['agent_hp']
KeyError: 'agent_hp'`

昨天使用cmd运行成功了 今天想在vscode启动 在输入命令python test.py后报出这个错误

林哥,运行出错TypeError: filename must be a str or bytes object, or a file

(.venv) F:\street-fighter-ai\main>python test.py
Traceback (most recent call last):
File "test.py", line 49, in
env = make_env(game, state="Champion.Level12.RyuVsBison")()
File "test.py", line 38, in init
env = retro.make(
File "F:\street-fighter-ai\main.venv\lib\site-packages\retro_init
.py", line 55, in make
return RetroEnv(game, state, inttype=inttype, **kwargs)
File "F:\street-fighter-ai\main.venv\lib\site-packages\retro\retro_env.py", line 60, in init
self.load_state(self.statename, inttype)
File "F:\street-fighter-ai\main.venv\lib\site-packages\retro\retro_env.py", line 271, in load_state
with gzip.open(retro.data.get_file_path(self.gamename, statename, inttype), 'rb') as fh:
File "C:\Users\Datou\AppData\Local\Programs\Python\Python38\lib\gzip.py", line 62, in open
raise TypeError("filename must be a str or bytes object, or a file")
TypeError: filename must be a str or bytes object, or a file

ROM文件已经放好了
image

ROM Removal

Good work, but I'm worried you linking to the ROM will have negative ramifications for you. Be careful!

使用模型打不贏第一關

state file: Champion.Level1.RyuVsGuile.state
model file: ppo_ryu_7000000_steps_updated.zip

哪邊有問題 or 可以優化?

最后林哥是如何搞定生成最后一关存档文件的?

看了你的GPT对话,GTP一直都没有给出有效方法。林哥最后说他自己搞定了。请问林哥是如何搞定的?
我自己修改了一下代码,让可以2P模式并支持键盘。我希望自己可以操作角色对战训练好的AI。可惜现有的存档都是1P的。我尝试按2P的“START”按钮,也没办法加入2P对战(可能没放币)。
所以应该只有两张方法:

  1. 用其他模拟器玩游戏,并进入2P对战,然后存档。只是根据GPT对话,没有找到合适的其他模拟器。
  2. 用键盘自己玩GymRetro,并进入2P对战,然后存档。我修改的代码可以用键盘玩,但不知如何投币。

after running train.py,failed to run test.py

below is the error message.

C:\Users\xqjco\anaconda3\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\save_util.py:166: UserWarning: Could not deserialize object observation_space. Consider usingcustom_objectsargument to replace this object. Exception: __randomstate_ctor() takes from 0 to 1 positional arguments but 2 were given warnings.warn( Traceback (most recent call last): File "test.py", line 53, in <module> model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env) File "C:\Users\xqjco\anaconda3\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\base_class.py", line 678, in load raise KeyError("The observation_space and action_space were not given, can't verify new environments") KeyError: "The observation_space and action_space were not given, can't verify new environments"

Input two-player actions

Hi,

Thanks for open-sourcing this great project!

I'm wondering if it's possible to support controlling both side of players in the game, the env.step() will take two actions per step, one for each player. Any suggestion on how to support that?

希望林哥能出点免费课程或会员课程

这个AI项目真好玩,但对小白来说,运行起来基本就到头了,想要更深入了解这个项目甚至动手改这个项目,太难了。

比如我一开始尝试在test.py里面增加游戏声音,失败了,只能断断续续出点声,都听不出来和游戏有没有关系。

我的显卡是4090,用林哥的训练参数跑不满,我跟chatgpt聊怎么可以最大化训练效率,它告诉我可以调几个参数,调完之后cpu满了,显卡还是比较空闲,再问它,它说有些参数占用cpu,有些参数占用显卡,你自己多试试。对这些参数缺乏基本的认知,瞎蒙的效率就很低。

怎么理解TensorBoard里的曲线和数值,它们理论上可以指导训练参数的设置对吧,看了几个TensorBoard教程,和AI对战项目对不上号。

我还想调整训练的设置,改成每次训练输一场结束或赢两场结束,而不是打完一场就结束,没找到代码位置。

训练之后的结果在哪,如何变成林哥那种ppo_ryu_2500000_steps_updated.zip在test.py里调用,不知道。

代码量不算大,但代码背后的所涉及的知识太多了 T_T

image
image

已成功运行项目

就是对战速度有点慢呢 我没有台式机 在笔记本上搭的环境 AMD Ryzen 7 4800U 1.8GHz
问题怎么一直重复 没有看到通关结局呢 是每次开局都会加载存档么

請益: 關於增強式學習的損失函數

林哥您好,我想請問在此專案使用的增強式學習,您的損失函數是定義在哪個py檔案呢?
我在street_fighter_custom_wrapper.py中只看到遊戲輸贏時更新custom_reward變數,我想知道您對AI的獎懲演算法的細節。
譬如: 如果成功使出過肩摔這種高難度技巧重創對手,會增加x分...等獎懲策略,如果能有程式碼細節就更好了~

我目前是專攻電腦視覺與深度學習的研究生,很喜歡您的視頻,希望林哥有機會解答我的疑惑~

正在学习中,想问一下哪里能得到ROM?

首先感谢LinYi,提供这样好的一下学习入门的示例。真的是从各个方面,都很平衡:现实,有限,有趣,而且对硬件要求有限,特别是非常有价值,能够体现RL的作用。
以前我是写过编译器的前端,对这些很感兴趣,但没有机会接解AI。所以,这个示例,我打算一定详细学习清楚。发现自己的兴趣还是在AI相关的方向。

现在已经可以运行的单步调试了。环境算是搭好了。

但是我的问题是,想了解下,大家是如何得到、和运行的 Street Fighter II Special Champion Edition 的ROM和运行环境。

如果有详细的说明,请有心人,帮我找一下,如果不涉及到法律相关的,请回复到这个issue下面。如果 有顾虑 , 请发email to me : [email protected]
[email protected]
非常感谢。

mac 下环境配置问题。 比如一定要求py 3.8.10么。

➜  ai /opt/homebrew/anaconda3/bin/conda create -n StreetFighterAI python=3.8.10

Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - python=3.8.10

Current channels:

  - https://repo.anaconda.com/pkgs/main/osx-arm64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/osx-arm64
  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

运行test.py 报错:Error: Unexpected observation shape

Wrapping the env with a `Monitor` wrapper
Wrapping the env in a DummyVecEnv.
Wrapping the env in a VecTransposeImage.
Traceback (most recent call last):
  File "D:/Projects/street-fighter-ai/main/test.py", line 78, in <module>
    action, _states = model.predict(obs)
  File "D:\Anaconda3\envs\street\lib\site-packages\stable_baselines3\common\base_class.py", line 535, in predict
    return self.policy.predict(observation, state, episode_start, deterministic)
  File "D:\Anaconda3\envs\street\lib\site-packages\stable_baselines3\common\policies.py", line 340, in predict
    observation, vectorized_env = self.obs_to_tensor(observation)
  File "D:\Anaconda3\envs\street\lib\site-packages\stable_baselines3\common\policies.py", line 255, in obs_to_tensor
    vectorized_env = is_vectorized_observation(observation, self.observation_space)
  File "D:\Anaconda3\envs\street\lib\site-packages\stable_baselines3\common\utils.py", line 380, in is_vectorized_observation
    return is_vec_obs_func(observation, observation_space)
  File "D:\Anaconda3\envs\street\lib\site-packages\stable_baselines3\common\utils.py", line 247, in is_vectorized_box_observation
    raise ValueError(
ValueError: Error: Unexpected observation shape (112, 160, 3) for Box environment, please use (3, 100, 128) or (n_env, 3, 100, 128) for the observation shape.

Fighting Begins!

运行环境:
Python 3.8.10、Windows 10 家庭中文版

Package                 Version
----------------------- ---------
absl-py                 1.4.0
cachetools              5.3.0
certifi                 2022.12.7
charset-normalizer      3.1.0
cloudpickle             2.2.1
contourpy               1.0.7
cycler                  0.11.0
filelock                3.12.0
fonttools               4.39.3
google-auth             2.17.3
google-auth-oauthlib    1.0.0
grpcio                  1.54.0
gym                     0.21.0
gym-retro               0.8.0
idna                    3.4
importlib-metadata      4.13.0
importlib-resources     5.12.0
Jinja2                  3.1.2
kiwisolver              1.4.4
Markdown                3.4.3
MarkupSafe              2.1.2
matplotlib              3.7.1
mpmath                  1.3.0
networkx                3.1
numpy                   1.24.2
oauthlib                3.2.2
packaging               23.1
pandas                  2.0.0
Pillow                  9.5.0
pip                     23.0.1
protobuf                4.22.3
pyasn1                  0.5.0
pyasn1-modules          0.3.0
pyglet                  1.5.27
pyparsing               3.0.9
python-dateutil         2.8.2
pytz                    2023.3
requests                2.28.2
requests-oauthlib       1.3.1
rsa                     4.9
setuptools              66.0.0
six                     1.16.0
stable-baselines3       1.7.0
sympy                   1.11.1
tensorboard             2.12.1
tensorboard-data-server 0.7.0
tensorboard-plugin-wit  1.8.1
torch                   2.0.0
typing_extensions       4.5.0
tzdata                  2023.3
urllib3                 1.26.15
Werkzeug                2.2.3
wheel                   0.38.4
zipp                    3.15.0

太牛了up ,让我看到了ai的恐怖

赞你一个,我也对靠ai学习有兴趣,可是不会,今天你的街霸视频我看了三遍,水平低没办法,我也想依靠ai来个爆炸式的效率学习

关于小白想学习GYM的请求

我想玩CSGO等射击游戏,我看林哥有之前的视频,可惜小白不会设置,有大佬写一个萌新都能设置的教程嘛

【无法运行项目】setuptools无论怎么调整版本都无法正确支持安装gym、stable-baseline3

使用的gym、stable-baseline3库由于setuptools库的问题一直无法安装,即便降低setuptools的版本也没用
Collecting stable-baselines3==1.7.0
Using cached stable_baselines3-1.7.0-py3-none-any.whl (171 kB)
Collecting gym==0.21 (from stable-baselines3==1.7.0)
Using cached gym-0.21.0.tar.gz (1.5 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error in gym setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

参考一下gym、setuptools的issue解决,无果:
DLR-RM/stable-baselines3#1324
openai/gym#3176

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.