Last active
September 1, 2024 10:15
-
-
Save dacr/8f9e4501c98ce80e539343ef7f630f03 to your computer and use it in GitHub Desktop.
image classification try / published by https://github.com/dacr/code-examples-manager #32c4a82b-8a40-480a-bad9-5390bceed3dd/c49b6ec266249eb67d7c6866045612a1304a06cb
This file contains 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
// summary : image classification try | |
// keywords : djl, machine-learning, tutorial, detection, ai, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 32c4a82b-8a40-480a-bad9-5390bceed3dd | |
// created-on : 2022-03-09T20:17:01+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.slf4j:slf4j-api:2.0.13" | |
//> using dep "org.slf4j:slf4j-simple:2.0.13" | |
//> using dep "net.java.dev.jna:jna:5.14.0" | |
//> using dep "ai.djl:api:0.29.0" | |
//> using dep "ai.djl:basicdataset:0.29.0" | |
//> using dep "ai.djl:model-zoo:0.29.0" | |
//> using dep "ai.djl.huggingface:tokenizers:0.29.0" | |
//> using dep "ai.djl.mxnet:mxnet-engine:0.29.0" | |
//> using dep "ai.djl.mxnet:mxnet-model-zoo:0.29.0" | |
//> using dep "ai.djl.pytorch:pytorch-engine:0.29.0" | |
//> using dep "ai.djl.pytorch:pytorch-model-zoo:0.29.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-engine:0.29.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-model-zoo:0.29.0" | |
//> using dep "ai.djl.onnxruntime:onnxruntime-engine:0.29.0" | |
// --------------------- | |
//System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug") | |
import ai.djl.Application | |
import ai.djl.engine.Engine | |
import ai.djl.modality.Classifications | |
import ai.djl.modality.Classifications.Classification | |
import ai.djl.modality.cv.Image | |
import ai.djl.modality.cv.ImageFactory | |
import ai.djl.repository.zoo.Criteria | |
import ai.djl.repository.zoo.ModelZoo | |
import ai.djl.repository.zoo.ZooModel | |
import ai.djl.training.util.ProgressBar | |
import java.nio.file.Files | |
import java.nio.file.Path | |
import java.nio.file.Paths | |
import scala.jdk.CollectionConverters.* | |
// ---------------------------------------------------------------------------------------------- | |
val criteria = | |
Criteria.builder | |
.optApplication(Application.CV.IMAGE_CLASSIFICATION) | |
.setTypes(classOf[Image], classOf[Classifications]) | |
// ------------------------------------ | |
//.optFilter("flavor","v1") | |
//.optFilter("dataset","cifar10") | |
// ------------------------------------ | |
//.optFilter("flavor","v3_large") | |
//.optFilter("dataset","imagenet") | |
// ------------------------------------ | |
.optFilter("flavor","v1d") | |
.optFilter("dataset","imagenet") | |
.optProgress(new ProgressBar) | |
.build | |
val model = ModelZoo.loadModel(criteria) | |
val predictor = model.newPredictor() | |
val inputImageURL = "https://mapland.fr/data/ai/images-samples/example-001.jpg" | |
val img = ImageFactory.getInstance().fromUrl(inputImageURL) | |
val found: Classifications = predictor.predict(img) | |
found.items() | |
.asScala | |
.toList | |
.asInstanceOf[List[Classification]] | |
.filter(_.getProbability > 0.5d) | |
.foreach{cl => | |
println(cl.getClassName+" "+cl.getProbability) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment