Build and train a Neural Network with Tensorflow to recognize handwritten digits with ~92% accuracy.
Adapted from this tutorial by Ellie Birbeck, updated for later Tensorflow versions and with documentation inlined as comments.
Note that this uses Tensorflow 1.14, not 2.
The MacOS built-in Python is 2.7.X, which is old and soon to be deprecated. Like Ruby, you can use a version manager tool to manage and use multiple versions instead of the default one.
- Install pyenv, a Python version manager (like RVM):
brew install pyenv
- Install the latest stable of Python into pyenv:
pyenv install 3.7.4
- Set the global default Python version:
pyenv global 3.7.4
- To load pyenv into your bash shell sessions:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
- And load it into your current session:
source ~/.bash_profile
- Check that pyenv hijacks Python in the shell:
which python
- Check the active version is what you installed:
python -V
- And check that you have pip (the Python package manager):
pip -V
cd to where you keep your projects, then:
mkdir tensorflow_number_recognition
cd tensorflow_number_recognition
git init
Using venv, the built-in module for virtual environments in Python.
python -m venv tensorflow_number_recognition
touch .gitignore
echo 'tensorflow_number_recognition' > .gitignore
source tensorflow_number_recognition/bin/activate
touch dependencies.txt
Add to the file:
image==1.5.20
numpy==1.14.5
tensorflow==1.14.0
And install the dependencies:
pip install -r dependencies.txt
touch main.py
echo 'MNIST_data' > .gitignore
See main.py file in this repo.
python main.py
curl -O https://raw.githubusercontent.com/do-community/tensorflow-digit-recognition/master/test_img.png
Then uncomment the last section of main.py and run it again.