Skip to content

Commit

Permalink
Updated inference examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zaleslaw committed Dec 8, 2020
1 parent 12d645f commit 16083f8
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 134 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ private val fashionMnistLabelEncoding = mapOf(
9 to "Ankle boot"
)

/**
* This examples demonstrates model and model weights export and import back.
*
* Models is exported as graph in .pb format, weights are exported in custom (txt) format.
*
* Model is trained on FashionMnist dataset.
*
* It saves all the data to the project root directory.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
FASHION_TRAIN_IMAGES_ARCHIVE,
Expand All @@ -57,10 +66,6 @@ fun main() {
verbose = true
)

val weights = it.layers[0].getWeights() // first conv2d layer

drawFilters(weights[0])

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

println("Accuracy $accuracy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import java.io.File

private const val PATH_TO_MODEL = "savedmodels/fashionLenet"


/**
* Inference model is used here, separately from model training code to illustrate the ability to load model graph and weights to start prediction process.
*
* NOTE: The example requires the saved model in the appropriate directory (run LeNetFashionMnistExportImport.kt firstly).
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
FASHION_TRAIN_IMAGES_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ private const val EPOCHS = 1
private const val TRAINING_BATCH_SIZE = 500
private const val TEST_BATCH_SIZE = 1000

/**
* This examples demonstrates model and model weights export and import back.
*
* Models is exported as graph in .pb format, weights are exported in custom (txt) format.
*
* Model is trained on Mnist dataset.
*
* It saves all the data to the project root directory.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ private const val EPOCHS = 1
private const val TRAINING_BATCH_SIZE = 1000
private const val TEST_BATCH_SIZE = 1000

/**
* This examples demonstrates model and model weights export and import back.
*
* Models is exported in Keras-style json format, weights are exported in custom (txt) format.
*
* After loading model is trained again with another optimizer with frozen Conv2D layers. Only weights in Dense layers can be updated.
*
* Model is trained on Mnist dataset.
*
* It saves all the data to the project root directory.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import java.io.File

private const val PATH_TO_MODEL = "savedmodels/lenet5"

/**
* Inference model is used here, separately from model training code to illustrate the ability to load model graph and weights to start prediction process.
*
* NOTE: The example requires the saved model in the appropriate directory (run LeNetMnistExportImport.kt firstly).
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
Expand All @@ -35,8 +40,6 @@ fun main() {

if (prediction == getLabel(train, imageId))
accuracy += (1.0 / amountOfTestSet)

//println("Prediction: $prediction Ground Truth: ${getLabel(train, imageId)}")
}
println("Accuracy: $accuracy")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ private const val EPOCHS = 1
private const val TRAINING_BATCH_SIZE = 1000
private const val TEST_BATCH_SIZE = 1000

/**
* This examples demonstrates model, model weights and optimizer weights export and import back.
*
* Models is exported in json format, weights are exported in custom (txt) format.
*
* It saves all the data to the project root directory.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ private val fashionMnistLabelEncoding = mapOf(
9 to "Ankle boot"
)

/**
* This examples demonstrates model activations and Conv2D filters visualisation.
*
* Model is trained on FashionMnist dataset.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
FASHION_TRAIN_IMAGES_ARCHIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import org.jetbrains.kotlinx.dl.api.core.metric.Metrics
import org.jetbrains.kotlinx.dl.api.core.optimizer.Adam
import org.jetbrains.kotlinx.dl.datasets.Dataset
import org.jetbrains.kotlinx.dl.datasets.handlers.*
import javax.swing.JFrame

private const val EPOCHS = 1
private const val TRAINING_BATCH_SIZE = 500
private const val TEST_BATCH_SIZE = 1000

/**
* This examples demonstrates model activations and Conv2D filters visualisation.
*
* Model is trained on Mnist dataset.
*/
fun main() {
val (train, test) = Dataset.createTrainAndTestDatasets(
TRAIN_IMAGES_ARCHIVE,
Expand Down Expand Up @@ -72,28 +76,3 @@ fun main() {
println("Ground Truth: $maxIdx")
}
}

fun drawActivations(activations: List<*>) {
val frame = JFrame("Visualise the matrix weights on Relu")
frame.contentPane.add(ReluGraphics(activations[0] as Array<Array<Array<FloatArray>>>))
frame.setSize(1500, 1500)
frame.isVisible = true
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.isResizable = false

val frame2 = JFrame("Visualise the matrix weights on Relu_1")
frame2.contentPane.add(ReluGraphics2(activations[1] as Array<Array<Array<FloatArray>>>))
frame2.setSize(1500, 1500)
frame2.isVisible = true
frame2.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame2.isResizable = false
}

fun drawFilters(filters: Array<*>, colorCoefficient: Double = 2.0) {
val frame = JFrame("Filters")
frame.contentPane.add(Conv2dJPanel(filters as Array<Array<Array<FloatArray>>>, colorCoefficient))
frame.setSize(1000, 1000)
frame.isVisible = true
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.isResizable = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package examples.inference.production

import java.awt.Color
import java.awt.Graphics
import javax.swing.JFrame
import javax.swing.JPanel
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -151,3 +152,28 @@ class ReluGraphics2(private val dst: Array<Array<Array<FloatArray>>>) : JPanel()
}
}
}

fun drawActivations(activations: List<*>) {
val frame = JFrame("Visualise the matrix weights on Relu")
frame.contentPane.add(ReluGraphics(activations[0] as Array<Array<Array<FloatArray>>>))
frame.setSize(1500, 1500)
frame.isVisible = true
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.isResizable = false

val frame2 = JFrame("Visualise the matrix weights on Relu_1")
frame2.contentPane.add(ReluGraphics2(activations[1] as Array<Array<Array<FloatArray>>>))
frame2.setSize(1500, 1500)
frame2.isVisible = true
frame2.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame2.isResizable = false
}

fun drawFilters(filters: Array<*>, colorCoefficient: Double = 2.0) {
val frame = JFrame("Filters")
frame.contentPane.add(Conv2dJPanel(filters as Array<Array<Array<FloatArray>>>, colorCoefficient))
frame.setSize(1000, 1000)
frame.isVisible = true
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.isResizable = false
}

0 comments on commit 16083f8

Please sign in to comment.