├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── libs │ ├── armeabi-v7a │ │ └── libtensorflow_inference.so │ └── libandroid_tensorflow_inference_java.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── wangchenlong │ │ └── tfandroid │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── optimized_tfdroid.pb │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── wangchenlong │ │ │ └── tfandroid │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── wangchenlong │ └── tfandroid │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TFAndroid 2 | 3 | TensorFlow集成Android工程的框架 4 | 5 | 框架细节,[参考](http://www.jianshu.com/p/870e9a54749a) 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "26.0.1" 6 | defaultConfig { 7 | applicationId "com.example.wangchenlong.tfandroid" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | sourceSets { 21 | main { 22 | jniLibs.srcDirs = ['libs'] 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(include: ['*.jar'], dir: 'libs') 29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 30 | exclude group: 'com.android.support', module: 'support-annotations' 31 | }) 32 | compile 'com.android.support:appcompat-v7:25.3.1' 33 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 34 | testCompile 'junit:junit:4.12' 35 | } 36 | -------------------------------------------------------------------------------- /app/libs/armeabi-v7a/libtensorflow_inference.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpikeKing/TFAndroid/ec9f02f8aef83d7d3ac2ce801699b9fa7173e59b/app/libs/armeabi-v7a/libtensorflow_inference.so -------------------------------------------------------------------------------- /app/libs/libandroid_tensorflow_inference_java.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpikeKing/TFAndroid/ec9f02f8aef83d7d3ac2ce801699b9fa7173e59b/app/libs/libandroid_tensorflow_inference_java.jar -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wangchenlong/Installations/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/wangchenlong/tfandroid/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.wangchenlong.tfandroid; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.wangchenlong.tfandroid", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/assets/optimized_tfdroid.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpikeKing/TFAndroid/ec9f02f8aef83d7d3ac2ce801699b9fa7173e59b/app/src/main/assets/optimized_tfdroid.pb -------------------------------------------------------------------------------- /app/src/main/java/com/example/wangchenlong/tfandroid/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.wangchenlong.tfandroid; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.EditText; 8 | import android.widget.TextView; 9 | 10 | import org.tensorflow.contrib.android.TensorFlowInferenceInterface; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private static final String MODEL_FILE = "file:///android_asset/optimized_tfdroid.pb"; 15 | private static final String INPUT_NODE = "I"; 16 | private static final String OUTPUT_NODE = "O"; 17 | 18 | private static final int[] INPUT_SIZE = {1, 3}; 19 | 20 | private TensorFlowInferenceInterface mInferenceInterface; 21 | 22 | static { 23 | System.loadLibrary("tensorflow_inference"); 24 | } 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | 31 | mInferenceInterface = new TensorFlowInferenceInterface(); 32 | mInferenceInterface.initializeTensorFlow(getAssets(), MODEL_FILE); 33 | 34 | 35 | final Button button = (Button) findViewById(R.id.button); 36 | 37 | button.setOnClickListener(new View.OnClickListener() { 38 | public void onClick(View v) { 39 | 40 | final EditText editNum1 = (EditText) findViewById(R.id.editNum1); 41 | final EditText editNum2 = (EditText) findViewById(R.id.editNum2); 42 | final EditText editNum3 = (EditText) findViewById(R.id.editNum3); 43 | 44 | float num1 = Float.parseFloat(editNum1.getText().toString()); 45 | float num2 = Float.parseFloat(editNum2.getText().toString()); 46 | float num3 = Float.parseFloat(editNum3.getText().toString()); 47 | 48 | float[] inputFloats = {num1, num2, num3}; 49 | mInferenceInterface.fillNodeFloat(INPUT_NODE, INPUT_SIZE, inputFloats); 50 | 51 | mInferenceInterface.runInference(new String[]{OUTPUT_NODE}); 52 | 53 | float[] resu = {0, 0}; 54 | mInferenceInterface.readNodeFloat(OUTPUT_NODE, resu); 55 | 56 | final TextView textViewR = (TextView) findViewById(R.id.txtViewResult); 57 | textViewR.setText(Float.toString(resu[0]) + ", " + Float.toString(resu[1])); 58 | } 59 | }); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 20 | 21 | 31 | 32 |