AI Bot to play rock-paper-scissors against a human
https://towardsdatascience.com/rock-paper-scissors-ai-bot-janken-ee2d3089b778
Rock Paper Scissors was first introduced by China during the 17th century, it was called Janken.
- Code Engine: Image Classifier with 3 categories (rock, paper, scissors)
- SqueezeNet will be our pre-trained CNN
- We'll re-train its output layers for our 3 new categories
- Collecting data - gathering images from your own web camera
- Creating a Neural Network & Training the model
- Test the model
- Play the game!
-
git clone / download this project Create a virtual environment (recommended for python, I'm using python3) Your pyCharm or chosen IDE might offer (venv) on new a project. You can run source venv/bin/activate if it comes pre-setup, if not, here are the steps.
-
run in terminal "python3 -m venv ." (there's a space after the last 'v'). This will create a bin folder in root with an activate script for a virtual env.
-
If you get error like "Unable to symlink" delete the bin folder that was created in root and try again. Didn't work? Try checking your path from python "which python3" or $PATH in bash_profile "echo $PATH" or "open .bash_profile". For example, here is my path export PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:$PATH"
-
run "source bin/activate" from folder root. Should prefix your folder name in brackets.
-
"python -V" should return Python 3 or >
-
Install pip3 "brew install python3" (pip3 is auto installed with this command)
-
There is a dependency_list.txt, run "pip3 install -r dependency_list.txt"
- Add a dir folder in root called "collected_images"
- In this dir add 4 more folders like so;
- script to run first is "collect_image_data.py"
- Run command example: "python collect_image_data.py rock 200"
- Press 's' to start/pause and q to quit.
- Images stored in 'collected_images' dir with label set as 1st argument
- You can skip all of step 2 if you just want to test a model, I've provided the necessary file.
- The training part is all in "train_model.py" and the squeeze-net model weights .h5 files.
- Script to run first is "python train_model.py"
- Training time for a batch of (200 images across 4 categories) should be less than 10 minutes.
- When finished will produce a model file called "rps-model-1.h5" in project root.
- You will also see something like
Epoch 10/10 1200/1200 [==============================] - 74s 62ms/step - loss: 4.4444e-05 - acc: 1.0000
Two important figures to look at is 'loss' and 'acc'. This is showing us the loss is an extremely low value and the accuracy is 100% For now just note that both of these ranges are extremely desired and if you get something similar, this will be enough to test out.
-
You can use your model or the example one I've provided, "example-rps-model-1.h5".
-
This file contains all the training parameter, weights and biases which the NN learnt.
-
Change model name if you have trained a new model (line with comment "# Change for your model name")
-
I've provided test images in "test_images" dir.
-
Make sure when you run the below command you include the extension .jpg
-
Script to test model is "test_model.py", run for any image e.g. "python test_model.py test_images/paper.jpg".
-
Script will print string prediction at end like "Predicted the image is: paper".
- Script to run is "python play.py".
- Place your gesture in user box and test it out.
- p:Pause/UnPause Image
- u:Add User Win
- j:Add Janken Win
- r:Restart Game
- q:Quit
-
A running server to run the game to allow easier playing and no script running necessary.
-
rock paper scissors lizard spock The rules: "Scissors decapitate Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors."
-
Evaluation (reported testing) during model training.
-
Tensor board setup (Only put in as a dependency for now)
-
Score counter for unlimited gaming but more useful to see if Janken gets 9/10 or 99/100 wins.
-
Feature focus and black and white imaging only for training and for game running.
-
Introduce more hyper parameters and tweak existing ones (to optimise training).