This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val trainDataset = OnHeapDataset.create(File(datasetPath, "train"), labelGenerator, preprocessing) | |
| val valDataset = OnHeapDataset.create(File(datasetPath, "val"), labelGenerator, preprocessing) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val preprocessing = preprocess { | |
| transformImage { | |
| centerCrop { | |
| size = 214 | |
| } | |
| pad { | |
| top = 10 | |
| bottom = 10 | |
| left = 10 | |
| right = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.use { | |
| it.compile( | |
| optimizer = Adam(clipGradient = ClipGradientByValue(0.1f)), | |
| loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS, | |
| metric = Metrics.ACCURACY | |
| ) | |
| it.logSummary() | |
| it.fit( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val earlyStopping = EarlyStopping( | |
| monitor = EpochTrainingEvent::valLossValue, | |
| minDelta = 0.0, | |
| patience = 2, | |
| verbose = true, | |
| mode = EarlyStoppingMode.AUTO, | |
| baseline = 0.1, | |
| restoreBestWeights = false | |
| ) | |
| val terminateOnNaN = TerminateOnNaN() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.use { | |
| it.compile( | |
| optimizer = Adam(), | |
| loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS, | |
| metric = Metrics.ACCURACY | |
| ) | |
| it.loadWeightsForFrozenLayers(hdfFile) | |
| it.fit( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val topModel = Sequential.of( | |
| GlobalAvgPool2D( | |
| name = "top_avg_pool", | |
| ), | |
| Dense( | |
| name = "top_dense", | |
| kernelInitializer = GlorotUniform(), | |
| biasInitializer = GlorotUniform(), | |
| outputSize = 200, | |
| activation = Activations.Relu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val modelHub = TFModelHub(cacheDirectory = File("cache/pretrainedModels")) | |
| val modelType = TFModels.CV.ResNet50(noTop = true, inputShape = intArrayOf(IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS)) | |
| val noTopModel = modelHub.loadModel(modelType) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.use { poseDetectionModel -> | |
| val imageFile = … | |
| val detectedPoses = poseDetectionModel.detectPoses(imageFile = imageFile, confidence = 0.0f) | |
| detectedPoses.multiplePoses.forEach { detectedPose -> | |
| println("Found ${detectedPose.first.classLabel} with probability ${detectedPose.first.probability}") | |
| detectedPose.second.poseLandmarks.forEach { | |
| println("Found ${it.poseLandmarkLabel} with probability ${it.probability}") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val modelHub = ONNXModelHub(cacheDirectory = File("cache/pretrainedModels")) | |
| val model = ONNXModels.PoseDetection.MoveNetMultiPoseLighting.pretrainedModel(modelHub) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.use { poseDetectionModel -> | |
| val imageFile = … | |
| val detectedPose = poseDetectionModel.detectPose(imageFile = imageFile) | |
| detectedPose.poseLandmarks.forEach { | |
| println("Found ${it.poseLandmarkLabel} with probability ${it.probability}") | |
| } | |
| detectedPose.edges.forEach { | |
| println("The ${it.poseEdgeLabel} starts at ${it.start.poseLandmarkLabel} and ends with ${it.end.poseLandmarkLabel}") |
NewerOlder