= Deep Java Library Component
:doctitle: Deep Java Library
:shortname: djl
:artifactid: camel-djl
:description: Infer Deep Learning models from message exchanges data using Deep Java Library (DJL).
:since: 3.3
:supportlevel: Stable
:component-header: Only producer is supported
//Manually maintained attributes
:camel-spring-boot-name: djl

*Since Camel {since}*

*{component-header}*

The *Deep Java Library* component is used to infer Deep Learning models from message exchanges data.
This component uses https://djl.ai/[Deep Java Library] as underlying library.

In order to use the DJL component, Maven users will need to add the
following dependency to their `pom.xml`:

*pom.xml*

[source,xml]
----
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-djl</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
----

== URI format

----
djl:application
----

// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END


== Model Zoo

The following table contains supported models in the model zoo:

[width="100%",cols="1,3,5,3,5,5",options="header"]
|===
| CV | Image  Classification | Resnet image classification | `cv/image_classification` | `ai.djl.zoo:resnet:0.0.1` | {layers=50, flavor=v1, dataset=cifar10}
| CV | Image  Classification | MLP image classification | `cv/image_classification` | `ai.djl.zoo:mlp:0.0.2` | {dataset=mnist}
| CV | Image  Classification | MLP image classification | `cv/image_classification` | `ai.djl.mxnet:mlp:0.0.1` | {dataset=mnist}
| CV | Image  Classification | Resnet image classification | `cv/image_classification` | `ai.djl.mxnet:resnet:0.0.1` | {layers=18, flavor=v1, dataset=imagenet}
| CV | Image  Classification | Resnet image classification | `cv/image_classification` | `ai.djl.mxnet:resnet:0.0.1` | {layers=50, flavor=v2, dataset=imagenet}
| CV | Image  Classification | Resnet image classification | `cv/image_classification` | `ai.djl.mxnet:resnet:0.0.1` | {layers=152, flavor=v1d, dataset=imagenet}
| CV | Image  Classification | Resnet image classification | `cv/image_classification` | `ai.djl.mxnet:resnet:0.0.1` | {layers=50, flavor=v1, dataset=cifar10}
| CV | Image  Classification | Resnext image classification | `cv/image_classification` | `ai.djl.mxnet:resnext:0.0.1` | {layers=101, flavor=64x4d, dataset=imagenet}
| CV | Image  Classification | Senet image classification | `cv/image_classification` | `ai.djl.mxnet:senet:0.0.1` | {layers=154, dataset=imagenet}
| CV | Image  Classification | Senet and Resnext image classification | `cv/image_classification` | `ai.djl.mxnet:se_resnext:0.0.1` | {layers=101, flavor=32x4d, dataset=imagenet}
| CV | Image  Classification | Senet and Resnext image classification | `cv/image_classification` | `ai.djl.mxnet:se_resnext:0.0.1` | {layers=101, flavor=64x4d, dataset=imagenet}
| CV | Image  Classification | Squeezenet image classification | `cv/image_classification` | `ai.djl.mxnet:squeezenet:0.0.1` | {flavor=1.0, dataset=imagenet}
| CV | Object  Detection | Single Shot Detection for Object Detection | `cv/object_detection` | `ai.djl.zoo:ssd:0.0.1` | {flavor=tiny, dataset=pikachu}
| CV | Object  Detection | Single-shot object detection | `cv/object_detection` | `ai.djl.mxnet:ssd:0.0.1` | {size=512, backbone=resnet50, flavor=v1, dataset=voc}
| CV | Object  Detection | Single-shot object detection | `cv/object_detection` | `ai.djl.mxnet:ssd:0.0.1` | {size=512, backbone=vgg16, flavor=atrous, dataset=coco}
| CV | Object  Detection | Single-shot object detection | `cv/object_detection` | `ai.djl.mxnet:ssd:0.0.1` | {size=512, backbone=mobilenet1.0, dataset=voc}
| CV | Object  Detection | Single-shot object detection | `cv/object_detection` | `ai.djl.mxnet:ssd:0.0.1` | {size=300, backbone=vgg16, flavor=atrous, dataset=voc}
|===


