I
- the input typeO
- the output typepublic interface Translator<I,O> extends PreProcessor<I>, PostProcessor<O>
Translator
interface provides model pre-processing and postprocessing functionality.
Users can use this in Predictor
with input and output objects specified. The following
is an example of processing an image and creating classification output:
private static final class MyTranslator implements Translator<BufferedImage, Classification> { private int imageWidth; private int imageHeight; private Shape shape; public MyTranslator(int imageWidth, int imageHeight) { this.imageWidth = imageWidth; this.imageHeight = imageHeight; shape = new Shape(1, 3, imageWidth, imageHeight); } @Override public NDList processInput(TranslatorContext ctx, BufferedImage input) { BufferedImage image = Images.resize(input, imageWidth, imageHeight); FloatBuffer buffer = Images.toFloatBuffer(image); return new NDList(ctx.getNDManager().create(buffer, shape)); } @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) { ... } }
Modifier and Type | Method and Description |
---|---|
default Batchifier |
getBatchifier()
Gets the
Batchifier . |
default void |
prepare(NDManager manager,
Model model)
Prepares the translator with the manager and model to use.
|
getPipeline, processInput
processOutput
default Batchifier getBatchifier()
Batchifier
.Batchifier
default void prepare(NDManager manager, Model model) throws java.io.IOException
manager
- the manager for the translatormodel
- the model to translate forjava.io.IOException
- if there is an error reading inputs for preparing the translator