From 526fb725ce3b4c807786ec14d5cfd223d76dcf76 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 7 Jul 2020 19:11:56 -0300 Subject: [PATCH 1/2] Add native vector_lenght() function which calculates the euclidean distance 80% faster. See https://github.com/matiasdelellis/pdlib/pull/2. Increase the version to 1.0.2 since this is a required function for our application and we must control it. Add more compilation info in php-info(), and print a final message with the build options when configure. Fix that when dlib is not installed, the configuration returns that it cannot find pkgconfig. See https://github.com/matiasdelellis/facerecognition/issues/261 Also implement the test on travis. See for example: (https://travis-ci.com/github/matiasdelellis/pdlib/builds/161854044) --- .travis.yml | 28 +++++++++++++++++++++++++ config.m4 | 42 +++++++++++++++++++++++++------------ pdlib.cc | 45 +++++++++++++++++++++++++++++++++++++++- php_pdlib.h | 3 ++- src/vector.cc | 43 ++++++++++++++++++++++++++++++++++++++ src/vector.h | 10 +++++++++ tests/vector_length.phpt | 20 ++++++++++++++++++ tests/version.phpt | 10 +++++++++ 8 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 .travis.yml create mode 100644 src/vector.cc create mode 100644 src/vector.h create mode 100644 tests/vector_length.phpt create mode 100644 tests/version.phpt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..79a44ea --- /dev/null +++ b/.travis.yml @@ -0,0 +1,28 @@ +dist: bionic + +sudo: false + +language: php + +php: + - 7.1 + - 7.2 + - 7.3 + - 7.4 + +before_install: + - sudo sh -c 'echo "deb https://repo.delellis.com.ar bionic bionic" > /etc/apt/sources.list.d/20-pdlib.list' + - wget -qO - https://repo.delellis.com.ar/repo.gpg.key | sudo apt-key add - + - sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 762E3157 + - sudo apt update + - sudo apt-get install libdlib-dev + - phpize + - ./configure --enable-debug + - make + - sudo make install + - echo "extension=pdlib.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` + +script: + - export TEST_PHP_EXECUTABLE=`which php` + - export NO_INTERACTION=1 + - php run-tests.php -q --show-diff \ No newline at end of file diff --git a/config.m4 b/config.m4 index d51d38e..b7515b2 100644 --- a/config.m4 +++ b/config.m4 @@ -1,10 +1,3 @@ -dnl $Id$ -dnl config.m4 for extension pdlib - -dnl Comments in this file start with the string 'dnl'. -dnl Remove where necessary. This file will not work -dnl without editing. - if test -z "$PHP_DEBUG"; then AC_ARG_ENABLE(debug, [ --enable-debug compile with debugging symbols],[ @@ -27,26 +20,49 @@ if test "$PHP_PDLIB" != "no"; then src/face_detection.cc \ src/face_landmark_detection.cc \ src/face_recognition.cc \ - src/cnn_face_detection.cc " + src/cnn_face_detection.cc + src/vector.cc" AC_MSG_CHECKING(for pkg-config) AC_PATH_PROG(PKG_CONFIG, pkg-config, no) - - if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists dlib-1; then - if $PKG_CONFIG dlib-1 --atleast-version 19.00; then + if test -x "$PKG_CONFIG"; then + AC_MSG_RESULT(found) + AC_MSG_CHECKING(for dlib-1) + if $PKG_CONFIG --exists dlib-1; then + if $PKG_CONFIG dlib-1 --atleast-version 19.00; then LIBDLIB_CFLAGS=`$PKG_CONFIG dlib-1 --cflags` LIBDLIB_LIBDIR=`$PKG_CONFIG dlib-1 --libs` LIBDLIB_VERSON=`$PKG_CONFIG dlib-1 --modversion` AC_MSG_RESULT(from pkgconfig: dlib version $LIBDLIB_VERSON) - else + else AC_MSG_ERROR(system dlib is too old: version 19.00 required) - fi + fi + else + AC_MSG_ERROR(dlib-1 not found) + fi else AC_MSG_ERROR(pkg-config not found) fi + PHP_EVAL_LIBLINE($LIBDLIB_LIBDIR, PDLIB_SHARED_LIBADD) PHP_EVAL_INCLINE($LIBDLIB_CFLAGS) dnl using C++11 PHP_NEW_EXTENSION(pdlib, $pdlib_src_files, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -std=c++11, cxx) fi + +AC_CONFIG_COMMANDS_POST([ + echo "" + echo "Build configuration for PDlib v1.0.2 done correctly." + echo "" + echo " dlib version: $LIBDLIB_VERSON" + echo "" + echo " CXXFLAGS : $CXXFLAGS" + echo " LDFLAGS : $LDFLAGS" + echo " LIBDIR: : $LIBDLIB_LIBDIR" + echo " LIBADD: : $PDLIB_SHARED_LIBADD" + echo "" + echo "Please submit bug reports at:" + echo " https://github.com/goodspb/pdlib/issues" + echo "" +]) \ No newline at end of file diff --git a/pdlib.cc b/pdlib.cc index 68354de..933531d 100644 --- a/pdlib.cc +++ b/pdlib.cc @@ -33,6 +33,8 @@ extern "C" { #include "src/face_recognition.h" #include "src/cnn_face_detection.h" #include "src/face_landmark_detection.h" +#include "src/vector.h" + #include /* If you declare any globals in php_pdlib.h uncomment this: @@ -222,6 +224,46 @@ PHP_MINFO_FUNCTION(pdlib) php_info_print_table_row(2, "pdlib extension version", PHP_PDLIB_VERSION); snprintf(buf, sizeof(buf), "%d.%d.%d", DLIB_MAJOR_VERSION, DLIB_MINOR_VERSION, DLIB_PATCH_VERSION); php_info_print_table_row(2, "dlib library version", buf); +#ifdef DLIB_USE_CUDA + php_info_print_table_header(2, "DLIB_USE_CUDA", "true"); +#else + php_info_print_table_header(2, "DLIB_USE_CUDA", "false"); +#endif +#ifdef DLIB_USE_BLAS + php_info_print_table_header(2, "DLIB_USE_BLAS", "true"); +#else + php_info_print_table_header(2, "DLIB_USE_BLAS", "false"); +#endif +#ifdef DLIB_USE_LAPACK + php_info_print_table_header(2, "DLIB_USE_LAPACK", "true"); +#else + php_info_print_table_header(2, "DLIB_USE_LAPACK", "false"); +#endif +#ifdef DLIB_HAVE_AVX + php_info_print_table_header(2, "USE_AVX_INSTRUCTIONS", "true"); +#else + php_info_print_table_header(2, "USE_AVX_INSTRUCTIONS", "false"); +#endif +#ifdef DLIB_HAVE_AVX2 + php_info_print_table_header(2, "USE_AVX2_INSTRUCTIONS", "true"); +#else + php_info_print_table_header(2, "USE_AVX2_INSTRUCTIONS", "false"); +#endif +#ifdef DLIB_HAVE_NEON + php_info_print_table_header(2, "USE_NEON_INSTRUCTIONS", "true"); +#else + php_info_print_table_header(2, "USE_NEON_INSTRUCTIONS", "false"); +#endif +#ifdef DLIB_HAVE_SSE2 + php_info_print_table_header(2, "USE_SSE2_INSTRUCTIONS", "true"); +#else + php_info_print_table_header(2, "USE_SSE2_INSTRUCTIONS", "false"); +#endif +#ifdef DLIB_HAVE_SSE41 + php_info_print_table_header(2, "USE_SSE4_INSTRUCTIONS", "true"); +#else + php_info_print_table_header(2, "USE_SSE4_INSTRUCTIONS", "false"); +#endif php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -238,6 +280,7 @@ const zend_function_entry pdlib_functions[] = { PHP_FE(dlib_chinese_whispers, dlib_chinese_whispers_arginfo) PHP_FE(dlib_face_detection, dlib_face_detection_arginfo) PHP_FE(dlib_face_landmark_detection, dlib_face_landmark_detection_arginfo) + PHP_FE(dlib_vector_length, dlib_vector_length_arginfo) PHP_FE_END /* Must be the last line in pdlib_functions[] */ }; /* }}} */ @@ -246,7 +289,7 @@ const zend_function_entry pdlib_functions[] = { */ zend_module_entry pdlib_module_entry = { STANDARD_MODULE_HEADER, - "pdlib", + PHP_PDLIB_NAME, pdlib_functions, PHP_MINIT(pdlib), PHP_MSHUTDOWN(pdlib), diff --git a/php_pdlib.h b/php_pdlib.h index efe9f79..688d591 100644 --- a/php_pdlib.h +++ b/php_pdlib.h @@ -31,7 +31,8 @@ extern "C" { extern zend_module_entry pdlib_module_entry; #define phpext_pdlib_ptr &pdlib_module_entry -#define PHP_PDLIB_VERSION "1.0.1" /* Replace with version number for your extension */ +#define PHP_PDLIB_VERSION "1.0.2" +#define PHP_PDLIB_NAME "pdlib" #ifdef PHP_WIN32 # define PHP_PDLIB_API __declspec(dllexport) diff --git a/src/vector.cc b/src/vector.cc new file mode 100644 index 0000000..2123579 --- /dev/null +++ b/src/vector.cc @@ -0,0 +1,43 @@ + +#include "../php_pdlib.h" + +#include "vector.h" + +#include +#include +#include + +using namespace std; + +PHP_FUNCTION(dlib_vector_length) +{ + zval *x_arg, *y_arg; + + if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "aa", &x_arg, &y_arg) == FAILURE) { + return; + } + + zval *elem_x, *elem_y; + double sum = 0.0; + unsigned int i, len; + + len = zend_hash_num_elements(Z_ARRVAL_P(x_arg)); + + if (len != zend_hash_num_elements(Z_ARRVAL_P(y_arg))) { + zend_throw_exception_ex( + zend_ce_exception, + 0, + "The arrays have different sizes"); + return; + } + + for (i = 0 ; i < len ; i++) { + elem_x = zend_hash_index_find(Z_ARRVAL_P(x_arg), i); + elem_y = zend_hash_index_find(Z_ARRVAL_P(y_arg), i); + + sum += (Z_DVAL_P(elem_x) - Z_DVAL_P(elem_y))*(Z_DVAL_P(elem_x) - Z_DVAL_P(elem_y)); + } + + RETURN_DOUBLE(sqrt(sum)); +} + diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..e05a699 --- /dev/null +++ b/src/vector.h @@ -0,0 +1,10 @@ +#ifndef PHP_DLIB_VECTOR_H +#define PHP_DLIB_VECTOR_H + +ZEND_BEGIN_ARG_INFO_EX(dlib_vector_length_arginfo, 0, 0, 2) + ZEND_ARG_ARRAY_INFO(0, x_arg, 0) + ZEND_ARG_ARRAY_INFO(0, y_arg, 0) +ZEND_END_ARG_INFO() +PHP_FUNCTION(dlib_vector_length); + +#endif //PHP_DLIB_VECTOR_H diff --git a/tests/vector_length.phpt b/tests/vector_length.phpt new file mode 100644 index 0000000..60b4747 --- /dev/null +++ b/tests/vector_length.phpt @@ -0,0 +1,20 @@ +--TEST-- +Basic tests for dlib_vector_length +--SKIPIF-- + +--FILE-- + +--EXPECT-- +float(1) +float(2) +float(1.5) \ No newline at end of file diff --git a/tests/version.phpt b/tests/version.phpt new file mode 100644 index 0000000..21a891e --- /dev/null +++ b/tests/version.phpt @@ -0,0 +1,10 @@ +--TEST-- +Just test php extension version +--SKIPIF-- + +--FILE-- + +--EXPECT-- +string(5) "1.0.2" \ No newline at end of file From 32f94d32b90d647da49d5dffc60cd5b6e4f895ec Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 7 Jul 2020 19:30:34 -0300 Subject: [PATCH 2/2] Fix test. Probably Remi changed some default option on php 8.0 update --- tests/integration_face_recognition.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_face_recognition.phpt b/tests/integration_face_recognition.phpt index 9179577..ca9d75d 100644 --- a/tests/integration_face_recognition.phpt +++ b/tests/integration_face_recognition.phpt @@ -59,6 +59,6 @@ Processing prediction model Processing recognition model Detection Faces found = 1 -Face[0] in bounding box (left=187, top=186, right=357, bottom=355) +Face[0] in bounding box (left=191, top=200, right=355, bottom=363) Since we used model with 5 shape predictions, we found 5 landmark parts Descriptor is vector of 128 dimensions