Deep Learning with Kotlin: the alpha version of KotlinDL

The translation of the material was prepared as part of the online course Kotlin Backend Developer“.

We also invite everyone to a free demo lesson “Object Oriented Programming in Kotlin” Lesson objectives:
– know the elements of the Kotlin object model;
– create various classes and objects;
– perform inheritance and delegation;
– use getters and setters.


Hi friends! Today we will talk about the first preview version. KotlinDL (v.0.1.0) – a high-level deep learning framework similar to Kerasbut written in Kotlin. It has simple APIs for building, training, and deploying deep learning models in the JVM. High-level APIs and fine-tuned parameters make it easy to get started with KotlinDL. To create and train your first neural network, you just need to write a few lines in Kotlin:

private val model = Sequential.of(
    Input(28, 28, 1),
    Flatten(),
    Dense(300),
    Dense(100),
    Dense(10)
)

fun main() {
    val (train, test) = Dataset.createTrainAndTestDatasets(
        trainFeaturesPath = "datasets/mnist/train-images-idx3-ubyte.gz",
        trainLabelsPath = "datasets/mnist/train-labels-idx1-ubyte.gz",
        testFeaturesPath = "datasets/mnist/t10k-images-idx3-ubyte.gz",
        testLabelsPath = "datasets/mnist/t10k-labels-idx1-ubyte.gz",
        numClasses = 10,
        ::extractImages,
        ::extractLabels
    )
    val (newTrain, validation) = train.split(splitRatio = 0.95)

    model.use {
        it.compile(
            optimizer = Adam(),
            loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS,
            metric = Metrics.ACCURACY
        )

        it.summary()

        it.fit(
            dataset = newTrain,
            epochs = 10,
            batchSize = 100,
            verbose = false
        )

        val accuracy = it.evaluate(
            dataset = validation,
            batchSize = 100
        ).metrics[Metrics.ACCURACY]

        println("Accuracy: $accuracy")
        it.save(File("src/model/my_model"))
    }
}

GPU support

Training models is a resource intensive task. Using a GPU can speed up this process significantly. This is where KotlinDL comes in! You only need to add one dependency to run the code on an NVIDIA device.

Extensive API capabilities

KotlinDL has all the APIs you need to build and train feedforward neural networks, including convolutional networks. It defaults to reasonable values ​​for most hyperparameters. The framework offers a wide range of optimizers, weight initializers, activation functions, and other options for fine-tuning the model. You can save the resulting model and import it into the JVM backend application.

Importing Keras Trained Models

The built-in APIs in KotlinDL allow you to create, train, and save deep learning models and use them to work with new data. You can import and use a model trained with KotlinDL or Keras version 2. * in Python.

For both models, a transfer learning method is available: just load an existing trained model and fit it to your task.

Temporary restrictions

In the current alpha version, only a few layers are available: Input(), Flatten(), Dense(), Dropout(), Conv2D(), MaxPool2D() and AvgPool2D()… This means that not all Keras trained models are supported yet. You can import and configure trained VGG-16 or VGG-19 models, but the ResNet50 model, for example, cannot be loaded. New layers will appear in the next releases.

Another time constraint has to do with deployment. You can deploy the model to the back end of the JVM, however, support for Android devices will only appear in future releases.

What’s under the hood?

KotlinDL uses the TensorFlow Java API, which is actively developed by the open source community.

Try it yourself!

We have prepared several articles (in English) to help you get started with KotlinDL:

We will be glad to receive your feedback and pull requests in GitHub Issues… Join the #deeplearning channel at Kotlin Slack Community


Learn more about the course Kotlin Backend Developer

Watch the webinar “Object Oriented Programming in Kotlin”

Similar Posts

Leave a Reply