Skip to main content

ktrain is a lightweight wrapper for Keras to help train neural networks

Project description

# ktrain

*ktrain* is a lightweight wrapper for the deep learning library [Keras](https://keras.io/) to help build, train, and deploy neural networks. With only a few lines of code, ktrain allows you to easily and quickly:

- estimate an optimal learning rate for your model given your data using a Learning Rate Finder
- employ learning rate schedules such as the [triangular policy](https://arxiv.org/abs/1506.01186), the [1cycle policy](https://arxiv.org/abs/1803.09820), and [SGDR](https://arxiv.org/abs/1608.03983) to effectively minimize loss and improve generalization
- fast and easy-to-use pre-canned models for both text classification (e.g., NBSVM, fastText, logreg) and image classification (e.g., ResNet, Wide ResNet, Inception)
- methods to help you easily load and preprocess text and image data from a variety of formats
- easily inspect data points that were misclassified to help improve your model
- a simple prediction API for saving and deploying models and data-preprocessing steps to easily make predictions on new raw data

#### Tutorial Notebooks
Please see the following tutorial notebooks for a guide on how to use *ktrain* on your projects:
* Tutorial 1: [Introduction](https://github.com/amaiya/ktrain/blob/master/tutorial-01-introduction.ipynb)
* Tutorial 2: [Tuning Learning Rates](https://github.com/amaiya/ktrain/blob/master/tutorial-02-tuning-learning-rates.ipynb)
* Tutorial 3: [Image Classification](https://github.com/amaiya/ktrain/blob/master/tutorial-03-image-classification.ipynb)
* Tutorial 4: [Text Classification](https://github.com/amaiya/ktrain/blob/master/tutorial-04-text-classification.ipynb)
* Tutorial A1: [Additional tricks](https://github.com/amaiya/ktrain/blob/master/tutorial-A1-additional-tricks.ipynb), which covers topics such as examining misclassifications, inspecting intermediate output of Keras models for debugging, and built-in callbacks.

Tasks such as text classification and image classification can be accomplished easily with
only a few lines of codes.

#### Example: Classifying Images of [Dogs and Cats](https://www.kaggle.com/c/dogs-vs-cats) Using *ktrain*
```
import ktrain
from ktrain import vision as vis
(train_data, val_data, preproc) = vis.images_from_folder(
datadir='data/dogscats',
data_aug = vis.get_data_aug(horizontal_flip=True),
train_test_names=['train', 'valid'],
target_size=(224,224), color_mode='rgb')
model = vis.image_classifier('pretrained_resnet50', train_data, val_data, freeze_layers=80)
learner = ktrain.get_learner(model=model, train_data=train_data, val_data=val_data,
workers=8, use_multiprocessing=False, batch_size=64)
learner.lr_find() # briefly simulate training to find good learning rate
learner.lr_plot() # visually identify best learning rate
# implicitly employs triangular learning rate policy with ReduceLROnPlateau, ModelCheckpoint, and EarlyStopping
learner.autofit(1e-4, checkpoint_folder='/tmp')
```


### Requirements

The following software/libraries should be installed:

- [Python 3.6+](https://www.python.org/) (tested on 3.6.7)
- [Keras](https://keras.io/) (tested on 2.2.4)
- [TensorFlow](https://www.tensorflow.org/) (tested on 1.10.1)
- [scikit-learn](https://scikit-learn.org/stable/) (tested on 0.20.0)
- [matplotlib](https://matplotlib.org/) (tested on 3.0.0)
- [pandas](https://pandas.pydata.org/) (tested on 0.24.2)
<!--- [PIL](https://pillow.readthedocs.io/en/stable/) (tested on 5.1.0) -->
<!-- - [scipy](https://www.scipy.org/) (tested on 1.2.1) - insalled by scikit-learn-->
<!-- - [numpy](http://www.numpy.org/) (tested on 1.16.1) - installed by scikit-learn -->

This code was tested on Ubuntu 18.04 LTS using Keras 2.2.4 with a TensorFlow 1.10 backend.
There are a few portions of the code that may explicitly depend on TensorFlow, but
such dependencies are kept to a minimum.

----
**Creator: [Arun S. Maiya](http://arun.maiya.net)**

**Email:** arun [at] maiya [dot] net


Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ktrain-0.1.2.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

ktrain-0.1.2-py3-none-any.whl (47.2 kB view details)

Uploaded Python 3

File details

Details for the file ktrain-0.1.2.tar.gz.

File metadata

  • Download URL: ktrain-0.1.2.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for ktrain-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cee4f5b7dab535c5c62e9c3ed59698cf907eae000fc00379f83df86f32194ea8
MD5 4fdcf5e59347bf06c70765fba68a398b
BLAKE2b-256 4c9028cf5d0f2aad2cebd63e74d292be22b5a16ff9f915671bf3a38b8998eabf

See more details on using hashes here.

File details

Details for the file ktrain-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ktrain-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 47.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for ktrain-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c6fde9586964e63c7e31a93396a4b3a948d0ead46691e818ed5a64afff03751a
MD5 13298ccb1558a7bdc5ad05be672212dc
BLAKE2b-256 1c36aa1f76377b87ca55d847f8287ee0c38f7060ffa6637533372562e783ba9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page