KotlinDL is a high-level Deep Learning API written in Kotlin and inspired by Keras. Under the hood it is using TensorFlow Java API. KotlinDL offers simple APIs for training deep learning models from scratch, importing existing Keras models for inference, and leveraging transfer learning for tweaking existing pre-trained models to your tasks.
This project aims to make Deep Learning easier for JVM developers, and to simplify deploying deep learning models in JVM production environments.
Here's an example of what a classic convolutional neural network LeNet would look like in KotlinDL:
private const val EPOCHS = 3
private const val TRAINING_BATCH_SIZE = 1000
private const val NUM_CHANNELS = 1L
private const val IMAGE_SIZE = 28L
private const val SEED = 12L
private const val TEST_BATCH_SIZE = 1000
private val lenet5Classic = Sequential.of(
Input(
IMAGE_SIZE,
IMAGE_SIZE,
NUM_CHANNELS
),
Conv2D(
filters = 6,
kernelSize = longArrayOf(5, 5),
strides = longArrayOf(1, 1, 1, 1),
activation = Activations.Tanh,
kernelInitializer = GlorotNormal(SEED),
biasInitializer = Zeros(),
padding = ConvPadding.SAME
),
AvgPool2D(
poolSize = intArrayOf(1, 2, 2, 1),
strides = intArrayOf(1, 2, 2, 1),
padding = ConvPadding.VALID
),
Conv2D(
filters = 16,
kernelSize = longArrayOf(5, 5),
strides = longArrayOf(1, 1, 1, 1),
activation = Activations.Tanh,
kernelInitializer = GlorotNormal(SEED),
biasInitializer = Zeros(),
padding = ConvPadding.SAME
),
AvgPool2D(
poolSize = intArrayOf(1, 2, 2, 1),
strides = intArrayOf(1, 2, 2, 1),
padding = ConvPadding.VALID
),
Flatten(), // 3136
Dense(
outputSize = 120,
activation = Activations.Tanh,
kernelInitializer = GlorotNormal(SEED),
biasInitializer = Constant(0.1f)
),
Dense(
outputSize = 84,
activation = Activations.Tanh,
kernelInitializer = GlorotNormal(SEED),
biasInitializer = Constant(0.1f)
),
Dense(
outputSize = NUMBER_OF_CLASSES,
activation = Activations.Linear,
kernelInitializer = GlorotNormal(SEED),
biasInitializer = Constant(0.1f)
)
)
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
TRAIN_LABELS_ARCHIVE,
TEST_IMAGES_ARCHIVE,
TEST_LABELS_ARCHIVE,
NUMBER_OF_CLASSES,
::extractImages,
::extractLabels
)
lenet5Classic.use {
it.compile(
optimizer = Adam(clipGradient = ClipGradientByValue(0.1f)),
loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS,
metric = Metrics.ACCURACY
)
it.summary()
it.fit(dataset = train, epochs = EPOCHS, batchSize = TRAINING_BATCH_SIZE, verbose = true)
val accuracy = it.evaluate(dataset = test, batchSize = TEST_BATCH_SIZE).metrics[Metrics.ACCURACY]
println("Accuracy: $accuracy")
}
}
- TensorFlow Engine
- Limitations
- How to configure KotlinDL in your project
- Working with KotlinDL in Jupyter Notebook
- Examples and tutorials
- Running KotlinDL on GPU
- Reporting issues/Support
- Code of Conduct
- License
KotlinDL is built on top of TensorFlow 1.15 Java API. The Java API for TensorFlow 2.+ has recently had first public release, and this project will be switching to it in the nearest future. This, however, does not affect the high-level API.
Currently, only a limited set of deep learning architectures is supported. Here's the list of available layers:
- Input()
- Flatten()
- Dense()
- Dropout()
- Conv2D()
- MaxPool2D()
- AvgPool2D()
KotlinDL supports model inference in JVM backend applications, Android support is coming in later releases.
To use KotlinDL in your project, you need to add the following dependency to your build.gradle
file:
repositories {
jcenter()
maven {
url "https://kotlin.bintray.com/kotlin-datascience"
}
}
dependencies {
implementation 'org.jetbrains.kotlin-deeplearning:api:[KOTLIN-DL-VERSION]'
}
The latest KotlinDL version is 0.1.0.
For more details, as well as for pom.xml
and build.gradle.kts
examples, please refer to the Quick Start Guide.
You can work with KotlinDL interactively in Jupyter Notebook with Kotlin kernel. To do so, add the following dependency in your notebook:
@file:Repository("https://kotlin.bintray.com/kotlin-datascience")
@file:DependsOn("org.jetbrains.kotlin-deeplearning:api:[KOTLIN-DL-VERSION]")
For more details on how to install Jupyter Notebook and add Kotlin kernel, check out the Quick Start Guide.
You do not need to have any prior deep learning experience to start using KotlinDL. We are working on including extensive documentation to help you get started. At this point, feel free to check out the following tutorials:
- Quick Start Guide
- Creating your first neural network
- Training a model
- Running inference with a trained model
- Importing a Keras model
- Transfer learning
For more inspiration, take a look at the code examples in this repo.
To enable the training and inference on GPU, please read this TensorFlow GPU Support page and install the CUDA framework to enable calculations on a GPU device.
Note that only NVIDIA devices are supported.
You will also need to add the following dependencies in your project if you wish to leverage GPU:
* _compile 'org.tensorflow:libtensorflow:1.15.0'_
* _compile 'org.tensorflow:libtensorflow_jni_gpu:1.15.0'_
On Windows the following distributions are required:
- CUDA cuda_10.0.130_411.31_win10
- cudnn-10.0
- C++ redistributable parts
Please use GitHub issues for filing feature requests and bug reports. You are also welcome to join #deeplearning channel in the Kotlin Slack.
This project and the corresponding community is governed by the JetBrains Open Source and Community Code of Conduct. Please make sure you read it.
KotlinDL is licensed under the Apache 2.0 License.