[Android/Test] add simple test for NNFW
authorJaeyun <jy1210.jung@samsung.com>
Wed, 8 Apr 2020 11:36:07 +0000 (20:36 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Fri, 10 Apr 2020 05:16:14 +0000 (14:16 +0900)
Both single-shot and pipeline, add simple test to validate NNFW model.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCommon.java
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestPipeline.java
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestSingleShot.java

index 9f4fd54..7364a00 100644 (file)
@@ -129,8 +129,9 @@ public class APITestCommon {
     public static File getTFLiteAddModel() {
         String root = Environment.getExternalStorageDirectory().getAbsolutePath();
         File model = new File(root + "/nnstreamer/test/add.tflite");
+        File meta = new File(root + "/nnstreamer/test/metadata/MANIFEST");
 
-        if (!model.exists()) {
+        if (!model.exists() || !meta.exists()) {
             fail();
         }
 
@@ -222,6 +223,7 @@ public class APITestCommon {
     public void enumNNFWType() {
         assertEquals(NNStreamer.NNFWType.TENSORFLOW_LITE, NNStreamer.NNFWType.valueOf("TENSORFLOW_LITE"));
         assertEquals(NNStreamer.NNFWType.SNAP, NNStreamer.NNFWType.valueOf("SNAP"));
+        assertEquals(NNStreamer.NNFWType.NNFW, NNStreamer.NNFWType.valueOf("NNFW"));
         assertEquals(NNStreamer.NNFWType.UNKNOWN, NNStreamer.NNFWType.valueOf("UNKNOWN"));
     }
 }
index be0532c..f912a6e 100644 (file)
@@ -1209,4 +1209,66 @@ public class APITestPipeline {
 
         runSNAPCaffe(true);
     }
+
+    @Test
+    public void testNNFWTFLite() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.NNFW)) {
+            /* cannot run the test */
+            return;
+        }
+
+        File model = APITestCommon.getTFLiteAddModel();
+        String desc = "appsrc name=srcx ! " +
+                "other/tensor,dimension=(string)1:1:1:1,type=(string)float32,framerate=(fraction)0/1 ! " +
+                "tensor_filter framework=nnfw model=" + model.getAbsolutePath() + " ! " +
+                "tensor_sink name=sinkx";
+
+        try (Pipeline pipe = new Pipeline(desc)) {
+            TensorsInfo info = new TensorsInfo();
+            info.addTensorInfo(NNStreamer.TensorType.FLOAT32, new int[]{1,1,1,1});
+
+            /* register sink callback */
+            pipe.registerSinkCallback("sinkx", new Pipeline.NewDataCallback() {
+                @Override
+                public void onNewDataReceived(TensorsData data) {
+                    if (data == null || data.getTensorsCount() != 1) {
+                        mInvalidState = true;
+                        return;
+                    }
+
+                    ByteBuffer buffer = data.getTensorData(0);
+                    float expected = buffer.getFloat(0);
+
+                    /* check received data */
+                    if (expected != 3.5f) {
+                        mInvalidState = true;
+                    }
+
+                    mReceived++;
+                }
+            });
+
+            /* start pipeline */
+            pipe.start();
+
+            /* push input buffer */
+            TensorsData input = info.allocate();
+
+            ByteBuffer buffer = input.getTensorData(0);
+            buffer.putFloat(0, 1.5f);
+
+            input.setTensorData(0, buffer);
+
+            pipe.inputData("srcx", input);
+
+            /* sleep 1000 to invoke */
+            Thread.sleep(1000);
+
+            /* check received data from sink */
+            assertFalse(mInvalidState);
+            assertTrue(mReceived > 0);
+        } catch (Exception e) {
+            fail();
+        }
+    }
 }
index 101a1e7..1aa5416 100644 (file)
@@ -10,6 +10,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import java.io.File;
+import java.nio.ByteBuffer;
 
 import static org.junit.Assert.*;
 
@@ -649,4 +650,46 @@ public class APITestSingleShot {
 
         runSNAPCaffe(true);
     }
+
+    @Test
+    public void testNNFWTFLite() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.NNFW)) {
+            /* cannot run the test */
+            return;
+        }
+
+        try {
+            File model = APITestCommon.getTFLiteAddModel();
+
+            SingleShot single = new SingleShot(model, NNStreamer.NNFWType.NNFW);
+            TensorsInfo in = single.getInputInfo();
+
+            /* let's ignore timeout (set 60 sec) */
+            single.setTimeout(60000);
+
+            /* single-shot invoke */
+            for (int i = 0; i < 5; i++) {
+                /* input data */
+                TensorsData input = in.allocate();
+
+                ByteBuffer buffer = input.getTensorData(0);
+                buffer.putFloat(0, i + 1.5f);
+
+                input.setTensorData(0, buffer);
+
+                /* invoke */
+                TensorsData output = single.invoke(input);
+
+                /* check output */
+                float expected = i + 3.5f;
+                assertTrue(expected == output.getTensorData(0).getFloat(0));
+
+                Thread.sleep(30);
+            }
+
+            single.close();
+        } catch (Exception e) {
+            fail();
+        }
+    }
 }