Class BasicTranslator<I,O>

java.lang.Object
ai.djl.translate.BasicTranslator<I,O>
Type Parameters:
I - the input class
O - the output class
All Implemented Interfaces:
PostProcessor<O>, PreProcessor<I>, Translator<I,O>

public class BasicTranslator<I,O> extends Object implements Translator<I,O>
A Translator made by combining a PreProcessor and a PostProcessor.
  • Constructor Details

    • BasicTranslator

      public BasicTranslator(PreProcessor<I> preProcessor, PostProcessor<O> postProcessor)
      Constructs a BasicTranslator with the default Batchifier.
      Parameters:
      preProcessor - the preProcessor to use for pre-processing
      postProcessor - the postProcessor to use for post-processing
    • BasicTranslator

      public BasicTranslator(PreProcessor<I> preProcessor, PostProcessor<O> postProcessor, Batchifier batchifier)
      Constructs a BasicTranslator.
      Parameters:
      preProcessor - the preProcessor to use for pre-processing
      postProcessor - the postProcessor to use for post-processing
      batchifier - the batchifier to use
  • Method Details

    • processOutput

      public O processOutput(TranslatorContext ctx, NDList list) throws Exception
      Processes the output NDList to the corresponding output object.
      Specified by:
      processOutput in interface PostProcessor<I>
      Parameters:
      ctx - the toolkit used for post-processing
      list - the output NDList after inference, usually immutable in engines like PyTorch. @see Issue 1774
      Returns:
      the output object of expected type
      Throws:
      Exception - if an error occurs during processing output
    • processInput

      public NDList processInput(TranslatorContext ctx, I input) throws Exception
      Processes the input and converts it to NDList.
      Specified by:
      processInput in interface PreProcessor<I>
      Parameters:
      ctx - the toolkit for creating the input NDArray
      input - the input object
      Returns:
      the NDList after pre-processing
      Throws:
      Exception - if an error occurs during processing input
    • getBatchifier

      public Batchifier getBatchifier()
      Returns the Batchifier.
      Specified by:
      getBatchifier in interface Translator<I,O>
      Returns:
      the Batchifier
    • prepare

      public void prepare(TranslatorContext ctx) throws Exception
      Prepares the translator with the manager and model to use.
      Specified by:
      prepare in interface Translator<I,O>
      Parameters:
      ctx - the context for the Predictor.
      Throws:
      Exception - if there is an error for preparing the translator