[Android/Test] check nnfw availability
authorJaeyun <jy1210.jung@samsung.com>
Fri, 20 Mar 2020 08:07:57 +0000 (17:07 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 25 Mar 2020 02:38:41 +0000 (11:38 +0900)
Before running the test, check nnfw (tf-lite or snap) is available and test can be run.

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 240df5b..ccdd5bc 100644 (file)
@@ -104,7 +104,7 @@ public class APITestCommon {
      * GPU: "custom=ModelFWType:CAFFE,ExecutionDataType:FLOAT32,ComputingUnit:GPU,GpuCacheSource:/sdcard/nnstreamer/"
      */
     public static String getSNAPCaffeOption(boolean useGPU) {
-        String option = "ModelFWType:CAFFE,ExecutionDataType:FLOAT32,";
+        String option = "ModelFWType:CAFFE,ExecutionDataType:FLOAT32,InputFormat:NHWC,OutputFormat:NCHW,";
 
         if (useGPU) {
             String root = Environment.getExternalStorageDirectory().getAbsolutePath();
@@ -167,10 +167,4 @@ public class APITestCommon {
         assertEquals(NNStreamer.NNFWType.SNAP, NNStreamer.NNFWType.valueOf("SNAP"));
         assertEquals(NNStreamer.NNFWType.UNKNOWN, NNStreamer.NNFWType.valueOf("UNKNOWN"));
     }
-
-    @Test
-    public void testAvailability() {
-        /* tensorflow-lite is always available */
-        assertTrue(NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE));
-    }
 }
index b61156d..470727f 100644 (file)
@@ -368,6 +368,11 @@ public class APITestPipeline {
 
     @Test
     public void testRunModel() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         File model = APITestCommon.getTFLiteImgModel();
         String desc = "appsrc name=srcx ! " +
                 "other/tensor,dimension=(string)3:224:224:1,type=(string)uint8,framerate=(fraction)0/1 ! " +
index e7a50cf..bae0b0a 100644 (file)
@@ -19,36 +19,31 @@ import static org.junit.Assert.*;
  */
 @RunWith(AndroidJUnit4.class)
 public class APITestSingleShot {
-    private SingleShot mSingle;
-
     @Rule
     public GrantPermissionRule mPermissionRule = APITestCommon.grantPermissions();
 
     @Before
     public void setUp() {
         APITestCommon.initNNStreamer();
-
-        try {
-            mSingle = new SingleShot(APITestCommon.getTFLiteImgModel());
-        } catch (Exception e) {
-            fail();
-        }
-    }
-
-    @After
-    public void tearDown() {
-        mSingle.close();
     }
 
     @Test
     public void testGetInputInfo() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            TensorsInfo info = mSingle.getInputInfo();
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+            TensorsInfo info = single.getInputInfo();
 
             /* input: uint8 3:224:224:1 */
             assertEquals(1, info.getTensorsCount());
             assertEquals(NNStreamer.TensorType.UINT8, info.getTensorType(0));
             assertArrayEquals(new int[]{3,224,224,1}, info.getTensorDimension(0));
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -56,13 +51,21 @@ public class APITestSingleShot {
 
     @Test
     public void testGetOutputInfo() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            TensorsInfo info = mSingle.getOutputInfo();
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+            TensorsInfo info = single.getOutputInfo();
 
             /* output: uint8 1001:1 */
             assertEquals(1, info.getTensorsCount());
             assertEquals(NNStreamer.TensorType.UINT8, info.getTensorType(0));
             assertArrayEquals(new int[]{1001,1,1,1}, info.getTensorDimension(0));
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -70,8 +73,14 @@ public class APITestSingleShot {
 
     @Test
     public void testSetNullInputInfo_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setInputInfo(null);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+            single.setInputInfo(null);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -80,9 +89,14 @@ public class APITestSingleShot {
 
     @Test
     public void testSetInputInfo() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            SingleShot addSingle = new SingleShot(APITestCommon.getTFLiteAddModel());
-            TensorsInfo info = addSingle.getInputInfo();
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteAddModel());
+            TensorsInfo info = single.getInputInfo();
 
             /* input: float32 with dimension 1 */
             assertEquals(1, info.getTensorsCount());
@@ -92,19 +106,21 @@ public class APITestSingleShot {
             TensorsInfo newInfo = new TensorsInfo();
             newInfo.addTensorInfo(NNStreamer.TensorType.FLOAT32, new int[]{10});
 
-            addSingle.setInputInfo(newInfo);
+            single.setInputInfo(newInfo);
 
-            info = addSingle.getInputInfo();
+            info = single.getInputInfo();
             /* input: float32 with dimension 10 */
             assertEquals(1, info.getTensorsCount());
             assertEquals(NNStreamer.TensorType.FLOAT32, info.getTensorType(0));
             assertArrayEquals(new int[]{10,1,1,1}, info.getTensorDimension(0));
 
-            info = addSingle.getOutputInfo();
+            info = single.getOutputInfo();
             /* output: float32 with dimension 10 */
             assertEquals(1, info.getTensorsCount());
             assertEquals(NNStreamer.TensorType.FLOAT32, info.getTensorType(0));
             assertArrayEquals(new int[]{10,1,1,1}, info.getTensorDimension(0));
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -112,16 +128,22 @@ public class APITestSingleShot {
 
     @Test
     public void testInvoke() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            TensorsInfo info = mSingle.getInputInfo();
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+            TensorsInfo info = single.getInputInfo();
 
             /* let's ignore timeout (set 10 sec) */
-            mSingle.setTimeout(10000);
+            single.setTimeout(10000);
 
             /* single-shot invoke */
             for (int i = 0; i < 600; i++) {
                 /* dummy input */
-                TensorsData out = mSingle.invoke(info.allocate());
+                TensorsData out = single.invoke(info.allocate());
 
                 /* output: uint8 1001:1 */
                 assertEquals(1, out.getTensorsCount());
@@ -129,6 +151,8 @@ public class APITestSingleShot {
 
                 Thread.sleep(30);
             }
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -136,8 +160,13 @@ public class APITestSingleShot {
 
     @Test
     public void testInvokeDynamicVary() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            SingleShot addSingle = new SingleShot(APITestCommon.getTFLiteAddModel());
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteAddModel());
 
             /* single-shot invoke */
             for (int i = 1; i < 2; i++) {
@@ -145,7 +174,7 @@ public class APITestSingleShot {
                 info.addTensorInfo(NNStreamer.TensorType.FLOAT32, new int[]{1,1,1,i});
 
                 /* dummy input */
-                TensorsData out = addSingle.invoke(TensorsData.allocate(info));
+                TensorsData out = single.invoke(TensorsData.allocate(info));
 
                 /* output: float32 1:1:1:i */
                 assertEquals(1, out.getTensorsCount());
@@ -153,6 +182,8 @@ public class APITestSingleShot {
 
                 Thread.sleep(30);
             }
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -160,14 +191,20 @@ public class APITestSingleShot {
 
     @Test
     public void testInvokeTimeout_n() {
-        TensorsInfo info = mSingle.getInputInfo();
-
-        /* timeout 5ms */
-        mSingle.setTimeout(5);
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
 
         try {
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+            TensorsInfo info = single.getInputInfo();
+
+            /* timeout 5ms (not enough time to invoke the model */
+            single.setTimeout(5);
+
             /* dummy input */
-            mSingle.invoke(TensorsData.allocate(info));
+            single.invoke(TensorsData.allocate(info));
             fail();
         } catch (Exception e) {
             /* expected */
@@ -209,6 +246,11 @@ public class APITestSingleShot {
 
     @Test
     public void testInvalidInputType_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         /* input: uint8 3:224:224:1 */
         TensorsInfo info = new TensorsInfo();
         info.addTensorInfo(NNStreamer.TensorType.UINT16, new int[]{3,224,224,1});
@@ -223,6 +265,11 @@ public class APITestSingleShot {
 
     @Test
     public void testInvalidInputDimension_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         /* input: uint8 3:224:224:1 */
         TensorsInfo info = new TensorsInfo();
         info.addTensorInfo(NNStreamer.TensorType.UINT8, new int[]{2,224,224});
@@ -237,6 +284,11 @@ public class APITestSingleShot {
 
     @Test
     public void testInvalidOutputType_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         /* output: uint8 1001:1 */
         TensorsInfo info = new TensorsInfo();
         info.addTensorInfo(NNStreamer.TensorType.INT16, new int[]{1001,1});
@@ -251,6 +303,11 @@ public class APITestSingleShot {
 
     @Test
     public void testInvalidOutputDimension_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         /* output: uint8 1001:1 */
         TensorsInfo info = new TensorsInfo();
         info.addTensorInfo(NNStreamer.TensorType.UINT8, new int[]{1001,2,1,1});
@@ -265,8 +322,15 @@ public class APITestSingleShot {
 
     @Test
     public void testInvokeNullData_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.invoke(null);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.invoke(null);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -275,12 +339,19 @@ public class APITestSingleShot {
 
     @Test
     public void testInvokeInvalidData_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         /* input data size: 3 * 224 * 224 */
         TensorsInfo info = new TensorsInfo();
         info.addTensorInfo(NNStreamer.TensorType.UINT8, new int[]{100});
 
         try {
-            mSingle.invoke(TensorsData.allocate(info));
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.invoke(TensorsData.allocate(info));
             fail();
         } catch (Exception e) {
             /* expected */
@@ -289,8 +360,15 @@ public class APITestSingleShot {
 
     @Test
     public void testSetZeroTimeout_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setTimeout(0);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.setTimeout(0);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -299,8 +377,15 @@ public class APITestSingleShot {
 
     @Test
     public void testSetInvalidTimeout_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setTimeout(-1);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.setTimeout(-1);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -309,8 +394,15 @@ public class APITestSingleShot {
 
     @Test
     public void testGetInvalidPropertyName_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.getValue("");
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.getValue("");
             fail();
         } catch (Exception e) {
             /* expected */
@@ -319,8 +411,15 @@ public class APITestSingleShot {
 
     @Test
     public void testGetUnknownPropertyName_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.getValue("unknown_prop");
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.getValue("unknown_prop");
             fail();
         } catch (Exception e) {
             /* expected */
@@ -329,8 +428,15 @@ public class APITestSingleShot {
 
     @Test
     public void testGetNullPropertyName_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.getValue(null);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.getValue(null);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -339,8 +445,15 @@ public class APITestSingleShot {
 
     @Test
     public void testUnknownPropertyName_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setValue("unknown_prop", "unknown");
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.setValue("unknown_prop", "unknown");
             fail();
         } catch (Exception e) {
             /* expected */
@@ -349,8 +462,15 @@ public class APITestSingleShot {
 
     @Test
     public void testSetNullPropertyName_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setValue(null, "ANY");
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.setValue(null, "ANY");
             fail();
         } catch (Exception e) {
             /* expected */
@@ -359,8 +479,15 @@ public class APITestSingleShot {
 
     @Test
     public void testSetNullPropertyValue_n() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            mSingle.setValue("inputlayout", null);
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            single.setValue("inputlayout", null);
             fail();
         } catch (Exception e) {
             /* expected */
@@ -369,9 +496,18 @@ public class APITestSingleShot {
 
     @Test
     public void testGetPropertyDimension() {
+        if (!NNStreamer.isAvailable(NNStreamer.NNFWType.TENSORFLOW_LITE)) {
+            /* cannot run the test */
+            return;
+        }
+
         try {
-            assertEquals("3:224:224:1", mSingle.getValue("input"));
-            assertEquals("1001:1:1:1", mSingle.getValue("output"));
+            SingleShot single = new SingleShot(APITestCommon.getTFLiteImgModel());
+
+            assertEquals("3:224:224:1", single.getValue("input"));
+            assertEquals("1001:1:1:1", single.getValue("output"));
+
+            single.close();
         } catch (Exception e) {
             fail();
         }
@@ -396,10 +532,6 @@ public class APITestSingleShot {
             /* let's ignore timeout (set 60 sec) */
             single.setTimeout(60000);
 
-            /* set layout */
-            single.setValue("inputlayout", "NHWC");
-            single.setValue("outputlayout", "NCHW");
-
             /* single-shot invoke */
             for (int i = 0; i < 10; i++) {
                 /* dummy input */