/tests/nnstreamer_filter_armnn/unittest_filter_armnn.cc

https://github.com/nnstreamer/nnstreamer · C++ · 717 lines · 499 code · 132 blank · 86 comment · 9 complexity · cc33c07bca88d0205da7eb6d35315e1f MD5 · raw file

  1. /**
  2. * @file unittest_filter_armnn.cc
  3. * @date 13 Dec 2019
  4. * @brief Unit test for armnn tensor filter plugin.
  5. * @see https://github.com/nnstreamer/nnstreamer
  6. * @author Parichay Kapoor <pk.kapoor@samsung.com>
  7. * @bug No known bugs
  8. */
  9. #include <gtest/gtest.h>
  10. #include <fcntl.h>
  11. #include <glib.h>
  12. #include <glib/gstdio.h> /* GStatBuf */
  13. #include <sys/stat.h>
  14. #include <nnstreamer_plugin_api.h>
  15. #include <nnstreamer_plugin_api_filter.h>
  16. /**
  17. * @brief Set tensor filter properties
  18. */
  19. static void
  20. _SetFilterProp (GstTensorFilterProperties *prop, const gchar *name, const gchar **models)
  21. {
  22. memset (prop, 0, sizeof (GstTensorFilterProperties));
  23. prop->fwname = name;
  24. prop->fw_opened = 0;
  25. prop->model_files = models;
  26. prop->num_models = g_strv_length ((gchar **)models);
  27. }
  28. /**
  29. * @brief Test armnn subplugin existence.
  30. */
  31. TEST (nnstreamerFilterArmnn, checkExistence)
  32. {
  33. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  34. EXPECT_NE (sp, (void *)NULL);
  35. }
  36. /**
  37. * @brief Test armnn subplugin with failing open/close (no model file)
  38. */
  39. TEST (nnstreamerFilterArmnn, openClose00_n)
  40. {
  41. int ret;
  42. const gchar *model_files[] = {
  43. "temp.armnn", NULL,
  44. };
  45. GstTensorFilterProperties prop;
  46. void *data = NULL;
  47. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  48. EXPECT_NE (sp, (void *)NULL);
  49. _SetFilterProp (&prop, "armnn", model_files);
  50. ret = sp->open (&prop, &data);
  51. EXPECT_NE (ret, 0);
  52. }
  53. /**
  54. * @brief Test armnn subplugin with successful open/close
  55. */
  56. TEST (nnstreamerFilterArmnn, openClose01_n)
  57. {
  58. int ret;
  59. void *data = NULL;
  60. gchar *model_file;
  61. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  62. GstTensorFilterProperties prop;
  63. ASSERT_NE (root_path, nullptr);
  64. model_file = g_build_filename (
  65. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  66. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  67. const gchar *model_files[] = {
  68. model_file, NULL,
  69. };
  70. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  71. EXPECT_NE (sp, (void *)NULL);
  72. _SetFilterProp (&prop, "armnn", model_files);
  73. /** close without open, should not crash */
  74. sp->close (&prop, &data);
  75. /** open and close successfully */
  76. ret = sp->open (&prop, &data);
  77. EXPECT_EQ (ret, 0);
  78. sp->close (&prop, &data);
  79. /** close twice, should not crash */
  80. sp->close (&prop, &data);
  81. g_free (model_file);
  82. }
  83. /**
  84. * @brief Get input/output dimensions with armnn subplugin
  85. */
  86. TEST (nnstreamerFilterArmnn, getDimension)
  87. {
  88. int ret;
  89. void *data = NULL;
  90. gchar *model_file;
  91. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  92. GstTensorsInfo info, res;
  93. GstTensorFilterProperties prop;
  94. ASSERT_NE (root_path, nullptr);
  95. /** armnn needs a directory with model file and metadata in that directory */
  96. model_file = g_build_filename (
  97. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  98. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  99. const gchar *model_files[] = {
  100. model_file, NULL,
  101. };
  102. _SetFilterProp (&prop, "armnn", model_files);
  103. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  104. EXPECT_NE (sp, (void *)NULL);
  105. ret = sp->open (&prop, &data);
  106. EXPECT_EQ (ret, 0);
  107. info.num_tensors = 1;
  108. info.info[0].type = _NNS_FLOAT32;
  109. info.info[0].dimension[0] = 1;
  110. info.info[0].dimension[1] = 1;
  111. info.info[0].dimension[2] = 1;
  112. info.info[0].dimension[3] = 1;
  113. /** get input/output dimension successfully */
  114. ret = sp->getInputDimension (&prop, &data, NULL);
  115. EXPECT_NE (ret, 0);
  116. ret = sp->getInputDimension (&prop, &data, &res);
  117. EXPECT_EQ (ret, 0);
  118. EXPECT_EQ (res.num_tensors, info.num_tensors);
  119. EXPECT_EQ (res.info[0].type, info.info[0].type);
  120. EXPECT_EQ (res.info[0].dimension[0], info.info[0].dimension[0]);
  121. EXPECT_EQ (res.info[0].dimension[1], info.info[0].dimension[1]);
  122. EXPECT_EQ (res.info[0].dimension[2], info.info[0].dimension[2]);
  123. EXPECT_EQ (res.info[0].dimension[3], info.info[0].dimension[3]);
  124. ret = sp->getOutputDimension (&prop, &data, NULL);
  125. EXPECT_NE (ret, 0);
  126. ret = sp->getOutputDimension (&prop, &data, &res);
  127. EXPECT_EQ (ret, 0);
  128. EXPECT_EQ (res.num_tensors, info.num_tensors);
  129. EXPECT_EQ (res.info[0].type, info.info[0].type);
  130. EXPECT_EQ (res.info[0].dimension[0], info.info[0].dimension[0]);
  131. EXPECT_EQ (res.info[0].dimension[1], info.info[0].dimension[1]);
  132. EXPECT_EQ (res.info[0].dimension[2], info.info[0].dimension[2]);
  133. EXPECT_EQ (res.info[0].dimension[3], info.info[0].dimension[3]);
  134. sp->close (&prop, &data);
  135. g_free (model_file);
  136. }
  137. /**
  138. * @brief Get input dimensions before open
  139. */
  140. TEST (nnstreamerFilterArmnn, getDimension1_n)
  141. {
  142. int ret;
  143. void *data = NULL;
  144. gchar *model_file;
  145. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  146. GstTensorsInfo res;
  147. GstTensorFilterProperties prop;
  148. ASSERT_NE (root_path, nullptr);
  149. /** armnn needs a directory with model file and metadata in that directory */
  150. model_file = g_build_filename (
  151. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  152. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  153. const gchar *model_files[] = {
  154. model_file, NULL,
  155. };
  156. _SetFilterProp (&prop, "armnn", model_files);
  157. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  158. EXPECT_NE (sp, (void *)NULL);
  159. /** get input dimension without open */
  160. ret = sp->getInputDimension (&prop, &data, &res);
  161. EXPECT_NE (ret, 0);
  162. g_free (model_file);
  163. }
  164. /**
  165. * @brief Get output dimensions before open
  166. */
  167. TEST (nnstreamerFilterArmnn, getDimension2_n)
  168. {
  169. int ret;
  170. void *data = NULL;
  171. gchar *model_file;
  172. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  173. GstTensorsInfo res;
  174. GstTensorFilterProperties prop;
  175. ASSERT_NE (root_path, nullptr);
  176. /** armnn needs a directory with model file and metadata in that directory */
  177. model_file = g_build_filename (
  178. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  179. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  180. const gchar *model_files[] = {
  181. model_file, NULL,
  182. };
  183. _SetFilterProp (&prop, "armnn", model_files);
  184. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  185. EXPECT_NE (sp, (void *)NULL);
  186. /** get output dimension without open */
  187. ret = sp->getOutputDimension (&prop, &data, &res);
  188. EXPECT_NE (ret, 0);
  189. g_free (model_file);
  190. }
  191. /**
  192. * @brief Test armnn subplugin with successful invoke for tflite
  193. */
  194. TEST (nnstreamerFilterArmnn, invoke00)
  195. {
  196. int ret;
  197. void *data = NULL;
  198. gchar *model_file;
  199. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  200. GstTensorMemory input, output;
  201. GstTensorFilterProperties prop;
  202. ASSERT_NE (root_path, nullptr);
  203. /** armnn needs a directory with model file and metadata in that directory */
  204. model_file = g_build_filename (
  205. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  206. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  207. const gchar *model_files[] = {
  208. model_file, NULL,
  209. };
  210. _SetFilterProp (&prop, "armnn", model_files);
  211. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  212. EXPECT_NE (sp, (void *)NULL);
  213. output.size = input.size = sizeof (float) * 1;
  214. input.data = g_malloc (input.size);
  215. output.data = g_malloc (output.size);
  216. ret = sp->open (&prop, &data);
  217. EXPECT_EQ (ret, 0);
  218. /** invoke successful */
  219. *((float *)input.data) = 10.0;
  220. ret = sp->invoke_NN (&prop, &data, &input, &output);
  221. EXPECT_EQ (ret, 0);
  222. EXPECT_EQ (*((float *)output.data), 12.0);
  223. *((float *)input.data) = 1.0;
  224. ret = sp->invoke_NN (&prop, &data, &input, &output);
  225. EXPECT_EQ (ret, 0);
  226. EXPECT_EQ (*((float *)output.data), 3.0);
  227. g_free (input.data);
  228. g_free (output.data);
  229. sp->close (&prop, &data);
  230. g_free (model_file);
  231. }
  232. /**
  233. * @brief Test armnn invoke before open
  234. */
  235. TEST (nnstreamerFilterArmnn, invoke01_n)
  236. {
  237. int ret;
  238. void *data = NULL;
  239. gchar *model_file;
  240. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  241. GstTensorMemory input, output;
  242. GstTensorFilterProperties prop;
  243. ASSERT_NE (root_path, nullptr);
  244. /** armnn needs a directory with model file and metadata in that directory */
  245. model_file = g_build_filename (
  246. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  247. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  248. const gchar *model_files[] = {
  249. model_file, NULL,
  250. };
  251. _SetFilterProp (&prop, "armnn", model_files);
  252. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  253. EXPECT_NE (sp, (void *)NULL);
  254. output.size = input.size = sizeof (float) * 1;
  255. input.data = g_malloc (input.size);
  256. output.data = g_malloc (output.size);
  257. /** invoke before open */
  258. ret = sp->invoke_NN (&prop, &data, &input, &output);
  259. EXPECT_NE (ret, 0);
  260. g_free (model_file);
  261. }
  262. /**
  263. * @brief Test armnn invoke with invalid param
  264. */
  265. TEST (nnstreamerFilterArmnn, invoke02_n)
  266. {
  267. int ret;
  268. void *data = NULL;
  269. gchar *model_file;
  270. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  271. GstTensorMemory input, output;
  272. GstTensorFilterProperties prop;
  273. ASSERT_NE (root_path, nullptr);
  274. /** armnn needs a directory with model file and metadata in that directory */
  275. model_file = g_build_filename (
  276. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  277. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  278. const gchar *model_files[] = {
  279. model_file, NULL,
  280. };
  281. _SetFilterProp (&prop, "armnn", model_files);
  282. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  283. EXPECT_NE (sp, (void *)NULL);
  284. output.size = input.size = sizeof (float) * 1;
  285. input.data = g_malloc (input.size);
  286. output.data = g_malloc (output.size);
  287. ret = sp->open (&prop, &data);
  288. EXPECT_EQ (ret, 0);
  289. /** invoke without subplugin data */
  290. ret = sp->invoke_NN (&prop, NULL, &input, &output);
  291. EXPECT_NE (ret, 0);
  292. g_free (input.data);
  293. g_free (output.data);
  294. sp->close (&prop, &data);
  295. g_free (model_file);
  296. }
  297. /**
  298. * @brief Test armnn invoke with invalid param
  299. */
  300. TEST (nnstreamerFilterArmnn, invoke03_n)
  301. {
  302. int ret;
  303. void *data = NULL;
  304. gchar *model_file;
  305. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  306. GstTensorMemory output;
  307. GstTensorFilterProperties prop;
  308. ASSERT_NE (root_path, nullptr);
  309. /** armnn needs a directory with model file and metadata in that directory */
  310. model_file = g_build_filename (
  311. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  312. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  313. const gchar *model_files[] = {
  314. model_file, NULL,
  315. };
  316. _SetFilterProp (&prop, "armnn", model_files);
  317. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  318. EXPECT_NE (sp, (void *)NULL);
  319. output.size = sizeof (float) * 1;
  320. output.data = g_malloc (output.size);
  321. ret = sp->open (&prop, &data);
  322. EXPECT_EQ (ret, 0);
  323. /** invoke without input */
  324. ret = sp->invoke_NN (&prop, &data, NULL, &output);
  325. EXPECT_NE (ret, 0);
  326. g_free (output.data);
  327. sp->close (&prop, &data);
  328. g_free (model_file);
  329. }
  330. /**
  331. * @brief Test armnn invoke with invalid param
  332. */
  333. TEST (nnstreamerFilterArmnn, invoke04_n)
  334. {
  335. int ret;
  336. void *data = NULL;
  337. gchar *model_file;
  338. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  339. GstTensorMemory input;
  340. GstTensorFilterProperties prop;
  341. ASSERT_NE (root_path, nullptr);
  342. /** armnn needs a directory with model file and metadata in that directory */
  343. model_file = g_build_filename (
  344. root_path, "tests", "test_models", "models", "add.tflite", NULL);
  345. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  346. const gchar *model_files[] = {
  347. model_file, NULL,
  348. };
  349. _SetFilterProp (&prop, "armnn", model_files);
  350. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  351. EXPECT_NE (sp, (void *)NULL);
  352. input.size = sizeof (float) * 1;
  353. input.data = g_malloc (input.size);
  354. ret = sp->open (&prop, &data);
  355. EXPECT_EQ (ret, 0);
  356. /** invoke without output */
  357. ret = sp->invoke_NN (&prop, &data, &input, NULL);
  358. EXPECT_NE (ret, 0);
  359. g_free (input.data);
  360. sp->close (&prop, &data);
  361. g_free (model_file);
  362. }
  363. /**
  364. * @brief get argmax from the array
  365. */
  366. template <typename T>
  367. size_t
  368. get_argmax (T *array, size_t size)
  369. {
  370. size_t idx, max_idx = 0;
  371. T max_value = 0;
  372. for (idx = 0; idx < size; idx++) {
  373. if (max_value < array[idx]) {
  374. max_idx = idx;
  375. max_value = array[idx];
  376. }
  377. }
  378. return max_idx;
  379. }
  380. /**
  381. * @brief Test armnn subplugin with successful invoke for tflite advanced model
  382. */
  383. TEST (nnstreamerFilterArmnn, invokeAdvanced)
  384. {
  385. int ret, fd;
  386. void *data = NULL;
  387. gchar *model_file;
  388. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  389. GstTensorMemory input, output;
  390. GstTensorsInfo info, res;
  391. char *data_file;
  392. ssize_t data_read;
  393. size_t max_idx;
  394. gboolean status;
  395. GstTensorFilterProperties prop;
  396. ASSERT_NE (root_path, nullptr);
  397. /** armnn needs a directory with model file and metadata in that directory */
  398. model_file = g_build_filename (root_path, "tests", "test_models", "models",
  399. "mobilenet_v1_1.0_224_quant.tflite", NULL);
  400. status = g_file_test (model_file, G_FILE_TEST_EXISTS);
  401. if (!status) {
  402. g_free (model_file);
  403. ASSERT_EQ (status, TRUE);
  404. return;
  405. }
  406. const gchar *model_files[] = {
  407. model_file, NULL,
  408. };
  409. _SetFilterProp (&prop, "armnn", model_files);
  410. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  411. EXPECT_NE (sp, (void *)NULL);
  412. info.num_tensors = 1;
  413. info.info[0].type = _NNS_UINT8;
  414. info.info[0].dimension[0] = 3;
  415. info.info[0].dimension[1] = 224;
  416. info.info[0].dimension[2] = 224;
  417. info.info[0].dimension[3] = 1;
  418. ret = sp->open (&prop, &data);
  419. if (ret == -EPERM) { /** TFLite Parser is not included in this instance */
  420. g_free (model_file);
  421. return;
  422. }
  423. EXPECT_EQ (ret, 0);
  424. /** get input/output dimension successfully */
  425. ret = sp->getInputDimension (&prop, &data, &res);
  426. EXPECT_EQ (ret, 0);
  427. EXPECT_EQ (res.num_tensors, info.num_tensors);
  428. EXPECT_EQ (res.info[0].type, info.info[0].type);
  429. EXPECT_EQ (res.info[0].dimension[0], info.info[0].dimension[0]);
  430. EXPECT_EQ (res.info[0].dimension[1], info.info[0].dimension[1]);
  431. EXPECT_EQ (res.info[0].dimension[2], info.info[0].dimension[2]);
  432. EXPECT_EQ (res.info[0].dimension[3], info.info[0].dimension[3]);
  433. input.size = gst_tensor_info_get_size (&res.info[0]);
  434. ret = sp->getOutputDimension (&prop, &data, &res);
  435. EXPECT_EQ (ret, 0);
  436. info.num_tensors = 1;
  437. info.info[0].type = _NNS_UINT8;
  438. info.info[0].dimension[0] = 1001;
  439. info.info[0].dimension[1] = 1;
  440. info.info[0].dimension[2] = 1;
  441. info.info[0].dimension[3] = 1;
  442. EXPECT_EQ (res.num_tensors, info.num_tensors);
  443. EXPECT_EQ (res.info[0].type, info.info[0].type);
  444. EXPECT_EQ (res.info[0].dimension[0], info.info[0].dimension[0]);
  445. EXPECT_EQ (res.info[0].dimension[1], info.info[0].dimension[1]);
  446. EXPECT_EQ (res.info[0].dimension[2], info.info[0].dimension[2]);
  447. EXPECT_EQ (res.info[0].dimension[3], info.info[0].dimension[3]);
  448. output.size = gst_tensor_info_get_size (&res.info[0]);
  449. input.data = g_malloc (input.size);
  450. output.data = g_malloc (output.size);
  451. data_file = g_build_filename (
  452. root_path, "tests", "test_models", "data", "orange.raw", NULL);
  453. fd = open (data_file, O_RDONLY);
  454. EXPECT_TRUE (fd >= 0);
  455. /** Invoke output will not match with fd < 0 - input will be random data */
  456. if (fd >= 0) {
  457. data_read = read (fd, input.data, input.size);
  458. EXPECT_EQ (data_read, input.size);
  459. close (fd);
  460. }
  461. ret = sp->invoke_NN (&prop, &data, &input, &output);
  462. EXPECT_EQ (ret, 0);
  463. /** entry 952 (idx 951) is orange as per tests/test_models/labels/labels.txt
  464. */
  465. max_idx = get_argmax<guint8> ((guint8 *)output.data, output.size);
  466. EXPECT_EQ (max_idx, 951);
  467. g_free (data_file);
  468. g_free (output.data);
  469. g_free (input.data);
  470. g_free (model_file);
  471. sp->close (&prop, &data);
  472. }
  473. /**
  474. * @brief Test armnn subplugin with successful invoke for caffe
  475. */
  476. TEST (nnstreamerFilterArmnn, invoke01)
  477. {
  478. int ret;
  479. void *data = NULL;
  480. gchar *model_file, *data_file;
  481. const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
  482. gchar *input_uint8_data = NULL;
  483. gsize input_uint8_size = 0;
  484. GstTensorMemory output, input;
  485. ssize_t max_idx;
  486. const unsigned int num_labels = 10;
  487. GstTensorFilterProperties prop;
  488. ASSERT_NE (root_path, nullptr);
  489. /** armnn needs a directory with model file and metadata in that directory */
  490. model_file = g_build_filename (root_path, "tests", "test_models", "models",
  491. "lenet_iter_9000.caffemodel", NULL);
  492. ASSERT_TRUE (g_file_test (model_file, G_FILE_TEST_EXISTS));
  493. data_file = g_build_filename (root_path, "tests", "test_models", "data", "9.raw", NULL);
  494. ASSERT_TRUE (g_file_test (data_file, G_FILE_TEST_EXISTS));
  495. const gchar *model_files[] = {
  496. model_file, NULL,
  497. };
  498. _SetFilterProp (&prop, "armnn", model_files);
  499. /** Manually configure the input for test */
  500. gst_tensors_info_init (&prop.input_meta);
  501. gst_tensors_info_init (&prop.output_meta);
  502. prop.output_meta.num_tensors = 1;
  503. prop.output_meta.info[0].type = _NNS_FLOAT32;
  504. prop.output_meta.info[0].name = g_strdup ("prob");
  505. prop.input_meta.num_tensors = 1;
  506. prop.input_meta.info[0].name = g_strdup ("data");
  507. EXPECT_TRUE (g_file_get_contents (data_file, &input_uint8_data, &input_uint8_size, NULL));
  508. /** Convert the data from uint8 to float */
  509. input.size = input_uint8_size * gst_tensor_get_element_size (_NNS_FLOAT32);
  510. input.data = g_malloc (input.size);
  511. for (gsize idx = 0; idx < input_uint8_size; idx++) {
  512. ((float *)input.data)[idx] = static_cast<float> (((guint8 *)input_uint8_data)[idx]);
  513. ((float *)input.data)[idx] -= 127.5;
  514. ((float *)input.data)[idx] /= 127.5;
  515. }
  516. const GstTensorFilterFramework *sp = nnstreamer_filter_find ("armnn");
  517. EXPECT_NE (sp, (void *)NULL);
  518. output.size = gst_tensor_get_element_size (_NNS_FLOAT32) * num_labels;
  519. output.data = g_malloc (output.size);
  520. ret = sp->open (&prop, &data);
  521. if (ret == -EPERM) { /** Caffe Parser Not Included in This System */
  522. g_free (data_file);
  523. g_free (input.data);
  524. g_free (input_uint8_data);
  525. g_free (output.data);
  526. g_free (prop.output_meta.info[0].name);
  527. g_free (prop.input_meta.info[0].name);
  528. g_free (model_file);
  529. return;
  530. }
  531. EXPECT_EQ (ret, 0);
  532. /** invoke successful */
  533. ret = sp->invoke_NN (&prop, &data, &input, &output);
  534. EXPECT_EQ (ret, 0);
  535. max_idx = get_argmax<float> ((float *)output.data, num_labels);
  536. EXPECT_EQ (max_idx, 9);
  537. sp->close (&prop, &data);
  538. /** Run the test again but with setting input meta as well */
  539. gst_tensors_info_init (&prop.input_meta);
  540. gst_tensors_info_init (&prop.output_meta);
  541. prop.output_meta.num_tensors = 1;
  542. prop.output_meta.info[0].type = _NNS_FLOAT32;
  543. prop.output_meta.info[0].name = g_strdup ("prob");
  544. prop.input_meta.num_tensors = 1;
  545. prop.input_meta.info[0].type = _NNS_FLOAT32;
  546. prop.input_meta.info[0].name = g_strdup ("data");
  547. prop.input_meta.info[0].dimension[0] = 28;
  548. prop.input_meta.info[0].dimension[1] = 28;
  549. prop.input_meta.info[0].dimension[2] = 1;
  550. prop.input_meta.info[0].dimension[3] = 1;
  551. ret = sp->open (&prop, &data);
  552. EXPECT_EQ (ret, 0);
  553. /** invoke successful */
  554. ret = sp->invoke_NN (&prop, &data, &input, &output);
  555. EXPECT_EQ (ret, 0);
  556. max_idx = get_argmax<float> ((float *)output.data, num_labels);
  557. EXPECT_EQ (max_idx, 9);
  558. sp->close (&prop, &data);
  559. g_free (data_file);
  560. g_free (input.data);
  561. g_free (input_uint8_data);
  562. g_free (output.data);
  563. g_free (prop.output_meta.info[0].name);
  564. g_free (prop.input_meta.info[0].name);
  565. g_free (model_file);
  566. }
  567. /**
  568. * @brief Main gtest
  569. */
  570. int
  571. main (int argc, char **argv)
  572. {
  573. int result = -1;
  574. try {
  575. testing::InitGoogleTest (&argc, argv);
  576. } catch (...) {
  577. g_warning ("catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'");
  578. }
  579. try {
  580. result = RUN_ALL_TESTS ();
  581. } catch (...) {
  582. g_warning ("catch `testing::internal::GoogleTestFailureException`");
  583. }
  584. return result;
  585. }