Skip to content

Commit

Permalink
[examples] Moves nlp examples into nlp folder (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Aug 7, 2024
1 parent d7c8a74 commit a70360e
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/tensorflow/how_to_import_tensorflow_models_in_DJL.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Note that you need to click the download model button to find the actual Google
Please refer to these two examples:

1. [Object Detection with TensorFlow](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/cv/ObjectDetection.java) for loading from TensorFlow Hub url.
2. [BERT Classification](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/BertClassification.java) for loading from local downloaded model.
2. [BERT Classification](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/nlp/BertClassification.java) for loading from local downloaded model.

## How to import TensorFlow models that use [TensorFlow Extensions](https://www.tensorflow.org/resources/libraries-extensions)

Expand All @@ -68,7 +68,7 @@ This is a text encoder from tensorflow hub that uses ops from the TensorFlow Tex
- You need to download the library files for the extension so that we can load them for use. [Here](https://github.com/tensorflow/text#install-using-pip) are instructions for downloading TensorFlow text. We'll use version `2.7.0` in this example.
- After the library files are downloaded, you are ready to load them for use in DJL. You do this by using
TensorFlow Java `TensorFlow.loadLibrary(...)` API before loading the model in DJL.
- We can update the existing [UniversalSentenceEncoder](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/UniversalSentenceEncoder.java) example to use the Multilingual model:
- We can update the existing [UniversalSentenceEncoder](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/nlp/UniversalSentenceEncoder.java) example to use the Multilingual model:

```java
// Build Criteria for Multilingual Universal Sentence Encoder from TensorFlow Hub
Expand Down
4 changes: 2 additions & 2 deletions examples/docs/BERT_question_and_answer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

In this example, you learn how to use the BERT QA model trained by GluonNLP (Apache MXNet) and PyTorch.
You can provide the model with a question and a paragraph containing an answer. The model is then able to find the best answer from the answer paragraph.
You can find the source code in [BertQaInference.java](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/BertQaInference.java).
You can find the source code in [BertQaInference.java](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/nlp/BertQaInference.java).

Note that Apache MXNet BERT model has a limitation where the max size of the tokens including the question and the paragraph is 384.

Expand Down Expand Up @@ -33,5 +33,5 @@ Follow [setup](../../docs/development/setup.md) to configure your development en

```sh
cd examples
./gradlew run -Dmain=ai.djl.examples.inference.BertQaInference
./gradlew run -Dmain=ai.djl.examples.inference.nlp.BertQaInference
```
4 changes: 2 additions & 2 deletions examples/docs/sentiment_analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

In this example, you learn how to use the DistilBERT model trained by HuggingFace using PyTorch.
You can provide the model with a question and a paragraph containing an answer. The model is then able to find the best answer from the answer paragraph.
You can find the source code in [SentimentAnalysis.java](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/SentimentAnalysis.java).
You can find the source code in [SentimentAnalysis.java](https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/nlp/SentimentAnalysis.java).

Example:

Expand All @@ -29,5 +29,5 @@ Follow [setup](../../docs/development/setup.md) to configure your development en

```sh
cd examples
./gradlew run -Dmain=ai.djl.examples.inference.SentimentAnalysis
./gradlew run -Dmain=ai.djl.examples.inference.nlp.SentimentAnalysis
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.MalformedModelException;
import ai.djl.ModelException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
* and limitations under the License.
*/

package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.Application;
import ai.djl.Device;
import ai.djl.ModelException;
import ai.djl.huggingface.translator.QuestionAnsweringTranslatorFactory;
import ai.djl.inference.Predictor;
import ai.djl.modality.nlp.qa.QAInput;
import ai.djl.repository.zoo.Criteria;
Expand Down Expand Up @@ -65,11 +64,11 @@ public static String predict() throws IOException, TranslateException, ModelExce

Criteria<QAInput, String> criteria =
Criteria.builder()
.optApplication(Application.NLP.QUESTION_ANSWER)
.setTypes(QAInput.class, String.class)
.optFilter("backbone", "bert")
.optModelUrls(
"djl://ai.djl.huggingface.pytorch/deepset/minilm-uncased-squad2")
.optEngine("PyTorch")
.optDevice(Device.cpu())
.optTranslatorFactory(new QuestionAnsweringTranslatorFactory())
.optProgress(new ProgressBar())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* and limitations under the License.
*/

package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.Application;
import ai.djl.Device;
import ai.djl.MalformedModelException;
import ai.djl.ModelException;
Expand Down Expand Up @@ -58,8 +57,8 @@ public static Classifications predict()

Criteria<String, Classifications> criteria =
Criteria.builder()
.optApplication(Application.NLP.SENTIMENT_ANALYSIS)
.setTypes(String.class, Classifications.class)
.optModelUrls("djl://ai.djl.pytorch/distilbert")
.optEngine("PyTorch")
// This model was traced on CPU and can only run on CPU
.optDevice(Device.cpu())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.Application;
import ai.djl.MalformedModelException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.ActionRecognition;
import ai.djl.modality.Classifications;
import ai.djl.translate.TranslateException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.BigGAN;
import ai.djl.modality.cv.Image;
import ai.djl.testing.TestRequirements;
import ai.djl.translate.TranslateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.InstanceSegmentation;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.translate.TranslateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.MaskDetection;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.ObjectDetection;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* and limitations under the License.
*/

package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.ObjectDetectionWithTensorflowSavedModel;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.PoseEstimation;
import ai.djl.modality.cv.output.Joints;
import ai.djl.translate.TranslateException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.StyleTransfer;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.cv;

import ai.djl.ModelException;
import ai.djl.examples.inference.cv.Yolov8Detection;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

/** Contains tests for the inference examples. */
package ai.djl.examples.inference.cv;
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.face;

import ai.djl.ModelException;
import ai.djl.examples.inference.face.FeatureComparison;
import ai.djl.examples.inference.face.FeatureExtraction;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.face;

import ai.djl.ModelException;
import ai.djl.examples.inference.face.FeatureExtraction;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.face;

import ai.djl.ModelException;
import ai.djl.examples.inference.face.LightFaceDetection;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.face;

import ai.djl.ModelException;
import ai.djl.examples.inference.face.RetinaFaceDetection;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

/** Contains tests for the inference examples. */
package ai.djl.examples.inference.face;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.ModelException;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.ModelException;
import ai.djl.modality.Classifications;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.nlp;

import ai.djl.ModelException;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference;
package ai.djl.examples.inference.sr;

import ai.djl.ModelException;
import ai.djl.examples.inference.sr.SuperResolution;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.testing.TestRequirements;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

/** Contains tests for the inference examples. */
package ai.djl.examples.inference.sr;
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
* and limitations under the License.
*/

package ai.djl.examples.inference;
package ai.djl.examples.inference.timeseries;

import ai.djl.ModelException;
import ai.djl.examples.inference.timeseries.AirPassengersDeepAR;
import ai.djl.examples.inference.timeseries.M5ForecastingDeepAR;
import ai.djl.testing.TestRequirements;
import ai.djl.translate.TranslateException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

/** Contains tests for the inference examples. */
package ai.djl.examples.inference.timeseries;

0 comments on commit a70360e

Please sign in to comment.