deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.05k stars 648 forks source link

Can 't correctly initialize the `TfEngine` #538

Closed Noricks closed 3 years ago

Noricks commented 3 years ago

General Information of the Questions

First of all, thank you for reading my post.

Sorry for the long post. For me, the problem is hard to identify. So I post all the related information which I think is useful.

System Info

Target Platform:

Android Virtual Device
    Name: Pixel_4_XL_API_29 
    CPU/ABI: Google APIs Intel Atom (x86_64) 
    Target: google_apis [Google APIs] (API level 29)

Developing Platform:

Android Studio 4.4.1/2020.3
Windows 10 20H2

build.grandle (Build Script):

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.testdjl2"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    // DJL TensorFlow
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.0'
    api "ai.djl:api:0.9.0"
    // implementation "ai.djl:api:0.9.0"
    implementation "ai.djl.tensorflow:tensorflow-engine:0.9.0"
    implementation "ai.djl.tensorflow:tensorflow-native-auto:2.3.1"
}

Related Code


File file = context.getFileStreamPath("model.zip"); // file has been copied to the path
Path modelPath = file.getAbsoluteFile().toPath();
File cacheDir = context.getCacheDir();
System.setProperty("DJL_CACHE_DIR", cacheDir.toString());
new Thread(() -> {
    Criteria<NDArray, NDArray> criteria = null;
    try {
        criteria = Criteria.builder()
                .setTypes(NDArray.class, NDArray.class)
                .optModelPath(modelPath)
                .build();

        ZooModel<NDArray, NDArray> model = ModelZoo.loadModel(criteria); // The line exception occurs
    } catch (Exception e) {
        e.printStackTrace();
    }
}).start();

General exception

The line exception occurs:

ZooModel<NDArray, NDArray> model = ModelZoo.loadModel(criteria);

Exception:

W/System.err: ai.djl.repository.zoo.ModelNotFoundException: No matching model with specified Input/Output type found.
        at ai.djl.repository.zoo.ModelZoo.loadModel(ModelZoo.java:181)
        at com.example.testdjl2.DJLTest.lambda$new$0(DJLTest.java:119)
        at com.example.testdjl2.-$$Lambda$DJLTest$aiQGMpZBnacmoD1wbS3zej5VMtQ.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:919)
    Caused by: ai.djl.repository.zoo.ModelNotFoundException: No matching default translator found
        at ai.djl.repository.zoo.BaseModelLoader.loadModel(BaseModelLoader.java:97)
        at ai.djl.repository.zoo.ModelZoo.loadModel(ModelZoo.java:169)
        ... 3 more

This exception is highly related to file: ai.djl.tensorflow.engine.TfEngine Related Code:

    private TfEngine() {}

    static TfEngine newInstance() {
        try {
            LibUtils.loadLibrary();
            EagerSession.getDefault();

            return new TfEngine();
        } catch (Throwable t) {
            throw new EngineException("Failed to load TensorFlow native library", t);
        }
    }

There are two questions behind this exception:

Question 1:

The question is related to LibUtils.loadLibrary();.

Is the program have to download the TensorFlow binary through LibUtils.downloadTensorFlow(Platform platform) at first run?

If the problem is caused by my in-appropriate build script, could you help me to fix it?

Question 2:

The question is related to EagerSession.getDefault();

After downloading the TensorFlow binary through LibUtils.downloadTensorFlow(Platform platform), an exception will occur when the program tries to initialize the TensorFlow.

The decompiler shows that the exception is thrown by TensorFlow.init() in Tensorflow.class.

frankfliu commented 3 years ago

For android project, DJL currently only support pytorch engine. There is some effort to support MXNet for android, but we don't have a concrete plan yet.

lanking520 commented 3 years ago

@Noricks Close the issue for now. If you would like to have TFLite Android support, please feel free to open another issue. Note: We now have TFLite for Server, but not for Android. But we are open to the community if anyone is looking for a build.