Code Monkey home page Code Monkey logo

handwritten-multi-digit-number-recognition's Introduction

Handwritten Multi-Digit Number Recognition

Streamlit App Code style: black pre-commit License

Screenshot

Introduction

In this project, I built a model to perform handwritten digit string recognition using synthetic data generated by concatenating digits from the MNIST dataset. Different overlapping rates and paddings were used to increase the generalizability of the model. Data augmentation (e.g., random scaling, rotation, shear) was also used during training to increase the divesity of the samples.

Here are some examples of the synthetic data:

Synthetic Data

I replicated the CRNN model proposed in Shi, Bai & Yao (2015), which uses a CNN as encoder and a bidirecitonal LSTM as decoder, along with a CTC loss. I was able to achieve a character error rate of 0.0114 and an accuracy of 0.9865 in the test set (see the best run here).

Model Architecture

Model architecture, from Shi, Bai & Yao (2015)

For deployment, I built a Docker image, pushed it to Amazon Elastic Container Registry (ECR), created a AWS Lambda function from the container image, and finally, put a REST API gateway in front of the Lambda function.

Setup

Create a virtual environment.

make venv

Install dependencies.

make install-dev

Usage

Run an experiement

Example command to run an experiment:

python scripts/run_experiment.py \
    trainer.gpus=1 trainer.max_epochs=100 trainer.auto_lr_find=True \
    data.batch_size=128 data.num_workers=2 data.pin_memory=True \
    data.num_train=20000 data.num_val=2000 data.num_test=2000

Running scripts/run_experiment.py will download the MNIST dataset, generate a synthetic dataset and train a CRNN model. The best model checkpoint will be uploaded to Weights & Biases (W&B) automatically (you will be asked to register or login to W&B before the training starts).

Configurations can be modified in config.yaml or in command line. See Hydra's documentation to learn more.

Example command to download a trained model checkpoint from W&B

python scripts/download_checkpoint.py RUN_PATH

Replace RUN_PATH with the path of your run. The run path can be found in the W&B dashboard and should look something like kingyiusuen/handwritten-multi-digit-number-recognition/3r47q0f5

Docker Image for AWS Lambda

Build a Docker image for AWS Lambda:

docker build -t handwritten-multi-digit-number-recognition -f aws_lambda/Dockerfile .

Run the Docker image:

docker run -p 9000:8080 -it --rm handwritten-multi-digit-number-recognition

Example command to test the API locally:

curl -X POST "http://localhost:9000/2015-03-31/functions/function/invocations" -H 'Content-Type: application/json' -d '{ "image": "data:image/png;base64,'$(base64 -i images/test_image.png)'" }'

Acknowledgements

Full Stack Deep Learning Spring 2021 Labs

handwritten-multi-digit-number-recognition's People

Contributors

kingyiusuen avatar

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.