Search in sources :

Example 1 with Interpreter

use of org.tensorflow.lite.Interpreter in project MasterProject by Alexsogge.

the class HandWashDetection method loadTFModel.

private void loadTFModel() throws IOException {
    Log.d("pred", "Load TF model");
    tfliteModel = loadTFModelFile(context);
    if (tfliteModel == null) {
        initModelFallback();
    }
    try {
        tfInterpreter = new Interpreter(tfliteModel, tfliteOptions);
    } catch (IllegalArgumentException | NullPointerException e) {
        Log.e("Tensorflow", "Error during load tf model. Use fallback...");
        e.printStackTrace();
        initModelFallback();
        tfInterpreter = new Interpreter(tfliteModel, tfliteOptions);
    }
}
Also used : Interpreter(org.tensorflow.lite.Interpreter)

Example 2 with Interpreter

use of org.tensorflow.lite.Interpreter in project djl by deepjavalibrary.

the class TfLiteModel method load.

/**
 * {@inheritDoc}
 */
@Override
public void load(Path modelPath, String prefix, Map<String, ?> options) throws IOException {
    setModelDir(modelPath);
    if (block != null) {
        throw new UnsupportedOperationException("TFLite does not support dynamic blocks");
    }
    Path modelFile = findModelFile(prefix);
    if (modelFile == null) {
        modelFile = findModelFile(modelDir.toFile().getName());
        if (modelFile == null) {
            throw new FileNotFoundException("TFLite model file not found in: " + modelPath);
        }
    }
    Interpreter interpreter = new Interpreter(modelFile.toFile());
    setBlock(new TfLiteSymbolBlock(interpreter, getNDManager()));
}
Also used : Path(java.nio.file.Path) Interpreter(org.tensorflow.lite.Interpreter) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Interpreter

use of org.tensorflow.lite.Interpreter in project djl by deepjavalibrary.

the class TfLiteModel method load.

/**
 * {@inheritDoc}
 */
@Override
public void load(InputStream is, Map<String, ?> options) throws IOException {
    if (block != null) {
        throw new UnsupportedOperationException("TFLite does not support dynamic blocks");
    }
    modelDir = Files.createTempDirectory("tflite-model");
    modelDir.toFile().deleteOnExit();
    byte[] buf = Utils.toByteArray(is);
    Engine engine = Engine.getEngine(TfLiteEngine.ENGINE_NAME);
    ByteBuffer bb = engine.newBaseManager().allocateDirect(buf.length);
    bb.put(buf);
    Interpreter interpreter = new Interpreter(bb);
    setBlock(new TfLiteSymbolBlock(interpreter, getNDManager()));
}
Also used : Interpreter(org.tensorflow.lite.Interpreter) ByteBuffer(java.nio.ByteBuffer) Engine(ai.djl.engine.Engine)

Example 4 with Interpreter

use of org.tensorflow.lite.Interpreter in project amplify-android by aws-amplify.

the class TextClassificationModel method load.

/**
 * Loads the pre-trained text classification model into
 * TensorFlow Lite interpreter.
 */
@WorkerThread
@Override
public synchronized void load() {
    // No-op if loaded already
    if (loaded) {
        return;
    }
    try {
        ByteBuffer buffer = loadModelFile();
        interpreter = new Interpreter(buffer);
        if (onLoaded != null) {
            onLoaded.accept(interpreter);
        }
        loaded = true;
    } catch (PredictionsException exception) {
        if (onLoadError != null) {
            onLoadError.accept(exception);
        }
    }
}
Also used : Interpreter(org.tensorflow.lite.Interpreter) PredictionsException(com.amplifyframework.predictions.PredictionsException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

Interpreter (org.tensorflow.lite.Interpreter)4 ByteBuffer (java.nio.ByteBuffer)2 Engine (ai.djl.engine.Engine)1 WorkerThread (androidx.annotation.WorkerThread)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 FileNotFoundException (java.io.FileNotFoundException)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 Path (java.nio.file.Path)1