'Which Java class is compatible with python Pandas DataFrame when using DJL(Deep Java Library)?

I'm trying to import Python Tensorflow custom model to spring-boot using DJL Tensorflow, and the model gets Pandas DataFrame as both input and output.

I'm wondering if there is any particular table or dataFrame class that is applicable for Criteria<I, O> and ZooModel<I, O> since there is only Image class from ai.djl.modality.cv.Image in example codes.

If there is, is the class compatible with Pandas DataFrame? There are some codes that I wrote and error messages below.

I've already tried tablesaw.api.Table and apache.spark.sql.Dataset but it did not work. If you know any information about it, please let me know.

conditions

  • Input class I and Output class O should be equivalent to Pandas DataFrame
  • This class should be applicable for Criteria<I, O> and ZooModel<I, O>

codes

Criteria<I, O> criteria = Criteria.builder()
                 .setTypes(I.class, O.class) // defines input and output data type
                 .optModelUrls("../model/mlpDemo/") // search models in specified path
                 .build();
        
try (ZooModel<I, O> model = criteria.loadModel(); 
        Predictor<I, O> predictor = model.newPredictor();) {
    O result = predictor.predict(dataFrame); // here should be loaded dataFrame
    System.out.print(result);
} catch (Exception e) {
    e.printStackTrace();
}

error messages

ai.djl.repository.zoo.ModelNotFoundException: No matching model with specified Input/Output type found.
    at ai.djl.repository.zoo.Criteria.loadModel(Criteria.java:186)
    at com.example.demo.DemoApplication.main(DemoApplication.java:69)
Caused by: ai.djl.repository.zoo.ModelNotFoundException: No matching default translator found. The valid input and output classes are: 
    (ai.djl.ndarray.NDList, ai.djl.ndarray.NDList)

    at ai.djl.repository.zoo.BaseModelLoader.loadModel(BaseModelLoader.java:97)
    at ai.djl.repository.zoo.Criteria.loadModel(Criteria.java:174)
    ... 1 more



Solution 1:[1]

You need create your own Translator to convert DataFrame into NDList:

    class MyTranslator implements NoBatchifyTranslator<DataFrame, Classifcations> {

        @Override
        public NDList processInput(TranslatorContext ctx, DataFrame input) {
            // convert DataFrame columns into NDArrays
            NDArray col1 = ...
            NDArray col2 = ...
            return new NDList(col1, col2);
        }

        /** {@inheritDoc} */
        @Override
        public Classifications processOutput(TranslatorContext ctx, NDList list) {
            // implement your post processing
            return new Classifications();
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank Liu