opencv/modules/objc/generator/CMakeLists.txt
Vadim Pisarevsky 3cd57ea09e
Merge pull request #26056 from vpisarev:new_dnn_engine
New dnn engine #26056

This is the 1st PR with the new engine; CI is green and PR is ready to be merged, I think.
Merge together with https://github.com/opencv/opencv_contrib/pull/3794

---

**Known limitations:**
* [solved] OpenVINO is temporarily disabled, but is probably easy to restore (it's not a deal breaker to merge this PR, I guess)
* The new engine does not support any backends nor any targets except for the default CPU implementation. But it's possible to choose the old engine when loading a model, then all the functionality is available.
* [Caffe patch is here: #26208] The new engine only supports ONNX. When a model is constructed manually or is loaded from a file of different format (.tf, .tflite, .caffe, .darknet), the old engine is used.
* Even in the case of ONNX some layers are not supported by the new engine, such as all quantized layers (including DequantizeLinear, QuantizeLinear, QLinearConv etc.), LSTM, GRU, .... It's planned, of course, to have full support for ONNX by OpenCV 5.0 gold release. When a loaded model contains unsupported layers, we switch to the old engine automatically  (at ONNX parsing time, not at `forward()` time).
* Some layers , e.g. Expat, are only partially supported by the new engine. In the case of unsupported flavours it switches to the old engine automatically (at ONNX parsing time, not at `forward()` time).
* 'Concat' graph optimization is disabled. The optimization eliminates Concat layer and instead makes the layers that generate tensors to be concatenated to write the outputs to the final destination. Of course, it's only possible when `axis=0` or `axis=N=1`. The optimization is not compatible with dynamic shapes since we need to know in advance where to store the tensors. Because some of the layer implementations have been modified to become more compatible with the new engine, the feature appears to be broken even when the old engine is used.
* Some `dnn::Net` API is not available with the new engine. Also, shape inference may return false if some of the output or intermediate tensors' shapes cannot be inferred without running the model. Probably this can be fixed by a dummy run of the model with zero inputs.
* Some overloads of `dnn::Net::getFLOPs()` and `dnn::Net::getMemoryConsumption()` are not exposed any longer in wrapper generators; but the most useful overloads are exposed (and checked by Java tests).
* [in progress] A few Einsum tests related to empty shapes have been disabled due to crashes in the tests and in Einsum implementations. The code and the tests need to be repaired.
* OpenCL implementation of Deconvolution is disabled. It's very bad and very slow anyway; need to be completely revised.
* Deconvolution3D test is now skipped, because it was only supported by CUDA and OpenVINO backends, both of which are not supported by the new engine.
* Some tests, such as FastNeuralStyle, checked that the in the case of CUDA backend there is no fallback to CPU. Currently all layers in the new engine are processed on CPU, so there are many fallbacks. The checks, therefore, have been temporarily disabled.

---

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2024-10-16 15:28:19 +03:00

131 lines
4.8 KiB
CMake

set(MODULE_NAME "objc_bindings_generator")
set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
ocv_add_module(${MODULE_NAME} INTERNAL opencv_core opencv_imgproc)
#set(OPENCV_OBJC_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/opencv_objc_signatures.json" CACHE INTERNAL "")
set(OPENCV_OBJC_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
file(REMOVE_RECURSE "${OPENCV_OBJC_BINDINGS_DIR}/osx")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source_osx") # force re-run after CMake
file(REMOVE_RECURSE "${OPENCV_OBJC_BINDINGS_DIR}/ios")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source_ios") # force re-run after CMake
file(REMOVE_RECURSE "${OPENCV_OBJC_BINDINGS_DIR}/visionos")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source_visionos") # force re-run after CMake
# This file is included from a subdirectory
set(OBJC_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
include(${OBJC_SOURCE_DIR}/common.cmake) # fill OPENCV_OBJC_MODULES
# common files
file(GLOB_RECURSE deps "${CMAKE_CURRENT_SOURCE_DIR}/templates/*")
set(__modules_config "") # list of OpenCV modules
foreach(m ${OPENCV_OBJC_MODULES})
set(module_objc_dir "${OPENCV_MODULE_${m}_LOCATION}/misc/objc")
list(APPEND deps ${OPENCV_MODULE_${m}_HEADERS})
file(GLOB_RECURSE misc_files "${module_objc_dir}/*")
list(APPEND deps ${misc_files})
string(REGEX REPLACE "^opencv_" "" m_ "${m}")
if(__modules_config)
set(__modules_config "${__modules_config},\n")
endif()
file(RELATIVE_PATH rel_path "${OpenCV_SOURCE_DIR}" "${OPENCV_MODULE_${m}_LOCATION}")
set(__modules_config "${__modules_config} { \"name\": \"${m_}\", \"location\": \"${rel_path}\" }")
endforeach(m)
if(HAVE_opencv_objc)
set(__objc_build_dir "\"objc_build_dir\": \"${CMAKE_CURRENT_BINARY_DIR}/../objc\",")
endif()
set(CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json")
set(__config_str
"{
\"rootdir\": \"${OpenCV_SOURCE_DIR}\",
${__objc_build_dir}
\"modules\": [
${__modules_config}
]
}
")
#TODO: ocv_update_file("${CONFIG_FILE}" "${__config_str}" ON_CHANGE_REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source")
if(EXISTS "${CONFIG_FILE}")
file(READ "${CONFIG_FILE}" __content)
else()
set(__content "")
endif()
if(NOT "${__content}" STREQUAL "${__config_str}")
file(WRITE "${CONFIG_FILE}" "${__config_str}")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source")
endif()
unset(__config_str)
set(objc_generated_files
# "${OPENCV_OBJC_SIGNATURES_FILE}"
)
string(REPLACE "opencv_" "" MODULES "${OPENCV_OBJC_MODULES}")
if(NOT DEFINED OPENCV_OBJC_TARGET AND APPLE_FRAMEWORK)
if(IOS)
set(OPENCV_OBJC_TARGET "ios")
elseif(XROS)
set(OPENCV_OBJC_TARGET "visionos")
else()
set(OPENCV_OBJC_TARGET "osx")
endif()
endif()
if(NOT DEFINED OPENCV_OBJC_FRAMEWORK_NAME)
if(DEFINED FRAMEWORK_NAME)
set(OPENCV_OBJC_FRAMEWORK_NAME "${FRAMEWORK_NAME}")
else()
set(OPENCV_OBJC_FRAMEWORK_NAME "opencv2")
endif()
endif()
set(objc_generated_targets "")
macro(ocv_add_objc_generated_target TARGET)
set(objc_${TARGET}_generated_output_dependecy "${OPENCV_DEPHELPER}/gen_opencv_objc_source_${TARGET}")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}")
add_custom_command(
OUTPUT ${objc_generated_files} "${objc_${TARGET}_generated_output_dependecy}"
COMMAND ${PYTHON3_EXECUTABLE} "${OBJC_SOURCE_DIR}/generator/gen_objc.py"
-p "${OBJC_SOURCE_DIR}/../python/src2/gen2.py"
-c "${CONFIG_FILE}"
-t "${TARGET}"
-f "${OPENCV_OBJC_FRAMEWORK_NAME}"
COMMAND ${CMAKE_COMMAND} -E touch "${objc_${TARGET}_generated_output_dependecy}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}"
DEPENDS "${OpenCV_SOURCE_DIR}/modules/objc/generator/gen_objc.py"
"${OpenCV_SOURCE_DIR}/modules/python/src2/gen2.py"
"${OpenCV_SOURCE_DIR}/modules/python/src2/hdr_parser.py"
# don't, result of file(WRITE): "${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json"
${deps}
# not allowed (file(WRITE) result): "${CONFIG_FILE}"
COMMENT "Generate files for Objective-C bindings (${TARGET})"
)
add_custom_target(gen_opencv_objc_source_${TARGET}
# excluded from all: ALL
DEPENDS ${objc_generated_files} ${objc_${TARGET}_generated_output_dependecy}
SOURCES "${OBJC_SOURCE_DIR}/generator/gen_objc.py"
"${OBJC_SOURCE_DIR}/generator/templates/cmakelists.template"
"${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json"
)
list(APPEND objc_generated_targets gen_opencv_objc_source_${TARGET})
endmacro()
if(OPENCV_OBJC_TARGET)
ocv_add_objc_generated_target(${OPENCV_OBJC_TARGET})
else()
ocv_add_objc_generated_target(osx)
ocv_add_objc_generated_target(ios)
ocv_add_objc_generated_target(visionos)
endif()
add_custom_target(gen_opencv_objc_source
# excluded from all: ALL
DEPENDS ${objc_generated_targets}
)