pytorch / android-demo-app

PyTorch android examples of usage in applications
1.47k stars 605 forks source link

NativeApp #100

Open ichernob opened 3 years ago

ichernob commented 3 years ago

It even doesn't compile.

I've copied code from tutorial first - no result I've copied code from NativeApp - no result.

For some reason torch/script.h could not be determined despite the fact that pytorch_android aar downloaded and extracted and cmake build completed! my CMakeLists.txt `cmake_minimum_required(VERSION 3.10.2) set(TARGET pytorchnative) project(${TARGET} CXX) set(CMAKE_CXX_STANDARD 14) set(build_DIR ${CMAKE_SOURCE_DIR}/build) set(pytorch_testapp_cpp_DIR ${CMAKE_CURRENT_LIST_DIR}/src/main/cpp) set(BUILD_SUBDIR ${ANDROID_ABI})

file(GLOB pytorch_testapp_SOURCES ${pytorch_testapp_cpp_DIR}/pytorchnative.cpp) add_library(${TARGET} SHARED ${pytorch_testapp_SOURCES})

file(GLOB PYTORCH_INCLUDE_DIRS "${build_DIR}/pytorch_android.aar/headers") file(GLOB PYTORCH_LINK_DIRS "${build_DIR}/pytorch_android.aar/jni/${ANDROID_ABI}")

target_compile_options(${TARGET} PRIVATE -fexceptions)

find_library(PYTORCH_LIBRARY pytorch_jni PATHS ${PYTORCH_LINK_DIRS} NO_CMAKE_FIND_ROOT_PATH) find_library(FBJNI_LIBRARY fbjni PATHS ${PYTORCH_LINK_DIRS} NO_CMAKE_FIND_ROOT_PATH)

find_library(log-lib log)

target_link_libraries(${TARGET} ${PYTORCH_LIBRARY} ${FBJNI_LIBRARY} ${log-lib})`

my app.build.gradle: `plugins { id 'com.android.application' id 'kotlin-android' }

repositories { jcenter() maven { url "https://oss.sonatype.org/content/repositories/snapshots" } flatDir { dirs 'aars' } }

android { configurations { extractForNativeBuild } compileSdkVersion 30 buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.harman.pytorchnative"
    minSdkVersion 28
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {

// cppFlags "-std=c++14" arguments "-DANDROID_STL=c++_shared" } } ndk { abiFilters 'arm64-v8a' } }

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
        version "3.10.2"
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

implementation 'org.pytorch:pytorch_android:1.6.0-SNAPSHOT'

extractForNativeBuild 'org.pytorch:pytorch_android:1.6.0-SNAPSHOT'

}

task extractAARForNativeBuild { doLast { configurations.extractForNativeBuild.files.each { def file = it.absoluteFile copy { from zipTree(file) into "$buildDir/$file.name" include "headers/" include "jni/" } } } }

tasks.whenTaskAdded { task -> if (task.name.contains('externalNativeBuild')) { task.dependsOn(extractAARForNativeBuild) } }`

my pytorchnative.cpp: `#include <android/log.h>

include

include

include

include

include

define ALOGI(...) \

android_log_print(ANDROID_LOG_INFO, "PyTorchNativeApp", VA_ARGS__)

define ALOGE(...) \

android_log_print(ANDROID_LOG_ERROR, "PyTorchNativeApp", VA_ARGS__)

include "jni.h"

//#include <opencv2/opencv.hpp>

include <torch/script.h>

namespace pytorchnative { namespace { // torch::Tensor warp_perspective(torch::Tensor image, torch::Tensor warp) { // cv::Mat image_mat(/rows=/image.size(0), // /cols=/image.size(1), // /type=/CV_32FC1, // /data=/image.data_ptr()); // cv::Mat warp_mat(/rows=/warp.size(0), // /cols=/warp.size(1), // /type=/CV_32FC1, // /data=/warp.data_ptr()); // // cv::Mat output_mat; // cv::warpPerspective(image_mat, output_mat, warp_mat, /dsize=/{8, 8}); // // torch::Tensor output = // torch::from_blob(output_mat.ptr(), /sizes=/{8, 8}); // return output.clone(); // } // // static auto registry = // torch::RegisterOperators("my_ops::warp_perspective", &warp_perspective);

    template <typename T> void log(const char *m, T t) {
        std::ostringstream os;
        os << t << std::endl;
        ALOGI("%s %s", m, os.str().c_str());
    }

    struct JITCallGuard {
        torch::autograd::AutoGradMode no_autograd_guard{false};
        torch::AutoNonVariableTypeMode non_var_guard{true};
        torch::jit::GraphOptimizerEnabledGuard no_optimizer_guard{false};
    };
} // namespace

static void loadAndForwardModel(JNIEnv *env, jclass, jstring jModelPath) {
    const char *modelPath = env->GetStringUTFChars(jModelPath, 0);
    assert(modelPath);

    // To load torchscript model for mobile we need set these guards,
    // because mobile build doesn't support features like autograd for smaller
    // build size which is placed in `struct JITCallGuard` in this example. It may
    // change in future, you can track the latest changes keeping an eye in
    // android/pytorch_android/src/main/cpp/pytorch_jni_jit.cpp
    JITCallGuard guard;
    torch::jit::Module module = torch::jit::load(modelPath);
    module.eval();
    torch::Tensor x = torch::randn({4, 8});
    torch::Tensor y = torch::randn({8, 5});
    log("x:", x);
    log("y:", y);
    c10::IValue t_out = module.forward({x, y});
    log("result:", t_out);
    env->ReleaseStringUTFChars(jModelPath, modelPath);
}

} // namespace pytorch_nativeapp

//extern "C" JNIEXPORT jstring JNICALL // Java_com_harman_pytorchnative_MainActivity_stringFromJNI

JNIEXPORT jint JNI_OnLoad(JavaVM vm, void ) { JNIEnv *env; if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) { return JNI_ERR; } jclass c = env->FindClass("com/harman/pytorchnative/NativeClient$NativePeer"); if (c == nullptr) { return JNI_ERR; }

static const JNINativeMethod methods[] = { {"loadAndForwardModel", "(Ljava/lang/String;)V", (void *)pytorchnative::loadAndForwardModel}, }; int rc = env->RegisterNatives(c, methods, sizeof(methods) / sizeof(JNINativeMethod));

if (rc != JNI_OK) { return rc; }

return JNI_VERSION_1_6; }`

It stops with error: PytorchNative\app\src\main\cpp\pytorchnative.cpp:15:10: fatal error: 'torch/script.h' file not found However I do have extracted aar under build directory with necessary files: build\pytorch_android-1.6.0-SNAPSHOT.aar\headers\torch\script.h

How to made it visible? Has that tutorial ever worked????

ichernob commented 3 years ago

UPD. I even attempted to clone the whole repository and build NativeApp.

android-demo-app\NativeApp\app\CMakeLists.txt : C/C++ debug|armeabi-v7a : CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: FBJNI_LIBRARY linked by target "pytorch_nativeapp" in directory android-demo-app/NativeApp/app PYTORCH_LIBRARY linked by target "pytorch_nativeapp" in directory android-demo-app/NativeApp/app

kimishpatel commented 3 years ago

cc: @jeffxtang @IvanKobzarev

xdinos-rkk commented 2 years ago

any news on that? I'm having the same issue with the CMake error mentioned above