在 Ubuntu20.02下编译 FaceRecognition_MTCNN_FaceNet

1.  compile FaceRecognition_MTCNN_FaceNet-master

git clone https://github.com/Chanstk/FaceRecognition_MTCNN_FaceNet.git
cd FaceRecognition_MTCNN_FaceNet
mkdir build && cd build
cmake ..  && make

       +----------------------------solve compile error --------------------+
 报错信息 : error adding symbols: DSO missing from command line
        原因 : shared lib called by main calls another shared lib, but CMakeLists.txt has not add -llibother.so
        解决 : CXX_FLAGS += -Wl,--copy-dt-needed-entries
        +----------------------------solve compile error --------------------+
 报错信息 : /usr/bin/ld: warning: libmkl_intel_lp64.so.2, needed by /usr/local/lib/libopencv_core.so.4.8.0, not found (try using -rpath or -rpath-link)
        解决 : 编辑 CMakeLists.txt,增加

link_directories( /opt/intel/oneapi/mkl/2023.2.0/lib/intel64 )


        +----------------------------solve compile error --------------------+
报错信息 : 2023-09-08 14:13:58.777362: E /home/rd/NN/FaceRecognition_MTCNN_FaceNet-master/src/main.cpp:107] Read proto
        解决 : 编辑  src/main.cpp 

   - string graph_path = "./model/20170323-142841.pb";
   + string graph_path = "./model/mtcnn_frozen_model.pb";


         +--------------------------------------------------------------------------------------------------------------------+
报错信息 :

has not enablesd AVX2 FMA
            2023-09-08 14:16:17.991925: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
            2023-09-08 14:16:18.070278: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
            To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
            2023-09-08 14:16:18.218639: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:375] MLIR V1 optimization pass is not enabled
            terminate called after throwing an instance of 'cv::Exception'
              what():  OpenCV(4.8.0) /home/rd/opencv/opencv-4.8.0/modules/core/src/arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'arithm_op'

       解决方法 :