== DJL Engine implementation

Because DJL is deep learning framework agnostic, you don't have to make a choice between frameworks when creating your projects.
You can switch frameworks at any point.
To ensure the best performance, DJL also provides automatic CPU/GPU choice based on hardware configuration.

=== MxNet engine

You can pull the MXNet engine from the central Maven repository by including the following dependency:

[source,xml]
----
<dependency>
    <groupId>ai.djl.mxnet</groupId>
    <artifactId>mxnet-engine</artifactId>
    <version>x.x.x</version>
    <scope>runtime</scope>
</dependency>
----

DJL offers an automatic option that will download the jars the first time you run DJL.
It will automatically determine the appropriate jars for your system based on the platform and GPU support.

[source,xml]
----
    <dependency>
      <groupId>ai.djl.mxnet</groupId>
      <artifactId>mxnet-native-auto</artifactId>
      <version>1.7.0-a</version>
      <scope>runtime</scope>
    </dependency>
----

More information about https://docs.djl.ai/engines/mxnet/index.html[MxNet engine installation]

=== PyTorch engine

You can pull the PyTorch engine from the central Maven repository by including the following dependency:

[source,xml]
----
<dependency>
    <groupId>ai.djl.pytorch</groupId>
    <artifactId>pytorch-engine</artifactId>
    <version>x.x.x</version>
    <scope>runtime</scope>
</dependency>
----

DJL offers an automatic option that will download the jars the first time you run DJL.
It will automatically determine the appropriate jars for your system based on the platform and GPU support.

[source,xml]
----
    <dependency>
      <groupId>ai.djl.pytorch</groupId>
      <artifactId>pytorch-native-auto</artifactId>
      <version>1.5.0</version>
      <scope>runtime</scope>
    </dependency>
----

More information about https://docs.djl.ai/engines/pytorch/index.html[PyTorch engine installation]

=== Tensorflow engine

You can pull the Tensorflow engine from the central Maven repository by including the following dependency:

[source,xml]
----
<dependency>
    <groupId>ai.djl.tensorflow</groupId>
    <artifactId>tensorflow-engine</artifactId>
    <version>x.x.x</version>
    <scope>runtime</scope>
</dependency>
----

DJL offers an automatic option that will download the jars the first time you run DJL.
It will automatically determine the appropriate jars for your system based on the platform and GPU support.

[source,xml]
----
    <dependency>
      <groupId>ai.djl.tensorflow</groupId>
      <artifactId>tensorflow-native-auto</artifactId>
      <version>2.1.0</version>
      <scope>runtime</scope>
    </dependency>
----

More information about https://docs.djl.ai/engines/tensorflow/index.html[Tensorflow engine installation]



== Examples

=== MNIST image classification from file

[source,java]
----
from("file:/data/mnist/0/10.png")
    .to("djl:cv/image_classification?artifactId=ai.djl.mxnet:mlp:0.0.1");
----

=== Object detection
[source,java]
----
from("file:/data/mnist/0/10.png")
    .to("djl:cv/image_classification?artifactId=ai.djl.mxnet:mlp:0.0.1");
----

=== Custom deep learning model
[source,java]
----
// create deep learning model
Model model = Model.newInstance();
model.setBlock(new Mlp(28 * 28, 10, new int[]{128, 64}));
model.load(Paths.get(MODEL_DIR), MODEL_NAME);

// create translator for pre-processing and postprocessing
ImageClassificationTranslator.Builder builder = ImageClassificationTranslator.builder();
builder.setSynsetArtifactName("synset.txt");
builder.setPipeline(new Pipeline(new ToTensor()));
builder.optApplySoftmax(true);
ImageClassificationTranslator translator = new ImageClassificationTranslator(builder);

// Bind model and translator beans
context.getRegistry().bind("MyModel", model);
context.getRegistry().bind("MyTranslator", translator);

from("file:/data/mnist/0/10.png")
    .to("djl:cv/image_classification?model=MyModel&translator=MyTranslator");
----

include::spring-boot:partial$starter.adoc[]
