[Android] add method about property
authorJaeyun <jy1210.jung@samsung.com>
Fri, 7 Feb 2020 11:09:03 +0000 (20:09 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 11 Feb 2020 05:58:32 +0000 (21:58 -0800)
1. add new method to set/get the property in single-shot.
2. add/modify testcases for snap and property value.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestSNAP.java
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestSingleShot.java
api/android/api/src/main/java/org/nnsuite/nnstreamer/SingleShot.java
api/android/api/src/main/jni/nnstreamer-native-singleshot.c

index 9c943f6..e3623e6 100644 (file)
@@ -78,6 +78,10 @@ public class APITestSNAP {
             /* let's ignore timeout (set 60 sec) */
             single.setTimeout(60000);
 
+            /* set layout */
+            single.setProperty("inputlayout", "NHWC");
+            single.setProperty("outputlayout", "NCHW");
+
             /* single-shot invoke */
             for (int i = 0; i < 10; i++) {
                 /* dummy input */
@@ -105,8 +109,8 @@ public class APITestSNAP {
                 "other/tensor,dimension=(string)3:224:224:1,type=(string)float32,framerate=(fraction)0/1 ! " +
                 "tensor_filter framework=snap " +
                     "model=" + models[0].getAbsolutePath() + "," + models[1].getAbsolutePath() + " " +
-                    "input=3:224:224:1 inputtype=float32 inputname=data " +
-                    "output=1:1:1000:1 outputtype=float32 outputname=prob " +
+                    "input=3:224:224:1 inputtype=float32 inputlayout=NHWC inputname=data " +
+                    "output=1:1:1000:1 outputtype=float32 outputlayout=NCHW outputname=prob " +
                     "custom=" + option + " ! " +
                 "tensor_sink name=sinkx";
 
index 83e7e0f..6308657 100644 (file)
@@ -320,4 +320,74 @@ public class APITestSingleShot {
             /* expected */
         }
     }
+
+    @Test
+    public void testGetInvalidPropertyName_n() {
+        try {
+            mSingle.getProperty("");
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testGetUnknownPropertyName_n() {
+        try {
+            mSingle.getProperty("unknown_prop");
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testGetNullPropertyName_n() {
+        try {
+            mSingle.getProperty(null);
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testUnknownPropertyName_n() {
+        try {
+            mSingle.setProperty("unknown_prop", "unknown");
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testSetNullPropertyName_n() {
+        try {
+            mSingle.setProperty(null, "ANY");
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testSetNullPropertyValue_n() {
+        try {
+            mSingle.setProperty("inputlayout", null);
+            fail();
+        } catch (Exception e) {
+            /* expected */
+        }
+    }
+
+    @Test
+    public void testGetPropertyDimension() {
+        try {
+            assertEquals("3:224:224:1", mSingle.getProperty("input"));
+            assertEquals("1001:1:1:1", mSingle.getProperty("output"));
+        } catch (Exception e) {
+            fail();
+        }
+    }
 }
index a1ce5b1..29dd6da 100644 (file)
@@ -39,6 +39,8 @@ public final class SingleShot implements AutoCloseable {
     private native TensorsData nativeInvoke(long handle, TensorsData in);
     private native TensorsInfo nativeGetInputInfo(long handle);
     private native TensorsInfo nativeGetOutputInfo(long handle);
+    private native boolean nativeSetProperty(long handle, String name, String value);
+    private native String nativeGetProperty(long handle, String name);
     private native boolean nativeSetInputInfo(long handle, TensorsInfo in);
     private native boolean nativeSetTimeout(long handle, int timeout);
 
@@ -187,6 +189,52 @@ public final class SingleShot implements AutoCloseable {
     }
 
     /**
+     * Sets the property value for the given model.
+     * Note that a model/framework may not support to change the property after opening the model.
+     *
+     * @param name  The property name
+     * @param value The property value
+     *
+     * @throws IllegalArgumentException if given param is invalid
+     */
+    public void setProperty(@NonNull String name, @NonNull String value) {
+        if (name == null || name.isEmpty()) {
+            throw new IllegalArgumentException("Given property name is invalid");
+        }
+
+        if (value == null || value.isEmpty()) {
+            throw new IllegalArgumentException("Given property value is invalid");
+        }
+
+        if (!nativeSetProperty(mHandle, name, value)) {
+            throw new IllegalArgumentException("Failed to set the property");
+        }
+    }
+
+    /**
+     * Gets the property value for the given model.
+     *
+     * @param name The property name
+     *
+     * @return The property value
+     *
+     * @throws IllegalArgumentException if given param is invalid
+     */
+    public String getProperty(@NonNull String name) {
+        if (name == null || name.isEmpty()) {
+            throw new IllegalArgumentException("Given property name is invalid");
+        }
+
+        String value = nativeGetProperty(mHandle, name);
+
+        if (value == null || value.isEmpty()) {
+            throw new IllegalArgumentException("Failed to get the property");
+        }
+
+        return value;
+    }
+
+    /**
      * Sets the maximum amount of time to wait for an output, in milliseconds.
      *
      * @param timeout The time to wait for an output
index 05326cd..c7dcba1 100644 (file)
@@ -259,6 +259,62 @@ done:
  * @brief Native method for single-shot API.
  */
 jboolean
+Java_org_nnsuite_nnstreamer_SingleShot_nativeSetProperty (JNIEnv * env,
+    jobject thiz, jlong handle, jstring name, jstring value)
+{
+  pipeline_info_s *pipe_info;
+  ml_single_h single;
+  jboolean ret = JNI_FALSE;
+
+  const char *prop_name = (*env)->GetStringUTFChars (env, name, NULL);
+  const char *prop_value = (*env)->GetStringUTFChars (env, value, NULL);
+
+  pipe_info = CAST_TO_TYPE (handle, pipeline_info_s*);
+  single = pipe_info->pipeline_handle;
+
+  if (ml_single_set_property (single, prop_name, prop_value) == ML_ERROR_NONE) {
+    ret = JNI_TRUE;
+  } else {
+    nns_loge ("Failed to set the property (%s:%s).", prop_name, prop_value);
+  }
+
+  (*env)->ReleaseStringUTFChars (env, name, prop_name);
+  (*env)->ReleaseStringUTFChars (env, name, prop_value);
+  return ret;
+}
+
+/**
+ * @brief Native method for single-shot API.
+ */
+jstring
+Java_org_nnsuite_nnstreamer_SingleShot_nativeGetProperty (JNIEnv * env,
+    jobject thiz, jlong handle, jstring name)
+{
+  pipeline_info_s *pipe_info;
+  ml_single_h single;
+
+  const char *prop_name = (*env)->GetStringUTFChars (env, name, NULL);
+  char *prop_value = NULL;
+  jstring value = NULL;
+
+  pipe_info = CAST_TO_TYPE (handle, pipeline_info_s*);
+  single = pipe_info->pipeline_handle;
+
+  if (ml_single_get_property (single, prop_name, &prop_value) == ML_ERROR_NONE) {
+    value = (*env)->NewStringUTF (env, prop_value);
+    g_free (prop_value);
+  } else {
+    nns_loge ("Failed to get the property (%s).", prop_name);
+  }
+
+  (*env)->ReleaseStringUTFChars (env, name, prop_name);
+  return value;
+}
+
+/**
+ * @brief Native method for single-shot API.
+ */
+jboolean
 Java_org_nnsuite_nnstreamer_SingleShot_nativeSetTimeout (JNIEnv * env,
     jobject thiz, jlong handle, jint timeout)
 {