Skip to content

Instantly share code, notes, and snippets.

View lukasjapan's full-sized avatar

Lukas lukasjapan

View GitHub Profile
@lukasjapan
lukasjapan / using-deeplearning4j-to-distinguish-between-cats-and-dogs.1.kt
Last active February 10, 2021 07:37
using-deeplearning4j-to-distinguish-between-cats-and-dogs 1
val model = VGG16().initPretrained(PretrainedType.IMAGENET) as ComputationGraph
val image = FileInputStream("input.png")
val input = NativeImageLoader(224, 224, 3).asMatrix(image).also { VGG16ImagePreProcessor().transform(it) }
val output = model.outputSingle(input)
@lukasjapan
lukasjapan / using-deeplearning4j-to-distinguish-between-cats-and-dogs.2.kt
Created February 10, 2021 07:38
using-deeplearning4j-to-distinguish-between-cats-and-dogs.2.kt
// designing the model - mostly the same as shown at https://deeplearning4j.org/mnist-for-beginners
val conf = NeuralNetConfiguration.Builder()
.seed(ThreadLocalRandom.current().nextLong())
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.iterations(1)
.learningRate(0.006)
.updater(Nesterovs(0.9))
.regularization(true).l2(1e-4)
.list()
@lukasjapan
lukasjapan / using-deeplearning4j-to-distinguish-between-cats-and-dogs.3.kt
Created February 10, 2021 07:39
using-deeplearning4j-to-distinguish-between-cats-and-dogs.3.kt
// load vgg16 model from model zoo
val model = VGG16().initPretrained(PretrainedType.IMAGENET) as ComputationGraph
// restore the new model that was saved to a file
val catsdogsModel = ModelSerializer.restoreMultiLayerNetwork(javaClass.getResource("/catdogmodel.dl4j").openStream())
// get vgg16 labels
val image = FileInputStream("input.png")
val input = NativeImageLoader(224, 224, 3).asMatrix(image).also { VGG16ImagePreProcessor().transform(it) }
val vgg16labels = vgg16model.outputSingle(input)