Package ai.djl.translate
Interface Translator<I,O>
- Type Parameters:
I
- the input typeO
- the output type
- All Superinterfaces:
PostProcessor<O>
,PreProcessor<I>
- All Known Subinterfaces:
NoBatchifyTranslator<I,
,O> ServingTranslator
,StreamingTranslator<I,
O>
- All Known Implementing Classes:
BaseImageTranslator
,BasicTranslator
,BigGANTranslator
,CrossEncoderServingTranslator
,ImageClassificationTranslator
,ImageFeatureExtractor
,ImageServingTranslator
,InstanceSegmentationTranslator
,NoopTranslator
,ObjectDetectionTranslator
,QaServingTranslator
,QATranslator
,Sam2ServingTranslator
,Sam2Translator
,SemanticSegmentationTranslator
,SimplePoseTranslator
,SimpleText2TextTranslator
,SingleShotDetectionTranslator
,SpeechRecognitionTranslator
,StyleTransferTranslator
,TextClassificationServingTranslator
,TextEmbeddingServingTranslator
,TokenClassificationServingTranslator
,YoloPoseTranslator
,YoloSegmentationTranslator
,YoloTranslator
,YoloV5Translator
,YoloV8Translator
,ZeroShotClassificationServingTranslator
The
Translator
interface provides model pre-processing and postprocessing functionality.
Users can use this in Predictor
with input and output objects specified. The
recommended flow is to use the Translator to translate only a single data item at a time (Record
) rather than a Batch. For example, the input parameter would then
be Image
rather than Image[]
. The Record
s will
then be combined using a Batchifier
. If it is easier in your use case to work with
batches directly or your model uses records instead of batches, you can use the NoBatchifyTranslator
.
The following is an example of processing an image and creating classification output:
private static final class MyTranslator implements Translator<Image, Classification> { private int imageWidth; private int imageHeight; public MyTranslator(int imageWidth, int imageHeight) { this.imageWidth = imageWidth; this.imageHeight = imageHeight; } @Override public NDList processInput(TranslatorContext ctx, Image input) { NDArray imageND = input.toNDArray(ctx.getNDManager()); return new NDList(NDImageUtils.resize(imageND, imageWidth, imageHeight); } @Override public Classification processOutput(TranslatorContext ctx, NDList list) throws TranslateException { Model model = ctx.getModel(); NDArray array = list.get(0).at(0); NDArray sorted = array.argSort(-1, false); NDArray top = sorted.at(0); float[] probabilities = array.toFloatArray(); int topIndex = top.toIntArray()[0]; String[] synset; try { synset = model.getArtifact("synset.txt", MyTranslator::loadSynset); } catch (IOException e) { throw new TranslateException(e); } return new Classification(synset[topIndex], probabilities[topIndex]); } private static String[] loadSynset(InputStream is) { ... } }
-
Method Summary
Modifier and TypeMethodDescriptiondefault NDList
batchProcessInput
(TranslatorContext ctx, List<I> inputs) Batch processes the inputs and converts it to NDList.batchProcessOutput
(TranslatorContext ctx, NDList list) Batch processes the output NDList to the corresponding output objects.default Batchifier
Returns theBatchifier
.default TranslatorOptions
Returns possibleTranslatorOptions
that can be built using thisTranslator
.default void
prepare
(TranslatorContext ctx) Prepares the translator with the manager and model to use.Methods inherited from interface ai.djl.translate.PostProcessor
processOutput
Methods inherited from interface ai.djl.translate.PreProcessor
processInput
-
Method Details
-
getBatchifier
Returns theBatchifier
.- Returns:
- the
Batchifier
-
batchProcessInput
Batch processes the inputs and converts it to NDList. -
batchProcessOutput
Batch processes the output NDList to the corresponding output objects.- Parameters:
ctx
- the toolkit used for post-processinglist
- the output NDList after inference, usually immutable in engines like PyTorch. @see Issue 1774- Returns:
- a list of the output object of expected type
- Throws:
Exception
- if an error occurs during processing output
-
prepare
Prepares the translator with the manager and model to use.- Parameters:
ctx
- the context for thePredictor
.- Throws:
Exception
- if there is an error for preparing the translator
-
getExpansions
Returns possibleTranslatorOptions
that can be built using thisTranslator
.- Returns:
- possible options or null if not defined
-