├── vision ├── __init__.py ├── nn │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── mb_tiny.cpython-36.pyc │ │ ├── mb_tiny_RFB.cpython-36.pyc │ │ └── multibox_loss.cpython-36.pyc │ ├── mb_tiny.py │ ├── multibox_loss.py │ └── mb_tiny_RFB.py ├── ssd │ ├── __init__.py │ ├── config │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── __init__.cpython-37.pyc │ │ │ ├── fd_config.cpython-36.pyc │ │ │ └── fd_config.cpython-37.pyc │ │ └── fd_config.py │ ├── __pycache__ │ │ ├── ssd.cpython-36.pyc │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── mb_tiny_fd.cpython-36.pyc │ │ ├── predictor.cpython-36.pyc │ │ ├── mb_tiny_RFB_fd.cpython-36.pyc │ │ └── data_preprocessing.cpython-36.pyc │ ├── data_preprocessing.py │ ├── mb_tiny_fd.py │ ├── mb_tiny_RFB_fd.py │ ├── predictor.py │ └── ssd.py ├── datasets │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ └── voc_dataset.cpython-36.pyc │ └── voc_dataset.py ├── transforms │ ├── __init__.py │ └── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ └── transforms.cpython-36.pyc └── utils │ ├── __init__.py │ ├── __pycache__ │ ├── misc.cpython-36.pyc │ ├── misc.cpython-37.pyc │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── box_utils.cpython-36.pyc │ └── box_utils_numpy.cpython-36.pyc │ ├── misc.py │ ├── box_utils_numpy.py │ └── box_utils.py ├── MNN ├── build │ ├── CMakeFiles │ │ ├── progress.marks │ │ ├── cmake.check_cache │ │ ├── Ultra-face-mnn.dir │ │ │ ├── progress.make │ │ │ ├── src │ │ │ │ ├── main.cpp.o │ │ │ │ └── UltraFace.cpp.o │ │ │ ├── cmake_clean.cmake │ │ │ ├── flags.make │ │ │ ├── DependInfo.cmake │ │ │ ├── depend.internal │ │ │ ├── depend.make │ │ │ ├── link.txt │ │ │ ├── CXX.includecache │ │ │ └── build.make │ │ ├── feature_tests.bin │ │ ├── 3.10.2 │ │ │ ├── CompilerIdC │ │ │ │ └── a.out │ │ │ ├── CompilerIdCXX │ │ │ │ └── a.out │ │ │ ├── CMakeDetermineCompilerABI_C.bin │ │ │ ├── CMakeDetermineCompilerABI_CXX.bin │ │ │ ├── CMakeSystem.cmake │ │ │ ├── CMakeCCompiler.cmake │ │ │ └── CMakeCXXCompiler.cmake │ │ ├── TargetDirectories.txt │ │ ├── CMakeDirectoryInformation.cmake │ │ ├── feature_tests.c │ │ ├── Makefile2 │ │ └── Makefile.cmake │ ├── result2.jpg │ ├── result3.jpg │ ├── result4.jpg │ ├── result5.jpg │ ├── Ultra-face-mnn │ ├── cmake_install.cmake │ └── Makefile ├── result.jpg ├── imgs │ ├── tim1g.jpg │ ├── timg.jpg │ ├── tim75g.jpg │ ├── timg33.jpg │ ├── timg333.jpg │ ├── timg444.jpg │ ├── timg78.jpg │ ├── 2020-06-13-14-40-48-563490.jpg │ ├── COCO_train2014_000000001580.jpg │ ├── COCO_train2014_000000001586.jpg │ ├── COCO_train2014_000000001706.jpg │ ├── COCO_train2014_000000001790.jpg │ ├── COCO_train2014_000000002658.jpg │ └── COCO_train2014_000000002755.jpg ├── mnn │ ├── lib │ │ └── libMNN.so │ └── include │ │ ├── NonCopyable.hpp │ │ ├── ErrorCode.hpp │ │ ├── revertMNNModel.hpp │ │ ├── MNNSharedContext.h │ │ ├── AutoTime.hpp │ │ ├── MNNForwardType.h │ │ ├── MNNDefine.h │ │ ├── ImageProcess.hpp │ │ ├── Backend.hpp │ │ ├── Tensor.hpp │ │ └── Interpreter.hpp ├── model │ ├── version-RFB │ │ ├── 61.mnn │ │ ├── 62.mnn │ │ ├── 617-1.mnn │ │ ├── 617-2.mnn │ │ ├── new.mnn │ │ ├── quan.mnn │ │ ├── RFB-320.mnn │ │ ├── XXX530.mnn │ │ ├── 617-1-sq.mnn │ │ ├── 617-2-sq.mnn │ │ ├── RFB-320-quant-ADMM-32.mnn │ │ ├── RFB-320-quant-KL-5792.mnn │ │ └── RFB-Epoch-199-Loss-3-sim.mnn │ └── version-slim │ │ ├── slim-320.mnn │ │ └── slim-320-quant-ADMM-50.mnn ├── python │ ├── output │ │ └── person_pr.pkl │ ├── main.py │ ├── person-detector-pic.py │ ├── person-detector-video.py │ └── voc_eval.py ├── CMakeLists.txt └── src │ ├── main.cpp │ ├── UltraFace.hpp │ └── UltraFace.cpp ├── models ├── voc-model-labels.txt └── train-version-RFB │ ├── voc-model-labels.txt │ ├── RFB-Epoch-299-Loss-2.968528504555042.pth │ └── logs │ └── log ├── imgs ├── 1.jpg ├── 10.jpg ├── 11.jpg ├── 12.jpg ├── 13.jpg ├── 15.jpg ├── 16.jpg ├── 17.jpg ├── 18.jpg ├── 19.jpg ├── 2.jpg ├── 20.jpg ├── 21.jpg ├── 22.jpg ├── 23.jpg ├── 24.jpg ├── 25.jpg ├── 26.jpg ├── 27.jpg ├── 3.jpg ├── 5.jpg ├── 6.jpg ├── 8.jpg ├── 9.jpg ├── 123.jpg ├── tim1g.jpg ├── tim75g.jpg ├── timg33.jpg ├── timg333.jpg ├── timg444.jpg ├── timg78.jpg ├── COCO_train2014_000000000036.jpg ├── COCO_train2014_000000000110.jpg ├── COCO_train2014_000000000113.jpg ├── COCO_train2014_000000000165.jpg ├── COCO_train2014_000000000308.jpg ├── u=1236916689,3553454666&fm=26&gp=0.jpg ├── u=2286004306,1565835660&fm=26&gp=0.jpg └── u=2379790160,1121193717&fm=26&gp=0.jpg ├── train-version-RFB.sh ├── convert_to_onnx.py ├── README.md └── detect_imgs.py /vision/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vision/nn/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vision/ssd/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vision/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vision/ssd/config/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vision/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /vision/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .misc import * 2 | -------------------------------------------------------------------------------- /models/voc-model-labels.txt: -------------------------------------------------------------------------------- 1 | BACKGROUND 2 | person 3 | -------------------------------------------------------------------------------- /models/train-version-RFB/voc-model-labels.txt: -------------------------------------------------------------------------------- 1 | BACKGROUND 2 | person -------------------------------------------------------------------------------- /imgs/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/1.jpg -------------------------------------------------------------------------------- /imgs/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/10.jpg -------------------------------------------------------------------------------- /imgs/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/11.jpg -------------------------------------------------------------------------------- /imgs/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/12.jpg -------------------------------------------------------------------------------- /imgs/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/13.jpg -------------------------------------------------------------------------------- /imgs/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/15.jpg -------------------------------------------------------------------------------- /imgs/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/16.jpg -------------------------------------------------------------------------------- /imgs/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/17.jpg -------------------------------------------------------------------------------- /imgs/18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/18.jpg -------------------------------------------------------------------------------- /imgs/19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/19.jpg -------------------------------------------------------------------------------- /imgs/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/2.jpg -------------------------------------------------------------------------------- /imgs/20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/20.jpg -------------------------------------------------------------------------------- /imgs/21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/21.jpg -------------------------------------------------------------------------------- /imgs/22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/22.jpg -------------------------------------------------------------------------------- /imgs/23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/23.jpg -------------------------------------------------------------------------------- /imgs/24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/24.jpg -------------------------------------------------------------------------------- /imgs/25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/25.jpg -------------------------------------------------------------------------------- /imgs/26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/26.jpg -------------------------------------------------------------------------------- /imgs/27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/27.jpg -------------------------------------------------------------------------------- /imgs/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/3.jpg -------------------------------------------------------------------------------- /imgs/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/5.jpg -------------------------------------------------------------------------------- /imgs/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/6.jpg -------------------------------------------------------------------------------- /imgs/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/8.jpg -------------------------------------------------------------------------------- /imgs/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/9.jpg -------------------------------------------------------------------------------- /imgs/123.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/123.jpg -------------------------------------------------------------------------------- /MNN/result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/result.jpg -------------------------------------------------------------------------------- /imgs/tim1g.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/tim1g.jpg -------------------------------------------------------------------------------- /imgs/tim75g.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/tim75g.jpg -------------------------------------------------------------------------------- /imgs/timg33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/timg33.jpg -------------------------------------------------------------------------------- /imgs/timg333.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/timg333.jpg -------------------------------------------------------------------------------- /imgs/timg444.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/timg444.jpg -------------------------------------------------------------------------------- /imgs/timg78.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/timg78.jpg -------------------------------------------------------------------------------- /MNN/imgs/tim1g.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/tim1g.jpg -------------------------------------------------------------------------------- /MNN/imgs/timg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/timg.jpg -------------------------------------------------------------------------------- /MNN/build/result2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/result2.jpg -------------------------------------------------------------------------------- /MNN/build/result3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/result3.jpg -------------------------------------------------------------------------------- /MNN/build/result4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/result4.jpg -------------------------------------------------------------------------------- /MNN/build/result5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/result5.jpg -------------------------------------------------------------------------------- /MNN/imgs/tim75g.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/tim75g.jpg -------------------------------------------------------------------------------- /MNN/imgs/timg33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/timg33.jpg -------------------------------------------------------------------------------- /MNN/imgs/timg333.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/timg333.jpg -------------------------------------------------------------------------------- /MNN/imgs/timg444.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/timg444.jpg -------------------------------------------------------------------------------- /MNN/imgs/timg78.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/timg78.jpg -------------------------------------------------------------------------------- /MNN/mnn/lib/libMNN.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/mnn/lib/libMNN.so -------------------------------------------------------------------------------- /MNN/build/Ultra-face-mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/Ultra-face-mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/61.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/61.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/62.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/62.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/617-1.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/617-1.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/617-2.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/617-2.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/new.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/new.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/quan.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/quan.mnn -------------------------------------------------------------------------------- /MNN/python/output/person_pr.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/python/output/person_pr.pkl -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/cmake.check_cache: -------------------------------------------------------------------------------- 1 | # This file is generated by cmake for dependency checking of the CMakeCache.txt file 2 | -------------------------------------------------------------------------------- /MNN/model/version-RFB/RFB-320.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/RFB-320.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/XXX530.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/XXX530.mnn -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 1 2 | CMAKE_PROGRESS_2 = 2 3 | CMAKE_PROGRESS_3 = 3 4 | 5 | -------------------------------------------------------------------------------- /MNN/model/version-RFB/617-1-sq.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/617-1-sq.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/617-2-sq.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/617-2-sq.mnn -------------------------------------------------------------------------------- /MNN/model/version-slim/slim-320.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-slim/slim-320.mnn -------------------------------------------------------------------------------- /imgs/COCO_train2014_000000000036.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/COCO_train2014_000000000036.jpg -------------------------------------------------------------------------------- /imgs/COCO_train2014_000000000110.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/COCO_train2014_000000000110.jpg -------------------------------------------------------------------------------- /imgs/COCO_train2014_000000000113.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/COCO_train2014_000000000113.jpg -------------------------------------------------------------------------------- /imgs/COCO_train2014_000000000165.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/COCO_train2014_000000000165.jpg -------------------------------------------------------------------------------- /imgs/COCO_train2014_000000000308.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/COCO_train2014_000000000308.jpg -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/feature_tests.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/feature_tests.bin -------------------------------------------------------------------------------- /MNN/imgs/2020-06-13-14-40-48-563490.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/2020-06-13-14-40-48-563490.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000001580.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000001580.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000001586.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000001586.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000001706.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000001706.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000001790.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000001790.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000002658.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000002658.jpg -------------------------------------------------------------------------------- /MNN/imgs/COCO_train2014_000000002755.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/imgs/COCO_train2014_000000002755.jpg -------------------------------------------------------------------------------- /vision/ssd/__pycache__/ssd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/ssd.cpython-36.pyc -------------------------------------------------------------------------------- /imgs/u=1236916689,3553454666&fm=26&gp=0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/u=1236916689,3553454666&fm=26&gp=0.jpg -------------------------------------------------------------------------------- /imgs/u=2286004306,1565835660&fm=26&gp=0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/u=2286004306,1565835660&fm=26&gp=0.jpg -------------------------------------------------------------------------------- /imgs/u=2379790160,1121193717&fm=26&gp=0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/imgs/u=2379790160,1121193717&fm=26&gp=0.jpg -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CompilerIdC/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/3.10.2/CompilerIdC/a.out -------------------------------------------------------------------------------- /vision/nn/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/nn/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /vision/nn/__pycache__/mb_tiny.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/nn/__pycache__/mb_tiny.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/misc.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/misc.cpython-36.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/misc.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/misc.cpython-37.pyc -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CompilerIdCXX/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/3.10.2/CompilerIdCXX/a.out -------------------------------------------------------------------------------- /MNN/model/version-RFB/RFB-320-quant-ADMM-32.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/RFB-320-quant-ADMM-32.mnn -------------------------------------------------------------------------------- /MNN/model/version-RFB/RFB-320-quant-KL-5792.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/RFB-320-quant-KL-5792.mnn -------------------------------------------------------------------------------- /vision/nn/__pycache__/mb_tiny_RFB.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/nn/__pycache__/mb_tiny_RFB.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/mb_tiny_fd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/mb_tiny_fd.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/predictor.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/predictor.cpython-36.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /MNN/model/version-RFB/RFB-Epoch-199-Loss-3-sim.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-RFB/RFB-Epoch-199-Loss-3-sim.mnn -------------------------------------------------------------------------------- /MNN/model/version-slim/slim-320-quant-ADMM-50.mnn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/model/version-slim/slim-320-quant-ADMM-50.mnn -------------------------------------------------------------------------------- /vision/datasets/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/datasets/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /vision/nn/__pycache__/multibox_loss.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/nn/__pycache__/multibox_loss.cpython-36.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/box_utils.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/box_utils.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/mb_tiny_RFB_fd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/mb_tiny_RFB_fd.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/config/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/config/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/config/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/config/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /vision/transforms/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/transforms/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o -------------------------------------------------------------------------------- /vision/datasets/__pycache__/voc_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/datasets/__pycache__/voc_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/__pycache__/data_preprocessing.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/__pycache__/data_preprocessing.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/config/__pycache__/fd_config.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/config/__pycache__/fd_config.cpython-36.pyc -------------------------------------------------------------------------------- /vision/ssd/config/__pycache__/fd_config.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/ssd/config/__pycache__/fd_config.cpython-37.pyc -------------------------------------------------------------------------------- /vision/transforms/__pycache__/transforms.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/transforms/__pycache__/transforms.cpython-36.pyc -------------------------------------------------------------------------------- /vision/utils/__pycache__/box_utils_numpy.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/vision/utils/__pycache__/box_utils_numpy.cpython-36.pyc -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CMakeDetermineCompilerABI_C.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/3.10.2/CMakeDetermineCompilerABI_C.bin -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CMakeDetermineCompilerABI_CXX.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/3.10.2/CMakeDetermineCompilerABI_CXX.bin -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o -------------------------------------------------------------------------------- /models/train-version-RFB/RFB-Epoch-299-Loss-2.968528504555042.pth: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ziweizhan/person-detector/HEAD/models/train-version-RFB/RFB-Epoch-299-Loss-2.968528504555042.pth -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/TargetDirectories.txt: -------------------------------------------------------------------------------- 1 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/rebuild_cache.dir 2 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/Ultra-face-mnn.dir 3 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/edit_cache.dir 4 | -------------------------------------------------------------------------------- /MNN/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(Ultra-face-mnn) 3 | 4 | set(CMAKE_CXX_STANDARD 11) 5 | 6 | find_package(OpenCV REQUIRED) 7 | include_directories( 8 | mnn/include 9 | src 10 | ) 11 | 12 | link_directories(mnn/lib) 13 | 14 | add_executable(Ultra-face-mnn src/main.cpp src/UltraFace.cpp) 15 | target_link_libraries(Ultra-face-mnn MNN ${OpenCV_LIBS}) -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o" 3 | "CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o" 4 | "Ultra-face-mnn.pdb" 5 | "Ultra-face-mnn" 6 | ) 7 | 8 | # Per-language clean rules from dependency scanning. 9 | foreach(lang CXX) 10 | include(CMakeFiles/Ultra-face-mnn.dir/cmake_clean_${lang}.cmake OPTIONAL) 11 | endforeach() 12 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # compile CXX with /usr/bin/c++ 5 | CXX_FLAGS = -std=gnu++11 6 | 7 | CXX_DEFINES = 8 | 9 | CXX_INCLUDES = -I/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/mnn/include -I/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src -isystem /usr/include/opencv 10 | 11 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CMakeSystem.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_HOST_SYSTEM "Linux-5.3.0-53-generic") 2 | set(CMAKE_HOST_SYSTEM_NAME "Linux") 3 | set(CMAKE_HOST_SYSTEM_VERSION "5.3.0-53-generic") 4 | set(CMAKE_HOST_SYSTEM_PROCESSOR "x86_64") 5 | 6 | 7 | 8 | set(CMAKE_SYSTEM "Linux-5.3.0-53-generic") 9 | set(CMAKE_SYSTEM_NAME "Linux") 10 | set(CMAKE_SYSTEM_VERSION "5.3.0-53-generic") 11 | set(CMAKE_SYSTEM_PROCESSOR "x86_64") 12 | 13 | set(CMAKE_CROSSCOMPILING "FALSE") 14 | 15 | set(CMAKE_SYSTEM_LOADED 1) 16 | -------------------------------------------------------------------------------- /MNN/python/main.py: -------------------------------------------------------------------------------- 1 | from voc_eval import voc_eval 2 | import os 3 | 4 | mAP = [] 5 | # 计算每个类别的AP 6 | for i in range(1): 7 | class_name = 'person' # 这里的类别名称为0,1,2,3,4,5,6,7 8 | rec, prec, ap = voc_eval('result/{}.txt', '/home/uc/RFSong-779-master/VOC2007/VOCdevkit/VOC2007/Annotations/{}.xml', 'test.txt', class_name, './') 9 | print("{} :\t {} ".format(class_name, ap)) 10 | #mAP.append(ap) 11 | 12 | #mAP = tuple(mAP) 13 | 14 | #print("***************************") 15 | # 输出总的mAP 16 | #print("mAP :\t {}".format( float( sum(mAP)/len(mAP)) )) 17 | -------------------------------------------------------------------------------- /MNN/mnn/include/NonCopyable.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // NonCopyable.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/09/19. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef NonCopyable_hpp 10 | #define NonCopyable_hpp 11 | 12 | namespace MNN { 13 | /** protocol class. used to delete assignment operator. */ 14 | class NonCopyable { 15 | public: 16 | NonCopyable() = default; 17 | NonCopyable(const NonCopyable&) = delete; 18 | NonCopyable(const NonCopyable&&) = delete; 19 | NonCopyable& operator=(const NonCopyable&) = delete; 20 | NonCopyable& operator=(const NonCopyable&&) = delete; 21 | }; 22 | } // namespace MNN 23 | 24 | #endif /* NonCopyable_hpp */ 25 | -------------------------------------------------------------------------------- /train-version-RFB.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | model_root_path="./models/train-version-RFB" 3 | log_dir="$model_root_path/logs" 4 | log="$log_dir/log" 5 | mkdir -p "$log_dir" 6 | 7 | python3 -u train.py \ 8 | --datasets \ 9 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB-1/VOC2007 \ 10 | --validation_dataset \ 11 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB-1/VOC2007 \ 12 | --net \ 13 | RFB \ 14 | --num_epochs \ 15 | 200 \ 16 | --milestones \ 17 | "95,150" \ 18 | --lr \ 19 | 1e-2 \ 20 | --batch_size \ 21 | 24 \ 22 | --input_size \ 23 | 320 \ 24 | --checkpoint_folder \ 25 | ${model_root_path} \ 26 | --num_workers \ 27 | 4 \ 28 | --log_dir \ 29 | ${log_dir} \ 30 | --cuda_index \ 31 | 0 \ 32 | 2>&1 | tee "$log" 33 | -------------------------------------------------------------------------------- /MNN/mnn/include/ErrorCode.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ErrorCode.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/09/18. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef ErrorCode_h 10 | #define ErrorCode_h 11 | 12 | namespace MNN { 13 | enum ErrorCode { 14 | #ifdef NO_ERROR 15 | #undef NO_ERROR 16 | #endif // NO_ERROR 17 | NO_ERROR = 0, 18 | OUT_OF_MEMORY = 1, 19 | NOT_SUPPORT = 2, 20 | COMPUTE_SIZE_ERROR = 3, 21 | NO_EXECUTION = 4, 22 | INVALID_VALUE = 5, 23 | 24 | // User error 25 | INPUT_DATA_ERROR = 10, 26 | CALL_BACK_STOP = 11, 27 | 28 | // Op Resize Error 29 | TENSOR_NOT_SUPPORT = 20, 30 | TENSOR_NEED_DIVIDE = 21, 31 | }; 32 | } // namespace MNN 33 | 34 | #endif /* ErrorCode_h */ 35 | -------------------------------------------------------------------------------- /MNN/mnn/include/revertMNNModel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // revertMNNModel.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2019/01/31. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef REVERTMNNMODEL_HPP 10 | #define REVERTMNNMODEL_HPP 11 | 12 | #include "mnn/MNN_generated.h" 13 | 14 | class Revert { 15 | public: 16 | Revert(const char* originalModelFileName); 17 | ~Revert(); 18 | void* getBuffer() const; 19 | const size_t getBufferSize() const; 20 | void initialize(); 21 | static float getRandValue(); 22 | 23 | private: 24 | Revert(); 25 | std::unique_ptr mMNNNet; 26 | size_t mBufferSize; 27 | std::shared_ptr mBuffer; 28 | void randStart(); 29 | void packMNNNet(); 30 | }; 31 | 32 | #endif // REVERTMNNMODEL_HPP 33 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/feature_tests.c: -------------------------------------------------------------------------------- 1 | 2 | const char features[] = {"\n" 3 | "C_FEATURE:" 4 | #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 304 5 | "1" 6 | #else 7 | "0" 8 | #endif 9 | "c_function_prototypes\n" 10 | "C_FEATURE:" 11 | #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 304 && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 12 | "1" 13 | #else 14 | "0" 15 | #endif 16 | "c_restrict\n" 17 | "C_FEATURE:" 18 | #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201000L 19 | "1" 20 | #else 21 | "0" 22 | #endif 23 | "c_static_assert\n" 24 | "C_FEATURE:" 25 | #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 304 && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 26 | "1" 27 | #else 28 | "0" 29 | #endif 30 | "c_variadic_macros\n" 31 | 32 | }; 33 | 34 | int main(int argc, char** argv) { (void)argv; return features[argc]; } 35 | -------------------------------------------------------------------------------- /MNN/mnn/include/MNNSharedContext.h: -------------------------------------------------------------------------------- 1 | // 2 | // MNNSharedContext.h 3 | // MNN 4 | // 5 | // Created by MNN on 2018/10/11. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef MNNSharedContext_h 10 | #define MNNSharedContext_h 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include /*uint32_t*/ 16 | 17 | #ifndef VK_DEFINE_HANDLE 18 | #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object; 19 | VK_DEFINE_HANDLE(VkInstance) 20 | VK_DEFINE_HANDLE(VkPhysicalDevice) 21 | VK_DEFINE_HANDLE(VkDevice) 22 | VK_DEFINE_HANDLE(VkQueue) 23 | #endif 24 | struct MNNVulkanContext { 25 | VkInstance pInstance; 26 | VkPhysicalDevice pPhysicalDevice; 27 | VkDevice pDevice; 28 | VkQueue pQueue; 29 | uint32_t iQueueFamilyIndex; 30 | }; 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif /* MNNSharedContext_h */ 36 | -------------------------------------------------------------------------------- /MNN/mnn/include/AutoTime.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // AutoTime.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/07/27. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef AutoTime_hpp 10 | #define AutoTime_hpp 11 | 12 | #include 13 | #include 14 | #include "MNNDefine.h" 15 | 16 | namespace MNN { 17 | 18 | /** time tracing util. prints duration between init and deinit. */ 19 | class MNN_PUBLIC AutoTime { 20 | public: 21 | AutoTime(int line, const char* func); 22 | ~AutoTime(); 23 | AutoTime(const AutoTime&) = delete; 24 | AutoTime(const AutoTime&&) = delete; 25 | AutoTime& operator=(const AutoTime&) = delete; 26 | AutoTime& operator=(const AutoTime&&) = delete; 27 | 28 | private: 29 | int mLine; 30 | char* mName; 31 | uint64_t mCurrentTime; 32 | }; 33 | } // namespace MNN 34 | 35 | #ifdef MNN_OPEN_TIME_TRACE 36 | #define AUTOTIME MNN::AutoTime ___t(__LINE__, __func__) 37 | #else 38 | #define AUTOTIME 39 | #endif 40 | 41 | #endif /* AutoTime_hpp */ 42 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "CXX" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_CXX 7 | "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp" "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o" 8 | "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp" "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o" 9 | ) 10 | set(CMAKE_CXX_COMPILER_ID "GNU") 11 | 12 | # The include file search paths: 13 | set(CMAKE_CXX_TARGET_INCLUDE_PATH 14 | "../mnn/include" 15 | "../src" 16 | "/usr/include/opencv" 17 | ) 18 | 19 | # Targets to which this target links. 20 | set(CMAKE_TARGET_LINKED_INFO_FILES 21 | ) 22 | 23 | # Fortran module output directory. 24 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 25 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o 5 | ../mnn/include/ErrorCode.hpp 6 | ../mnn/include/HalideRuntime.h 7 | ../mnn/include/ImageProcess.hpp 8 | ../mnn/include/Interpreter.hpp 9 | ../mnn/include/MNNDefine.h 10 | ../mnn/include/MNNForwardType.h 11 | ../mnn/include/Matrix.h 12 | ../mnn/include/Rect.h 13 | ../mnn/include/Tensor.hpp 14 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp 15 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.hpp 16 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o 17 | ../mnn/include/ErrorCode.hpp 18 | ../mnn/include/HalideRuntime.h 19 | ../mnn/include/ImageProcess.hpp 20 | ../mnn/include/Interpreter.hpp 21 | ../mnn/include/MNNDefine.h 22 | ../mnn/include/MNNForwardType.h 23 | ../mnn/include/Matrix.h 24 | ../mnn/include/Rect.h 25 | ../mnn/include/Tensor.hpp 26 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.hpp 27 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp 28 | -------------------------------------------------------------------------------- /vision/utils/misc.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | import torch 4 | 5 | 6 | def str2bool(s): 7 | return s.lower() in ('true', '1') 8 | 9 | 10 | class Timer: 11 | def __init__(self): 12 | self.clock = {} 13 | 14 | def start(self, key="default"): 15 | self.clock[key] = datetime.datetime.now() 16 | 17 | def end(self, key="default"): 18 | if key not in self.clock: 19 | raise Exception(f"{key} is not in the clock.") 20 | interval = datetime.datetime.now() - self.clock[key] 21 | del self.clock[key] 22 | return interval.total_seconds() 23 | 24 | 25 | def save_checkpoint(epoch, net_state_dict, optimizer_state_dict, best_score, checkpoint_path, model_path): 26 | torch.save({ 27 | 'epoch': epoch, 28 | 'model': net_state_dict, 29 | 'optimizer': optimizer_state_dict, 30 | 'best_score': best_score 31 | }, checkpoint_path) 32 | torch.save(net_state_dict, model_path) 33 | 34 | 35 | def load_checkpoint(checkpoint_path): 36 | return torch.load(checkpoint_path) 37 | 38 | 39 | def freeze_net_layers(net): 40 | for param in net.parameters(): 41 | param.requires_grad = False 42 | 43 | 44 | def store_labels(path, labels): 45 | with open(path, "w") as f: 46 | f.write("\n".join(labels)) 47 | -------------------------------------------------------------------------------- /MNN/src/main.cpp: -------------------------------------------------------------------------------- 1 | // Created by Linzaer on 2019/11/15. 2 | // Copyright © 2019 Linzaer. All rights reserved. 3 | 4 | #include "UltraFace.hpp" 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(int argc, char **argv) { 11 | if (argc <= 2) { 12 | fprintf(stderr, "Usage: %s [image files...]\n", argv[0]); 13 | return 1; 14 | } 15 | 16 | string mnn_path = argv[1]; 17 | UltraFace ultraface(mnn_path, 300, 300, 4, 0.6); // config model input 18 | 19 | for (int i = 2; i < argc; i++) { 20 | string image_file = argv[i]; 21 | cout << "Processing " << image_file << endl; 22 | 23 | cv::Mat frame = cv::imread(image_file); 24 | auto start = chrono::steady_clock::now(); 25 | vector face_info; 26 | ultraface.detect(frame, face_info); 27 | 28 | for (auto face : face_info) { 29 | cv::Point pt1(face.x1, face.y1); 30 | cv::Point pt2(face.x2, face.y2); 31 | cv::rectangle(frame, pt1, pt2, cv::Scalar(0, 255, 0), 2); 32 | } 33 | 34 | auto end = chrono::steady_clock::now(); 35 | chrono::duration elapsed = end - start; 36 | cout << "all time: " << elapsed.count() << " s" << endl; 37 | cv::imshow("UltraFace", frame); 38 | cv::waitKey(); 39 | string result_name = "result" + to_string(i) + ".jpg"; 40 | cv::imwrite(result_name, frame); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /convert_to_onnx.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code is used to convert the pytorch model into an onnx format model. 3 | """ 4 | import sys 5 | 6 | import torch.onnx 7 | 8 | from vision.ssd.config.fd_config import define_img_size 9 | 10 | input_img_size = 320 # define input size ,default optional(128/160/320/480/640/1280) 11 | define_img_size(input_img_size) 12 | from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd 13 | from vision.ssd.mb_tiny_fd import create_mb_tiny_fd 14 | 15 | # net_type = "slim" # inference faster,lower precision 16 | net_type = "RFB" # inference lower,higher precision 17 | 18 | label_path = "models/voc-model-labels.txt" 19 | class_names = [name.strip() for name in open(label_path).readlines()] 20 | num_classes = len(class_names) 21 | 22 | if net_type == 'slim': 23 | model_path = "models/pretrained/version-slim-320.pth" 24 | # model_path = "models/pretrained/version-slim-640.pth" 25 | net = create_mb_tiny_fd(len(class_names), is_test=True) 26 | elif net_type == 'RFB': 27 | model_path = "models/train-version-RFB/RFB-Epoch-299-Loss-2.968528504555042.pth" 28 | # model_path = "models/pretrained/version-RFB-640.pth" 29 | net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True) 30 | 31 | else: 32 | print("unsupport network type.") 33 | sys.exit(1) 34 | net.load(model_path) 35 | net.eval() 36 | net.to("cuda") 37 | 38 | model_name = model_path.split("/")[-1].split(".")[0] 39 | model_path = f"models/onnx/{model_name}.onnx" 40 | 41 | dummy_input = torch.randn(1, 3, 300, 300).to("cuda") 42 | # dummy_input = torch.randn(1, 3, 480, 640).to("cuda") #if input size is 640*480 43 | torch.onnx.export(net, dummy_input, model_path, verbose=False, input_names=['input'], output_names=['scores', 'boxes']) 44 | -------------------------------------------------------------------------------- /MNN/build/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | if(CMAKE_INSTALL_COMPONENT) 41 | set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt") 42 | else() 43 | set(CMAKE_INSTALL_MANIFEST "install_manifest.txt") 44 | endif() 45 | 46 | string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT 47 | "${CMAKE_INSTALL_MANIFEST_FILES}") 48 | file(WRITE "/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/${CMAKE_INSTALL_MANIFEST}" 49 | "${CMAKE_INSTALL_MANIFEST_CONTENT}") 50 | -------------------------------------------------------------------------------- /vision/ssd/config/fd_config.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from vision.utils.box_utils import generate_priors 4 | 5 | image_mean_test = image_mean = np.array([127, 127, 127]) 6 | image_std = 128.0 7 | iou_threshold = 0.3 8 | center_variance = 0.1 9 | size_variance = 0.2 10 | 11 | min_boxes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]] 12 | shrinkage_list = [] 13 | image_size = [300, 300] # default input size 320*240 14 | feature_map_w_h_list = [[40, 20, 10, 5], [30, 15, 8, 4]] # default feature map size 15 | priors = [] 16 | 17 | 18 | def define_img_size(size): 19 | global image_size, feature_map_w_h_list, priors 20 | img_size_dict = {128: [128, 96], 21 | 160: [160, 120], 22 | 320: [300, 300], 23 | 480: [480, 360], 24 | 640: [640, 480], 25 | 1280: [1280, 960]} 26 | image_size = img_size_dict[size] 27 | 28 | feature_map_w_h_list_dict = {128: [[16, 8, 4, 2], [12, 6, 3, 2]], 29 | 160: [[20, 10, 5, 3], [15, 8, 4, 2]], 30 | 320: [[38, 19, 10, 5], [38, 19, 10, 5]], 31 | 480: [[60, 30, 15, 8], [45, 23, 12, 6]], 32 | 640: [[80, 40, 20, 10], [60, 30, 15, 8]], 33 | 1280: [[160, 80, 40, 20], [120, 60, 30, 15]]} 34 | feature_map_w_h_list = feature_map_w_h_list_dict[size] 35 | 36 | for i in range(0, len(image_size)): 37 | item_list = [] 38 | for k in range(0, len(feature_map_w_h_list[i])): 39 | item_list.append(image_size[i] / feature_map_w_h_list[i][k]) 40 | shrinkage_list.append(item_list) 41 | priors = generate_priors(feature_map_w_h_list, shrinkage_list, image_size, min_boxes) 42 | -------------------------------------------------------------------------------- /MNN/mnn/include/MNNForwardType.h: -------------------------------------------------------------------------------- 1 | // 2 | // MNNForwardType.h 3 | // MNN 4 | // 5 | // Created by MNN on 2019/01/19. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef MNNForwardType_h 10 | #define MNNForwardType_h 11 | #include 12 | #include 13 | 14 | typedef enum { 15 | MNN_FORWARD_CPU = 0, 16 | 17 | /* 18 | Firtly find the first available backends not equal to CPU 19 | If no other backends, use cpu 20 | */ 21 | MNN_FORWARD_AUTO = 4, 22 | 23 | /*Hand write metal*/ 24 | MNN_FORWARD_METAL = 1, 25 | 26 | /*Use IOS's MPS instead of hand-write metal, Not Support yet*/ 27 | MNN_FORWARD_MPS = 2, 28 | 29 | /*Android / Common Device GPU API*/ 30 | MNN_FORWARD_OPENCL = 3, 31 | MNN_FORWARD_OPENGL = 6, 32 | MNN_FORWARD_VULKAN = 7, 33 | 34 | /*Android 8.1's NNAPI, Not Support yet*/ 35 | MNN_FORWARD_NN = 5, 36 | 37 | /*User can use API from Backend.hpp to add or search Backend*/ 38 | MNN_FORWARD_USER_0 = 8, 39 | MNN_FORWARD_USER_1 = 9, 40 | MNN_FORWARD_USER_2 = 10, 41 | MNN_FORWARD_USER_3 = 11, 42 | 43 | MNN_FORWARD_ALL 44 | } MNNForwardType; 45 | #ifdef __cplusplus 46 | namespace MNN { 47 | struct BackendConfig { 48 | enum MemoryMode { Memory_Normal = 0, Memory_High, Memory_Low }; 49 | 50 | MemoryMode memory = Memory_Normal; 51 | 52 | enum PowerMode { Power_Normal = 0, Power_High, Power_Low }; 53 | 54 | PowerMode power = Power_Normal; 55 | 56 | enum PrecisionMode { Precision_Normal = 0, Precision_High, Precision_Low }; 57 | 58 | PrecisionMode precision = Precision_Normal; 59 | 60 | /** user defined context */ 61 | union { 62 | void* sharedContext = nullptr; 63 | size_t flags; // Valid for CPU Backend 64 | }; 65 | }; 66 | }; // namespace MNN 67 | #endif 68 | #endif /* MNNForwardType_h */ 69 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/ErrorCode.hpp 5 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/HalideRuntime.h 6 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/ImageProcess.hpp 7 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/Interpreter.hpp 8 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/MNNDefine.h 9 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/MNNForwardType.h 10 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/Matrix.h 11 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/Rect.h 12 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../mnn/include/Tensor.hpp 13 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../src/UltraFace.cpp 14 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../src/UltraFace.hpp 15 | 16 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/ErrorCode.hpp 17 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/HalideRuntime.h 18 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/ImageProcess.hpp 19 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/Interpreter.hpp 20 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/MNNDefine.h 21 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/MNNForwardType.h 22 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/Matrix.h 23 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/Rect.h 24 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../mnn/include/Tensor.hpp 25 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../src/UltraFace.hpp 26 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../src/main.cpp 27 | 28 | -------------------------------------------------------------------------------- /vision/ssd/data_preprocessing.py: -------------------------------------------------------------------------------- 1 | from ..transforms.transforms import * 2 | 3 | 4 | class TrainAugmentation: 5 | def __init__(self, size, mean=0, std=1.0): 6 | """ 7 | Args: 8 | size: the size the of final image. 9 | mean: mean pixel value per channel. 10 | """ 11 | self.mean = mean 12 | self.size = size 13 | self.augment = Compose([ 14 | ConvertFromInts(), 15 | PhotometricDistort(), 16 | RandomSampleCrop_v2(), 17 | RandomMirror(), 18 | ToPercentCoords(), 19 | Resize(self.size), 20 | SubtractMeans(self.mean), 21 | lambda img, boxes=None, labels=None: (img / std, boxes, labels), 22 | ToTensor(), 23 | ]) 24 | 25 | def __call__(self, img, boxes, labels): 26 | """ 27 | 28 | Args: 29 | img: the output of cv.imread in RGB layout. 30 | boxes: boundding boxes in the form of (x1, y1, x2, y2). 31 | labels: labels of boxes. 32 | """ 33 | return self.augment(img, boxes, labels) 34 | 35 | 36 | class TestTransform: 37 | def __init__(self, size, mean=0.0, std=1.0): 38 | self.transform = Compose([ 39 | ToPercentCoords(), 40 | Resize(size), 41 | SubtractMeans(mean), 42 | lambda img, boxes=None, labels=None: (img / std, boxes, labels), 43 | ToTensor(), 44 | ]) 45 | 46 | def __call__(self, image, boxes, labels): 47 | return self.transform(image, boxes, labels) 48 | 49 | 50 | class PredictionTransform: 51 | def __init__(self, size, mean=0.0, std=1.0): 52 | self.transform = Compose([ 53 | Resize(size), 54 | SubtractMeans(mean), 55 | lambda img, boxes=None, labels=None: (img / std, boxes, labels), 56 | ToTensor() 57 | ]) 58 | 59 | def __call__(self, image): 60 | image, _, _ = self.transform(image) 61 | return image 62 | -------------------------------------------------------------------------------- /models/train-version-RFB/logs/log: -------------------------------------------------------------------------------- 1 | 2020-06-23 10:18:36,862 - root - INFO - inpu size :320 2 | priors nums:5329 3 | 2020-06-23 10:18:37,015 - root - INFO - Use Cuda. 4 | 2020-06-23 10:18:37,015 - root - INFO - Namespace(balance_data=False, base_net=None, base_net_lr=None, batch_size=24, checkpoint_folder='./models/train-version-RFB', cuda_index='0', dataset_type='voc', datasets=['/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB-1/VOC2007'], debug_steps=100, extra_layers_lr=None, freeze_base_net=False, freeze_net=False, gamma=0.1, input_size=320, log_dir='./models/train-version-RFB/logs', lr=0.01, milestones='95,150', momentum=0.9, net='RFB', num_epochs=200, num_workers=4, optimizer_type='SGD', overlap_threshold=0.35, power=2, pretrained_ssd=None, resume=None, scheduler='multi-step', t_max=120, use_cuda=True, validation_dataset='/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB-1/VOC2007', validation_epochs=5, weight_decay=0.0005) 5 | 2020-06-23 10:18:37,015 - root - INFO - Prepare training datasets. 6 | 2020-06-23 10:18:37,026 - root - INFO - No labels file, using default VOC classes. 7 | 2020-06-23 10:18:37,026 - root - INFO - Stored labels into file ./models/train-version-RFB/voc-model-labels.txt. 8 | 2020-06-23 10:18:37,026 - root - INFO - Train dataset size: 45174 9 | 2020-06-23 10:18:37,026 - root - INFO - Prepare Validation datasets. 10 | 2020-06-23 10:18:37,026 - root - INFO - No labels file, using default VOC classes. 11 | 2020-06-23 10:18:37,026 - root - INFO - validation dataset size: 576 12 | 2020-06-23 10:18:37,026 - root - INFO - Build network. 13 | 2020-06-23 10:18:38,212 - root - INFO - use gpu :[0] 14 | 2020-06-23 10:18:38,213 - root - INFO - Took 0.00 seconds to load the model. 15 | 2020-06-23 10:18:38,214 - root - INFO - Learning rate: 0.01, Base net learning rate: 0.01, Extra Layers learning rate: 0.01. 16 | 2020-06-23 10:18:38,214 - root - INFO - Uses MultiStepLR scheduler. 17 | 2020-06-23 10:18:38,215 - root - INFO - Start training from epoch 0. 18 | 2020-06-23 10:18:49,184 - root - INFO - Epoch: 0, Step: 100, Average Loss: 7.1722, Average Regression Loss 4.6603, Average Classification Loss: 2.5120 19 | -------------------------------------------------------------------------------- /vision/nn/mb_tiny.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | 5 | class Mb_Tiny(nn.Module): 6 | 7 | def __init__(self, num_classes=2): 8 | super(Mb_Tiny, self).__init__() 9 | self.base_channel = 8 * 2 10 | 11 | def conv_bn(inp, oup, stride): 12 | return nn.Sequential( 13 | nn.Conv2d(inp, oup, 3, stride, 1, bias=False), 14 | nn.BatchNorm2d(oup), 15 | nn.ReLU(inplace=True) 16 | ) 17 | 18 | def conv_dw(inp, oup, stride): 19 | return nn.Sequential( 20 | nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False), 21 | nn.BatchNorm2d(inp), 22 | nn.ReLU(inplace=True), 23 | 24 | nn.Conv2d(inp, oup, 1, 1, 0, bias=False), 25 | nn.BatchNorm2d(oup), 26 | nn.ReLU(inplace=True), 27 | ) 28 | 29 | self.model = nn.Sequential( 30 | conv_bn(3, self.base_channel, 2), # 160*120 31 | conv_dw(self.base_channel, self.base_channel * 2, 1), 32 | conv_dw(self.base_channel * 2, self.base_channel * 2, 2), # 80*60 33 | conv_dw(self.base_channel * 2, self.base_channel * 2, 1), 34 | conv_dw(self.base_channel * 2, self.base_channel * 4, 2), # 40*30 35 | conv_dw(self.base_channel * 4, self.base_channel * 4, 1), 36 | conv_dw(self.base_channel * 4, self.base_channel * 4, 1), 37 | conv_dw(self.base_channel * 4, self.base_channel * 4, 1), 38 | conv_dw(self.base_channel * 4, self.base_channel * 8, 2), # 20*15 39 | conv_dw(self.base_channel * 8, self.base_channel * 8, 1), 40 | conv_dw(self.base_channel * 8, self.base_channel * 8, 1), 41 | conv_dw(self.base_channel * 8, self.base_channel * 16, 2), # 10*8 42 | conv_dw(self.base_channel * 16, self.base_channel * 16, 1) 43 | ) 44 | self.fc = nn.Linear(1024, num_classes) 45 | 46 | def forward(self, x): 47 | x = self.model(x) 48 | x = F.avg_pool2d(x, 7) 49 | x = x.view(-1, 1024) 50 | x = self.fc(x) 51 | return x 52 | -------------------------------------------------------------------------------- /vision/nn/multibox_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | from ..utils import box_utils 6 | 7 | 8 | class MultiboxLoss(nn.Module): 9 | def __init__(self, priors, neg_pos_ratio, 10 | center_variance, size_variance, device): 11 | """Implement SSD Multibox Loss. 12 | 13 | Basically, Multibox loss combines classification loss 14 | and Smooth L1 regression loss. 15 | """ 16 | super(MultiboxLoss, self).__init__() 17 | self.neg_pos_ratio = neg_pos_ratio 18 | self.center_variance = center_variance 19 | self.size_variance = size_variance 20 | self.priors = priors 21 | self.priors.to(device) 22 | 23 | def forward(self, confidence, predicted_locations, labels, gt_locations): 24 | """Compute classification loss and smooth l1 loss. 25 | 26 | Args: 27 | confidence (batch_size, num_priors, num_classes): class predictions. 28 | locations (batch_size, num_priors, 4): predicted locations. 29 | labels (batch_size, num_priors): real labels of all the priors. 30 | boxes (batch_size, num_priors, 4): real boxes corresponding all the priors. 31 | """ 32 | num_classes = confidence.size(2) 33 | with torch.no_grad(): 34 | # derived from cross_entropy=sum(log(p)) 35 | loss = -F.log_softmax(confidence, dim=2)[:, :, 0] 36 | mask = box_utils.hard_negative_mining(loss, labels, self.neg_pos_ratio) 37 | 38 | confidence = confidence[mask, :] 39 | classification_loss = F.cross_entropy(confidence.reshape(-1, num_classes), labels[mask], reduction='sum') 40 | pos_mask = labels > 0 41 | predicted_locations = predicted_locations[pos_mask, :].reshape(-1, 4) 42 | gt_locations = gt_locations[pos_mask, :].reshape(-1, 4) 43 | smooth_l1_loss = F.smooth_l1_loss(predicted_locations, gt_locations, reduction='sum') # smooth_l1_loss 44 | # smooth_l1_loss = F.mse_loss(predicted_locations, gt_locations, reduction='sum') #l2 loss 45 | num_pos = gt_locations.size(0) 46 | return smooth_l1_loss / num_pos, classification_loss / num_pos 47 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/c++ CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o -o Ultra-face-mnn -L/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/mnn/lib -Wl,-rpath,/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/mnn/lib -lMNN /usr/lib/x86_64-linux-gnu/libopencv_shape.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_superres.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_aruco.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_bgsegm.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_bioinspired.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_ccalib.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_datasets.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_dpm.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_face.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_freetype.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_fuzzy.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_hdf.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_line_descriptor.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_optflow.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_plot.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_reg.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_saliency.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_stereo.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_structured_light.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_surface_matching.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_text.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_ximgproc.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_xobjdetect.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_xphoto.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_video.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_viz.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_phase_unwrapping.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_rgbd.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_photo.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_videoio.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2.0 /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2.0 2 | -------------------------------------------------------------------------------- /MNN/mnn/include/MNNDefine.h: -------------------------------------------------------------------------------- 1 | // 2 | // MNNDefine.h 3 | // MNN 4 | // 5 | // Created by MNN on 2018/08/09. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef MNNDefine_h 10 | #define MNNDefine_h 11 | 12 | #include 13 | #include 14 | 15 | #if defined(__APPLE__) 16 | #include "TargetConditionals.h" 17 | #if TARGET_OS_IPHONE 18 | #define MNN_BUILD_FOR_IOS 19 | #endif 20 | #endif 21 | 22 | #ifdef MNN_USE_LOGCAT 23 | #include 24 | #define MNN_ERROR(format, ...) __android_log_print(ANDROID_LOG_ERROR, "MNNJNI", format, ##__VA_ARGS__) 25 | #define MNN_PRINT(format, ...) __android_log_print(ANDROID_LOG_INFO, "MNNJNI", format, ##__VA_ARGS__) 26 | #else 27 | #define MNN_PRINT(format, ...) printf(format, ##__VA_ARGS__) 28 | #define MNN_ERROR(format, ...) printf(format, ##__VA_ARGS__) 29 | #endif 30 | 31 | #ifdef DEBUG 32 | #define MNN_ASSERT(x) \ 33 | { \ 34 | int res = (x); \ 35 | if (!res) { \ 36 | MNN_ERROR("Error for %s, %d\n", __FILE__, __LINE__); \ 37 | assert(res); \ 38 | } \ 39 | } 40 | #else 41 | #define MNN_ASSERT(x) \ 42 | { \ 43 | int res = (x); \ 44 | if (!res) { \ 45 | MNN_ERROR("Error for %s, %d\n", __FILE__, __LINE__); \ 46 | } \ 47 | } 48 | #endif 49 | 50 | #define FUNC_PRINT(x) MNN_PRINT(#x "=%d in %s, %d \n", x, __func__, __LINE__); 51 | #define FUNC_PRINT_ALL(x, type) MNN_PRINT(#x "=" #type " %" #type " in %s, %d \n", x, __func__, __LINE__); 52 | 53 | #define MNN_CHECK(success, log) \ 54 | if(!(success)){ \ 55 | MNN_ERROR("Check failed: %s ==> %s\n", #success, #log); \ 56 | } 57 | 58 | #if defined(_MSC_VER) 59 | #if defined(BUILDING_MNN_DLL) 60 | #define MNN_PUBLIC __declspec(dllexport) 61 | #elif defined(USING_MNN_DLL) 62 | #define MNN_PUBLIC __declspec(dllimport) 63 | #else 64 | #define MNN_PUBLIC 65 | #endif 66 | #else 67 | #define MNN_PUBLIC __attribute__((visibility("default"))) 68 | #endif 69 | 70 | #endif /* MNNDefine_h */ 71 | -------------------------------------------------------------------------------- /MNN/src/UltraFace.hpp: -------------------------------------------------------------------------------- 1 | // Created by Linzaer on 2019/11/15. 2 | // Copyright © 2019 Linzaer. All rights reserved. 3 | 4 | #ifndef UltraFace_hpp 5 | #define UltraFace_hpp 6 | 7 | #pragma once 8 | 9 | #include "Interpreter.hpp" 10 | 11 | #include "MNNDefine.h" 12 | #include "Tensor.hpp" 13 | #include "ImageProcess.hpp" 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #define num_featuremap 4 23 | #define hard_nms 1 24 | #define blending_nms 2 /* mix nms was been proposaled in paper blaze face, aims to minimize the temporal jitter*/ 25 | typedef struct FaceInfo { 26 | float x1; 27 | float y1; 28 | float x2; 29 | float y2; 30 | float score; 31 | 32 | } FaceInfo; 33 | 34 | class UltraFace { 35 | public: 36 | UltraFace(const std::string &mnn_path, 37 | int input_width, int input_length, int num_thread_ = 4, float score_threshold_ = 0.7, float iou_threshold_ = 0.3, 38 | int topk_ = -1); 39 | 40 | ~UltraFace(); 41 | 42 | int detect(cv::Mat &img, std::vector &face_list); 43 | 44 | private: 45 | void generateBBox(std::vector &bbox_collection, MNN::Tensor *scores, MNN::Tensor *boxes); 46 | 47 | void nms(std::vector &input, std::vector &output, int type = blending_nms); 48 | 49 | private: 50 | 51 | std::shared_ptr ultraface_interpreter; 52 | MNN::Session *ultraface_session = nullptr; 53 | MNN::Tensor *input_tensor = nullptr; 54 | 55 | int num_thread; 56 | int image_w; 57 | int image_h; 58 | 59 | int in_w; 60 | int in_h; 61 | int num_anchors; 62 | 63 | float score_threshold; 64 | float iou_threshold; 65 | 66 | 67 | const float mean_vals[3] = {127, 127, 127}; 68 | const float norm_vals[3] = {1.0 / 128, 1.0 / 128, 1.0 / 128}; 69 | 70 | const float center_variance = 0.1; 71 | const float size_variance = 0.2; 72 | const std::vector> min_boxes = { 73 | {10.0f, 16.0f, 24.0f}, 74 | {32.0f, 48.0f}, 75 | {64.0f, 96.0f}, 76 | {128.0f, 192.0f, 256.0f}}; 77 | const std::vector strides = {8.0, 16.0, 32.0, 64.0}; 78 | std::vector> featuremap_size; 79 | std::vector> shrinkage_size; 80 | std::vector w_h_list; 81 | 82 | std::vector> priors = {}; 83 | }; 84 | 85 | #endif /* UltraFace_hpp */ 86 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CMakeCCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_COMPILER "/usr/bin/cc") 2 | set(CMAKE_C_COMPILER_ARG1 "") 3 | set(CMAKE_C_COMPILER_ID "GNU") 4 | set(CMAKE_C_COMPILER_VERSION "7.5.0") 5 | set(CMAKE_C_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_C_COMPILER_WRAPPER "") 7 | set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "11") 8 | set(CMAKE_C_COMPILE_FEATURES "c_std_90;c_function_prototypes;c_std_99;c_restrict;c_variadic_macros;c_std_11;c_static_assert") 9 | set(CMAKE_C90_COMPILE_FEATURES "c_std_90;c_function_prototypes") 10 | set(CMAKE_C99_COMPILE_FEATURES "c_std_99;c_restrict;c_variadic_macros") 11 | set(CMAKE_C11_COMPILE_FEATURES "c_std_11;c_static_assert") 12 | 13 | set(CMAKE_C_PLATFORM_ID "Linux") 14 | set(CMAKE_C_SIMULATE_ID "") 15 | set(CMAKE_C_SIMULATE_VERSION "") 16 | 17 | 18 | 19 | set(CMAKE_AR "/usr/bin/ar") 20 | set(CMAKE_C_COMPILER_AR "/usr/bin/gcc-ar-7") 21 | set(CMAKE_RANLIB "/usr/bin/ranlib") 22 | set(CMAKE_C_COMPILER_RANLIB "/usr/bin/gcc-ranlib-7") 23 | set(CMAKE_LINKER "/usr/bin/ld") 24 | set(CMAKE_COMPILER_IS_GNUCC 1) 25 | set(CMAKE_C_COMPILER_LOADED 1) 26 | set(CMAKE_C_COMPILER_WORKS TRUE) 27 | set(CMAKE_C_ABI_COMPILED TRUE) 28 | set(CMAKE_COMPILER_IS_MINGW ) 29 | set(CMAKE_COMPILER_IS_CYGWIN ) 30 | if(CMAKE_COMPILER_IS_CYGWIN) 31 | set(CYGWIN 1) 32 | set(UNIX 1) 33 | endif() 34 | 35 | set(CMAKE_C_COMPILER_ENV_VAR "CC") 36 | 37 | if(CMAKE_COMPILER_IS_MINGW) 38 | set(MINGW 1) 39 | endif() 40 | set(CMAKE_C_COMPILER_ID_RUN 1) 41 | set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m) 42 | set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC) 43 | set(CMAKE_C_LINKER_PREFERENCE 10) 44 | 45 | # Save compiler ABI information. 46 | set(CMAKE_C_SIZEOF_DATA_PTR "8") 47 | set(CMAKE_C_COMPILER_ABI "ELF") 48 | set(CMAKE_C_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 49 | 50 | if(CMAKE_C_SIZEOF_DATA_PTR) 51 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}") 52 | endif() 53 | 54 | if(CMAKE_C_COMPILER_ABI) 55 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}") 56 | endif() 57 | 58 | if(CMAKE_C_LIBRARY_ARCHITECTURE) 59 | set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 60 | endif() 61 | 62 | set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "") 63 | if(CMAKE_C_CL_SHOWINCLUDES_PREFIX) 64 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}") 65 | endif() 66 | 67 | 68 | 69 | 70 | 71 | set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "gcc;gcc_s;c;gcc;gcc_s") 72 | set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/7;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib") 73 | set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 74 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/CXX.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | ../mnn/include/ErrorCode.hpp 10 | 11 | ../mnn/include/HalideRuntime.h 12 | stddef.h 13 | - 14 | stdint.h 15 | - 16 | stdbool.h 17 | - 18 | 19 | ../mnn/include/ImageProcess.hpp 20 | ErrorCode.hpp 21 | ../mnn/include/ErrorCode.hpp 22 | Matrix.h 23 | ../mnn/include/Matrix.h 24 | Tensor.hpp 25 | ../mnn/include/Tensor.hpp 26 | 27 | ../mnn/include/Interpreter.hpp 28 | functional 29 | - 30 | map 31 | - 32 | string 33 | - 34 | ErrorCode.hpp 35 | ../mnn/include/ErrorCode.hpp 36 | MNNForwardType.h 37 | ../mnn/include/MNNForwardType.h 38 | Tensor.hpp 39 | ../mnn/include/Tensor.hpp 40 | 41 | ../mnn/include/MNNDefine.h 42 | assert.h 43 | - 44 | stdio.h 45 | - 46 | TargetConditionals.h 47 | ../mnn/include/TargetConditionals.h 48 | android/log.h 49 | - 50 | 51 | ../mnn/include/MNNForwardType.h 52 | stdint.h 53 | - 54 | stddef.h 55 | - 56 | 57 | ../mnn/include/Matrix.h 58 | string.h 59 | - 60 | cstdint 61 | - 62 | Rect.h 63 | ../mnn/include/Rect.h 64 | 65 | ../mnn/include/Rect.h 66 | math.h 67 | - 68 | algorithm 69 | - 70 | utility 71 | - 72 | MNNDefine.h 73 | ../mnn/include/MNNDefine.h 74 | 75 | ../mnn/include/Tensor.hpp 76 | vector 77 | - 78 | HalideRuntime.h 79 | ../mnn/include/HalideRuntime.h 80 | MNNDefine.h 81 | ../mnn/include/MNNDefine.h 82 | 83 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp 84 | UltraFace.hpp 85 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.hpp 86 | 87 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.hpp 88 | Interpreter.hpp 89 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/Interpreter.hpp 90 | MNNDefine.h 91 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/MNNDefine.h 92 | Tensor.hpp 93 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/Tensor.hpp 94 | ImageProcess.hpp 95 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/ImageProcess.hpp 96 | opencv2/opencv.hpp 97 | - 98 | algorithm 99 | - 100 | iostream 101 | - 102 | string 103 | - 104 | vector 105 | - 106 | memory 107 | - 108 | chrono 109 | - 110 | 111 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp 112 | UltraFace.hpp 113 | /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.hpp 114 | iostream 115 | - 116 | opencv2/opencv.hpp 117 | - 118 | 119 | -------------------------------------------------------------------------------- /vision/ssd/mb_tiny_fd.py: -------------------------------------------------------------------------------- 1 | from torch.nn import Conv2d, Sequential, ModuleList, ReLU 2 | 3 | from vision.nn.mb_tiny import Mb_Tiny 4 | from vision.ssd.config import fd_config as config 5 | from vision.ssd.predictor import Predictor 6 | from vision.ssd.ssd import SSD 7 | 8 | 9 | def SeperableConv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0): 10 | """Replace Conv2d with a depthwise Conv2d and Pointwise Conv2d. 11 | """ 12 | return Sequential( 13 | Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, 14 | groups=in_channels, stride=stride, padding=padding), 15 | ReLU(), 16 | Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1), 17 | ) 18 | 19 | 20 | def create_mb_tiny_fd(num_classes, is_test=False, device="cuda"): 21 | base_net = Mb_Tiny(2) 22 | base_net_model = base_net.model # disable dropout layer 23 | 24 | source_layer_indexes = [ 25 | 8, 26 | 11, 27 | 13 28 | ] 29 | extras = ModuleList([ 30 | Sequential( 31 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=base_net.base_channel * 4, kernel_size=1), 32 | ReLU(), 33 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=base_net.base_channel * 16, kernel_size=3, stride=2, padding=1), 34 | ReLU() 35 | ) 36 | ]) 37 | 38 | regression_headers = ModuleList([ 39 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=3 * 4, kernel_size=3, padding=1), 40 | SeperableConv2d(in_channels=base_net.base_channel * 8, out_channels=2 * 4, kernel_size=3, padding=1), 41 | SeperableConv2d(in_channels=base_net.base_channel * 16, out_channels=2 * 4, kernel_size=3, padding=1), 42 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=3 * 4, kernel_size=3, padding=1) 43 | ]) 44 | 45 | classification_headers = ModuleList([ 46 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=3 * num_classes, kernel_size=3, padding=1), 47 | SeperableConv2d(in_channels=base_net.base_channel * 8, out_channels=2 * num_classes, kernel_size=3, padding=1), 48 | SeperableConv2d(in_channels=base_net.base_channel * 16, out_channels=2 * num_classes, kernel_size=3, padding=1), 49 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=3 * num_classes, kernel_size=3, padding=1) 50 | ]) 51 | 52 | return SSD(num_classes, base_net_model, source_layer_indexes, 53 | extras, classification_headers, regression_headers, is_test=is_test, config=config, device=device) 54 | 55 | 56 | def create_mb_tiny_fd_predictor(net, candidate_size=200, nms_method=None, sigma=0.5, device=None): 57 | predictor = Predictor(net, config.image_size, config.image_mean_test, 58 | config.image_std, 59 | nms_method=nms_method, 60 | iou_threshold=config.iou_threshold, 61 | candidate_size=candidate_size, 62 | sigma=sigma, 63 | device=device) 64 | return predictor 65 | -------------------------------------------------------------------------------- /vision/ssd/mb_tiny_RFB_fd.py: -------------------------------------------------------------------------------- 1 | from torch.nn import Conv2d, Sequential, ModuleList, ReLU 2 | 3 | from vision.nn.mb_tiny_RFB import Mb_Tiny_RFB 4 | from vision.ssd.config import fd_config as config 5 | from vision.ssd.predictor import Predictor 6 | from vision.ssd.ssd import SSD 7 | 8 | 9 | def SeperableConv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0): 10 | """Replace Conv2d with a depthwise Conv2d and Pointwise Conv2d. 11 | """ 12 | return Sequential( 13 | Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, 14 | groups=in_channels, stride=stride, padding=padding), 15 | ReLU(), 16 | Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1), 17 | ) 18 | 19 | 20 | def create_Mb_Tiny_RFB_fd(num_classes, is_test=False, device="cuda"): 21 | base_net = Mb_Tiny_RFB(2) 22 | base_net_model = base_net.model # disable dropout layer 23 | 24 | source_layer_indexes = [ 25 | 8, 26 | 11, 27 | 13 28 | ] 29 | extras = ModuleList([ 30 | Sequential( 31 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=base_net.base_channel * 4, kernel_size=1), 32 | ReLU(), 33 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=base_net.base_channel * 16, kernel_size=3, stride=2, padding=1), 34 | ReLU() 35 | ) 36 | ]) 37 | 38 | regression_headers = ModuleList([ 39 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=3 * 4, kernel_size=3, padding=1), 40 | SeperableConv2d(in_channels=base_net.base_channel * 8, out_channels=2 * 4, kernel_size=3, padding=1), 41 | SeperableConv2d(in_channels=base_net.base_channel * 16, out_channels=2 * 4, kernel_size=3, padding=1), 42 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=3 * 4, kernel_size=3, padding=1) 43 | ]) 44 | 45 | classification_headers = ModuleList([ 46 | SeperableConv2d(in_channels=base_net.base_channel * 4, out_channels=3 * num_classes, kernel_size=3, padding=1), 47 | SeperableConv2d(in_channels=base_net.base_channel * 8, out_channels=2 * num_classes, kernel_size=3, padding=1), 48 | SeperableConv2d(in_channels=base_net.base_channel * 16, out_channels=2 * num_classes, kernel_size=3, padding=1), 49 | Conv2d(in_channels=base_net.base_channel * 16, out_channels=3 * num_classes, kernel_size=3, padding=1) 50 | ]) 51 | 52 | return SSD(num_classes, base_net_model, source_layer_indexes, 53 | extras, classification_headers, regression_headers, is_test=is_test, config=config, device=device) 54 | 55 | 56 | def create_Mb_Tiny_RFB_fd_predictor(net, candidate_size=200, nms_method=None, sigma=0.5, device=None): 57 | predictor = Predictor(net, config.image_size, config.image_mean_test, 58 | config.image_std, 59 | nms_method=nms_method, 60 | iou_threshold=config.iou_threshold, 61 | candidate_size=candidate_size, 62 | sigma=sigma, 63 | device=device) 64 | return predictor 65 | -------------------------------------------------------------------------------- /vision/ssd/predictor.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from ..utils import box_utils 4 | from .data_preprocessing import PredictionTransform 5 | from ..utils.misc import Timer 6 | 7 | 8 | class Predictor: 9 | def __init__(self, net, size, mean=0.0, std=1.0, nms_method=None, 10 | iou_threshold=0.3, filter_threshold=0.01, candidate_size=200, sigma=0.5, device=None): 11 | self.net = net 12 | self.transform = PredictionTransform(size, mean, std) 13 | self.iou_threshold = iou_threshold 14 | self.filter_threshold = filter_threshold 15 | self.candidate_size = candidate_size 16 | self.nms_method = nms_method 17 | 18 | self.sigma = sigma 19 | if device: 20 | self.device = device 21 | else: 22 | self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 23 | 24 | self.net.to(self.device) 25 | self.net.eval() 26 | 27 | self.timer = Timer() 28 | 29 | def predict(self, image, top_k=-1, prob_threshold=None): 30 | cpu_device = torch.device("cpu") 31 | height, width, _ = image.shape 32 | image = self.transform(image) 33 | images = image.unsqueeze(0) 34 | images = images.to(self.device) 35 | with torch.no_grad(): 36 | for i in range(1): 37 | self.timer.start() 38 | scores, boxes = self.net.forward(images) 39 | print("Inference time: ", self.timer.end()) 40 | boxes = boxes[0] 41 | scores = scores[0] 42 | if not prob_threshold: 43 | prob_threshold = self.filter_threshold 44 | # this version of nms is slower on GPU, so we move data to CPU. 45 | boxes = boxes.to(cpu_device) 46 | scores = scores.to(cpu_device) 47 | picked_box_probs = [] 48 | picked_labels = [] 49 | for class_index in range(1, scores.size(1)): 50 | probs = scores[:, class_index] 51 | mask = probs > prob_threshold 52 | probs = probs[mask] 53 | if probs.size(0) == 0: 54 | continue 55 | subset_boxes = boxes[mask, :] 56 | box_probs = torch.cat([subset_boxes, probs.reshape(-1, 1)], dim=1) 57 | box_probs = box_utils.nms(box_probs, self.nms_method, 58 | score_threshold=prob_threshold, 59 | iou_threshold=self.iou_threshold, 60 | sigma=self.sigma, 61 | top_k=top_k, 62 | candidate_size=self.candidate_size) 63 | picked_box_probs.append(box_probs) 64 | picked_labels.extend([class_index] * box_probs.size(0)) 65 | if not picked_box_probs: 66 | return torch.tensor([]), torch.tensor([]), torch.tensor([]) 67 | picked_box_probs = torch.cat(picked_box_probs) 68 | picked_box_probs[:, 0] *= width 69 | picked_box_probs[:, 1] *= height 70 | picked_box_probs[:, 2] *= width 71 | picked_box_probs[:, 3] *= height 72 | return picked_box_probs[:, :4], torch.tensor(picked_labels), picked_box_probs[:, 4] 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 视频展示检测效果 2 | 由于训练的Epoch较少,检测效果并不是很理想。大家可以自己去调参来提升检测效果。 3 | https://www.bilibili.com/video/BV1hZ4y1H7f9/ 4 | # person-detector 5 | ## 环境搭建 6 | 1. 首先安装pytorch环境。pytorch版本1.2及以上版本。 7 | ```python 8 | conda install pytorch torchvision cudatoolkit=10.1 -c pytorch //安装最新pytorch1.5 9 | ``` 10 | 2. 安装编译MNN。 11 | 2.1. [MNN安装过程请参考我的博客](https://blog.csdn.net/donkey_1993/article/details/106378976) 12 | 2.2. 安装包安装完成之后会在`MNN/build`文件夹里面生成一个`libMNN.so`文件,需要将这个文件复制到`person-detector/MNN/mnn/lib`进行替换。 13 | 3. 其他安装包安装。 14 | ```python 15 | conda install 安装包 16 | or 17 | pip install 安装包 18 | ``` 19 | ## 测试过程 20 | ### python测试教程 21 | 1. 直接运行`detect_imgs.py`就可以检测`imgs`文件夹里面的图片。 22 | ```python 23 | python detect_imgs.py 24 | ``` 25 | ### MNN中测试过程 26 | #### 首先需要对person-detector/MNN进行编译 27 | 1. 将person-detector/MNN/build删除。 28 | 2. 然后建立build文件,重新进行编译。 29 | ```person 30 | cd person-detector/MNN 31 | mkdir build 32 | cd build 33 | cmake .. 34 | make -j8 35 | ``` 36 | #### 运行MNN中python版本 37 | ```python 38 | 1. cd person-detector/MNN/python 39 | 2. python person-detector-pic.py --imgs_path ../imgs # 检测图片,可以在person-detector-pic.py里面修改图片测试路径。 40 | 3. python person-detector-video.py # 检测视频,可以在person-detector-video.py里面修改视频测试路径。 41 | ``` 42 | #### 运行MNN中C++版本 43 | ```bash 44 | cd build 45 | ./Ultra-face-mnn ../model/version-RFB/617-1.mnn ../imgs/timg.jpg 46 | ``` 47 | ```bash 48 | # 量化之后的版本 49 | cd build 50 | ./Ultra-face-mnn ../model/version-RFB/617-1-sq.mnn ../imgs/timg.jpg 51 | ``` 52 | ## 训练过程 53 | ### 数据集准备 54 | 1. 训练数据集采用的是VOC格式的数据集。训练数据集路径可以在`train-version-RFB.sh`里面修改。 55 | 2. 使用脚本可以将`coco`数据集转成`VOC`格式且只有行人检测框的数据集。 56 | [COCO数据集转VOC数据集只包含行人的转换教程](https://blog.csdn.net/donkey_1993/article/details/106279988) 57 | ### 运行训练 58 | ```python 59 | bash train-version-RFB.sh 60 | ``` 61 | ## 模型转换 62 | 1. pth模型转onnx模型,在转模型之间需要将person-detector/version/ssd/ssd.py进行修改,修改成下面的样子。 63 | ```python 64 | if self.is_test: 65 | confidences = F.softmax(confidences, dim=2) 66 | boxes = locations 67 | #boxes = box_utils.convert_locations_to_boxes( 68 | # locations, self.priors, self.config.center_variance, self.config.size_variance 69 | #) 70 | #boxes = box_utils.center_form_to_corner_form(boxes) 71 | return confidences, boxes 72 | ``` 73 | 然后再运行转换文件。 74 | ```python 75 | python convert_to_onnx.py 76 | ``` 77 | 2. 将onnx模型转成mnn模型 78 | 2.1 进到主目录下的MNN/build文件里面(不是person-detector/MNN文件) 79 | 2.2 运行MNNConvert来转换模型 80 | ```python 81 | ./MNNConvert -f ONNX --modelFile XXX.onnx --MNNModel XXX.mnn --bizCode biz 82 | ``` 83 | 3. 模型的INT8量化 84 | 3.1 编译MNN的量化工具。 85 | ```python 86 | cd MNN/build 87 | cmake .. -DMNN_BUILD_QUANTOOLS=on 88 | make -j8 89 | ``` 90 | 3.2 构建一个pretreatConfig.json文件,代码如下: 91 | ```python 92 | { 93 | "format":"RGB", 94 | "mean":[ 95 | 127.5, 96 | 127.5, 97 | 127.5 98 | ], 99 | "normal":[ 100 | 0.00784314, 101 | 0.00784314, 102 | 0.00784314 103 | ], 104 | "width":224, 105 | "height":224, 106 | "path":"path/to/images/", 107 | "used_image_num":500, 108 | "feature_quantize_method":"KL", 109 | "weight_quantize_method":"MAX_ABS" 110 | } 111 | ``` 112 | 3.3 在MNN/build里面运行量化程序。 113 | ```python 114 | ./quantized.out XXX.mnn XXX-INT8.mnn pretreatConfig.json 115 | ``` 116 | ## 最后感谢下面的作者 117 | https://www.yuque.com/mnn/cn/tool_quantize 118 | https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB 119 | https://github.com/ruinmessi/RFBNet 120 | -------------------------------------------------------------------------------- /detect_imgs.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code is used to batch detect images in a folder. 3 | """ 4 | import argparse 5 | import os 6 | import sys 7 | import numpy 8 | import cv2 9 | 10 | from vision.ssd.config.fd_config import define_img_size 11 | 12 | parser = argparse.ArgumentParser( 13 | description='detect_imgs') 14 | 15 | parser.add_argument('--net_type', default="RFB", type=str, 16 | help='The network architecture ,optional: RFB (higher precision) or slim (faster)') 17 | parser.add_argument('--input_size', default=320, type=int, 18 | help='define network input size,default optional value 128/160/320/480/640/1280') 19 | parser.add_argument('--threshold', default=0.5, type=float, 20 | help='score threshold') 21 | parser.add_argument('--candidate_size', default=1500, type=int, 22 | help='nms candidate size') 23 | parser.add_argument('--path', default="imgs", type=str, 24 | help='imgs dir') 25 | parser.add_argument('--test_device', default="cuda:0", type=str, 26 | help='cuda:0 or cpu') 27 | args = parser.parse_args() 28 | define_img_size(args.input_size) # must put define_img_size() before 'import create_mb_tiny_fd, create_mb_tiny_fd_predictor' 29 | 30 | from vision.ssd.mb_tiny_fd import create_mb_tiny_fd, create_mb_tiny_fd_predictor 31 | from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd, create_Mb_Tiny_RFB_fd_predictor 32 | 33 | result_path = "./detect_imgs_results" 34 | label_path = "./models/voc-model-labels.txt" 35 | test_device = args.test_device 36 | 37 | class_names = [name.strip() for name in open(label_path).readlines()] 38 | if args.net_type == 'slim': 39 | model_path = "models/pretrained/version-slim-320.pth" 40 | # model_path = "models/pretrained/version-slim-640.pth" 41 | net = create_mb_tiny_fd(len(class_names), is_test=True, device=test_device) 42 | predictor = create_mb_tiny_fd_predictor(net, candidate_size=args.candidate_size, device=test_device) 43 | elif args.net_type == 'RFB': 44 | model_path = "models/train-version-RFB/RFB-Epoch-299-Loss-2.968528504555042.pth" 45 | # model_path = "models/pretrained/version-RFB-640.pth" 46 | net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True, device=test_device) 47 | predictor = create_Mb_Tiny_RFB_fd_predictor(net, candidate_size=args.candidate_size, device=test_device) 48 | else: 49 | print("The net type is wrong!") 50 | sys.exit(1) 51 | net.load(model_path) 52 | 53 | if not os.path.exists(result_path): 54 | os.makedirs(result_path) 55 | listdir = os.listdir(args.path) 56 | sum = 0 57 | f = open('dict.txt','w') 58 | for file_path in listdir: 59 | img_path = os.path.join(args.path, file_path) 60 | orig_image = cv2.imread(img_path) 61 | image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) 62 | boxes, labels, probs = predictor.predict(image, args.candidate_size / 2, args.threshold) 63 | probs = probs.numpy() 64 | sum += boxes.size(0) 65 | for i in range(boxes.size(0)): 66 | box = boxes[i, :] 67 | cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2) 68 | b = probs[i] 69 | f.writelines([file_path[:-4],' ',str(b),' ',str(int(box[0])),' ',str(int(box[1])),' ',str(int(box[2])),' ',str(int(box[3])),'\n']) 70 | # label = f"""{voc_dataset.class_names[labels[i]]}: {probs[i]:.2f}""" 71 | label = f"{probs[i]:.2f}" 72 | # cv2.putText(orig_image, label, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 73 | cv2.putText(orig_image, str(boxes.size(0)), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 74 | cv2.imwrite(os.path.join(result_path, file_path), orig_image) 75 | print(f"Found {len(probs)} faces. The output image is {result_path}") 76 | print(sum) 77 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Makefile2: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | # The main recursive all target 10 | all: 11 | 12 | .PHONY : all 13 | 14 | # The main recursive preinstall target 15 | preinstall: 16 | 17 | .PHONY : preinstall 18 | 19 | #============================================================================= 20 | # Special targets provided by cmake. 21 | 22 | # Disable implicit rules so canonical targets will work. 23 | .SUFFIXES: 24 | 25 | 26 | # Remove some rules from gmake that .SUFFIXES does not remove. 27 | SUFFIXES = 28 | 29 | .SUFFIXES: .hpux_make_needs_suffix_list 30 | 31 | 32 | # Suppress display of executed commands. 33 | $(VERBOSE).SILENT: 34 | 35 | 36 | # A target that is always out of date. 37 | cmake_force: 38 | 39 | .PHONY : cmake_force 40 | 41 | #============================================================================= 42 | # Set environment variables for the build. 43 | 44 | # The shell in which to execute make rules. 45 | SHELL = /bin/sh 46 | 47 | # The CMake executable. 48 | CMAKE_COMMAND = /usr/bin/cmake 49 | 50 | # The command to remove a file. 51 | RM = /usr/bin/cmake -E remove -f 52 | 53 | # Escaping for special characters. 54 | EQUALS = = 55 | 56 | # The top-level source directory on which CMake was run. 57 | CMAKE_SOURCE_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN 58 | 59 | # The top-level build directory on which CMake was run. 60 | CMAKE_BINARY_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build 61 | 62 | #============================================================================= 63 | # Target rules for target CMakeFiles/Ultra-face-mnn.dir 64 | 65 | # All Build rule for target. 66 | CMakeFiles/Ultra-face-mnn.dir/all: 67 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/depend 68 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/build 69 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles --progress-num=1,2,3 "Built target Ultra-face-mnn" 70 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/all 71 | 72 | # Include target in all. 73 | all: CMakeFiles/Ultra-face-mnn.dir/all 74 | 75 | .PHONY : all 76 | 77 | # Build rule for subdir invocation for target. 78 | CMakeFiles/Ultra-face-mnn.dir/rule: cmake_check_build_system 79 | $(CMAKE_COMMAND) -E cmake_progress_start /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles 3 80 | $(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/Ultra-face-mnn.dir/all 81 | $(CMAKE_COMMAND) -E cmake_progress_start /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles 0 82 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/rule 83 | 84 | # Convenience name for target. 85 | Ultra-face-mnn: CMakeFiles/Ultra-face-mnn.dir/rule 86 | 87 | .PHONY : Ultra-face-mnn 88 | 89 | # clean rule for target. 90 | CMakeFiles/Ultra-face-mnn.dir/clean: 91 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/clean 92 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/clean 93 | 94 | # clean rule for target. 95 | clean: CMakeFiles/Ultra-face-mnn.dir/clean 96 | 97 | .PHONY : clean 98 | 99 | #============================================================================= 100 | # Special targets to cleanup operation of make. 101 | 102 | # Special rule to run CMake to check the build system integrity. 103 | # No rule that depends on this can have commands that come from listfiles 104 | # because they might be regenerated. 105 | cmake_check_build_system: 106 | $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 107 | .PHONY : cmake_check_build_system 108 | 109 | -------------------------------------------------------------------------------- /vision/datasets/voc_dataset.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | import pathlib 4 | import xml.etree.ElementTree as ET 5 | 6 | import cv2 7 | import numpy as np 8 | 9 | 10 | class VOCDataset: 11 | 12 | def __init__(self, root, transform=None, target_transform=None, is_test=False, keep_difficult=False, label_file=None): 13 | """Dataset for VOC data. 14 | Args: 15 | root: the root of the VOC2007 or VOC2012 dataset, the directory contains the following sub-directories: 16 | Annotations, ImageSets, JPEGImages, SegmentationClass, SegmentationObject. 17 | """ 18 | self.root = pathlib.Path(root) 19 | self.transform = transform 20 | self.target_transform = target_transform 21 | if is_test: 22 | image_sets_file = self.root / "ImageSets/Main/test.txt" 23 | else: 24 | image_sets_file = self.root / "ImageSets/Main/trainval.txt" 25 | self.ids = VOCDataset._read_image_ids(image_sets_file) 26 | self.keep_difficult = keep_difficult 27 | 28 | # if the labels file exists, read in the class names 29 | label_file_name = self.root / "labels.txt" 30 | 31 | if os.path.isfile(label_file_name): 32 | class_string = "" 33 | with open(label_file_name, 'r') as infile: 34 | for line in infile: 35 | class_string += line.rstrip() 36 | 37 | # classes should be a comma separated list 38 | 39 | classes = class_string.split(',') 40 | # prepend BACKGROUND as first class 41 | classes.insert(0, 'BACKGROUND') 42 | classes = [elem.replace(" ", "") for elem in classes] 43 | self.class_names = tuple(classes) 44 | logging.info("VOC Labels read from file: " + str(self.class_names)) 45 | 46 | else: 47 | logging.info("No labels file, using default VOC classes.") 48 | self.class_names = ('BACKGROUND', 49 | 'person') 50 | 51 | self.class_dict = {class_name: i for i, class_name in enumerate(self.class_names)} 52 | 53 | def __getitem__(self, index): 54 | image_id = self.ids[index] 55 | boxes, labels, is_difficult = self._get_annotation(image_id) 56 | if not self.keep_difficult: 57 | boxes = boxes[is_difficult == 0] 58 | labels = labels[is_difficult == 0] 59 | image = self._read_image(image_id) 60 | if self.transform: 61 | image, boxes, labels = self.transform(image, boxes, labels) 62 | if self.target_transform: 63 | boxes, labels = self.target_transform(boxes, labels) 64 | return image, boxes, labels 65 | 66 | def get_image(self, index): 67 | image_id = self.ids[index] 68 | image = self._read_image(image_id) 69 | if self.transform: 70 | image, _ = self.transform(image) 71 | return image 72 | 73 | def get_annotation(self, index): 74 | image_id = self.ids[index] 75 | return image_id, self._get_annotation(image_id) 76 | 77 | def __len__(self): 78 | return len(self.ids) 79 | 80 | @staticmethod 81 | def _read_image_ids(image_sets_file): 82 | ids = [] 83 | with open(image_sets_file) as f: 84 | for line in f: 85 | ids.append(line.rstrip()) 86 | return ids 87 | 88 | def _get_annotation(self, image_id): 89 | annotation_file = self.root / f"Annotations/{image_id}.xml" 90 | objects = ET.parse(annotation_file).findall("object") 91 | boxes = [] 92 | labels = [] 93 | is_difficult = [] 94 | for object in objects: 95 | class_name = object.find('name').text.lower().strip() 96 | # we're only concerned with clases in our list 97 | if class_name in self.class_dict: 98 | bbox = object.find('bndbox') 99 | 100 | # VOC dataset format follows Matlab, in which indexes start from 0 101 | x1 = float(bbox.find('xmin').text) - 1 102 | y1 = float(bbox.find('ymin').text) - 1 103 | x2 = float(bbox.find('xmax').text) - 1 104 | y2 = float(bbox.find('ymax').text) - 1 105 | boxes.append([x1, y1, x2, y2]) 106 | 107 | labels.append(self.class_dict[class_name]) 108 | is_difficult_str = object.find('difficult').text 109 | is_difficult.append(int(is_difficult_str) if is_difficult_str else 0) 110 | 111 | return (np.array(boxes, dtype=np.float32), 112 | np.array(labels, dtype=np.int64), 113 | np.array(is_difficult, dtype=np.uint8)) 114 | 115 | def _read_image(self, image_id): 116 | image_file = self.root / f"JPEGImages/{image_id}.jpg" 117 | image = cv2.imread(str(image_file)) 118 | image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 119 | return image 120 | -------------------------------------------------------------------------------- /vision/utils/box_utils_numpy.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def convert_locations_to_boxes(locations, priors, center_variance, 5 | size_variance): 6 | """Convert regressional location results of SSD into boxes in the form of (center_x, center_y, h, w). 7 | 8 | The conversion: 9 | $$predicted\_center * center_variance = \frac {real\_center - prior\_center} {prior\_hw}$$ 10 | $$exp(predicted\_hw * size_variance) = \frac {real\_hw} {prior\_hw}$$ 11 | We do it in the inverse direction here. 12 | Args: 13 | locations (batch_size, num_priors, 4): the regression output of SSD. It will contain the outputs as well. 14 | priors (num_priors, 4) or (batch_size/1, num_priors, 4): prior boxes. 15 | center_variance: a float used to change the scale of center. 16 | size_variance: a float used to change of scale of size. 17 | Returns: 18 | boxes: priors: [[center_x, center_y, h, w]]. All the values 19 | are relative to the image size. 20 | """ 21 | # priors can have one dimension less. 22 | if len(priors.shape) + 1 == len(locations.shape): 23 | priors = np.expand_dims(priors, 0) 24 | return np.concatenate([ 25 | locations[..., :2] * center_variance * priors[..., 2:] + priors[..., :2], 26 | np.exp(locations[..., 2:] * size_variance) * priors[..., 2:] 27 | ], axis=len(locations.shape) - 1) 28 | 29 | 30 | def convert_boxes_to_locations(center_form_boxes, center_form_priors, center_variance, size_variance): 31 | # priors can have one dimension less 32 | if len(center_form_priors.shape) + 1 == len(center_form_boxes.shape): 33 | center_form_priors = np.expand_dims(center_form_priors, 0) 34 | return np.concatenate([ 35 | (center_form_boxes[..., :2] - center_form_priors[..., :2]) / center_form_priors[..., 2:] / center_variance, 36 | np.log(center_form_boxes[..., 2:] / center_form_priors[..., 2:]) / size_variance 37 | ], axis=len(center_form_boxes.shape) - 1) 38 | 39 | 40 | def area_of(left_top, right_bottom): 41 | """Compute the areas of rectangles given two corners. 42 | 43 | Args: 44 | left_top (N, 2): left top corner. 45 | right_bottom (N, 2): right bottom corner. 46 | 47 | Returns: 48 | area (N): return the area. 49 | """ 50 | hw = np.clip(right_bottom - left_top, 0.0, None) 51 | return hw[..., 0] * hw[..., 1] 52 | 53 | 54 | def iou_of(boxes0, boxes1, eps=1e-5): 55 | """Return intersection-over-union (Jaccard index) of boxes. 56 | 57 | Args: 58 | boxes0 (N, 4): ground truth boxes. 59 | boxes1 (N or 1, 4): predicted boxes. 60 | eps: a small number to avoid 0 as denominator. 61 | Returns: 62 | iou (N): IoU values. 63 | """ 64 | overlap_left_top = np.maximum(boxes0[..., :2], boxes1[..., :2]) 65 | overlap_right_bottom = np.minimum(boxes0[..., 2:], boxes1[..., 2:]) 66 | 67 | overlap_area = area_of(overlap_left_top, overlap_right_bottom) 68 | area0 = area_of(boxes0[..., :2], boxes0[..., 2:]) 69 | area1 = area_of(boxes1[..., :2], boxes1[..., 2:]) 70 | return overlap_area / (area0 + area1 - overlap_area + eps) 71 | 72 | 73 | def center_form_to_corner_form(locations): 74 | return np.concatenate([locations[..., :2] - locations[..., 2:] / 2, 75 | locations[..., :2] + locations[..., 2:] / 2], len(locations.shape) - 1) 76 | 77 | 78 | def corner_form_to_center_form(boxes): 79 | return np.concatenate([ 80 | (boxes[..., :2] + boxes[..., 2:]) / 2, 81 | boxes[..., 2:] - boxes[..., :2] 82 | ], len(boxes.shape) - 1) 83 | 84 | 85 | def hard_nms(box_scores, iou_threshold, top_k=-1, candidate_size=200): 86 | """ 87 | 88 | Args: 89 | box_scores (N, 5): boxes in corner-form and probabilities. 90 | iou_threshold: intersection over union threshold. 91 | top_k: keep top_k results. If k <= 0, keep all the results. 92 | candidate_size: only consider the candidates with the highest scores. 93 | Returns: 94 | picked: a list of indexes of the kept boxes 95 | """ 96 | scores = box_scores[:, -1] 97 | boxes = box_scores[:, :-1] 98 | picked = [] 99 | # _, indexes = scores.sort(descending=True) 100 | indexes = np.argsort(scores) 101 | # indexes = indexes[:candidate_size] 102 | indexes = indexes[-candidate_size:] 103 | while len(indexes) > 0: 104 | # current = indexes[0] 105 | current = indexes[-1] 106 | picked.append(current) 107 | if 0 < top_k == len(picked) or len(indexes) == 1: 108 | break 109 | current_box = boxes[current, :] 110 | # indexes = indexes[1:] 111 | indexes = indexes[:-1] 112 | rest_boxes = boxes[indexes, :] 113 | iou = iou_of( 114 | rest_boxes, 115 | np.expand_dims(current_box, axis=0), 116 | ) 117 | indexes = indexes[iou <= iou_threshold] 118 | 119 | return box_scores[picked, :] 120 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/3.10.2/CMakeCXXCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_CXX_COMPILER "/usr/bin/c++") 2 | set(CMAKE_CXX_COMPILER_ARG1 "") 3 | set(CMAKE_CXX_COMPILER_ID "GNU") 4 | set(CMAKE_CXX_COMPILER_VERSION "7.5.0") 5 | set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_CXX_COMPILER_WRAPPER "") 7 | set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "14") 8 | set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17") 9 | set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters") 10 | set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates") 11 | set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates") 12 | set(CMAKE_CXX17_COMPILE_FEATURES "cxx_std_17") 13 | 14 | set(CMAKE_CXX_PLATFORM_ID "Linux") 15 | set(CMAKE_CXX_SIMULATE_ID "") 16 | set(CMAKE_CXX_SIMULATE_VERSION "") 17 | 18 | 19 | 20 | set(CMAKE_AR "/usr/bin/ar") 21 | set(CMAKE_CXX_COMPILER_AR "/usr/bin/gcc-ar-7") 22 | set(CMAKE_RANLIB "/usr/bin/ranlib") 23 | set(CMAKE_CXX_COMPILER_RANLIB "/usr/bin/gcc-ranlib-7") 24 | set(CMAKE_LINKER "/usr/bin/ld") 25 | set(CMAKE_COMPILER_IS_GNUCXX 1) 26 | set(CMAKE_CXX_COMPILER_LOADED 1) 27 | set(CMAKE_CXX_COMPILER_WORKS TRUE) 28 | set(CMAKE_CXX_ABI_COMPILED TRUE) 29 | set(CMAKE_COMPILER_IS_MINGW ) 30 | set(CMAKE_COMPILER_IS_CYGWIN ) 31 | if(CMAKE_COMPILER_IS_CYGWIN) 32 | set(CYGWIN 1) 33 | set(UNIX 1) 34 | endif() 35 | 36 | set(CMAKE_CXX_COMPILER_ENV_VAR "CXX") 37 | 38 | if(CMAKE_COMPILER_IS_MINGW) 39 | set(MINGW 1) 40 | endif() 41 | set(CMAKE_CXX_COMPILER_ID_RUN 1) 42 | set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC) 43 | set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP) 44 | set(CMAKE_CXX_LINKER_PREFERENCE 30) 45 | set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1) 46 | 47 | # Save compiler ABI information. 48 | set(CMAKE_CXX_SIZEOF_DATA_PTR "8") 49 | set(CMAKE_CXX_COMPILER_ABI "ELF") 50 | set(CMAKE_CXX_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 51 | 52 | if(CMAKE_CXX_SIZEOF_DATA_PTR) 53 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}") 54 | endif() 55 | 56 | if(CMAKE_CXX_COMPILER_ABI) 57 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}") 58 | endif() 59 | 60 | if(CMAKE_CXX_LIBRARY_ARCHITECTURE) 61 | set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 62 | endif() 63 | 64 | set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "") 65 | if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX) 66 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}") 67 | endif() 68 | 69 | 70 | 71 | 72 | 73 | set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "stdc++;m;gcc_s;gcc;c;gcc_s;gcc") 74 | set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/7;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib") 75 | set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 76 | -------------------------------------------------------------------------------- /MNN/mnn/include/ImageProcess.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ImageProcess.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/09/19. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef ImageProcess_hpp 10 | #define ImageProcess_hpp 11 | 12 | #include "ErrorCode.hpp" 13 | #include "Matrix.h" 14 | #include "Tensor.hpp" 15 | 16 | namespace MNN { 17 | namespace CV { 18 | enum ImageFormat { 19 | RGBA = 0, 20 | RGB, 21 | BGR, 22 | GRAY, 23 | BGRA, 24 | YUV_NV21 = 11, 25 | }; 26 | 27 | enum Filter { NEAREST = 0, BILINEAR = 1, BICUBIC = 2 }; 28 | 29 | enum Wrap { CLAMP_TO_EDGE = 0, ZERO = 1, REPEAT = 2 }; 30 | 31 | /** 32 | * handle image process for tensor. 33 | * step: 34 | * 1: Do transform compute and get points 35 | * 2: Sample line and do format convert 36 | * 3: Turn RGBA to float tensor, and do sub and normalize 37 | */ 38 | class MNN_PUBLIC ImageProcess { 39 | public: 40 | struct Inside; 41 | struct Config { 42 | /** data filter */ 43 | Filter filterType = NEAREST; 44 | /** format of source data */ 45 | ImageFormat sourceFormat = RGBA; 46 | /** format of destination data */ 47 | ImageFormat destFormat = RGBA; 48 | 49 | // Only valid if the dest type is float 50 | float mean[4] = {0.0f, 0.0f, 0.0f, 0.0f}; 51 | float normal[4] = {1.0f, 1.0f, 1.0f, 1.0f}; 52 | 53 | /** edge wrapper */ 54 | Wrap wrap = CLAMP_TO_EDGE; 55 | }; 56 | 57 | public: 58 | /** 59 | * @brief create image process with given config for given tensor. 60 | * @param config given config. 61 | * @param dstTensor given tensor. 62 | * @return image processor. 63 | */ 64 | static ImageProcess* create(const Config& config, const Tensor* dstTensor = nullptr); 65 | 66 | /** 67 | * @brief create image process with given config for given tensor. 68 | * @param means given means 69 | * @param meanCount given means count 70 | * @param normals given normals 71 | * @param normalCount given normal count 72 | * @param sourceFormat format of source data 73 | * @param destFormat format of destination data 74 | * @param dstTensor given tensor. 75 | * @return image processor. 76 | */ 77 | static ImageProcess* create(const ImageFormat sourceFormat = RGBA, const ImageFormat destFormat = RGBA, 78 | const float* means = nullptr, const int meanCount = 0, const float* normals = nullptr, 79 | const int normalCount = 0, const Tensor* dstTensor = nullptr); 80 | 81 | ~ImageProcess(); 82 | 83 | /** 84 | * @brief get affine transform matrix. 85 | * @return affine transform matrix. 86 | */ 87 | inline const Matrix& matrix() const { 88 | return mTransform; 89 | } 90 | void setMatrix(const Matrix& matrix); 91 | 92 | /** 93 | * @brief convert source data to given tensor. 94 | * @param source source data. 95 | * @param iw source width. 96 | * @param ih source height. 97 | * @param stride number of elements per row. eg: 100 width RGB contains at least 300 elements. 98 | * @param dest given tensor. 99 | * @return result code. 100 | */ 101 | ErrorCode convert(const uint8_t* source, int iw, int ih, int stride, Tensor* dest); 102 | 103 | /** 104 | * @brief convert source data to given tensor. 105 | * @param source source data. 106 | * @param iw source width. 107 | * @param ih source height. 108 | * @param stride number of elements per row. eg: 100 width RGB contains at least 300 elements. 109 | * @param dest dest data. 110 | * @param ow output width. 111 | * @param oh output height. 112 | * @param outputBpp output bpp, if 0, set as the save and config.destFormat. 113 | * @param outputStride output stride, if 0, set as ow * outputBpp. 114 | * @param type Only support halide_type_of and halide_type_of. 115 | * @return result code. 116 | */ 117 | ErrorCode convert(const uint8_t* source, int iw, int ih, int stride, void* dest, int ow, int oh, int outputBpp = 0, 118 | int outputStride = 0, halide_type_t type = halide_type_of()); 119 | 120 | /** 121 | * @brief create tensor with given data. 122 | * @param w image width. 123 | * @param h image height. 124 | * @param bpp bytes per pixel. 125 | * @param p pixel data pointer. 126 | * @return created tensor. 127 | */ 128 | template 129 | static Tensor* createImageTensor(int w, int h, int bpp, void* p = nullptr) { 130 | return createImageTensor(halide_type_of(), w, h, bpp, p); 131 | } 132 | static Tensor* createImageTensor(halide_type_t type, int w, int h, int bpp, void* p = nullptr); 133 | 134 | private: 135 | ImageProcess(const Config& config); 136 | Matrix mTransform; 137 | Matrix mTransformInvert; 138 | Inside* mInside; 139 | }; 140 | } // namespace CV 141 | } // namespace MNN 142 | 143 | #endif /* ImageProcess_hpp */ 144 | -------------------------------------------------------------------------------- /vision/nn/mb_tiny_RFB.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class BasicConv(nn.Module): 7 | 8 | def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True): 9 | super(BasicConv, self).__init__() 10 | self.out_channels = out_planes 11 | if bn: 12 | self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False) 13 | self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True) 14 | self.relu = nn.ReLU(inplace=True) if relu else None 15 | else: 16 | self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True) 17 | self.bn = None 18 | self.relu = nn.ReLU(inplace=True) if relu else None 19 | 20 | def forward(self, x): 21 | x = self.conv(x) 22 | if self.bn is not None: 23 | x = self.bn(x) 24 | if self.relu is not None: 25 | x = self.relu(x) 26 | return x 27 | 28 | 29 | class BasicRFB(nn.Module): 30 | 31 | def __init__(self, in_planes, out_planes, stride=1, scale=0.1, map_reduce=8, vision=1, groups=1): 32 | super(BasicRFB, self).__init__() 33 | self.scale = scale 34 | self.out_channels = out_planes 35 | inter_planes = in_planes // map_reduce 36 | 37 | self.branch0 = nn.Sequential( 38 | BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False), 39 | BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups), 40 | BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 1, dilation=vision + 1, relu=False, groups=groups) 41 | ) 42 | self.branch1 = nn.Sequential( 43 | BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False), 44 | BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups), 45 | BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 2, dilation=vision + 2, relu=False, groups=groups) 46 | ) 47 | self.branch2 = nn.Sequential( 48 | BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False), 49 | BasicConv(inter_planes, (inter_planes // 2) * 3, kernel_size=3, stride=1, padding=1, groups=groups), 50 | BasicConv((inter_planes // 2) * 3, 2 * inter_planes, kernel_size=3, stride=stride, padding=1, groups=groups), 51 | BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 4, dilation=vision + 4, relu=False, groups=groups) 52 | ) 53 | 54 | self.ConvLinear = BasicConv(6 * inter_planes, out_planes, kernel_size=1, stride=1, relu=False) 55 | self.shortcut = BasicConv(in_planes, out_planes, kernel_size=1, stride=stride, relu=False) 56 | self.relu = nn.ReLU(inplace=False) 57 | 58 | def forward(self, x): 59 | x0 = self.branch0(x) 60 | x1 = self.branch1(x) 61 | x2 = self.branch2(x) 62 | 63 | out = torch.cat((x0, x1, x2), 1) 64 | out = self.ConvLinear(out) 65 | short = self.shortcut(x) 66 | out = out * self.scale + short 67 | out = self.relu(out) 68 | 69 | return out 70 | 71 | 72 | class Mb_Tiny_RFB(nn.Module): 73 | 74 | def __init__(self, num_classes=2): 75 | super(Mb_Tiny_RFB, self).__init__() 76 | self.base_channel = 8 * 2 77 | 78 | def conv_bn(inp, oup, stride): 79 | return nn.Sequential( 80 | nn.Conv2d(inp, oup, 3, stride, 1, bias=False), 81 | nn.BatchNorm2d(oup), 82 | nn.ReLU(inplace=True) 83 | ) 84 | 85 | def conv_dw(inp, oup, stride): 86 | return nn.Sequential( 87 | nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False), 88 | nn.BatchNorm2d(inp), 89 | nn.ReLU(inplace=True), 90 | 91 | nn.Conv2d(inp, oup, 1, 1, 0, bias=False), 92 | nn.BatchNorm2d(oup), 93 | nn.ReLU(inplace=True), 94 | ) 95 | 96 | self.model = nn.Sequential( 97 | conv_bn(3, self.base_channel, 2), # 160*120 98 | conv_dw(self.base_channel, self.base_channel * 2, 1), 99 | conv_dw(self.base_channel * 2, self.base_channel * 2, 2), # 80*60 100 | conv_dw(self.base_channel * 2, self.base_channel * 2, 1), 101 | conv_dw(self.base_channel * 2, self.base_channel * 4, 2), # 40*30 102 | conv_dw(self.base_channel * 4, self.base_channel * 4, 1), 103 | conv_dw(self.base_channel * 4, self.base_channel * 4, 1), 104 | BasicRFB(self.base_channel * 4, self.base_channel * 4, stride=1, scale=1.0), 105 | conv_dw(self.base_channel * 4, self.base_channel * 8, 2), # 20*15 106 | conv_dw(self.base_channel * 8, self.base_channel * 8, 1), 107 | conv_dw(self.base_channel * 8, self.base_channel * 8, 1), 108 | conv_dw(self.base_channel * 8, self.base_channel * 16, 2), # 10*8 109 | conv_dw(self.base_channel * 16, self.base_channel * 16, 1) 110 | ) 111 | self.fc = nn.Linear(1024, num_classes) 112 | 113 | def forward(self, x): 114 | x = self.model(x) 115 | x = F.avg_pool2d(x, 7) 116 | x = x.view(-1, 1024) 117 | x = self.fc(x) 118 | return x 119 | -------------------------------------------------------------------------------- /MNN/build/Makefile: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | # Allow only one "make -f Makefile2" at a time, but pass parallelism. 10 | .NOTPARALLEL: 11 | 12 | 13 | #============================================================================= 14 | # Special targets provided by cmake. 15 | 16 | # Disable implicit rules so canonical targets will work. 17 | .SUFFIXES: 18 | 19 | 20 | # Remove some rules from gmake that .SUFFIXES does not remove. 21 | SUFFIXES = 22 | 23 | .SUFFIXES: .hpux_make_needs_suffix_list 24 | 25 | 26 | # Suppress display of executed commands. 27 | $(VERBOSE).SILENT: 28 | 29 | 30 | # A target that is always out of date. 31 | cmake_force: 32 | 33 | .PHONY : cmake_force 34 | 35 | #============================================================================= 36 | # Set environment variables for the build. 37 | 38 | # The shell in which to execute make rules. 39 | SHELL = /bin/sh 40 | 41 | # The CMake executable. 42 | CMAKE_COMMAND = /usr/bin/cmake 43 | 44 | # The command to remove a file. 45 | RM = /usr/bin/cmake -E remove -f 46 | 47 | # Escaping for special characters. 48 | EQUALS = = 49 | 50 | # The top-level source directory on which CMake was run. 51 | CMAKE_SOURCE_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN 52 | 53 | # The top-level build directory on which CMake was run. 54 | CMAKE_BINARY_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build 55 | 56 | #============================================================================= 57 | # Targets provided globally by CMake. 58 | 59 | # Special rule for the target rebuild_cache 60 | rebuild_cache: 61 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." 62 | /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) 63 | .PHONY : rebuild_cache 64 | 65 | # Special rule for the target rebuild_cache 66 | rebuild_cache/fast: rebuild_cache 67 | 68 | .PHONY : rebuild_cache/fast 69 | 70 | # Special rule for the target edit_cache 71 | edit_cache: 72 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." 73 | /usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. 74 | .PHONY : edit_cache 75 | 76 | # Special rule for the target edit_cache 77 | edit_cache/fast: edit_cache 78 | 79 | .PHONY : edit_cache/fast 80 | 81 | # The main all target 82 | all: cmake_check_build_system 83 | $(CMAKE_COMMAND) -E cmake_progress_start /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/progress.marks 84 | $(MAKE) -f CMakeFiles/Makefile2 all 85 | $(CMAKE_COMMAND) -E cmake_progress_start /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles 0 86 | .PHONY : all 87 | 88 | # The main clean target 89 | clean: 90 | $(MAKE) -f CMakeFiles/Makefile2 clean 91 | .PHONY : clean 92 | 93 | # The main clean target 94 | clean/fast: clean 95 | 96 | .PHONY : clean/fast 97 | 98 | # Prepare targets for installation. 99 | preinstall: all 100 | $(MAKE) -f CMakeFiles/Makefile2 preinstall 101 | .PHONY : preinstall 102 | 103 | # Prepare targets for installation. 104 | preinstall/fast: 105 | $(MAKE) -f CMakeFiles/Makefile2 preinstall 106 | .PHONY : preinstall/fast 107 | 108 | # clear depends 109 | depend: 110 | $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 111 | .PHONY : depend 112 | 113 | #============================================================================= 114 | # Target rules for targets named Ultra-face-mnn 115 | 116 | # Build rule for target. 117 | Ultra-face-mnn: cmake_check_build_system 118 | $(MAKE) -f CMakeFiles/Makefile2 Ultra-face-mnn 119 | .PHONY : Ultra-face-mnn 120 | 121 | # fast build rule for target. 122 | Ultra-face-mnn/fast: 123 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/build 124 | .PHONY : Ultra-face-mnn/fast 125 | 126 | src/UltraFace.o: src/UltraFace.cpp.o 127 | 128 | .PHONY : src/UltraFace.o 129 | 130 | # target to build an object file 131 | src/UltraFace.cpp.o: 132 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o 133 | .PHONY : src/UltraFace.cpp.o 134 | 135 | src/UltraFace.i: src/UltraFace.cpp.i 136 | 137 | .PHONY : src/UltraFace.i 138 | 139 | # target to preprocess a source file 140 | src/UltraFace.cpp.i: 141 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.i 142 | .PHONY : src/UltraFace.cpp.i 143 | 144 | src/UltraFace.s: src/UltraFace.cpp.s 145 | 146 | .PHONY : src/UltraFace.s 147 | 148 | # target to generate assembly for a file 149 | src/UltraFace.cpp.s: 150 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.s 151 | .PHONY : src/UltraFace.cpp.s 152 | 153 | src/main.o: src/main.cpp.o 154 | 155 | .PHONY : src/main.o 156 | 157 | # target to build an object file 158 | src/main.cpp.o: 159 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o 160 | .PHONY : src/main.cpp.o 161 | 162 | src/main.i: src/main.cpp.i 163 | 164 | .PHONY : src/main.i 165 | 166 | # target to preprocess a source file 167 | src/main.cpp.i: 168 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.i 169 | .PHONY : src/main.cpp.i 170 | 171 | src/main.s: src/main.cpp.s 172 | 173 | .PHONY : src/main.s 174 | 175 | # target to generate assembly for a file 176 | src/main.cpp.s: 177 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.s 178 | .PHONY : src/main.cpp.s 179 | 180 | # Help Target 181 | help: 182 | @echo "The following are some of the valid targets for this Makefile:" 183 | @echo "... all (the default if no target is provided)" 184 | @echo "... clean" 185 | @echo "... depend" 186 | @echo "... rebuild_cache" 187 | @echo "... Ultra-face-mnn" 188 | @echo "... edit_cache" 189 | @echo "... src/UltraFace.o" 190 | @echo "... src/UltraFace.i" 191 | @echo "... src/UltraFace.s" 192 | @echo "... src/main.o" 193 | @echo "... src/main.i" 194 | @echo "... src/main.s" 195 | .PHONY : help 196 | 197 | 198 | 199 | #============================================================================= 200 | # Special targets to cleanup operation of make. 201 | 202 | # Special rule to run CMake to check the build system integrity. 203 | # No rule that depends on this can have commands that come from listfiles 204 | # because they might be regenerated. 205 | cmake_check_build_system: 206 | $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 207 | .PHONY : cmake_check_build_system 208 | 209 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Makefile.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # The generator used is: 5 | set(CMAKE_DEPENDS_GENERATOR "Unix Makefiles") 6 | 7 | # The top level Makefile was generated from the following files: 8 | set(CMAKE_MAKEFILE_DEPENDS 9 | "CMakeCache.txt" 10 | "../CMakeLists.txt" 11 | "CMakeFiles/3.10.2/CMakeCCompiler.cmake" 12 | "CMakeFiles/3.10.2/CMakeCXXCompiler.cmake" 13 | "CMakeFiles/3.10.2/CMakeSystem.cmake" 14 | "CMakeFiles/feature_tests.c" 15 | "CMakeFiles/feature_tests.cxx" 16 | "/usr/share/OpenCV/OpenCVConfig-version.cmake" 17 | "/usr/share/OpenCV/OpenCVConfig.cmake" 18 | "/usr/share/OpenCV/OpenCVModules-release.cmake" 19 | "/usr/share/OpenCV/OpenCVModules.cmake" 20 | "/usr/share/cmake-3.10/Modules/CMakeCCompiler.cmake.in" 21 | "/usr/share/cmake-3.10/Modules/CMakeCCompilerABI.c" 22 | "/usr/share/cmake-3.10/Modules/CMakeCInformation.cmake" 23 | "/usr/share/cmake-3.10/Modules/CMakeCXXCompiler.cmake.in" 24 | "/usr/share/cmake-3.10/Modules/CMakeCXXCompilerABI.cpp" 25 | "/usr/share/cmake-3.10/Modules/CMakeCXXInformation.cmake" 26 | "/usr/share/cmake-3.10/Modules/CMakeCommonLanguageInclude.cmake" 27 | "/usr/share/cmake-3.10/Modules/CMakeCompilerIdDetection.cmake" 28 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCCompiler.cmake" 29 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCXXCompiler.cmake" 30 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCompileFeatures.cmake" 31 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCompiler.cmake" 32 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCompilerABI.cmake" 33 | "/usr/share/cmake-3.10/Modules/CMakeDetermineCompilerId.cmake" 34 | "/usr/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake" 35 | "/usr/share/cmake-3.10/Modules/CMakeFindBinUtils.cmake" 36 | "/usr/share/cmake-3.10/Modules/CMakeGenericSystem.cmake" 37 | "/usr/share/cmake-3.10/Modules/CMakeLanguageInformation.cmake" 38 | "/usr/share/cmake-3.10/Modules/CMakeParseImplicitLinkInfo.cmake" 39 | "/usr/share/cmake-3.10/Modules/CMakeSystem.cmake.in" 40 | "/usr/share/cmake-3.10/Modules/CMakeSystemSpecificInformation.cmake" 41 | "/usr/share/cmake-3.10/Modules/CMakeSystemSpecificInitialize.cmake" 42 | "/usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake" 43 | "/usr/share/cmake-3.10/Modules/CMakeTestCXXCompiler.cmake" 44 | "/usr/share/cmake-3.10/Modules/CMakeTestCompilerCommon.cmake" 45 | "/usr/share/cmake-3.10/Modules/CMakeUnixFindMake.cmake" 46 | "/usr/share/cmake-3.10/Modules/Compiler/ADSP-DetermineCompiler.cmake" 47 | "/usr/share/cmake-3.10/Modules/Compiler/ARMCC-DetermineCompiler.cmake" 48 | "/usr/share/cmake-3.10/Modules/Compiler/AppleClang-DetermineCompiler.cmake" 49 | "/usr/share/cmake-3.10/Modules/Compiler/Borland-DetermineCompiler.cmake" 50 | "/usr/share/cmake-3.10/Modules/Compiler/Bruce-C-DetermineCompiler.cmake" 51 | "/usr/share/cmake-3.10/Modules/Compiler/CMakeCommonCompilerMacros.cmake" 52 | "/usr/share/cmake-3.10/Modules/Compiler/Clang-DetermineCompiler.cmake" 53 | "/usr/share/cmake-3.10/Modules/Compiler/Clang-DetermineCompilerInternal.cmake" 54 | "/usr/share/cmake-3.10/Modules/Compiler/Comeau-CXX-DetermineCompiler.cmake" 55 | "/usr/share/cmake-3.10/Modules/Compiler/Compaq-C-DetermineCompiler.cmake" 56 | "/usr/share/cmake-3.10/Modules/Compiler/Compaq-CXX-DetermineCompiler.cmake" 57 | "/usr/share/cmake-3.10/Modules/Compiler/Cray-DetermineCompiler.cmake" 58 | "/usr/share/cmake-3.10/Modules/Compiler/Embarcadero-DetermineCompiler.cmake" 59 | "/usr/share/cmake-3.10/Modules/Compiler/Fujitsu-DetermineCompiler.cmake" 60 | "/usr/share/cmake-3.10/Modules/Compiler/GHS-DetermineCompiler.cmake" 61 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-C-DetermineCompiler.cmake" 62 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-C-FeatureTests.cmake" 63 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-C.cmake" 64 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-CXX-DetermineCompiler.cmake" 65 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-CXX-FeatureTests.cmake" 66 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-CXX.cmake" 67 | "/usr/share/cmake-3.10/Modules/Compiler/GNU-FindBinUtils.cmake" 68 | "/usr/share/cmake-3.10/Modules/Compiler/GNU.cmake" 69 | "/usr/share/cmake-3.10/Modules/Compiler/HP-C-DetermineCompiler.cmake" 70 | "/usr/share/cmake-3.10/Modules/Compiler/HP-CXX-DetermineCompiler.cmake" 71 | "/usr/share/cmake-3.10/Modules/Compiler/IAR-DetermineCompiler.cmake" 72 | "/usr/share/cmake-3.10/Modules/Compiler/IBMCPP-C-DetermineVersionInternal.cmake" 73 | "/usr/share/cmake-3.10/Modules/Compiler/IBMCPP-CXX-DetermineVersionInternal.cmake" 74 | "/usr/share/cmake-3.10/Modules/Compiler/Intel-DetermineCompiler.cmake" 75 | "/usr/share/cmake-3.10/Modules/Compiler/MIPSpro-DetermineCompiler.cmake" 76 | "/usr/share/cmake-3.10/Modules/Compiler/MSVC-DetermineCompiler.cmake" 77 | "/usr/share/cmake-3.10/Modules/Compiler/NVIDIA-DetermineCompiler.cmake" 78 | "/usr/share/cmake-3.10/Modules/Compiler/OpenWatcom-DetermineCompiler.cmake" 79 | "/usr/share/cmake-3.10/Modules/Compiler/PGI-DetermineCompiler.cmake" 80 | "/usr/share/cmake-3.10/Modules/Compiler/PathScale-DetermineCompiler.cmake" 81 | "/usr/share/cmake-3.10/Modules/Compiler/SCO-DetermineCompiler.cmake" 82 | "/usr/share/cmake-3.10/Modules/Compiler/SDCC-C-DetermineCompiler.cmake" 83 | "/usr/share/cmake-3.10/Modules/Compiler/SunPro-C-DetermineCompiler.cmake" 84 | "/usr/share/cmake-3.10/Modules/Compiler/SunPro-CXX-DetermineCompiler.cmake" 85 | "/usr/share/cmake-3.10/Modules/Compiler/TI-DetermineCompiler.cmake" 86 | "/usr/share/cmake-3.10/Modules/Compiler/TinyCC-C-DetermineCompiler.cmake" 87 | "/usr/share/cmake-3.10/Modules/Compiler/VisualAge-C-DetermineCompiler.cmake" 88 | "/usr/share/cmake-3.10/Modules/Compiler/VisualAge-CXX-DetermineCompiler.cmake" 89 | "/usr/share/cmake-3.10/Modules/Compiler/Watcom-DetermineCompiler.cmake" 90 | "/usr/share/cmake-3.10/Modules/Compiler/XL-C-DetermineCompiler.cmake" 91 | "/usr/share/cmake-3.10/Modules/Compiler/XL-CXX-DetermineCompiler.cmake" 92 | "/usr/share/cmake-3.10/Modules/Compiler/zOS-C-DetermineCompiler.cmake" 93 | "/usr/share/cmake-3.10/Modules/Compiler/zOS-CXX-DetermineCompiler.cmake" 94 | "/usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake" 95 | "/usr/share/cmake-3.10/Modules/FindPackageMessage.cmake" 96 | "/usr/share/cmake-3.10/Modules/Internal/FeatureTesting.cmake" 97 | "/usr/share/cmake-3.10/Modules/Platform/Linux-Determine-CXX.cmake" 98 | "/usr/share/cmake-3.10/Modules/Platform/Linux-GNU-C.cmake" 99 | "/usr/share/cmake-3.10/Modules/Platform/Linux-GNU-CXX.cmake" 100 | "/usr/share/cmake-3.10/Modules/Platform/Linux-GNU.cmake" 101 | "/usr/share/cmake-3.10/Modules/Platform/Linux.cmake" 102 | "/usr/share/cmake-3.10/Modules/Platform/UnixPaths.cmake" 103 | ) 104 | 105 | # The corresponding makefile is: 106 | set(CMAKE_MAKEFILE_OUTPUTS 107 | "Makefile" 108 | "CMakeFiles/cmake.check_cache" 109 | ) 110 | 111 | # Byproducts of CMake generate step: 112 | set(CMAKE_MAKEFILE_PRODUCTS 113 | "CMakeFiles/3.10.2/CMakeSystem.cmake" 114 | "CMakeFiles/3.10.2/CMakeCCompiler.cmake" 115 | "CMakeFiles/3.10.2/CMakeCXXCompiler.cmake" 116 | "CMakeFiles/3.10.2/CMakeCCompiler.cmake" 117 | "CMakeFiles/3.10.2/CMakeCXXCompiler.cmake" 118 | "CMakeFiles/CMakeDirectoryInformation.cmake" 119 | ) 120 | 121 | # Dependency information for all targets: 122 | set(CMAKE_DEPEND_INFO_FILES 123 | "CMakeFiles/Ultra-face-mnn.dir/DependInfo.cmake" 124 | ) 125 | -------------------------------------------------------------------------------- /MNN/python/person-detector-pic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env/ python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author:linzai 5 | @file: ultraface_py_mnn.py 6 | @time: 2019-11-25 7 | """ 8 | from __future__ import print_function 9 | 10 | import os 11 | import argparse 12 | import sys 13 | import time 14 | from math import ceil 15 | 16 | import MNN 17 | import cv2 18 | import numpy as np 19 | import torch 20 | 21 | sys.path.append('../../') 22 | import vision.utils.box_utils_numpy as box_utils 23 | #/home/uc/RFSong-779-master/VOC2007/VOCdevkit/VOC2007/JPEGImages 24 | parser = argparse.ArgumentParser(description='run ultraface with MNN in py') 25 | parser.add_argument('--model_path', default="../model/version-RFB/617-1.mnn", type=str, help='model path') 26 | parser.add_argument('--input_size', default="300,300", type=str, 27 | help='define network input size,format: width,height') 28 | parser.add_argument('--threshold', default=0.5, type=float, help='score threshold') 29 | parser.add_argument('--imgs_path', default="/home/uc/RFSong-779-master/VOC2007/VOCdevkit/VOC2007/JPEGImages", type=str, help='imgs dir') 30 | parser.add_argument('--results_path', default="results", type=str, help='results dir') 31 | args = parser.parse_args() 32 | 33 | image_mean = np.array([127, 127, 127]) 34 | image_std = 128.0 35 | iou_threshold = 0.3 36 | center_variance = 0.1 37 | size_variance = 0.2 38 | min_boxes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]] 39 | strides = [8, 16, 32, 64] 40 | 41 | 42 | def define_img_size(image_size): 43 | shrinkage_list = [] 44 | feature_map_w_h_list = [] 45 | for size in image_size: 46 | feature_map = [ceil(size / stride) for stride in strides] 47 | feature_map_w_h_list.append(feature_map) 48 | feature_map_w_h_list = [[38,19,10,5],[38,19,10,5]] 49 | for i in range(0, len(image_size)): 50 | shrinkage_list.append(strides) 51 | 52 | for i in range(0, len(image_size)): 53 | item_list = [] 54 | for k in range(0, len(feature_map_w_h_list[i])): 55 | item_list.append(image_size[i] / feature_map_w_h_list[i][k]) 56 | shrinkage_list.append(item_list) 57 | 58 | priors = generate_priors(feature_map_w_h_list, shrinkage_list, image_size, min_boxes) 59 | return priors 60 | 61 | 62 | def generate_priors(feature_map_list, shrinkage_list, image_size, min_boxes, clamp=True): 63 | priors = [] 64 | for index in range(0, len(feature_map_list[0])): 65 | scale_w = image_size[0] / shrinkage_list[0][index] 66 | scale_h = image_size[1] / shrinkage_list[1][index] 67 | for j in range(0, feature_map_list[1][index]): 68 | for i in range(0, feature_map_list[0][index]): 69 | x_center = (i + 0.5) / scale_w 70 | y_center = (j + 0.5) / scale_h 71 | 72 | for min_box in min_boxes[index]: 73 | w = min_box / image_size[0] 74 | h = min_box / image_size[1]*2 75 | priors.append([ 76 | x_center, 77 | y_center, 78 | w, 79 | h 80 | ]) 81 | print("priors nums:{}".format(len(priors))) 82 | priors = torch.tensor(priors) 83 | if clamp: 84 | torch.clamp(priors, 0.0, 1.0, out=priors) 85 | return priors 86 | 87 | 88 | def predict(width, height, confidences, boxes, prob_threshold, iou_threshold=0.3, top_k=-1): 89 | boxes = boxes[0] 90 | confidences = confidences[0] 91 | picked_box_probs = [] 92 | picked_labels = [] 93 | for class_index in range(1, confidences.shape[1]): 94 | probs = confidences[:, class_index] 95 | mask = probs > prob_threshold 96 | probs = probs[mask] 97 | if probs.shape[0] == 0: 98 | continue 99 | subset_boxes = boxes[mask, :] 100 | box_probs = np.concatenate([subset_boxes, probs.reshape(-1, 1)], axis=1) 101 | box_probs = box_utils.hard_nms(box_probs, 102 | iou_threshold=iou_threshold, 103 | top_k=top_k, 104 | ) 105 | picked_box_probs.append(box_probs) 106 | picked_labels.extend([class_index] * box_probs.shape[0]) 107 | if not picked_box_probs: 108 | return np.array([]), np.array([]), np.array([]) 109 | picked_box_probs = np.concatenate(picked_box_probs) 110 | picked_box_probs[:, 0] *= width 111 | picked_box_probs[:, 1] *= height 112 | picked_box_probs[:, 2] *= width 113 | picked_box_probs[:, 3] *= height 114 | return picked_box_probs[:, :4].astype(np.int32), np.array(picked_labels), picked_box_probs[:, 4] 115 | 116 | 117 | def inference(): 118 | input_size = [int(v.strip()) for v in args.input_size.split(",")] 119 | 120 | priors = define_img_size(input_size) 121 | print(priors) 122 | result_path = args.results_path 123 | imgs_path = args.imgs_path 124 | if not os.path.exists(result_path): 125 | os.makedirs(result_path) 126 | listdir = os.listdir(imgs_path) 127 | f = open('person.txt','w') 128 | for file_path in listdir: 129 | img_path = os.path.join(imgs_path, file_path) 130 | #print(file_path[:-4]) 131 | image_ori = cv2.imread(img_path) 132 | interpreter = MNN.Interpreter(args.model_path) 133 | print(interpreter) 134 | session = interpreter.createSession() 135 | input_tensor = interpreter.getSessionInput(session) 136 | image = cv2.cvtColor(image_ori, cv2.COLOR_BGR2RGB) 137 | image = cv2.resize(image, tuple(input_size)) 138 | image = image.astype(float) 139 | image = (image - image_mean) / image_std 140 | image = image.transpose((2, 0, 1)) 141 | tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) 142 | input_tensor.copyFrom(tmp_input) 143 | time_time = time.time() 144 | interpreter.runSession(session) 145 | scores = interpreter.getSessionOutput(session, "scores").getData() 146 | boxes = interpreter.getSessionOutput(session, "boxes").getData() 147 | print(len(scores)) 148 | boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) 149 | scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) 150 | #print("inference time: {} s".format(round(time.time() - time_time, 4))) 151 | boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) 152 | boxes = box_utils.center_form_to_corner_form(boxes) 153 | boxes, labels, probs = predict(image_ori.shape[1], image_ori.shape[0], scores, boxes, args.threshold) 154 | for i in range(boxes.shape[0]): 155 | box = boxes[i, :] 156 | #print(probs[i]) 157 | cv2.rectangle(image_ori, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) 158 | f.writelines([file_path[:-4],' ',str(probs[i]),' ',str(int(box[0])),' ',str(int(box[1])),' ',str(int(box[2])),' ',str(int(box[3])),'\n']) 159 | cv2.imwrite(os.path.join(result_path, file_path), image_ori) 160 | #print("result_pic is written to {}".format(os.path.join(result_path, file_path))) 161 | cv2.imshow("UltraFace_mnn_py", image_ori) 162 | cv2.waitKey(10) 163 | cv2.destroyAllWindows() 164 | 165 | 166 | if __name__ == "__main__": 167 | inference() 168 | -------------------------------------------------------------------------------- /vision/ssd/ssd.py: -------------------------------------------------------------------------------- 1 | from collections import namedtuple 2 | from typing import List, Tuple 3 | 4 | import numpy as np 5 | import torch 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | 9 | from vision.utils import box_utils 10 | 11 | GraphPath = namedtuple("GraphPath", ['s0', 'name', 's1']) 12 | 13 | 14 | class SSD(nn.Module): 15 | def __init__(self, num_classes: int, base_net: nn.ModuleList, source_layer_indexes: List[int], 16 | extras: nn.ModuleList, classification_headers: nn.ModuleList, 17 | regression_headers: nn.ModuleList, is_test=False, config=None, device=None): 18 | """Compose a SSD model using the given components. 19 | """ 20 | super(SSD, self).__init__() 21 | 22 | self.num_classes = num_classes 23 | self.base_net = base_net 24 | self.source_layer_indexes = source_layer_indexes 25 | self.extras = extras 26 | self.classification_headers = classification_headers 27 | self.regression_headers = regression_headers 28 | self.is_test = is_test 29 | self.config = config 30 | 31 | # register layers in source_layer_indexes by adding them to a module list 32 | self.source_layer_add_ons = nn.ModuleList([t[1] for t in source_layer_indexes 33 | if isinstance(t, tuple) and not isinstance(t, GraphPath)]) 34 | if device: 35 | self.device = device 36 | else: 37 | self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 38 | if is_test: 39 | self.config = config 40 | self.priors = config.priors.to(self.device) 41 | 42 | def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: 43 | confidences = [] 44 | locations = [] 45 | start_layer_index = 0 46 | header_index = 0 47 | end_layer_index = 0 48 | for end_layer_index in self.source_layer_indexes: 49 | if isinstance(end_layer_index, GraphPath): 50 | path = end_layer_index 51 | end_layer_index = end_layer_index.s0 52 | added_layer = None 53 | elif isinstance(end_layer_index, tuple): 54 | added_layer = end_layer_index[1] 55 | end_layer_index = end_layer_index[0] 56 | path = None 57 | else: 58 | added_layer = None 59 | path = None 60 | for layer in self.base_net[start_layer_index: end_layer_index]: 61 | x = layer(x) 62 | if added_layer: 63 | y = added_layer(x) 64 | else: 65 | y = x 66 | if path: 67 | sub = getattr(self.base_net[end_layer_index], path.name) 68 | for layer in sub[:path.s1]: 69 | x = layer(x) 70 | y = x 71 | for layer in sub[path.s1:]: 72 | x = layer(x) 73 | end_layer_index += 1 74 | start_layer_index = end_layer_index 75 | confidence, location = self.compute_header(header_index, y) 76 | header_index += 1 77 | confidences.append(confidence) 78 | locations.append(location) 79 | 80 | for layer in self.base_net[end_layer_index:]: 81 | x = layer(x) 82 | 83 | for layer in self.extras: 84 | x = layer(x) 85 | confidence, location = self.compute_header(header_index, x) 86 | header_index += 1 87 | confidences.append(confidence) 88 | locations.append(location) 89 | 90 | confidences = torch.cat(confidences, 1) 91 | locations = torch.cat(locations, 1) 92 | 93 | if self.is_test: 94 | confidences = F.softmax(confidences, dim=2) 95 | #boxes = locations 96 | boxes = box_utils.convert_locations_to_boxes( 97 | locations, self.priors, self.config.center_variance, self.config.size_variance 98 | ) 99 | boxes = box_utils.center_form_to_corner_form(boxes) 100 | return confidences, boxes 101 | else: 102 | return confidences, locations 103 | 104 | def compute_header(self, i, x): 105 | confidence = self.classification_headers[i](x) 106 | confidence = confidence.permute(0, 2, 3, 1).contiguous() 107 | confidence = confidence.view(confidence.size(0), -1, self.num_classes) 108 | 109 | location = self.regression_headers[i](x) 110 | location = location.permute(0, 2, 3, 1).contiguous() 111 | location = location.view(location.size(0), -1, 4) 112 | 113 | return confidence, location 114 | 115 | def init_from_base_net(self, model): 116 | self.base_net.load_state_dict(torch.load(model, map_location=lambda storage, loc: storage), strict=True) 117 | self.source_layer_add_ons.apply(_xavier_init_) 118 | self.extras.apply(_xavier_init_) 119 | self.classification_headers.apply(_xavier_init_) 120 | self.regression_headers.apply(_xavier_init_) 121 | 122 | def init_from_pretrained_ssd(self, model): 123 | state_dict = torch.load(model, map_location=lambda storage, loc: storage) 124 | state_dict = {k: v for k, v in state_dict.items() if not (k.startswith("classification_headers") or k.startswith("regression_headers"))} 125 | model_dict = self.state_dict() 126 | model_dict.update(state_dict) 127 | self.load_state_dict(model_dict) 128 | self.classification_headers.apply(_xavier_init_) 129 | self.regression_headers.apply(_xavier_init_) 130 | 131 | def init(self): 132 | self.base_net.apply(_xavier_init_) 133 | self.source_layer_add_ons.apply(_xavier_init_) 134 | self.extras.apply(_xavier_init_) 135 | self.classification_headers.apply(_xavier_init_) 136 | self.regression_headers.apply(_xavier_init_) 137 | 138 | def load(self, model): 139 | self.load_state_dict(torch.load(model, map_location=lambda storage, loc: storage)) 140 | 141 | def save(self, model_path): 142 | torch.save(self.state_dict(), model_path) 143 | 144 | 145 | class MatchPrior(object): 146 | def __init__(self, center_form_priors, center_variance, size_variance, iou_threshold): 147 | self.center_form_priors = center_form_priors 148 | self.corner_form_priors = box_utils.center_form_to_corner_form(center_form_priors) 149 | self.center_variance = center_variance 150 | self.size_variance = size_variance 151 | self.iou_threshold = iou_threshold 152 | 153 | def __call__(self, gt_boxes, gt_labels): 154 | if type(gt_boxes) is np.ndarray: 155 | gt_boxes = torch.from_numpy(gt_boxes) 156 | if type(gt_labels) is np.ndarray: 157 | gt_labels = torch.from_numpy(gt_labels) 158 | boxes, labels = box_utils.assign_priors(gt_boxes, gt_labels, 159 | self.corner_form_priors, self.iou_threshold) 160 | boxes = box_utils.corner_form_to_center_form(boxes) 161 | locations = box_utils.convert_boxes_to_locations(boxes, self.center_form_priors, self.center_variance, self.size_variance) 162 | return locations, labels 163 | 164 | 165 | def _xavier_init_(m: nn.Module): 166 | if isinstance(m, nn.Conv2d): 167 | nn.init.xavier_uniform_(m.weight) 168 | -------------------------------------------------------------------------------- /MNN/python/person-detector-video.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env/ python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author:linzai 5 | @file: ultraface_py_mnn.py 6 | @time: 2019-11-25 7 | """ 8 | from __future__ import print_function 9 | 10 | import os 11 | import argparse 12 | import sys 13 | import time 14 | from math import ceil 15 | 16 | import MNN 17 | import cv2 18 | import numpy as np 19 | import torch 20 | 21 | sys.path.append('../../') 22 | import vision.utils.box_utils_numpy as box_utils 23 | #/home/uc/RFSong-779-master/VOC2007/VOCdevkit/VOC2007/JPEGImages 24 | parser = argparse.ArgumentParser(description='run ultraface with MNN in py') 25 | parser.add_argument('--model_path', default="../model/version-RFB/617-1-sq.mnn", type=str, help='model path') 26 | parser.add_argument('--input_size', default="300,300", type=str, 27 | help='define network input size,format: width,height') 28 | parser.add_argument('--threshold', default=0.4, type=float, help='score threshold') 29 | parser.add_argument('--imgs_path', default="/home/uc/RFSong-779-master/VOC2007/VOCdevkit/VOC2007/JPEGImages", type=str, help='imgs dir') 30 | parser.add_argument('--results_path', default="results", type=str, help='results dir') 31 | args = parser.parse_args() 32 | 33 | image_mean = np.array([127, 127, 127]) 34 | image_std = 128.0 35 | iou_threshold = 0.3 36 | center_variance = 0.1 37 | size_variance = 0.2 38 | min_boxes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]] 39 | strides = [8, 16, 32, 64] 40 | 41 | 42 | def define_img_size(image_size): 43 | shrinkage_list = [] 44 | feature_map_w_h_list = [] 45 | for size in image_size: 46 | feature_map = [ceil(size / stride) for stride in strides] 47 | feature_map_w_h_list.append(feature_map) 48 | feature_map_w_h_list = [[38,19,10,5],[38,19,10,5]] 49 | for i in range(0, len(image_size)): 50 | shrinkage_list.append(strides) 51 | 52 | for i in range(0, len(image_size)): 53 | item_list = [] 54 | for k in range(0, len(feature_map_w_h_list[i])): 55 | item_list.append(image_size[i] / feature_map_w_h_list[i][k]) 56 | shrinkage_list.append(item_list) 57 | 58 | priors = generate_priors(feature_map_w_h_list, shrinkage_list, image_size, min_boxes) 59 | return priors 60 | 61 | 62 | def generate_priors(feature_map_list, shrinkage_list, image_size, min_boxes, clamp=True): 63 | priors = [] 64 | for index in range(0, len(feature_map_list[0])): 65 | scale_w = image_size[0] / shrinkage_list[0][index] 66 | scale_h = image_size[1] / shrinkage_list[1][index] 67 | for j in range(0, feature_map_list[1][index]): 68 | for i in range(0, feature_map_list[0][index]): 69 | x_center = (i + 0.5) / scale_w 70 | y_center = (j + 0.5) / scale_h 71 | 72 | for min_box in min_boxes[index]: 73 | w = min_box / image_size[0] 74 | h = min_box / image_size[1]*2 75 | priors.append([ 76 | x_center, 77 | y_center, 78 | w, 79 | h 80 | ]) 81 | print("priors nums:{}".format(len(priors))) 82 | priors = torch.tensor(priors) 83 | if clamp: 84 | torch.clamp(priors, 0.0, 1.0, out=priors) 85 | return priors 86 | 87 | 88 | def predict(width, height, confidences, boxes, prob_threshold, iou_threshold=0.3, top_k=-1): 89 | boxes = boxes[0] 90 | confidences = confidences[0] 91 | picked_box_probs = [] 92 | picked_labels = [] 93 | for class_index in range(1, confidences.shape[1]): 94 | probs = confidences[:, class_index] 95 | mask = probs > prob_threshold 96 | probs = probs[mask] 97 | if probs.shape[0] == 0: 98 | continue 99 | subset_boxes = boxes[mask, :] 100 | box_probs = np.concatenate([subset_boxes, probs.reshape(-1, 1)], axis=1) 101 | box_probs = box_utils.hard_nms(box_probs, 102 | iou_threshold=iou_threshold, 103 | top_k=top_k, 104 | ) 105 | picked_box_probs.append(box_probs) 106 | picked_labels.extend([class_index] * box_probs.shape[0]) 107 | if not picked_box_probs: 108 | return np.array([]), np.array([]), np.array([]) 109 | picked_box_probs = np.concatenate(picked_box_probs) 110 | picked_box_probs[:, 0] *= width 111 | picked_box_probs[:, 1] *= height 112 | picked_box_probs[:, 2] *= width 113 | picked_box_probs[:, 3] *= height 114 | return picked_box_probs[:, :4].astype(np.int32), np.array(picked_labels), picked_box_probs[:, 4] 115 | 116 | 117 | def inference(): 118 | input_size = [int(v.strip()) for v in args.input_size.split(",")] 119 | 120 | priors = define_img_size(input_size) 121 | result_path = args.results_path 122 | imgs_path = args.imgs_path 123 | if not os.path.exists(result_path): 124 | os.makedirs(result_path) 125 | listdir = os.listdir(imgs_path) 126 | cap = cv2.VideoCapture('2020-06-17-11-32-11.mp4') 127 | 128 | while True: 129 | #for file_path in listdir: 130 | # img_path = os.path.join(imgs_path, file_path) 131 | ret,image_ori = cap.read() 132 | #print(file_path[:-4]) 133 | #image_ori = cv2.imread(img_path) 134 | interpreter = MNN.Interpreter(args.model_path) 135 | session = interpreter.createSession() 136 | input_tensor = interpreter.getSessionInput(session) 137 | image = cv2.cvtColor(image_ori, cv2.COLOR_BGR2RGB) 138 | image = cv2.resize(image, tuple(input_size)) 139 | image = image.astype(float) 140 | image = (image - image_mean) / image_std 141 | image = image.transpose((2, 0, 1)) 142 | tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) 143 | input_tensor.copyFrom(tmp_input) 144 | time_time = time.time() 145 | interpreter.runSession(session) 146 | scores = interpreter.getSessionOutput(session, "scores").getData() 147 | boxes = interpreter.getSessionOutput(session, "boxes").getData() 148 | boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) 149 | scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) 150 | #print("inference time: {} s".format(round(time.time() - time_time, 4))) 151 | boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) 152 | boxes = box_utils.center_form_to_corner_form(boxes) 153 | boxes, labels, probs = predict(image_ori.shape[1], image_ori.shape[0], scores, boxes, args.threshold) 154 | for i in range(boxes.shape[0]): 155 | box = boxes[i, :] 156 | #print(probs[i]) 157 | cv2.rectangle(image_ori, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) 158 | #f.writelines([file_path[:-4],' ',str(probs[i]),' ',str(int(box[0])),' ',str(int(box[1])),' ',str(int(box[2])),' ',str(int(box[3])),'\n']) 159 | #cv2.imwrite(os.path.join(result_path, file_path), image_ori) 160 | #print("result_pic is written to {}".format(os.path.join(result_path, file_path))) 161 | cv2.imshow("UltraFace_mnn_py", image_ori) 162 | cv2.waitKey(1) 163 | cv2.destroyAllWindows() 164 | 165 | 166 | if __name__ == "__main__": 167 | inference() 168 | -------------------------------------------------------------------------------- /MNN/mnn/include/Backend.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Backend.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/07/06. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef Backend_hpp 10 | #define Backend_hpp 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "ErrorCode.hpp" 17 | #include "MNNForwardType.h" 18 | #include "NonCopyable.hpp" 19 | #include "Tensor.hpp" 20 | 21 | namespace MNN { 22 | 23 | struct Op; 24 | struct GpuLibrary; 25 | class Execution; 26 | 27 | /** abstract backend */ 28 | class Backend : public NonCopyable { 29 | public: 30 | /** info used to create backend */ 31 | struct Info { 32 | /** forward type. */ 33 | MNNForwardType type = MNN_FORWARD_CPU; 34 | /** for CPU only. number of threads. */ 35 | int numThread = 4; 36 | /** user data. */ 37 | BackendConfig* user = NULL; 38 | enum Mode { 39 | // The Op will be run in execution->onExecute 40 | DIRECT = 0, 41 | 42 | // The Op will be recorded. Run in onExecuteBegin and Wait in onExecuteEnd 43 | INDIRECT = 1 44 | }; 45 | Mode mode = DIRECT; 46 | }; 47 | 48 | /** backend buffer storage type */ 49 | enum StorageType { 50 | /** 51 | use NOT reusable memory. 52 | - allocates memory when `onAcquireBuffer` is called. 53 | - releases memory when `onReleaseBuffer` is called or when the backend is deleted. 54 | - do NOTHING when `onClearBuffer` is called. 55 | */ 56 | STATIC, 57 | /** 58 | use reusable memory. 59 | - allocates or reuses memory when `onAcquireBuffer` is called. prefers reusing. 60 | - collects memory for reuse when `onReleaseBuffer` is called. 61 | - releases memory when `onClearBuffer` is called or when the backend is deleted. 62 | */ 63 | DYNAMIC, 64 | /** 65 | use NOT reusable memory. 66 | - allocates memory when `onAcquireBuffer` is called. 67 | - do NOTHING when `onReleaseBuffer` is called. 68 | - releases memory when `onClearBuffer` is called or when the backend is deleted. 69 | */ 70 | DYNAMIC_SEPERATE 71 | }; 72 | 73 | public: 74 | /** 75 | * @brief initializer. 76 | * @param type forward type. 77 | */ 78 | Backend(MNNForwardType type) : mType(type) { 79 | // nothing to do 80 | } 81 | 82 | /** 83 | * @brief deinitializer. 84 | */ 85 | virtual ~Backend() = default; 86 | 87 | public: 88 | /** 89 | * @brief measure the cost for op with input and output tensors. 90 | * @param inputs input tensors. 91 | * @param outputs output tensors. 92 | * @param op given op. 93 | * @return std::make_pair(timeDelayInMs, support); 94 | */ 95 | virtual std::pair onMeasure(const std::vector& inputs, const std::vector& outputs, 96 | const MNN::Op* op) { 97 | return std::make_pair(0.0f, false); 98 | } 99 | 100 | /** 101 | * @brief create execution for op with input and output tensors. 102 | * @param inputs input tensors. 103 | * @param outputs output tensors. 104 | * @param op given op. 105 | * @return created execution if op is supported, nullptr otherwise. 106 | */ 107 | virtual Execution* onCreate(const std::vector& inputs, const std::vector& outputs, 108 | const MNN::Op* op) = 0; 109 | 110 | /** 111 | * @brief callback before resize ops. 112 | */ 113 | virtual void onResizeBegin() { 114 | // nothing to do 115 | } 116 | /** 117 | * @brief callback after resize ops. 118 | */ 119 | virtual void onResizeEnd() { 120 | // nothing to do 121 | } 122 | 123 | /** 124 | * @brief callback before executing ops. 125 | */ 126 | virtual void onExecuteBegin() const = 0; 127 | /** 128 | * @brief callback after executing ops. 129 | */ 130 | virtual void onExecuteEnd() const = 0; 131 | /** 132 | * @brief wait for all async execution to be finished. 133 | * @return success or not. 134 | */ 135 | virtual bool onWaitFinish() { 136 | return true; 137 | } 138 | /** 139 | * @brief load GPU library resource. 140 | * @param library loading load GPU library. 141 | * @return success or not. 142 | */ 143 | virtual bool onLoadLibrary(const GpuLibrary* library) { 144 | return false; 145 | } 146 | 147 | public: 148 | /** 149 | * @brief allocate buffer of tensor for given storage type. 150 | * @param tensor buffer provider. 151 | * @param storageType buffer storage type. 152 | * @return success or not. 153 | */ 154 | virtual bool onAcquireBuffer(const Tensor* tensor, StorageType storageType) = 0; 155 | 156 | /** 157 | * @brief release buffer of tensor for given storage type. 158 | * @param tensor buffer provider. 159 | * @param storageType buffer storage type. 160 | * @return success or not. 161 | */ 162 | virtual bool onReleaseBuffer(const Tensor* tensor, StorageType storageType) = 0; 163 | 164 | /** 165 | * @brief callback after all buffers needed by backend ops were allocated. 166 | * @return success or not. (result not used currently) 167 | */ 168 | virtual bool onAllocateBuffer() { 169 | return true; 170 | } 171 | 172 | /** 173 | * @brief clear all dynamic buffers. 174 | * @return success or not. 175 | */ 176 | virtual bool onClearBuffer() = 0; 177 | 178 | /** 179 | * @brief copy buffer from tensor to tensor. 180 | * @param srcTensor source buffer provider. 181 | * @param dstTensor dest buffer provider. 182 | */ 183 | virtual void onCopyBuffer(const Tensor* srcTensor, const Tensor* dstTensor) const = 0; 184 | 185 | public: 186 | /** 187 | * @brief get forward type. 188 | * @return forward type. 189 | */ 190 | inline MNNForwardType type() const { 191 | return mType; 192 | } 193 | 194 | private: 195 | const MNNForwardType mType; 196 | }; 197 | 198 | /** abstract backend register */ 199 | class BackendCreator { 200 | public: 201 | /** 202 | @brief initializer. 203 | */ 204 | virtual ~BackendCreator() = default; 205 | 206 | /** 207 | @brief create backend with given info. 208 | @param info info to create backend. 209 | @return created backend 210 | */ 211 | virtual Backend* onCreate(const Backend::Info& info) const = 0; 212 | 213 | 214 | /** 215 | @brief Turn info to supported. 216 | @param info info to valid. 217 | @return success or not 218 | */ 219 | virtual bool onValid(Backend::Info& info) const { 220 | info.mode = Backend::Info::DIRECT; 221 | return true; 222 | } 223 | protected: 224 | /** 225 | @brief deinitializer. 226 | */ 227 | BackendCreator() = default; 228 | }; 229 | 230 | /** 231 | * @brief get registered backend creator for given forward type. 232 | * @param type given forward type. 233 | * @return backend creator pointer if registered, nullptr otherwise. 234 | */ 235 | MNN_PUBLIC const BackendCreator* MNNGetExtraBackendCreator(MNNForwardType type); 236 | 237 | /** 238 | * @brief register backend creator for given forward type. 239 | * @param type given forward type. 240 | * @param creator registering backend creator. 241 | * @return true if backend creator for given forward type was not registered before, false otherwise. 242 | */ 243 | MNN_PUBLIC bool MNNInsertExtraBackendCreator(MNNForwardType type, const BackendCreator* creator, 244 | bool needCheck = false); 245 | 246 | } // namespace MNN 247 | 248 | #endif /* Backend_hpp */ 249 | -------------------------------------------------------------------------------- /MNN/src/UltraFace.cpp: -------------------------------------------------------------------------------- 1 | // Created by Linzaer on 2019/11/15. 2 | // Copyright © 2019 Linzaer. All rights reserved. 3 | 4 | #define clip(x, y) (x < 0 ? 0 : (x > y ? y : x)) 5 | 6 | #include "UltraFace.hpp" 7 | 8 | using namespace std; 9 | 10 | UltraFace::UltraFace(const std::string &mnn_path, 11 | int input_width, int input_length, int num_thread_, 12 | float score_threshold_, float iou_threshold_, int topk_) { 13 | num_thread = num_thread_; 14 | score_threshold = score_threshold_; 15 | iou_threshold = iou_threshold_; 16 | in_w = input_width; 17 | in_h = input_length; 18 | w_h_list = {in_w, in_h}; 19 | 20 | for (auto size : w_h_list) { 21 | std::vector fm_item; 22 | for (float stride : strides) { 23 | fm_item.push_back(ceil(size / stride)); 24 | } 25 | featuremap_size.push_back(fm_item); 26 | } 27 | 28 | for (auto size : w_h_list) { 29 | shrinkage_size.push_back(strides); 30 | } 31 | /* generate prior anchors */ 32 | for (int index = 0; index < num_featuremap; index++) { 33 | float scale_w = in_w / shrinkage_size[0][index]; 34 | float scale_h = in_h / shrinkage_size[1][index]; 35 | for (int j = 0; j < featuremap_size[1][index]; j++) { 36 | for (int i = 0; i < featuremap_size[0][index]; i++) { 37 | float x_center = (i + 0.5) / scale_w; 38 | float y_center = (j + 0.5) / scale_h; 39 | 40 | for (float k : min_boxes[index]) { 41 | float w = k / in_w; 42 | float h = k / in_h*2; 43 | priors.push_back({clip(x_center, 1), clip(y_center, 1), clip(w, 1), clip(h, 1)}); 44 | } 45 | } 46 | } 47 | } 48 | /* generate prior anchors finished */ 49 | 50 | num_anchors = priors.size(); 51 | 52 | ultraface_interpreter = std::shared_ptr(MNN::Interpreter::createFromFile(mnn_path.c_str())); 53 | MNN::ScheduleConfig config; 54 | config.numThread = num_thread; 55 | MNN::BackendConfig backendConfig; 56 | backendConfig.precision = (MNN::BackendConfig::PrecisionMode) 2; 57 | config.backendConfig = &backendConfig; 58 | 59 | ultraface_session = ultraface_interpreter->createSession(config); 60 | 61 | input_tensor = ultraface_interpreter->getSessionInput(ultraface_session, nullptr); 62 | 63 | } 64 | 65 | UltraFace::~UltraFace() { 66 | ultraface_interpreter->releaseModel(); 67 | ultraface_interpreter->releaseSession(ultraface_session); 68 | } 69 | 70 | int UltraFace::detect(cv::Mat &raw_image, std::vector &face_list) { 71 | if (raw_image.empty()) { 72 | std::cout << "image is empty ,please check!" << std::endl; 73 | return -1; 74 | } 75 | 76 | image_h = raw_image.rows; 77 | image_w = raw_image.cols; 78 | cv::Mat image; 79 | cv::resize(raw_image, image, cv::Size(in_w, in_h)); 80 | 81 | ultraface_interpreter->resizeTensor(input_tensor, {1, 3, in_h, in_w}); 82 | ultraface_interpreter->resizeSession(ultraface_session); 83 | std::shared_ptr pretreat( 84 | MNN::CV::ImageProcess::create(MNN::CV::BGR, MNN::CV::RGB, mean_vals, 3, 85 | norm_vals, 3)); 86 | pretreat->convert(image.data, in_w, in_h, image.step[0], input_tensor); 87 | 88 | auto start = chrono::steady_clock::now(); 89 | 90 | 91 | // run network 92 | ultraface_interpreter->runSession(ultraface_session); 93 | 94 | // get output data 95 | 96 | string scores = "scores"; 97 | string boxes = "boxes"; 98 | MNN::Tensor *tensor_scores = ultraface_interpreter->getSessionOutput(ultraface_session, scores.c_str()); 99 | MNN::Tensor *tensor_boxes = ultraface_interpreter->getSessionOutput(ultraface_session, boxes.c_str()); 100 | 101 | MNN::Tensor tensor_scores_host(tensor_scores, tensor_scores->getDimensionType()); 102 | 103 | tensor_scores->copyToHostTensor(&tensor_scores_host); 104 | 105 | MNN::Tensor tensor_boxes_host(tensor_boxes, tensor_boxes->getDimensionType()); 106 | 107 | tensor_boxes->copyToHostTensor(&tensor_boxes_host); 108 | 109 | std::vector bbox_collection; 110 | 111 | 112 | auto end = chrono::steady_clock::now(); 113 | chrono::duration elapsed = end - start; 114 | cout << "inference time:" << elapsed.count() << " s" << endl; 115 | 116 | generateBBox(bbox_collection, tensor_scores, tensor_boxes); 117 | nms(bbox_collection, face_list); 118 | return 0; 119 | } 120 | 121 | void UltraFace::generateBBox(std::vector &bbox_collection, MNN::Tensor *scores, MNN::Tensor *boxes) { 122 | for (int i = 0; i < num_anchors; i++) { 123 | if (scores->host()[i * 2 + 1] > score_threshold) { 124 | FaceInfo rects; 125 | float x_center = boxes->host()[i * 4] * center_variance * priors[i][2] + priors[i][0]; 126 | float y_center = boxes->host()[i * 4 + 1] * center_variance * priors[i][3] + priors[i][1]; 127 | float w = exp(boxes->host()[i * 4 + 2] * size_variance) * priors[i][2]; 128 | float h = exp(boxes->host()[i * 4 + 3] * size_variance) * priors[i][3]; 129 | 130 | rects.x1 = clip(x_center - w / 2.0, 1) * image_w; 131 | rects.y1 = clip(y_center - h / 2.0, 1) * image_h; 132 | rects.x2 = clip(x_center + w / 2.0, 1) * image_w; 133 | rects.y2 = clip(y_center + h / 2.0, 1) * image_h; 134 | rects.score = clip(scores->host()[i * 2 + 1], 1); 135 | bbox_collection.push_back(rects); 136 | } 137 | } 138 | } 139 | 140 | void UltraFace::nms(std::vector &input, std::vector &output, int type) { 141 | std::sort(input.begin(), input.end(), [](const FaceInfo &a, const FaceInfo &b) { return a.score > b.score; }); 142 | 143 | int box_num = input.size(); 144 | 145 | std::vector merged(box_num, 0); 146 | 147 | for (int i = 0; i < box_num; i++) { 148 | if (merged[i]) 149 | continue; 150 | std::vector buf; 151 | 152 | buf.push_back(input[i]); 153 | merged[i] = 1; 154 | 155 | float h0 = input[i].y2 - input[i].y1 + 1; 156 | float w0 = input[i].x2 - input[i].x1 + 1; 157 | 158 | float area0 = h0 * w0; 159 | 160 | for (int j = i + 1; j < box_num; j++) { 161 | if (merged[j]) 162 | continue; 163 | 164 | float inner_x0 = input[i].x1 > input[j].x1 ? input[i].x1 : input[j].x1; 165 | float inner_y0 = input[i].y1 > input[j].y1 ? input[i].y1 : input[j].y1; 166 | 167 | float inner_x1 = input[i].x2 < input[j].x2 ? input[i].x2 : input[j].x2; 168 | float inner_y1 = input[i].y2 < input[j].y2 ? input[i].y2 : input[j].y2; 169 | 170 | float inner_h = inner_y1 - inner_y0 + 1; 171 | float inner_w = inner_x1 - inner_x0 + 1; 172 | 173 | if (inner_h <= 0 || inner_w <= 0) 174 | continue; 175 | 176 | float inner_area = inner_h * inner_w; 177 | 178 | float h1 = input[j].y2 - input[j].y1 + 1; 179 | float w1 = input[j].x2 - input[j].x1 + 1; 180 | 181 | float area1 = h1 * w1; 182 | 183 | float score; 184 | 185 | score = inner_area / (area0 + area1 - inner_area); 186 | 187 | if (score > iou_threshold) { 188 | merged[j] = 1; 189 | buf.push_back(input[j]); 190 | } 191 | } 192 | switch (type) { 193 | case hard_nms: { 194 | output.push_back(buf[0]); 195 | break; 196 | } 197 | case blending_nms: { 198 | float total = 0; 199 | for (int i = 0; i < buf.size(); i++) { 200 | total += exp(buf[i].score); 201 | } 202 | FaceInfo rects; 203 | memset(&rects, 0, sizeof(rects)); 204 | for (int i = 0; i < buf.size(); i++) { 205 | float rate = exp(buf[i].score) / total; 206 | rects.x1 += buf[i].x1 * rate; 207 | rects.y1 += buf[i].y1 * rate; 208 | rects.x2 += buf[i].x2 * rate; 209 | rects.y2 += buf[i].y2 * rate; 210 | rects.score += buf[i].score * rate; 211 | } 212 | output.push_back(rects); 213 | break; 214 | } 215 | default: { 216 | printf("wrong type of nms."); 217 | exit(-1); 218 | } 219 | } 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /MNN/mnn/include/Tensor.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Tensor.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/08/14. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef Tensor_hpp 10 | #define Tensor_hpp 11 | 12 | #include 13 | #include "HalideRuntime.h" 14 | #include "MNNDefine.h" 15 | 16 | namespace MNN { 17 | 18 | /** 19 | * data container. 20 | * data for host tensor is saved in `host` field. its memory is allocated malloc directly. 21 | * data for device tensor is saved in `deviceId` field. its memory is allocated by session's backend. 22 | * usually, device tensors are created by engine (like net, session). 23 | * meanwhile, host tensors could be created by engine or user. 24 | */ 25 | class MNN_PUBLIC Tensor { 26 | public: 27 | struct InsideDescribe; 28 | 29 | /** dimension type used to create tensor */ 30 | enum DimensionType { 31 | /** for tensorflow net type. uses NHWC as data format. */ 32 | TENSORFLOW, 33 | /** for caffe net type. uses NCHW as data format. */ 34 | CAFFE, 35 | /** for caffe net type. uses NC4HW4 as data format. */ 36 | CAFFE_C4 37 | }; 38 | 39 | /** handle type */ 40 | enum HandleDataType { 41 | /** default handle type */ 42 | HANDLE_NONE = 0, 43 | /** string handle type */ 44 | HANDLE_STRING = 1 45 | }; 46 | 47 | public: 48 | /** 49 | * @brief create a tensor with dimension size and type without acquire memory for data. 50 | * @param dimSize dimension size. 51 | * @param type dimension type. 52 | */ 53 | Tensor(int dimSize = 4, DimensionType type = CAFFE); 54 | 55 | /** 56 | * @brief create a tensor with same shape as given tensor. 57 | * @param tensor shape provider. 58 | * @param type dimension type. 59 | * @param allocMemory acquire memory for data or not. 60 | * @warning tensor data won't be copied. 61 | */ 62 | Tensor(const Tensor* tensor, DimensionType type = CAFFE, bool allocMemory = true); 63 | 64 | /** deinitializer */ 65 | ~Tensor(); 66 | 67 | private: 68 | // remove all assignment operator 69 | Tensor(const Tensor& tensor) = delete; 70 | Tensor(const Tensor&& tensor) = delete; 71 | Tensor& operator=(const Tensor&) = delete; 72 | Tensor& operator=(const Tensor&&) = delete; 73 | 74 | public: 75 | /** 76 | * @brief create tensor with shape, data type and dimension type. 77 | * @param shape tensor shape. 78 | * @param type data type. 79 | * @param dimType dimension type. 80 | * @return created tensor. 81 | * @warning memory for data won't be acquired. call backend's onAcquireBuffer to get memory ready. 82 | */ 83 | static Tensor* createDevice(const std::vector& shape, halide_type_t type, DimensionType dimType = TENSORFLOW); 84 | 85 | /** 86 | * @brief create tensor with shape and dimension type. data type is represented by `T`. 87 | * @param shape tensor shape. 88 | * @param dimType dimension type. 89 | * @return created tensor. 90 | * @warning memory for data won't be acquired. call backend's onAcquireBuffer to get memory ready. 91 | */ 92 | template 93 | static Tensor* createDevice(const std::vector& shape, DimensionType dimType = TENSORFLOW) { 94 | return createDevice(shape, halide_type_of(), dimType); 95 | } 96 | 97 | /** 98 | * @brief create tensor with shape, data type, data and dimension type. 99 | * @param shape tensor shape. 100 | * @param type data type. 101 | * @param data data to save. 102 | * @param dimType dimension type. 103 | * @return created tensor. 104 | */ 105 | static Tensor* create(const std::vector& shape, halide_type_t type, void* data = NULL, 106 | DimensionType dimType = TENSORFLOW); 107 | 108 | /** 109 | * @brief create tensor with shape, data and dimension type. data type is represented by `T`. 110 | * @param shape tensor shape. 111 | * @param data data to save. 112 | * @param dimType dimension type. 113 | * @return created tensor. 114 | */ 115 | template 116 | static Tensor* create(const std::vector& shape, void* data = NULL, DimensionType dimType = TENSORFLOW) { 117 | return create(shape, halide_type_of(), data, dimType); 118 | } 119 | 120 | public: 121 | /** 122 | * @brief for DEVICE tensor, copy data from given host tensor. 123 | * @param hostTensor host tensor, the data provider. 124 | * @return true for DEVICE tensor, and false for HOST tensor. 125 | */ 126 | bool copyFromHostTensor(const Tensor* hostTensor); 127 | 128 | /** 129 | * @brief for DEVICE tensor, copy data to given host tensor. 130 | * @param hostTensor host tensor, the data consumer. 131 | * @return true for DEVICE tensor, and false for HOST tensor. 132 | */ 133 | bool copyToHostTensor(Tensor* hostTensor) const; 134 | 135 | /** 136 | * @brief create HOST tensor from DEVICE tensor, with or without data copying. 137 | * @param deviceTensor given device tensor. 138 | * @param copyData copy data or not. 139 | * @return created host tensor. 140 | */ 141 | static Tensor* createHostTensorFromDevice(const Tensor* deviceTensor, bool copyData = true); 142 | 143 | public: 144 | const halide_buffer_t& buffer() const { 145 | return mBuffer; 146 | } 147 | halide_buffer_t& buffer() { 148 | return mBuffer; 149 | } 150 | 151 | /** 152 | * @brief get dimension type. 153 | * @return dimension type. 154 | */ 155 | DimensionType getDimensionType() const; 156 | 157 | /** 158 | * @brief handle data type. used when data type code is halide_type_handle. 159 | * @return handle data type. 160 | */ 161 | HandleDataType getHandleDataType() const; 162 | 163 | /** 164 | * @brief set data type. 165 | * @param type data type defined in 'Type_generated.h'. 166 | */ 167 | void setType(int type); 168 | 169 | /** 170 | * @brief get data type. 171 | * @return data type. 172 | */ 173 | inline halide_type_t getType() const { 174 | return mBuffer.type; 175 | } 176 | 177 | /** 178 | * @brief visit host memory, data type is represented by `T`. 179 | * @return data point in `T` type. 180 | */ 181 | template 182 | T* host() const { 183 | return (T*)mBuffer.host; 184 | } 185 | 186 | /** 187 | * @brief visit device memory. 188 | * @return device data ID. what the ID means varies between backends. 189 | */ 190 | uint64_t deviceId() const { 191 | return mBuffer.device; 192 | } 193 | 194 | public: 195 | int dimensions() const { 196 | return mBuffer.dimensions; 197 | } 198 | 199 | /** 200 | * @brief get all dimensions' extent. 201 | * @return dimensions' extent. 202 | */ 203 | std::vector shape() const; 204 | 205 | /** 206 | * @brief calculate number of bytes needed to store data taking reordering flag into account. 207 | * @return bytes needed to store data 208 | */ 209 | int size() const; 210 | 211 | /** 212 | * @brief calculate number of elements needed to store data taking reordering flag into account. 213 | * @return elements needed to store data 214 | */ 215 | inline int elementSize() const { 216 | return size() / mBuffer.type.bytes(); 217 | } 218 | 219 | public: 220 | inline int width() const { 221 | if (getDimensionType() == TENSORFLOW) { 222 | return mBuffer.dim[2].extent; 223 | } 224 | 225 | return mBuffer.dim[3].extent; 226 | } 227 | inline int height() const { 228 | if (getDimensionType() == TENSORFLOW) { 229 | return mBuffer.dim[1].extent; 230 | } 231 | return mBuffer.dim[2].extent; 232 | } 233 | inline int channel() const { 234 | if (getDimensionType() == TENSORFLOW) { 235 | return mBuffer.dim[3].extent; 236 | } 237 | return mBuffer.dim[1].extent; 238 | } 239 | inline int batch() const { 240 | return mBuffer.dim[0].extent; 241 | } 242 | 243 | // visit dimension's extent & stride 244 | inline int stride(int index) const { 245 | return mBuffer.dim[index].stride; 246 | } 247 | inline int length(int index) const { 248 | return mBuffer.dim[index].extent; 249 | } 250 | inline void setStride(int index, int stride) { 251 | mBuffer.dim[index].stride = stride; 252 | } 253 | inline void setLength(int index, int length) { 254 | mBuffer.dim[index].extent = length; 255 | } 256 | 257 | public: 258 | /** 259 | * @brief print tensor data. for DEBUG use only. 260 | */ 261 | void print() const; 262 | 263 | private: 264 | halide_buffer_t mBuffer; 265 | struct InsideDescribe* mDescribe; 266 | 267 | private: 268 | friend class TensorUtils; 269 | }; 270 | } // namespace MNN 271 | 272 | #endif /* Tensor_hpp */ 273 | -------------------------------------------------------------------------------- /MNN/mnn/include/Interpreter.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Interpreter.hpp 3 | // MNN 4 | // 5 | // Created by MNN on 2018/07/23. 6 | // Copyright © 2018, Alibaba Group Holding Limited 7 | // 8 | 9 | #ifndef Interpreter_hpp 10 | #define Interpreter_hpp 11 | 12 | #include 13 | #include 14 | #include 15 | #include "ErrorCode.hpp" 16 | #include "MNNForwardType.h" 17 | #include "Tensor.hpp" 18 | 19 | namespace MNN { 20 | 21 | /** session schedule config */ 22 | struct ScheduleConfig { 23 | /** which tensor should be kept */ 24 | std::vector saveTensors; 25 | /** forward type */ 26 | MNNForwardType type = MNN_FORWARD_CPU; 27 | /** number of threads in parallel */ 28 | int numThread = 4; 29 | 30 | /** subpath to run */ 31 | struct Path { 32 | std::vector inputs; 33 | std::vector outputs; 34 | 35 | enum Mode { 36 | /** 37 | * Op Mode 38 | * - inputs means the source op, can NOT be empty. 39 | * - outputs means the sink op, can be empty. 40 | * The path will start from source op, then flow when encounter the sink op. 41 | * The sink op will not be compute in this path. 42 | */ 43 | Op = 0, 44 | 45 | /** 46 | * Tensor Mode (NOT supported yet) 47 | * - inputs means the inputs tensors, can NOT be empty. 48 | * - outputs means the outputs tensors, can NOT be empty. 49 | * It will find the pipeline that compute outputs from inputs. 50 | */ 51 | Tensor = 1 52 | }; 53 | 54 | /** running mode */ 55 | Mode mode = Op; 56 | }; 57 | Path path; 58 | 59 | /** backup backend used to create execution when desinated backend do NOT support any op */ 60 | MNNForwardType backupType = MNN_FORWARD_CPU; 61 | 62 | /** extra backend config */ 63 | BackendConfig* backendConfig = nullptr; 64 | }; 65 | 66 | class Session; 67 | struct Content; 68 | class Tensor; 69 | class Backend; 70 | 71 | class MNN_PUBLIC OperatorInfo { 72 | struct Info; 73 | 74 | public: 75 | /** Operator's name*/ 76 | const std::string& name() const; 77 | 78 | /** Operator's type*/ 79 | const std::string& type() const; 80 | 81 | /** Operator's flops, in M*/ 82 | float flops() const; 83 | 84 | protected: 85 | OperatorInfo(); 86 | ~OperatorInfo(); 87 | Info* mContent; 88 | }; 89 | 90 | typedef std::function&, const std::string& /*opName*/)> TensorCallBack; 91 | typedef std::function&, const OperatorInfo*)> TensorCallBackWithInfo; 92 | 93 | /** net data holder. multiple sessions could share same net. */ 94 | class MNN_PUBLIC Interpreter { 95 | public: 96 | /** 97 | * @brief create net from file. 98 | * @param file given file. 99 | * @return created net if success, NULL otherwise. 100 | */ 101 | static Interpreter* createFromFile(const char* file); 102 | /** 103 | * @brief create net from buffer. 104 | * @param buffer given data buffer. 105 | * @param size size of data buffer. 106 | * @return created net if success, NULL otherwise. 107 | */ 108 | static Interpreter* createFromBuffer(const void* buffer, size_t size); 109 | ~Interpreter(); 110 | 111 | public: 112 | /** 113 | * @brief create session with schedule config. created session will be managed in net. 114 | * @param config session schedule config. 115 | * @return created session if success, NULL otherwise. 116 | */ 117 | Session* createSession(const ScheduleConfig& config); 118 | 119 | /** 120 | * @brief create multi-path session with schedule configs. created session will be managed in net. 121 | * @param configs session schedule configs. 122 | * @return created session if success, NULL otherwise. 123 | */ 124 | Session* createMultiPathSession(const std::vector& configs); 125 | 126 | /** 127 | * @brief release session. 128 | * @param session given session. 129 | * @return true if given session is held by net and is freed. 130 | */ 131 | bool releaseSession(Session* session); 132 | 133 | /** 134 | * @brief call this function to get tensors ready. output tensor buffer (host or deviceId) should be retrieved 135 | * after resize of any input tensor. 136 | * @param session given session. 137 | */ 138 | void resizeSession(Session* session); 139 | 140 | /** 141 | * @brief call this function if don't need resize or create session any more, it will save a few memory that equal 142 | * to the size of model buffer 143 | */ 144 | void releaseModel(); 145 | 146 | /** 147 | * @brief Get the model buffer for user to save 148 | * @return std::make_pair(modleBuffer, modelSize). 149 | * @example: 150 | * std::ofstream output("trainResult.alinn") 151 | * auto buffer = net->getModelBuffer(); 152 | * output.write((const char*)buffer.first, buffer.second); 153 | */ 154 | std::pair getModelBuffer() const; 155 | 156 | /** 157 | * @brief update Session's Tensor to model's Const Op 158 | * @param session given session. 159 | * @return result of running. 160 | */ 161 | ErrorCode updateSessionToModel(Session* session); 162 | 163 | /** 164 | * @brief run session. 165 | * @param session given session. 166 | * @return result of running. 167 | */ 168 | ErrorCode runSession(Session* session) const; 169 | 170 | /* 171 | * @brief run session. 172 | * @param session given session. 173 | * @param before callback before each op. return true to run the op; return false to skip the op. 174 | * @param after callback after each op. return true to continue running; return false to interrupt the session. 175 | * @param sync synchronously wait for finish of execution or not. 176 | * @return result of running. 177 | */ 178 | ErrorCode runSessionWithCallBack(const Session* session, const TensorCallBack& before, const TensorCallBack& end, 179 | bool sync = false) const; 180 | 181 | /* 182 | * @brief run session. 183 | * @param session given session. 184 | * @param before callback before each op. return true to run the op; return false to skip the op. 185 | * @param after callback after each op. return true to continue running; return false to interrupt the session. 186 | * @param sync synchronously wait for finish of execution or not. 187 | * @return result of running. 188 | */ 189 | ErrorCode runSessionWithCallBackInfo(const Session* session, const TensorCallBackWithInfo& before, 190 | const TensorCallBackWithInfo& end, bool sync = false) const; 191 | 192 | /** 193 | * @brief get input tensor for given name. 194 | * @param session given session. 195 | * @param name given name. if NULL, return first input. 196 | * @return tensor if found, NULL otherwise. 197 | */ 198 | Tensor* getSessionInput(const Session* session, const char* name); 199 | /** 200 | * @brief get output tensor for given name. 201 | * @param session given session. 202 | * @param name given name. if NULL, return first output. 203 | * @return tensor if found, NULL otherwise. 204 | */ 205 | Tensor* getSessionOutput(const Session* session, const char* name); 206 | 207 | /** 208 | * @brief get all input tensors. 209 | * @param session given session. 210 | * @return all input tensors mapped with name. 211 | */ 212 | const std::map& getSessionOutputAll(const Session* session) const; 213 | /** 214 | * @brief get all output tensors. 215 | * @param session given session. 216 | * @return all output tensors mapped with name. 217 | */ 218 | const std::map& getSessionInputAll(const Session* session) const; 219 | 220 | public: 221 | /** 222 | * @brief resize given tensor. 223 | * @param tensor given tensor. 224 | * @param dims new dims. at most 6 dims. 225 | */ 226 | void resizeTensor(Tensor* tensor, const std::vector& dims); 227 | 228 | /** 229 | * @brief resize given tensor by nchw. 230 | * @param batch / N. 231 | * @param channel / C. 232 | * @param height / H. 233 | * @param width / W 234 | */ 235 | void resizeTensor(Tensor* tensor, int batch, int channel, int height, int width); 236 | 237 | /** 238 | * @brief get backend used to create given tensor. 239 | * @param session given session. 240 | * @param tensor given tensor. 241 | * @return backend used to create given tensor, may be NULL. 242 | */ 243 | const Backend* getBackend(const Session* session, const Tensor* tensor) const; 244 | 245 | /** 246 | * @brief get business code (model identifier). 247 | * @return business code. 248 | */ 249 | const char* bizCode() const; 250 | 251 | private: 252 | static Interpreter* createFromBufferInternal(Content* net); 253 | 254 | Content* mNet = nullptr; 255 | Interpreter(Content* net); 256 | 257 | Interpreter(const Interpreter&) = delete; 258 | Interpreter(const Interpreter&&) = delete; 259 | Interpreter& operator=(const Interpreter&) = delete; 260 | Interpreter& operator=(const Interpreter&&) = delete; 261 | }; 262 | } // namespace MNN 263 | 264 | #endif /* Interpreter_hpp */ 265 | -------------------------------------------------------------------------------- /MNN/python/voc_eval.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------- 2 | # Fast/er R-CNN 3 | # Licensed under The MIT License [see LICENSE for details] 4 | # Written by Bharath Hariharan 5 | # -------------------------------------------------------- 6 | from __future__ import print_function 7 | import argparse 8 | import xml.etree.ElementTree as ET 9 | import os,sys 10 | import pickle 11 | import numpy as np 12 | def parse_args(): 13 | """Parse input arguments.""" 14 | parser = argparse.ArgumentParser(description='mAP Calculation') 15 | 16 | parser.add_argument('--path', dest='path', help='The data path', type=str) 17 | args = parser.parse_args() 18 | 19 | return args 20 | 21 | def parse_rec(filename): 22 | """ Parse a PASCAL VOC xml file """ 23 | tree = ET.parse(filename) 24 | objects = [] 25 | for obj in tree.findall('object'): 26 | obj_struct = {} 27 | obj_struct['name'] = obj.find('name').text 28 | obj_struct['pose'] = obj.find('pose').text 29 | obj_struct['truncated'] = int(obj.find('truncated').text) 30 | obj_struct['difficult'] = int(obj.find('difficult').text) 31 | bbox = obj.find('bndbox') 32 | obj_struct['bbox'] = [int(bbox.find('xmin').text), 33 | int(bbox.find('ymin').text), 34 | int(bbox.find('xmax').text), 35 | int(bbox.find('ymax').text)] 36 | objects.append(obj_struct) 37 | 38 | return objects 39 | 40 | def voc_ap(rec, prec, use_07_metric=False): 41 | """ ap = voc_ap(rec, prec, [use_07_metric]) 42 | Compute VOC AP given precision and recall. 43 | If use_07_metric is true, uses the 44 | VOC 07 11 point method (default:False). 45 | """ 46 | if use_07_metric: 47 | # 11 point metric 48 | ap = 0. 49 | for t in np.arange(0., 1.1, 0.1): 50 | if np.sum(rec >= t) == 0: 51 | p = 0 52 | else: 53 | p = np.max(prec[rec >= t]) 54 | ap = ap + p / 11. 55 | else: 56 | # correct AP calculation 57 | # first append sentinel values at the end 58 | mrec = np.concatenate(([0.], rec, [1.])) 59 | mpre = np.concatenate(([0.], prec, [0.])) 60 | 61 | # compute the precision envelope 62 | for i in range(mpre.size - 1, 0, -1): 63 | mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) 64 | 65 | # to calculate area under PR curve, look for points 66 | # where X axis (recall) changes value 67 | i = np.where(mrec[1:] != mrec[:-1])[0] 68 | 69 | # and sum (\Delta recall) * prec 70 | ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) 71 | return ap 72 | 73 | def voc_eval(detpath, 74 | annopath, 75 | imagesetfile, 76 | classname, 77 | cachedir, 78 | ovthresh=0.5, 79 | use_07_metric=False): 80 | """rec, prec, ap = voc_eval(detpath, 81 | annopath, 82 | imagesetfile, 83 | classname, 84 | [ovthresh], 85 | [use_07_metric]) 86 | Top level function that does the PASCAL VOC evaluation. 87 | detpath: Path to detections 88 | detpath.format(classname) should produce the detection results file. 89 | annopath: Path to annotations 90 | annopath.format(imagename) should be the xml annotations file. 91 | imagesetfile: Text file containing the list of images, one image per line. 92 | classname: Category name (duh) 93 | cachedir: Directory for caching the annotations 94 | [ovthresh]: Overlap threshold (default = 0.5) 95 | [use_07_metric]: Whether to use VOC07's 11 point AP computation 96 | (default False) 97 | """ 98 | # assumes detections are in detpath.format(classname) 99 | # assumes annotations are in annopath.format(imagename) 100 | # assumes imagesetfile is a text file with each line an image name 101 | # cachedir caches the annotations in a pickle file 102 | 103 | # first load gt 104 | if not os.path.isdir(cachedir): 105 | os.mkdir(cachedir) 106 | cachefile = os.path.join(cachedir, 'annots.pkl') 107 | # read list of images 108 | with open(imagesetfile, 'r') as f: 109 | lines = f.readlines() 110 | imagenames = [x.strip() for x in lines] 111 | 112 | if not os.path.isfile(cachefile): 113 | # load annots 114 | recs = {} 115 | for i, imagename in enumerate(imagenames): 116 | recs[imagename] = parse_rec(annopath.format(imagename)) 117 | if i % 100 == 0: 118 | print('Reading annotation for {:d}/{:d}'.format(i + 1, len(imagenames))) 119 | # save 120 | print('Saving cached annotations to {:s}'.format(cachefile)) 121 | with open(cachefile, 'wb') as f: 122 | pickle.dump(recs, f) 123 | else: 124 | # load 125 | with open(cachefile, 'rb') as f: 126 | recs = pickle.load(f) 127 | 128 | # extract gt objects for this class 129 | class_recs = {} 130 | npos = 0 131 | for imagename in imagenames: 132 | R = [obj for obj in recs[imagename] if obj['name'] == classname] 133 | bbox = np.array([x['bbox'] for x in R]) 134 | difficult = np.array([x['difficult'] for x in R]).astype(np.bool) 135 | det = [False] * len(R) 136 | npos = npos + sum(~difficult) 137 | class_recs[imagename] = {'bbox': bbox, 138 | 'difficult': difficult, 139 | 'det': det} 140 | 141 | # read dets 142 | detfile = detpath.format(classname) 143 | with open(detfile, 'r') as f: 144 | lines = f.readlines() 145 | 146 | splitlines = [x.strip().split(' ') for x in lines] 147 | image_ids = [x[0] for x in splitlines] 148 | confidence = np.array([float(x[1]) for x in splitlines]) 149 | BB = np.array([[float(z) for z in x[2:]] for x in splitlines]) 150 | 151 | # sort by confidence 152 | sorted_ind = np.argsort(-confidence) 153 | sorted_scores = np.sort(-confidence) 154 | BB = BB[sorted_ind, :] 155 | image_ids = [image_ids[x] for x in sorted_ind] 156 | 157 | # go down dets and mark TPs and FPs 158 | nd = len(image_ids) 159 | tp = np.zeros(nd) 160 | fp = np.zeros(nd) 161 | for d in range(nd): 162 | R = class_recs[image_ids[d]] 163 | bb = BB[d, :].astype(float) 164 | ovmax = -np.inf 165 | BBGT = R['bbox'].astype(float) 166 | 167 | if BBGT.size > 0: 168 | # compute overlaps 169 | # intersection 170 | ixmin = np.maximum(BBGT[:, 0], bb[0]) 171 | iymin = np.maximum(BBGT[:, 1], bb[1]) 172 | ixmax = np.minimum(BBGT[:, 2], bb[2]) 173 | iymax = np.minimum(BBGT[:, 3], bb[3]) 174 | iw = np.maximum(ixmax - ixmin + 1., 0.) 175 | ih = np.maximum(iymax - iymin + 1., 0.) 176 | inters = iw * ih 177 | 178 | # union 179 | uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) + 180 | (BBGT[:, 2] - BBGT[:, 0] + 1.) * 181 | (BBGT[:, 3] - BBGT[:, 1] + 1.) - inters) 182 | 183 | overlaps = inters / uni 184 | ovmax = np.max(overlaps) 185 | jmax = np.argmax(overlaps) 186 | 187 | if ovmax > ovthresh: 188 | if not R['difficult'][jmax]: 189 | if not R['det'][jmax]: 190 | tp[d] = 1. 191 | R['det'][jmax] = 1 192 | else: 193 | fp[d] = 1. 194 | else: 195 | fp[d] = 1. 196 | 197 | # compute precision recall 198 | fp = np.cumsum(fp) 199 | tp = np.cumsum(tp) 200 | rec = tp / float(npos) 201 | # avoid divide by zero in case the first detection matches a difficult 202 | # ground truth 203 | prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps) 204 | ap = voc_ap(rec, prec, use_07_metric) 205 | 206 | return rec, prec, ap 207 | 208 | 209 | 210 | def _do_python_eval(res_prefix, output_dir = 'output'): 211 | _devkit_path = '/home/uc/RFSong-779-master/VOC2007/VOCdevkit' 212 | _year = '2007' 213 | _classes = ('__background__', # always index 0 214 | 'person') 215 | 216 | res_prefix = res_prefix + 'comp3_det_test_' 217 | filename = res_prefix + '{:s}.txt' 218 | annopath = os.path.join( 219 | _devkit_path, 220 | 'VOC' + _year, 221 | 'Annotations', 222 | '{:s}.xml') 223 | imagesetfile = os.path.join( 224 | _devkit_path, 225 | 'VOC' + _year, 226 | 'ImageSets', 227 | 'Main', 228 | 'test.txt') 229 | cachedir = os.path.join(_devkit_path, 'annotations_cache') 230 | aps = [] 231 | # The PASCAL VOC metric changed in 2010 232 | # use_07_metric = True if int(_year) < 2010 else False 233 | use_07_metric = False 234 | print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No')) 235 | if not os.path.isdir(output_dir): 236 | os.mkdir(output_dir) 237 | for i, cls in enumerate(_classes): 238 | if cls == '__background__': 239 | continue 240 | 241 | rec, prec, ap = voc_eval( 242 | filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.3, 243 | use_07_metric=use_07_metric) 244 | aps += [ap] 245 | print('AP for {} = {:.4f}'.format(cls, ap)) 246 | with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f: 247 | pickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f) 248 | print('Mean AP = {:.4f}'.format(np.mean(aps))) 249 | print('~~~~~~~~') 250 | print('Results:') 251 | for ap in aps: 252 | print('{:.3f}'.format(ap)) 253 | print('{:.3f}'.format(np.mean(aps))) 254 | print('~~~~~~~~') 255 | 256 | if __name__ == '__main__': 257 | args = parse_args() 258 | _do_python_eval(args.path, output_dir = 'output') 259 | -------------------------------------------------------------------------------- /vision/utils/box_utils.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | import torch 4 | 5 | 6 | def generate_priors(feature_map_list, shrinkage_list, image_size, min_boxes, clamp=True) -> torch.Tensor: 7 | priors = [] 8 | for index in range(0, len(feature_map_list[0])): 9 | scale_w = image_size[0] / shrinkage_list[0][index] 10 | scale_h = image_size[1] / shrinkage_list[1][index] 11 | for j in range(0, feature_map_list[1][index]): 12 | for i in range(0, feature_map_list[0][index]): 13 | x_center = (i + 0.5) / scale_w 14 | y_center = (j + 0.5) / scale_h 15 | 16 | for min_box in min_boxes[index]: 17 | w = min_box / image_size[0] 18 | h = min_box / image_size[1]*2 19 | priors.append([ 20 | x_center, 21 | y_center, 22 | w, 23 | h 24 | ]) 25 | print("priors nums:{}".format(len(priors))) 26 | priors = torch.tensor(priors) 27 | if clamp: 28 | torch.clamp(priors, 0.0, 1.0, out=priors) 29 | return priors 30 | 31 | 32 | def convert_locations_to_boxes(locations, priors, center_variance, 33 | size_variance): 34 | """Convert regressional location results of SSD into boxes in the form of (center_x, center_y, h, w). 35 | 36 | The conversion: 37 | $$predicted\_center * center_variance = \frac {real\_center - prior\_center} {prior\_hw}$$ 38 | $$exp(predicted\_hw * size_variance) = \frac {real\_hw} {prior\_hw}$$ 39 | We do it in the inverse direction here. 40 | Args: 41 | locations (batch_size, num_priors, 4): the regression output of SSD. It will contain the outputs as well. 42 | priors (num_priors, 4) or (batch_size/1, num_priors, 4): prior boxes. 43 | center_variance: a float used to change the scale of center. 44 | size_variance: a float used to change of scale of size. 45 | Returns: 46 | boxes: priors: [[center_x, center_y, h, w]]. All the values 47 | are relative to the image size. 48 | """ 49 | # priors can have one dimension less. 50 | if priors.dim() + 1 == locations.dim(): 51 | priors = priors.unsqueeze(0) 52 | return torch.cat([ 53 | locations[..., :2] * center_variance * priors[..., 2:] + priors[..., :2], 54 | torch.exp(locations[..., 2:] * size_variance) * priors[..., 2:] 55 | ], dim=locations.dim() - 1) 56 | 57 | 58 | def convert_boxes_to_locations(center_form_boxes, center_form_priors, center_variance, size_variance): 59 | # priors can have one dimension less 60 | if center_form_priors.dim() + 1 == center_form_boxes.dim(): 61 | center_form_priors = center_form_priors.unsqueeze(0) 62 | return torch.cat([ 63 | (center_form_boxes[..., :2] - center_form_priors[..., :2]) / center_form_priors[..., 2:] / center_variance, 64 | torch.log(center_form_boxes[..., 2:] / center_form_priors[..., 2:]) / size_variance 65 | ], dim=center_form_boxes.dim() - 1) 66 | 67 | 68 | def area_of(left_top, right_bottom) -> torch.Tensor: 69 | """Compute the areas of rectangles given two corners. 70 | 71 | Args: 72 | left_top (N, 2): left top corner. 73 | right_bottom (N, 2): right bottom corner. 74 | 75 | Returns: 76 | area (N): return the area. 77 | """ 78 | hw = torch.clamp(right_bottom - left_top, min=0.0) 79 | return hw[..., 0] * hw[..., 1] 80 | 81 | 82 | def iou_of(boxes0, boxes1, eps=1e-5): 83 | """Return intersection-over-union (Jaccard index) of boxes. 84 | 85 | Args: 86 | boxes0 (N, 4): ground truth boxes. 87 | boxes1 (N or 1, 4): predicted boxes. 88 | eps: a small number to avoid 0 as denominator. 89 | Returns: 90 | iou (N): IoU values. 91 | """ 92 | overlap_left_top = torch.max(boxes0[..., :2], boxes1[..., :2]) 93 | overlap_right_bottom = torch.min(boxes0[..., 2:], boxes1[..., 2:]) 94 | 95 | overlap_area = area_of(overlap_left_top, overlap_right_bottom) 96 | area0 = area_of(boxes0[..., :2], boxes0[..., 2:]) 97 | area1 = area_of(boxes1[..., :2], boxes1[..., 2:]) 98 | return overlap_area / (area0 + area1 - overlap_area + eps) 99 | 100 | 101 | def assign_priors(gt_boxes, gt_labels, corner_form_priors, 102 | iou_threshold): 103 | """Assign ground truth boxes and targets to priors. 104 | 105 | Args: 106 | gt_boxes (num_targets, 4): ground truth boxes. 107 | gt_labels (num_targets): labels of targets. 108 | priors (num_priors, 4): corner form priors 109 | Returns: 110 | boxes (num_priors, 4): real values for priors. 111 | labels (num_priros): labels for priors. 112 | """ 113 | # size: num_priors x num_targets 114 | ious = iou_of(gt_boxes.unsqueeze(0), corner_form_priors.unsqueeze(1)) 115 | # size: num_priors 116 | best_target_per_prior, best_target_per_prior_index = ious.max(1) 117 | # size: num_targets 118 | best_prior_per_target, best_prior_per_target_index = ious.max(0) 119 | 120 | for target_index, prior_index in enumerate(best_prior_per_target_index): 121 | best_target_per_prior_index[prior_index] = target_index 122 | # 2.0 is used to make sure every target has a prior assigned 123 | best_target_per_prior.index_fill_(0, best_prior_per_target_index, 2) 124 | # size: num_priors 125 | labels = gt_labels[best_target_per_prior_index] 126 | labels[best_target_per_prior < iou_threshold] = 0 # the backgournd id 127 | boxes = gt_boxes[best_target_per_prior_index] 128 | return boxes, labels 129 | 130 | 131 | def hard_negative_mining(loss, labels, neg_pos_ratio): 132 | """ 133 | It used to suppress the presence of a large number of negative prediction. 134 | It works on image level not batch level. 135 | For any example/image, it keeps all the positive predictions and 136 | cut the number of negative predictions to make sure the ratio 137 | between the negative examples and positive examples is no more 138 | the given ratio for an image. 139 | 140 | Args: 141 | loss (N, num_priors): the loss for each example. 142 | labels (N, num_priors): the labels. 143 | neg_pos_ratio: the ratio between the negative examples and positive examples. 144 | """ 145 | pos_mask = labels > 0 146 | num_pos = pos_mask.long().sum(dim=1, keepdim=True) 147 | num_neg = num_pos * neg_pos_ratio 148 | 149 | loss[pos_mask] = -math.inf 150 | _, indexes = loss.sort(dim=1, descending=True) 151 | _, orders = indexes.sort(dim=1) 152 | neg_mask = orders < num_neg 153 | return pos_mask | neg_mask 154 | 155 | 156 | def center_form_to_corner_form(locations): 157 | return torch.cat([locations[..., :2] - locations[..., 2:] / 2, 158 | locations[..., :2] + locations[..., 2:] / 2], locations.dim() - 1) 159 | 160 | 161 | def corner_form_to_center_form(boxes): 162 | return torch.cat([ 163 | (boxes[..., :2] + boxes[..., 2:]) / 2, 164 | boxes[..., 2:] - boxes[..., :2] 165 | ], boxes.dim() - 1) 166 | 167 | 168 | def hard_nms(box_scores, iou_threshold, top_k=-1, candidate_size=200): 169 | """ 170 | 171 | Args: 172 | box_scores (N, 5): boxes in corner-form and probabilities. 173 | iou_threshold: intersection over union threshold. 174 | top_k: keep top_k results. If k <= 0, keep all the results. 175 | candidate_size: only consider the candidates with the highest scores. 176 | Returns: 177 | picked: a list of indexes of the kept boxes 178 | """ 179 | scores = box_scores[:, -1] 180 | boxes = box_scores[:, :-1] 181 | picked = [] 182 | _, indexes = scores.sort(descending=True) 183 | indexes = indexes[:candidate_size] 184 | while len(indexes) > 0: 185 | current = indexes[0] 186 | picked.append(current.item()) 187 | if 0 < top_k == len(picked) or len(indexes) == 1: 188 | break 189 | current_box = boxes[current, :] 190 | indexes = indexes[1:] 191 | rest_boxes = boxes[indexes, :] 192 | iou = iou_of( 193 | rest_boxes, 194 | current_box.unsqueeze(0), 195 | ) 196 | indexes = indexes[iou <= iou_threshold] 197 | 198 | return box_scores[picked, :] 199 | 200 | 201 | def nms(box_scores, nms_method=None, score_threshold=None, iou_threshold=None, 202 | sigma=0.5, top_k=-1, candidate_size=200): 203 | if nms_method == "soft": 204 | return soft_nms(box_scores, score_threshold, sigma, top_k) 205 | else: 206 | return hard_nms(box_scores, iou_threshold, top_k, candidate_size=candidate_size) 207 | 208 | 209 | def soft_nms(box_scores, score_threshold, sigma=0.5, top_k=-1): 210 | """Soft NMS implementation. 211 | 212 | References: 213 | https://arxiv.org/abs/1704.04503 214 | https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/cython_nms.pyx 215 | 216 | Args: 217 | box_scores (N, 5): boxes in corner-form and probabilities. 218 | score_threshold: boxes with scores less than value are not considered. 219 | sigma: the parameter in score re-computation. 220 | scores[i] = scores[i] * exp(-(iou_i)^2 / simga) 221 | top_k: keep top_k results. If k <= 0, keep all the results. 222 | Returns: 223 | picked_box_scores (K, 5): results of NMS. 224 | """ 225 | picked_box_scores = [] 226 | while box_scores.size(0) > 0: 227 | max_score_index = torch.argmax(box_scores[:, 4]) 228 | cur_box_prob = torch.tensor(box_scores[max_score_index, :]) 229 | picked_box_scores.append(cur_box_prob) 230 | if len(picked_box_scores) == top_k > 0 or box_scores.size(0) == 1: 231 | break 232 | cur_box = cur_box_prob[:-1] 233 | box_scores[max_score_index, :] = box_scores[-1, :] 234 | box_scores = box_scores[:-1, :] 235 | ious = iou_of(cur_box.unsqueeze(0), box_scores[:, :-1]) 236 | box_scores[:, -1] = box_scores[:, -1] * torch.exp(-(ious * ious) / sigma) 237 | box_scores = box_scores[box_scores[:, -1] > score_threshold, :] 238 | if len(picked_box_scores) > 0: 239 | return torch.stack(picked_box_scores) 240 | else: 241 | return torch.tensor([]) 242 | -------------------------------------------------------------------------------- /MNN/build/CMakeFiles/Ultra-face-mnn.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /usr/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /usr/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build 50 | 51 | # Include any dependencies generated for this target. 52 | include CMakeFiles/Ultra-face-mnn.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include CMakeFiles/Ultra-face-mnn.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include CMakeFiles/Ultra-face-mnn.dir/flags.make 59 | 60 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: CMakeFiles/Ultra-face-mnn.dir/flags.make 61 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o: ../src/main.cpp 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building CXX object CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o" 63 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -o CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o -c /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp 64 | 65 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing CXX source to CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.i" 67 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -E /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp > CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.i 68 | 69 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling CXX source to assembly CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.s" 71 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -S /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/main.cpp -o CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.s 72 | 73 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.requires: 74 | 75 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.requires 76 | 77 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.provides: CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.requires 78 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.provides.build 79 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.provides 80 | 81 | CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.provides.build: CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o 82 | 83 | 84 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: CMakeFiles/Ultra-face-mnn.dir/flags.make 85 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o: ../src/UltraFace.cpp 86 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Building CXX object CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o" 87 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -o CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o -c /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp 88 | 89 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.i: cmake_force 90 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing CXX source to CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.i" 91 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -E /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp > CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.i 92 | 93 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.s: cmake_force 94 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling CXX source to assembly CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.s" 95 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -S /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/src/UltraFace.cpp -o CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.s 96 | 97 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.requires: 98 | 99 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.requires 100 | 101 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.provides: CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.requires 102 | $(MAKE) -f CMakeFiles/Ultra-face-mnn.dir/build.make CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.provides.build 103 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.provides 104 | 105 | CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.provides.build: CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o 106 | 107 | 108 | # Object files for target Ultra-face-mnn 109 | Ultra__face__mnn_OBJECTS = \ 110 | "CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o" \ 111 | "CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o" 112 | 113 | # External object files for target Ultra-face-mnn 114 | Ultra__face__mnn_EXTERNAL_OBJECTS = 115 | 116 | Ultra-face-mnn: CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o 117 | Ultra-face-mnn: CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o 118 | Ultra-face-mnn: CMakeFiles/Ultra-face-mnn.dir/build.make 119 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_shape.so.3.2.0 120 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.3.2.0 121 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_superres.so.3.2.0 122 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.3.2.0 123 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_aruco.so.3.2.0 124 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_bgsegm.so.3.2.0 125 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_bioinspired.so.3.2.0 126 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_ccalib.so.3.2.0 127 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_datasets.so.3.2.0 128 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_dpm.so.3.2.0 129 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_face.so.3.2.0 130 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_freetype.so.3.2.0 131 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_fuzzy.so.3.2.0 132 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_hdf.so.3.2.0 133 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_line_descriptor.so.3.2.0 134 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_optflow.so.3.2.0 135 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_plot.so.3.2.0 136 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_reg.so.3.2.0 137 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_saliency.so.3.2.0 138 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_stereo.so.3.2.0 139 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_structured_light.so.3.2.0 140 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_surface_matching.so.3.2.0 141 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_text.so.3.2.0 142 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_ximgproc.so.3.2.0 143 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_xobjdetect.so.3.2.0 144 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_xphoto.so.3.2.0 145 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_video.so.3.2.0 146 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_viz.so.3.2.0 147 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_phase_unwrapping.so.3.2.0 148 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_rgbd.so.3.2.0 149 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.3.2.0 150 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.3.2.0 151 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_flann.so.3.2.0 152 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.3.2.0 153 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_ml.so.3.2.0 154 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.3.2.0 155 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_photo.so.3.2.0 156 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_videoio.so.3.2.0 157 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2.0 158 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2.0 159 | Ultra-face-mnn: /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2.0 160 | Ultra-face-mnn: CMakeFiles/Ultra-face-mnn.dir/link.txt 161 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_3) "Linking CXX executable Ultra-face-mnn" 162 | $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/Ultra-face-mnn.dir/link.txt --verbose=$(VERBOSE) 163 | 164 | # Rule to build all files generated by this target. 165 | CMakeFiles/Ultra-face-mnn.dir/build: Ultra-face-mnn 166 | 167 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/build 168 | 169 | CMakeFiles/Ultra-face-mnn.dir/requires: CMakeFiles/Ultra-face-mnn.dir/src/main.cpp.o.requires 170 | CMakeFiles/Ultra-face-mnn.dir/requires: CMakeFiles/Ultra-face-mnn.dir/src/UltraFace.cpp.o.requires 171 | 172 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/requires 173 | 174 | CMakeFiles/Ultra-face-mnn.dir/clean: 175 | $(CMAKE_COMMAND) -P CMakeFiles/Ultra-face-mnn.dir/cmake_clean.cmake 176 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/clean 177 | 178 | CMakeFiles/Ultra-face-mnn.dir/depend: 179 | cd /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build /home/uc/Ultra-Light-Fast-Generic-Face-Detector-1MB/MNN/build/CMakeFiles/Ultra-face-mnn.dir/DependInfo.cmake --color=$(COLOR) 180 | .PHONY : CMakeFiles/Ultra-face-mnn.dir/depend 181 | 182 | --------------------------------------------------------------------------------