mkdir model;
cp ../../facenet-compare-cpp/facenet-compare-cpp-master/models/mtcnn_frozen_model.pb  model/


         +--------------------------------------------------------------------------------------------------------------------+
         (https://www.intel.com/content/www/us/en/developer/articles/guide/optimization-for-tensorflow-installation-guide.html)

wget https://repo.anaconda.com/archive/Anaconda3-2023.07-2-Linux-x86_64.sh
sudo chmod +x Anaconda3-2023.07-2-Linux-x86_64.sh 
sudo ./Anaconda3-2023.07-2-Linux-x86_64.sh 
sudo conda install intel-tensorflow -c intel

        [ Fail ] bazel build  --cxxopt=-D\_GLIBCXX\_USE\_CXX11\_ABI=0 --copt=-march=corei7-avx --copt=-mtune=core-avx-i --copt=-O3 --copt=-Wformat --copt=-Wformat-security --copt=-fstack-protector --copt=-fPIC --copt=-fpic --linkopt=-znoexecstack --linkopt=-zrelro --linkopt=-znow --linkopt=-fstack-protector  //tensorflow/tools/pip_package:build_pip_package
         
         [ OK ] bazel clean
                bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package


2.  运行 facenet 的测试脚本

        git clone https://github.com/davidsandberg/facenet.git

2.1 图像对齐

for N in {1..4}; do \
    python3 src/align/align_dataset_mtcnn.py \
        ~/datasets/lfw/raw \
        ~/datasets/lfw/lfw_mtcnnpy_160 \
        --image_size 160 \
        --margin 32 \
        --random_order \
        --gpu_memory_fraction 0.25 \
& done

        (https://github.com/davidsandberg/facenet/wiki/Validate-on-lfw)
        +--------------------------------------------------solve compile error ----------------------------------------------+
    报错信息 :    AttributeError: module ‘tensorflow‘ has no attribute 'GPUOptions'
                    AttributeError: module 'tensorflow' has no attribute 'Session'
                    AttributeError: module 'tensorflow' has no attribute 'variable_scope'
        cause    :    Tensorflow 1.X和 2.X不兼容。
        Solution :    [-]sed -i "s/tf.GPUOptions/tf.compat.v1.GPUOptions/g"    */*.py  */*/*.py  */*/*/*.py
                    [-]sed -i "s/tf.Session/tf.compat.v1.Session/g"          */*.py  */*/*.py  */*/*/*.py
                    [-]sed -i "s/tf.ConfigProto/tf.compat.v1.ConfigProto/g"  */*.py  */*/*.py  */*/*/*.py
                    
                    [+]sed -i "s/import tensorflow as tf/import tensorflow.compat.v1 as tf/g"  */*.py */*/*.py */*/*/*.py

        +--------------------------------------------------solve compile error ----------------------------------------------+
  报错信息 :   ValueError: Object arrays cannot be loaded when allow_pickle=False
        cause    :    Tensorflow 1.X和 2.X不兼容。
        Solution :    1.  pip install numpy=1.16.2
                    2.  vim src/align/detect_face.py +85
                        - data_dict = np.load(data_path, encoding='latin1'                   ).item() #pylint: disable=no-member
                        + data_dict = np.load(data_path, encoding='latin1', allow_pickle=True).item() #pylint: disable=no-member

        +--------------------------------------------------solve compile error ----------------------------------------------+
       报错信息 :    AtributeError: 'int' object has no attribute 'value'
              原因 :    Tensorflow 1.X和 2.X不兼容。
       解决方法 :   编辑 src/align/detect_face.py +194

   - feed_in, dim = (inp, input_shape[-1].value)
   + feed_in, dim = (inp, input_shape[-1])

        +--------------------------------------------------solve compile error ----------------------------------------------+
      报错信息 :   

              AttributeError: scipy.misc is deprecated and has no attribute imread
              AttributeError: scipy.misc is deprecated and has no attribute imresize.


             原因 :    官方scipy中提到,imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead
      解决方法 :  sudo pip3 install imageio
                    sed -i "s/misc.im/imageio.im/g"  */*.py  */*/*.py
                    sed -i "s/from scipy import misc/import imageio.v2 as imageio/g"  */*.py  */*/*.py   */*/*/*.py
                    vim src/align/align_dataset_mtcnn.py
                    + import imageio
                    - img = misc.imread(image_path)
                    + img = imageio.imread(image_path)
        +--------------------------------------------------solve compile error ----------------------------------------------+
       报错信息 :     AttributeError: module 'imageio.v2' has no attribute 'imresize'    
              原因 :    Tensorflow 1.X和 2.X不兼容。
       解决方法 :   

sed -i "s/imageio.imresize(cropped,/Image.fromarray(cropped).resize(/g"  */*.py  */*/*.py
sed -i "s/imageio.imresize(img,/Image.fromarray(img).resize(/g"  */*.py  */*/*.py   */*/*/*.py
sed -i "s/import imageio.v2 as imageio/import imageio.v2 as imageio\nfrom PIL import Image/g"  */*.py  */*/*.py   */*/*/*.py
sed -i "s/interp='bilinear'/Image.BILINEAR/g"  */*.py  */*/*.py

        +--------------------------------------------------solve compile error ----------------------------------------------+
#        sudo apt install libjpeg-dev  libtiff-dev
#        sudo pip install imageio iio  # image I/O
        
        +--------------------------------------------------solve compile error ----------------------------------------------+
       报错信息 :     div (from tensorflow.python.ops.math_ops) is deprecated and will be removed
       解决方法 :     https://docs.w3cub.com/tensorflow~1.15/div.html
                    1.  编辑  src/align/detect_face.py : 214 行
                        - tf.div
                        + tf.compat.v1.div\

2.2 validate on lfw dataset

            python src/validate_on_lfw.py \
            ~/datasets/lfw/lfw_mtcnnpy_160 \
            ~/models/facenet/20180402-114759 \
            --distance_metric 1 \
            --use_flipped_images \
            --subtract_mean \
            --use_fixed_image_standardization        
        +--------------------------------------------------solve compile error ----------------------------------------------+
        ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions.


        WARNING:tensorflow:From /home/rd/NN/facenet/src/facenet.py:112: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.


        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    WARNING:tensorflow:From /home/rd/NN/facenet/src/facenet.py:112: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
                    Instructions for updating:
                    tf.py_func is deprecated in TF V2. Instead, there are two
                        options available in V2.
                        - tf.py_function takes a python function which manipulates tf eager
                        tensors instead of numpy arrays. It's easy to convert a tf eager tensor to
                        an ndarray (just call tensor.numpy()) but having access to eager tensors
                        means `tf.py_function`s can use accelerators such as GPUs as well as
                        being differentiable using a gradient tape.
                        - tf.numpy_function maintains the semantics of the deprecated tf.py_func
                        (it is not differentiable, and manipulates numpy arrays). It drops the
                        stateful argument making all functions stateful.
        Solution :     vim src/facenet.py +112
        

        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    WARNING:tensorflow:From /home/rd/NN/facenet/src/facenet.py:131: batch_join (from tensorflow.python.training.input) is deprecated and will be removed in a future version.
Instructions for updating:
                    Queue-based input pipelines have been replaced by `tf.data`. Use `tf.data.Dataset.interleave(...).batch(batch_size)` (or `padded_batch(...)` if `dynamic_pad=True`).
        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    WARNING:tensorflow:From /home/rd/.local/lib/python3.8/site-packages/tensorflow/python/training/input.py:738: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
                    Instructions for updating:
                    To construct input pipelines, use the `tf.data` module.
                    WARNING:tensorflow:From /home/rd/.local/lib/python3.8/site-packages/tensorflow/python/training/input.py:738: add_queue_runner (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
                    Instructions for updating:
                    To construct input pipelines, use the `tf.data` module.
                    
        sudo pip install tf-slim
        sed -i "s/import tensorflow.contrib.slim as slim/import tf_slim as slim/g" */*.py  */*/*.py

2.3 比较图像 

python3  src/compare.py ~/NN/facenet-pre-trained-model/20180408-102900/20180408-102900.pb \
                        ~/datasets/lfw/Tony_Liu/Tony_Liu_0001.jpg \
                        ~/datasets/lfw/lfw_mtcnnpy_160/Tony_Blair/Tony_Blair_0001.png \
                        ~/opencv/study/video/Face-Samples/stark-face/stark-6.png \
                        ~/opencv/study/video/Face-Samples/stark-face/stark-2.png \
                        ~/opencv/study/video/Face-Samples/tony-face/tony-2.png \
                        ~/opencv/study/video/Face-Samples/tony-face/tony-5.png

        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    raise ValueError("Expect x to not have duplicates")
                    ValueError: Expect x to not have duplicates
            ERROR: Could not find a version that satisfies the requirement numpy<1.23.0,>=1.16.5 (from scipy==1.7.1) (from versions: none)
            ERROR: No matching distribution found for numpy<1.23.0,>=1.16.5 (from scipy==1.7.1)

        cause    : scipy包下得interpolate.interp1d()函数问题
        requirement : numpy<1.23.0,>=1.16.5 (from scipy==1.7.1)
        solution :降低scipy版本,我的是scipy(1.10.1)版本现换为版本1.7.1,可行!
                    pip uninstall numpy scipy
                    pip install scipy==1.7.1  
                    
        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    ValueError: Node 'gradients/InceptionResnetV1/Bottleneck/BatchNorm/cond/FusedBatchNorm_1_grad/FusedBatchNormGrad' has an _output_shapes attribute inconsistent with the GraphDef for output #3: Dimension 0 in both shapes must be equal, but are 0 and 512. Shapes are [0] and [512]
        cause    :    refer to https://github.com/davidsandberg/facenet/issues/1227 / https://github.com/openvinotoolkit/openvino/pull/11078
        Solution :    For those who undergo this problem. I would suggest following actions:
                    Add the directive "import tensorflow.compat.v1 as tf" to the corresponding .py files.
                    Use the arguments to specify the model file and pair.txt with absolute full path, as following,

                python3 FaceNet/src/validate_on_lfw.py ../Inventory/Aligned /Users/xxxx/Projects/Inventory/Models/20180402-114759.pb --distance_metric 1 --use_flipped_images --subtract_mean --use_fixed_image_standardization --lfw_pairs /Users/xxxx/Projects//FaceNet/data/pairs.txt

        +--------------------------------------------------solve compile error ----------------------------------------------+
        problem  :    2023-09-13 17:14:46.844967: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
                    Traceback (most recent call last):
                      File "src/compare.py", line 131, in
                        main(parse_arguments(sys.argv[1:]))
                      File "src/compare.py", line 42, in main
                        images = load_and_align_data(args.image_files, args.image_size, args.margin, args.gpu_memory_fraction)
                      File "src/compare.py", line 111, in load_and_align_data
                        prewhitened = facenet.prewhiten(aligned)
                      File "/home/rd/NN/facenet/src/facenet.py", line 220, in prewhiten
                        y = np.multiply(np.subtract(x, mean), 1/std_adj)
                    ValueError: operands could not be broadcast together with shapes (160,160,3) (2,) 
        solution :    vim src/facenet.py +220
                    std_adj = np.maximum(std, 1.0/np.sqrt(np.prod(x.size)))

你可能感兴趣的:(neo4j)