tree: a56b796dd47ae95a67db4b1fea791060480670f0
  1. src/
  2. test/
  3. .gitignore
  4. captcha_example.png
  5. gen_captcha.py
  6. get_data.sh
  7. project.clj
  8. README.md
contrib/clojure-package/examples/captcha/README.md

Captcha

This is the clojure version of captcha recognition example by xlvector and mirrors the R captcha example. It can be used as an example of multi-label training. For the following captcha example, we consider it as an image with 4 labels and train a CNN over the data set.

captcha example

Installation

Before you run this example, make sure that you have the clojure package installed. In the main clojure package directory, do lein install. Then you can run lein install in this directory.

Usage

Training

First the OCR model needs to be trained based on labeled data. The training can be started using the following:

$ lein train [:cpu|:gpu] [num-devices]

This downloads the training/evaluation data using the get_data.sh script before starting training.

It is possible that you will encounter some out-of-memory issues while training using :gpu on Ubuntu linux (18.04). However, the command lein train (training on one CPU) may resolve the issue.

The training runs for 10 iterations by default and saves the model with the prefix ocr-. The model achieved an exact match accuracy of ~0.954 and ~0.628 on training and validation data respectively.

Inference

Once the model has been saved, it can be used for prediction. This can be done by running:

$ lein infer
INFO  MXNetJVM: Try loading mxnet-scala from native path.
INFO  MXNetJVM: Try loading mxnet-scala-linux-x86_64-gpu from native path.
INFO  MXNetJVM: Try loading mxnet-scala-linux-x86_64-cpu from native path.
WARN  MXNetJVM: MXNet Scala native library not found in path. Copying native library from the archive. Consider installing the library somewhere in the path (for Windows: PATH, for Linux: LD_LIBRARY_PATH), or specifying by Java cmd option -Djava.library.path=[lib path].
WARN  org.apache.mxnet.DataDesc: Found Undefined Layout, will use default index 0 for batch axis
INFO  org.apache.mxnet.infer.Predictor: Latency increased due to batchSize mismatch 8 vs 1
WARN  org.apache.mxnet.DataDesc: Found Undefined Layout, will use default index 0 for batch axis
WARN  org.apache.mxnet.DataDesc: Found Undefined Layout, will use default index 0 for batch axis
CAPTCHA output: 6643
INFO  org.apache.mxnet.util.NativeLibraryLoader: Deleting /tmp/mxnet6045308279291774865/libmxnet.so
INFO  org.apache.mxnet.util.NativeLibraryLoader: Deleting /tmp/mxnet6045308279291774865/mxnet-scala
INFO  org.apache.mxnet.util.NativeLibraryLoader: Deleting /tmp/mxnet6045308279291774865

The model runs on captcha_example.png by default.

It can be run on other generated captcha images as well. The script gen_captcha.py generates random captcha images for length 4. Before running the python script, you will need to install the captcha library using pip3 install --user captcha. The captcha images are generated in the images/ folder and we can run the prediction using lein infer images/7534.png.