Giter VIP home page Giter VIP logo

nolearn_utils's Introduction

nolearn-utils

Build Status

Iterators and handlers for nolearn.lasagne to allow efficient real-time image augmentation and training progress monitoring

Real-time image augmentation

  • ShuffleBatchIteratorMixin to shuffle training samples
  • ReadImageBatchIteratorMixin to transform image file path into image as color or as gray, and with specified image size
  • RandomFlipBatchIteratorMixin to randomly (uniform) flip the image horizontally or verticaly
  • AffineTransformBatchIteratorMixin to apply affine transformation (scale, rotate, translate) to randomly selected images from the given transformation options - BufferedBatchIteratorMixin to perform transformation in another thread automatically and put the result in a buffer (default size = 5)
  • LCNBatchIteratorMixin to perform local contrast normalization to images
  • MeanSubtractBatchIteratorMixin to subtract samples from the pre-calculated mean

Example of using iterators as below:

train_iterator_mixins = [
    ShuffleBatchIteratorMixin,
    ReadImageBatchIteratorMixin,
    RandomFlipBatchIteratorMixin,
    AffineTransformBatchIteratorMixin,
    BufferedBatchIteratorMixin,
]
TrainIterator = make_iterator('TrainIterator', train_iterator_mixins)

train_iterator_kwargs = {
    'buffer_size': 5,
    'batch_size': batch_size,
    'read_image_size': (image_size, image_size),
    'read_image_as_gray': False,
    'read_image_prefix_path': './data/train/',
    'flip_horizontal_p': 0.5,
    'flip_vertical_p': 0,
    'affine_p': 0.5,
    'affine_scale_choices': np.linspace(0.75, 1.25, 5),
    'affine_translation_choices': np.arange(-3, 4, 1),
    'affine_rotation_choices': np.arange(-45, 50, 5)
}
train_iterator = TrainIterator(**train_iterator_kwargs)

The BaseBatchIterator is also modified from nolearn.lasagne to provide a progress bar for training process for each iteration

Handlers

  • EarlyStopping stops training when loss stop improving
  • StepDecay to gradually reduce a parameter (e.g. learning rate) over time
  • SaveTrainingHistory to save training history (e.g. training loss)
  • PlotTrainingHistory to plot out training loss and validation accuracy over time after each iteration with matplotlib

Examples

Example code requires scikit-learn

MNIST

example/mnist/train.py should produce a model of about 99.5% accuracy in less than 50 epoch.

MNIST data can be downloaded from Kaggle.

CIFAR10

CIFAR10 images can be downloaded from Kaggle. Place the downloaded data as follows:

examples/cifar10
├── data
│   ├── train
│   |   ├── 1.png
│   |   ├── 2.png
│   |   ├── 3.png
│   |   ├── ...
│   └── trainLabels.csv
└── train.py

example/cifat10/train.py should produce a model at about 85% accuracy at 100 epoch. Images are read from disk and augmented at training time (from another thread)

TODO

  • Embarrassingly parallelize transform

License

MIT & BSD

nolearn_utils's People

Contributors

cancan101 avatar dnouri avatar felixlaumon avatar

Stargazers

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

Watchers

 avatar  avatar

nolearn_utils's Issues

Improve concurrency for real-time augmentation

To prevent starving the GPU when using heavy real-time augmentation, BufferedBatchIteratorMixin is available to use another process to build up a queue of augmented training samples. Most of the time, this works well enough.

However there are cases which the augmentation takes longer than the GPU forward-backward pass per batch. In the other words the CPU cannot keep up with the GPU.

The solution is to utilize multiple CPU cores to perform real-time augmentations. However this doesn't seems to be trivial for this to work efficiently because we need to pickle the iterator which has a __iter__

An implementation might be to do something similar to https://gist.github.com/ebenolson/072712792c46aa192797 and handle IPC ourselves with /run/shm/. Also https://pypi.python.org/pypi/SharedArray might help as well.

Ultimately the idea is follow a producer-consumer pattern. Workers will generate and send augmented training sample to a master process. The master process will assemble the samples into batches and feed them into the GPU.

There will be 2 batch size parameters: one for GPU and one for the workers.

@dnouri will appreciate if you can offer some advice here

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.