├── Do_Quantize └── Dynamic_Quant │ ├── fp16.py │ ├── q4_f16.py │ ├── q4_f32.py │ ├── q4_q8_f16.py │ ├── q4_q8_f32.py │ ├── q8_f16.py │ └── q8_f32.py ├── Export_ONNX ├── Depth_Anything_Metric_V2 │ ├── Depth_Export.py │ ├── depth_config.py │ └── modeling_modified │ │ ├── attention.py │ │ ├── dinov2.py │ │ ├── dpt.py │ │ ├── mlp.py │ │ └── patch_embed.py ├── YOLO_NAS │ ├── YOLO_NAS_Export.py │ ├── modeling_modified │ │ ├── bbox_utils.py │ │ ├── customizable_detector.py │ │ ├── detection_modules.py │ │ ├── dfl_heads.py │ │ ├── panneck.py │ │ ├── qarepvgg_block.py │ │ ├── yolo_config.py │ │ └── yolo_stages.py │ └── yolo_config.py └── YOLO_v8_v9_v10_v11_v12 │ ├── YOLO_Export.py │ ├── modeling_modified │ ├── exporter.py │ ├── head.py │ └── tasks.py │ └── yolo_config.py ├── LICENSE ├── README.md ├── YOLO_Depth ├── app │ ├── build.gradle │ ├── libs │ │ └── arm64-v8a │ │ │ └── ort_1.21.0_qnn_2.32.zip │ ├── local.properties │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── assets │ │ ├── Depth_Anything_Metric_V2.zip │ │ ├── Model_YOLO_v12_n_f16.onnx │ │ ├── Model_YOLO_v12_n_f32.ort │ │ ├── camera_fragment_shader.glsl │ │ ├── camera_vertex_shader.glsl │ │ ├── class.txt │ │ ├── yolo_fragment_shader.glsl │ │ └── yolo_vertex_shader.glsl │ │ ├── cpp │ │ ├── CMakeLists.txt │ │ ├── project.cpp │ │ └── project.h │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── myapplication │ │ │ ├── Classifier.java │ │ │ ├── GLRender.java │ │ │ └── MainActivity.java │ │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── psyduck.png │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ ├── ic_launcher_round.webp │ │ └── ic_picture_snap.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── settings.gradle └── v10 │ ├── GLRender.java │ └── project.h ├── YOLO_Depth_Qualcomm_NPU ├── README.md ├── app │ ├── build.gradle │ ├── libs │ │ └── arm64-v8a │ │ │ └── ort_1.21.0_qnn_2.32.zip │ ├── local.properties │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── assets │ │ ├── Model_YOLO_v12_n_f16.onnx │ │ ├── camera_fragment_shader.glsl │ │ ├── camera_vertex_shader.glsl │ │ ├── class.txt │ │ ├── yolo_fragment_shader.glsl │ │ └── yolo_vertex_shader.glsl │ │ ├── cpp │ │ ├── CMakeLists.txt │ │ ├── project.cpp │ │ └── project.h │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── myapplication │ │ │ ├── Classifier.java │ │ │ ├── GLRender.java │ │ │ └── MainActivity.java │ │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── psyduck.png │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ ├── ic_launcher_round.webp │ │ └── ic_picture_snap.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties └── settings.gradle ├── yolo_depth.gif └── yolo_depth2.gif /Do_Quantize/Dynamic_Quant/fp16.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import onnx.version_converter 5 | from onnxslim import slim 6 | from onnxconverter_common import float16 7 | from onnxruntime.transformers.optimizer import optimize_model 8 | 9 | 10 | # Path Setting 11 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 12 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 13 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 14 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 15 | use_gpu = True # If true, the transformers.optimizer will remain the FP16 processes. 16 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider'] 17 | 18 | 19 | # Convert the fp32 to fp16 20 | model = onnx.load(model_path) 21 | model = float16.convert_float_to_float16(model, 22 | min_positive_val=1e-7, 23 | max_finite_val=65504, 24 | keep_io_types=True, # True for keep original input format. 25 | disable_shape_infer=False, # False for more optimize. 26 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'Resize'], # The op type list for skip the conversion. These are known unsupported op type for fp16. 27 | node_block_list=None) # The node name list for skip the conversion. 28 | 29 | # ONNX Model Optimizer 30 | slim( 31 | model=quanted_model_path, 32 | output_model=quanted_model_path, 33 | no_shape_infer=False, # True for more optimize but may get errors. 34 | skip_fusion_patterns=False, 35 | no_constant_folding=False, 36 | save_as_external_data=False, 37 | verbose=False 38 | ) 39 | 40 | # transformers.optimizer 41 | model = optimize_model(quanted_model_path, 42 | use_gpu=use_gpu, 43 | opt_level=1, # If use NPU-HTP, opt_level <=1 44 | num_heads=4 if "12" in model_path else 0, # For v12 series 45 | hidden_size=144 if "12" in model_path else 0, # For v12 series 46 | provider=provider, 47 | verbose=False, 48 | model_type='bert') 49 | model.convert_float_to_float16( 50 | keep_io_types=True, 51 | force_fp16_initializers=True, 52 | use_symbolic_shape_infer=True, # True for more optimize but may get errors. 53 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'DynamicQuantizeMatMul', 'Range'] 54 | ) 55 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 56 | del model 57 | gc.collect() 58 | 59 | 60 | # onnxslim 2nd 61 | slim( 62 | model=quanted_model_path, 63 | output_model=quanted_model_path, 64 | no_shape_infer=False, # True for more optimize but may get errors. 65 | skip_fusion_patterns=False, 66 | no_constant_folding=False, 67 | save_as_external_data=False, 68 | verbose=False 69 | ) 70 | 71 | # Upgrade the Opset version. (optional process) 72 | # model = onnx.load(quanted_model_path) 73 | # model = onnx.version_converter.convert_version(model, 21) 74 | # onnx.save(model, quanted_model_path, save_as_external_data=False) 75 | 76 | # It is not recommended to convert an FP16 ONNX model to the ORT format because this process adds a Cast operation to convert the FP16 process back to FP32. 77 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q4_f16.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxconverter_common import float16 8 | from onnxruntime.transformers.optimizer import optimize_model 9 | 10 | 11 | # Path Setting 12 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 13 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 14 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 15 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 16 | use_gpu = True # If true, the transformers.optimizer will remain the FP16 processes. 17 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider'] 18 | 19 | 20 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 21 | # (optional process) 22 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 23 | 24 | 25 | # Start Weight-Only Quantize 26 | block_size = 256 # [32, 64, 128, 256]; A smaller block_size yields greater accuracy but increases quantization time and model size. 27 | symmetric = False # False may get more accuracy. 28 | accuracy_level = 2 # 0:default, 1:fp32, 2:fp16, 3:bf16, 4:int8 29 | bits = 4 # [2, 4, 8] 30 | quant_method = 'default' # ["default", "hqq", "rtn", "gptq"]; default is recommended, or you will get errors. 31 | quant_format = 'QOperator' # ["QOperator", "QDQ"]; QOperator format quantizes the model with quantized operators directly. QDQ format quantize the model by inserting DeQuantizeLinear before the MatMul., 32 | nodes_to_exclude = None # Specify the unsupported op type, for example: ReduceMean 33 | # Call subprocess may get permission failed on Windows system. 34 | subprocess.run([f'python -m onnxruntime.quantization.matmul_4bits_quantizer --input_model {model_path} --output_model {quanted_model_path} --block_size {block_size} --symmetric {symmetric} --accuracy_level {accuracy_level} --bits {bits} --quant_method {quant_method} --quant_format {quant_format} --nodes_to_exclude {nodes_to_exclude}'], shell=True) 35 | 36 | 37 | # Convert the fp32 to fp16 38 | model = onnx.load(quanted_model_path) 39 | model = float16.convert_float_to_float16(model, 40 | min_positive_val=1e-7, 41 | max_finite_val=65504, 42 | keep_io_types=True, # True for keep original input format. 43 | disable_shape_infer=False, # False for more optimize. 44 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'Resize'], # The op type list for skip the conversion. These are known unsupported op type for fp16. 45 | node_block_list=None) # The node name list for skip the conversion. 46 | 47 | # onnxslim 48 | slim( 49 | model=quanted_model_path, 50 | output_model=quanted_model_path, 51 | no_shape_infer=False, # True for more optimize but may get errors. 52 | skip_fusion_patterns=False, 53 | no_constant_folding=False, 54 | save_as_external_data=False, 55 | verbose=False 56 | ) 57 | 58 | 59 | # transformers.optimizer 60 | model = optimize_model(quanted_model_path, 61 | use_gpu=use_gpu, 62 | opt_level=2, # If use NPU-HTP, opt_level <=1 63 | num_heads=4 if "12" in model_path else 0, # For v12 series 64 | hidden_size=144 if "12" in model_path else 0, # For v12 series 65 | provider=provider, 66 | verbose=False, 67 | model_type='bert') 68 | model.convert_float_to_float16( 69 | keep_io_types=True, 70 | force_fp16_initializers=True, 71 | use_symbolic_shape_infer=True, # True for more optimize but may get errors. 72 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'DynamicQuantizeMatMul', 'Range'] 73 | ) 74 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 75 | del model 76 | gc.collect() 77 | 78 | 79 | # onnxslim 2nd 80 | slim( 81 | model=quanted_model_path, 82 | output_model=quanted_model_path, 83 | no_shape_infer=False, # True for more optimize but may get errors. 84 | skip_fusion_patterns=False, 85 | no_constant_folding=False, 86 | save_as_external_data=False, 87 | verbose=False 88 | ) 89 | 90 | 91 | # Upgrade the Opset version. (optional process) 92 | model = onnx.load(quanted_model_path) 93 | model = onnx.version_converter.convert_version(model, 21) 94 | onnx.save(model, quanted_model_path, save_as_external_data=False) 95 | 96 | # It is not recommended to convert an FP16 ONNX model to the ORT format because this process adds a Cast operation to convert the FP16 process back to FP32. 97 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q4_f32.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxruntime.transformers.optimizer import optimize_model 8 | 9 | 10 | # Path Setting 11 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 12 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 13 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 14 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 15 | use_gpu = False # If true, the transformers.optimizer will remain the FP16 processes. 16 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider'] 17 | 18 | 19 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 20 | # (optional process) 21 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 22 | 23 | 24 | # Start Weight-Only Quantize 25 | block_size = 256 # [32, 64, 128, 256]; A smaller block_size yields greater accuracy but increases quantization time and model size. 26 | symmetric = False # False may get more accuracy. 27 | accuracy_level = 2 # 0:default, 1:fp32, 2:fp16, 3:bf16, 4:int8 28 | bits = 4 # [2, 4, 8] 29 | quant_method = 'default' # ["default", "hqq", "rtn", "gptq"]; default is recommended, or you will get errors. 30 | quant_format = 'QOperator' # ["QOperator", "QDQ"]; QOperator format quantizes the model with quantized operators directly. QDQ format quantize the model by inserting DeQuantizeLinear before the MatMul., 31 | nodes_to_exclude = None # Specify the unsupported op type, for example: ReduceMean 32 | # Call subprocess may get permission failed on Windows system. 33 | subprocess.run([f'python -m onnxruntime.quantization.matmul_4bits_quantizer --input_model {model_path} --output_model {quanted_model_path} --block_size {block_size} --symmetric {symmetric} --accuracy_level {accuracy_level} --bits {bits} --quant_method {quant_method} --quant_format {quant_format} --nodes_to_exclude {nodes_to_exclude}'], shell=True) 34 | 35 | 36 | # ONNX Model Optimizer 37 | slim( 38 | model=quanted_model_path, 39 | output_model=quanted_model_path, 40 | no_shape_infer=False, # True for more optimize but may get errors. 41 | skip_fusion_patterns=False, 42 | no_constant_folding=False, 43 | save_as_external_data=False, 44 | verbose=False 45 | ) 46 | 47 | 48 | # transformers.optimizer 49 | model = optimize_model(quanted_model_path, 50 | use_gpu=use_gpu, 51 | opt_level=2, 52 | num_heads=4 if "12" in model_path else 0, # For v12 series 53 | hidden_size=144 if "12" in model_path else 0, # For v12 series 54 | provider=provider, 55 | verbose=False, 56 | model_type='bert') 57 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 58 | del model 59 | gc.collect() 60 | 61 | 62 | # onnxsim 2nd 63 | slim( 64 | model=quanted_model_path, 65 | output_model=quanted_model_path, 66 | no_shape_infer=False, # True for more optimize but may get errors. 67 | skip_fusion_patterns=False, 68 | no_constant_folding=False, 69 | save_as_external_data=False, 70 | verbose=False 71 | ) 72 | 73 | 74 | # Upgrade the Opset version. (optional process) 75 | model = onnx.load(quanted_model_path) 76 | model = onnx.version_converter.convert_version(model, 21) 77 | onnx.save(model, quanted_model_path, save_as_external_data=False) 78 | del model 79 | gc.collect() 80 | 81 | 82 | # Convert the simplified model to ORT format. 83 | optimization_style = "Runtime" # ['Runtime', 'Fixed']; Runtime for XNNPACK/NNAPI/QNN/CoreML..., Fixed for CPU provider 84 | target_platform = "arm" # ['arm', 'amd64']; The 'amd64' means x86_64 desktop, not means the AMD chip. 85 | # Call subprocess may get permission failed on Windows system. 86 | subprocess.run([f'python -m onnxruntime.tools.convert_onnx_models_to_ort --output_dir {quanted_folder_path} --optimization_style {optimization_style} --target_platform {target_platform} --enable_type_reduction {quanted_folder_path}'], shell=True) 87 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q4_q8_f16.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxconverter_common import float16 8 | from onnxruntime.quantization import QuantType, quantize_dynamic 9 | from onnxruntime.transformers.optimizer import optimize_model 10 | 11 | 12 | # Path Setting 13 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 14 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 15 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 16 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 17 | use_gpu = True # If true, the transformers.optimizer will remain the FP16 processes. 18 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider'] 19 | 20 | 21 | 22 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 23 | # (optional process) 24 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 25 | 26 | 27 | # Start Weight-Only Quantize 28 | block_size = 256 # [32, 64, 128, 256]; A smaller block_size yields greater accuracy but increases quantization time and model size. 29 | symmetric = False # False may get more accuracy. 30 | accuracy_level = 2 # 0:default, 1:fp32, 2:fp16, 3:bf16, 4:int8 31 | bits = 4 # [2, 4, 8] 32 | quant_method = 'default' # ["default", "hqq", "rtn", "gptq"]; default is recommended, or you will get errors. 33 | quant_format = 'QOperator' # ["QOperator", "QDQ"]; QOperator format quantizes the model with quantized operators directly. QDQ format quantize the model by inserting DeQuantizeLinear before the MatMul., 34 | nodes_to_exclude = None # Specify the unsupported op type, for example: ReduceMean 35 | # Call subprocess may get permission failed on Windows system. 36 | subprocess.run([f'python -m onnxruntime.quantization.matmul_4bits_quantizer --input_model {model_path} --output_model {quanted_model_path} --block_size {block_size} --symmetric {symmetric} --accuracy_level {accuracy_level} --bits {bits} --quant_method {quant_method} --quant_format {quant_format} --nodes_to_exclude {nodes_to_exclude}'], shell=True) 37 | 38 | 39 | # Start Quantize 40 | def find_nodes_of_type(model_path, node_type): 41 | model = onnx.load(model_path) 42 | nodes_to_exclude = set() 43 | for node in model.graph.node: 44 | if node.op_type == node_type: 45 | nodes_to_exclude.add(node.name) 46 | return nodes_to_exclude 47 | 48 | 49 | nodes_to_exclude = find_nodes_of_type(quanted_model_path, "MatMulNBits") # "To avoid duplicate quantization." 50 | quantize_dynamic( 51 | model_input=quanted_model_path, 52 | model_output=quanted_model_path, 53 | per_channel=True, # True for model accuracy but cost a lot of time during quanting process. 54 | reduce_range=False, # True for some x86_64 platform. 55 | weight_type=QuantType.QUInt8, # It is recommended using uint8 + Symmetric False 56 | extra_options={'ActivationSymmetric': False, # True for inference speed. False may keep more accuracy. 57 | 'WeightSymmetric': False, # True for inference speed. False may keep more accuracy. 58 | 'EnableSubgraph': True, # True for more quant. 59 | 'ForceQuantizeNoInputCheck': False, # True for more quant. 60 | 'MatMulConstBOnly': False # False for more quant. Sometime, the inference speed may get worse. 61 | }, 62 | nodes_to_exclude=nodes_to_exclude, # Specify the node names to exclude quant process. Example: nodes_to_exclude={'/Gather'} 63 | use_external_data_format=False # Save the model into two parts. 64 | ) 65 | 66 | # Convert the fp32 to fp16 67 | model = onnx.load(quanted_model_path) 68 | model = float16.convert_float_to_float16(model, 69 | min_positive_val=1e-7, 70 | max_finite_val=65504, 71 | keep_io_types=True, # True for keep original input format. 72 | disable_shape_infer=False, # False for more optimize. 73 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'Resize'], # The op type list for skip the conversion. These are known unsupported op type for fp16. 74 | node_block_list=None) # The node name list for skip the conversion. 75 | 76 | # onnxslim 77 | slim( 78 | model=quanted_model_path, 79 | output_model=quanted_model_path, 80 | no_shape_infer=False, # True for more optimize but may get errors. 81 | skip_fusion_patterns=False, 82 | no_constant_folding=False, 83 | save_as_external_data=False, 84 | verbose=False 85 | ) 86 | 87 | 88 | # transformers.optimizer 89 | model = optimize_model(quanted_model_path, 90 | use_gpu=use_gpu, 91 | opt_level=2, # If use NPU-HTP, opt_level <=1 92 | num_heads=4 if "12" in model_path else 0, # For v12 series 93 | hidden_size=144 if "12" in model_path else 0, # For v12 series 94 | provider=provider, 95 | verbose=False, 96 | model_type='bert') 97 | model.convert_float_to_float16( 98 | keep_io_types=True, 99 | force_fp16_initializers=True, 100 | use_symbolic_shape_infer=True, # True for more optimize but may get errors. 101 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'DynamicQuantizeMatMul', 'Range'] 102 | ) 103 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 104 | del model 105 | gc.collect() 106 | 107 | 108 | # onnxslim 109 | slim( 110 | model=quanted_model_path, 111 | output_model=quanted_model_path, 112 | no_shape_infer=False, # True for more optimize but may get errors. 113 | skip_fusion_patterns=False, 114 | no_constant_folding=False, 115 | save_as_external_data=False, 116 | verbose=False 117 | ) 118 | 119 | 120 | # Upgrade the Opset version. (optional process) 121 | model = onnx.load(quanted_model_path) 122 | model = onnx.version_converter.convert_version(model, 21) 123 | onnx.save(model, quanted_model_path, save_as_external_data=False) 124 | 125 | # It is not recommended to convert an FP16 ONNX model to the ORT format because this process adds a Cast operation to convert the FP16 process back to FP32. 126 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q4_q8_f32.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxruntime.quantization import QuantType, quantize_dynamic 8 | from onnxruntime.transformers.optimizer import optimize_model 9 | 10 | 11 | # Path Setting 12 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 13 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 14 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 15 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 16 | use_gpu = False # If true, the transformers.optimizer will remain the FP16 processes. 17 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider'] 18 | 19 | 20 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 21 | # (optional process) 22 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 23 | 24 | 25 | # Start Weight-Only Quantize 26 | block_size = 256 # [32, 64, 128, 256]; A smaller block_size yields greater accuracy but increases quantization time and model size. 27 | symmetric = False # False may get more accuracy. 28 | accuracy_level = 2 # 0:default, 1:fp32, 2:fp16, 3:bf16, 4:int8 29 | bits = 4 # [2, 4, 8] 30 | quant_method = 'default' # ["default", "hqq", "rtn", "gptq"]; default is recommended, or you will get errors. 31 | quant_format = 'QOperator' # ["QOperator", "QDQ"]; QOperator format quantizes the model with quantized operators directly. QDQ format quantize the model by inserting DeQuantizeLinear before the MatMul., 32 | nodes_to_exclude = None # Specify the unsupported op type, for example: ReduceMean 33 | # Call subprocess may get permission failed on Windows system. 34 | subprocess.run([f'python -m onnxruntime.quantization.matmul_4bits_quantizer --input_model {model_path} --output_model {quanted_model_path} --block_size {block_size} --symmetric {symmetric} --accuracy_level {accuracy_level} --bits {bits} --quant_method {quant_method} --quant_format {quant_format} --nodes_to_exclude {nodes_to_exclude}'], shell=True) 35 | 36 | 37 | # Start Quantize 38 | def find_nodes_of_type(model_path, node_type): 39 | model = onnx.load(model_path) 40 | nodes_to_exclude = set() 41 | for node in model.graph.node: 42 | if node.op_type == node_type: 43 | nodes_to_exclude.add(node.name) 44 | return nodes_to_exclude 45 | 46 | 47 | nodes_to_exclude = find_nodes_of_type(quanted_model_path, "MatMulNBits") # "To avoid duplicate quantization." 48 | quantize_dynamic( 49 | model_input=quanted_model_path, 50 | model_output=quanted_model_path, 51 | per_channel=True, # True for model accuracy but cost a lot of time during quanting process. 52 | reduce_range=False, # True for some x86_64 platform. 53 | weight_type=QuantType.QUInt8, # It is recommended using uint8 + Symmetric False 54 | extra_options={'ActivationSymmetric': False, # True for inference speed. False may keep more accuracy. 55 | 'WeightSymmetric': False, # True for inference speed. False may keep more accuracy. 56 | 'EnableSubgraph': True, # True for more quant. 57 | 'ForceQuantizeNoInputCheck': False, # True for more quant. 58 | 'MatMulConstBOnly': False # False for more quant. Sometime, the inference speed may get worse. 59 | }, 60 | nodes_to_exclude=nodes_to_exclude, # Specify the node names to exclude quant process. Example: nodes_to_exclude={'/Gather'} 61 | use_external_data_format=False # Save the model into two parts. 62 | ) 63 | 64 | 65 | # onnxslim 66 | slim( 67 | model=quanted_model_path, 68 | output_model=quanted_model_path, 69 | no_shape_infer=False, # True for more optimize but may get errors. 70 | skip_fusion_patterns=False, 71 | no_constant_folding=False, 72 | save_as_external_data=False, 73 | verbose=False 74 | ) 75 | 76 | 77 | # transformers.optimizer 78 | model = optimize_model(quanted_model_path, 79 | use_gpu=use_gpu, 80 | opt_level=2, 81 | num_heads=4 if "12" in model_path else 0, # For v12 series 82 | hidden_size=144 if "12" in model_path else 0, # For v12 series 83 | provider=provider, 84 | verbose=False, 85 | model_type='bert') 86 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 87 | del model 88 | gc.collect() 89 | 90 | 91 | # onnxslim 92 | slim( 93 | model=quanted_model_path, 94 | output_model=quanted_model_path, 95 | no_shape_infer=False, # True for more optimize but may get errors. 96 | skip_fusion_patterns=False, 97 | no_constant_folding=False, 98 | save_as_external_data=False, 99 | verbose=False 100 | ) 101 | 102 | 103 | # Upgrade the Opset version. (optional process) 104 | model = onnx.load(quanted_model_path) 105 | model = onnx.version_converter.convert_version(model, 21) 106 | onnx.save(model, quanted_model_path, save_as_external_data=False) 107 | del model 108 | gc.collect() 109 | 110 | 111 | # Convert the simplified model to ORT format. 112 | optimization_style = "Runtime" # ['Runtime', 'Fixed']; Runtime for XNNPACK/NNAPI/QNN/CoreML..., Fixed for CPU provider 113 | target_platform = "arm" # ['arm', 'amd64']; The 'amd64' means x86_64 desktop, not means the AMD chip. 114 | # Call subprocess may get permission failed on Windows system. 115 | subprocess.run([f'python -m onnxruntime.tools.convert_onnx_models_to_ort --output_dir {quanted_folder_path} --optimization_style {optimization_style} --target_platform {target_platform} --enable_type_reduction {quanted_folder_path}'], shell=True) 116 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q8_f16.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxconverter_common import float16 8 | from onnxruntime.quantization import QuantType, quantize_dynamic 9 | from onnxruntime.transformers.optimizer import optimize_model 10 | 11 | 12 | # Path Setting 13 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 14 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 15 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 16 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 17 | use_gpu = True # If true, the transformers.optimizer will remain the FP16 processes. 18 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider', 'CoreMLExecutionProvider'] 19 | 20 | 21 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 22 | # (optional process) 23 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 24 | 25 | 26 | # Start Quantize 27 | quantize_dynamic( 28 | model_input=model_path, 29 | model_output=quanted_model_path, 30 | per_channel=True, # True for model accuracy but cost a lot of time during quanting process. 31 | reduce_range=False, # True for some x86_64 platform. 32 | weight_type=QuantType.QUInt8, # It is recommended using uint8 + Symmetric False 33 | extra_options={'ActivationSymmetric': False, # True for inference speed. False may keep more accuracy. 34 | 'WeightSymmetric': False, # True for inference speed. False may keep more accuracy. 35 | 'EnableSubgraph': True, # True for more quant. 36 | 'ForceQuantizeNoInputCheck': False, # True for more quant. 37 | 'MatMulConstBOnly': False # False for more quant. Sometime, the inference speed may get worse. 38 | }, 39 | nodes_to_exclude=None, # Specify the node names to exclude quant process. Example: nodes_to_exclude={'/Gather'} 40 | use_external_data_format=False # Save the model into two parts. 41 | ) 42 | 43 | 44 | # Convert the fp32 to fp16 45 | model = onnx.load(quanted_model_path) 46 | model = float16.convert_float_to_float16(model, 47 | min_positive_val=1e-7, 48 | max_finite_val=65504, 49 | keep_io_types=True, # True for keep original input format. 50 | disable_shape_infer=False, # False for more optimize. 51 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'Resize'], # The op type list for skip the conversion. These are known unsupported op type for fp16. 52 | node_block_list=None) # The node name list for skip the conversion. 53 | 54 | 55 | # onnxslim 56 | slim( 57 | model=quanted_model_path, 58 | output_model=quanted_model_path, 59 | no_shape_infer=False, # True for more optimize but may get errors. 60 | skip_fusion_patterns=False, 61 | no_constant_folding=False, 62 | save_as_external_data=False, 63 | verbose=False 64 | ) 65 | 66 | 67 | # transformers.optimizer 68 | model = optimize_model(quanted_model_path, 69 | use_gpu=use_gpu, 70 | opt_level=2, # If use NPU-HTP, opt_level <=1 71 | num_heads=4 if "12" in model_path else 0, # For v12 series 72 | hidden_size=144 if "12" in model_path else 0, # For v12 series 73 | provider=provider, 74 | verbose=False, 75 | model_type='bert') 76 | model.convert_float_to_float16( 77 | keep_io_types=True, 78 | force_fp16_initializers=True, 79 | use_symbolic_shape_infer=True, # True for more optimize but may get errors. 80 | op_block_list=['DynamicQuantizeLinear', 'DequantizeLinear', 'DynamicQuantizeMatMul', 'Range'] 81 | ) 82 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 83 | del model 84 | gc.collect() 85 | 86 | 87 | # onnxslim 88 | slim( 89 | model=quanted_model_path, 90 | output_model=quanted_model_path, 91 | no_shape_infer=False, # True for more optimize but may get errors. 92 | skip_fusion_patterns=False, 93 | no_constant_folding=False, 94 | save_as_external_data=False, 95 | verbose=False 96 | ) 97 | 98 | 99 | # Upgrade the Opset version. (optional process) 100 | model = onnx.load(quanted_model_path) 101 | model = onnx.version_converter.convert_version(model, 21) 102 | onnx.save(model, quanted_model_path, save_as_external_data=False) 103 | 104 | # It is not recommended to convert an FP16 ONNX model to the ORT format because this process adds a Cast operation to convert the FP16 process back to FP32. 105 | 106 | -------------------------------------------------------------------------------- /Do_Quantize/Dynamic_Quant/q8_f32.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gc 3 | import onnx 4 | import subprocess 5 | import onnx.version_converter 6 | from onnxslim import slim 7 | from onnxruntime.quantization import QuantType, quantize_dynamic 8 | from onnxruntime.transformers.optimizer import optimize_model 9 | 10 | 11 | # Path Setting 12 | original_folder_path = r"C:\Users\Downloads\Model_ONNX" # The original folder. 13 | quanted_folder_path = r"C:\Users\Downloads\Model_ONNX_Quanted" # The quanted folder. 14 | model_path = os.path.join(original_folder_path, "Model.onnx") # The original fp32 model path. 15 | quanted_model_path = os.path.join(quanted_folder_path, "Model_quanted.onnx") # The quanted model stored path. 16 | use_gpu = False # If true, the transformers.optimizer will remain the FP16 processes. 17 | provider = 'CPUExecutionProvider' # ['CPUExecutionProvider', 'CUDAExecutionProvider', 'CoreMLExecutionProvider'] 18 | 19 | 20 | # Preprocess, it also cost alot of memory during preprocess, you can close this command and keep quanting. Call subprocess may get permission failed on Windows system. 21 | # (optional process) 22 | # subprocess.run([f'python -m onnxruntime.quantization.preprocess --auto_merge --all_tensors_to_one_file --input {model_path} --output {quanted_folder_path}'], shell=True) 23 | 24 | 25 | # Start Quantize 26 | quantize_dynamic( 27 | model_input=model_path, 28 | model_output=quanted_model_path, 29 | per_channel=True, # True for model accuracy but cost a lot of time during quanting process. 30 | reduce_range=False, # True for some x86_64 platform. 31 | weight_type=QuantType.QUInt8, # It is recommended using uint8 + Symmetric False 32 | extra_options={'ActivationSymmetric': False, # True for inference speed. False may keep more accuracy. 33 | 'WeightSymmetric': False, # True for inference speed. False may keep more accuracy. 34 | 'EnableSubgraph': True, # True for more quant. 35 | 'ForceQuantizeNoInputCheck': False, # True for more quant. 36 | 'MatMulConstBOnly': False # False for more quant. Sometime, the inference speed may get worse. 37 | }, 38 | nodes_to_exclude=None, # Specify the node names to exclude quant process. Example: nodes_to_exclude={'/Gather'} 39 | use_external_data_format=False # Save the model into two parts. 40 | ) 41 | 42 | 43 | # onnxslim 44 | slim( 45 | model=quanted_model_path, 46 | output_model=quanted_model_path, 47 | no_shape_infer=False, # True for more optimize but may get errors. 48 | skip_fusion_patterns=False, 49 | no_constant_folding=False, 50 | save_as_external_data=False, 51 | verbose=False 52 | ) 53 | 54 | 55 | # transformers.optimizer 56 | model = optimize_model(quanted_model_path, 57 | use_gpu=use_gpu, 58 | opt_level=2, 59 | num_heads=4 if "12" in model_path else 0, # For v12 series 60 | hidden_size=144 if "12" in model_path else 0, # For v12 series 61 | provider=provider, 62 | verbose=False, 63 | model_type='bert') 64 | model.save_model_to_file(quanted_model_path, use_external_data_format=False) 65 | del model 66 | gc.collect() 67 | 68 | 69 | # onnxslim 70 | slim( 71 | model=quanted_model_path, 72 | output_model=quanted_model_path, 73 | no_shape_infer=False, # True for more optimize but may get errors. 74 | skip_fusion_patterns=False, 75 | no_constant_folding=False, 76 | save_as_external_data=False, 77 | verbose=False 78 | ) 79 | 80 | 81 | # Upgrade the Opset version. (optional process) 82 | model = onnx.load(quanted_model_path) 83 | model = onnx.version_converter.convert_version(model, 21) 84 | onnx.save(model, quanted_model_path, save_as_external_data=False) 85 | del model 86 | gc.collect() 87 | 88 | 89 | # Convert the simplified model to ORT format. 90 | optimization_style = "Runtime" # ['Runtime', 'Fixed']; Runtime for XNNPACK/NNAPI/QNN/CoreML..., Fixed for CPU provider 91 | target_platform = "arm" # ['arm', 'amd64']; The 'amd64' means x86_64 desktop, not means the AMD chip. 92 | # Call subprocess may get permission failed on Windows system. 93 | subprocess.run([f'python -m onnxruntime.tools.convert_onnx_models_to_ort --output_dir {quanted_folder_path} --optimization_style {optimization_style} --target_platform {target_platform} --enable_type_reduction {quanted_folder_path}'], shell=True) 94 | 95 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/Depth_Export.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import shutil 3 | from depth_config import EXPORT_MODEL_ENCODER_TYPE, EXPORT_DEPTH_INPUT_SIZE, MAX_DEPTH 4 | import sys 5 | import os 6 | 7 | # Set the path 8 | model_path = "C:/Users/dake/Desktop/Depth_Anything_Metric_V2/depth_anything_v2_metric_hypersim_vits.pth" # Specify the path where the depth model stored. Must match the "EXPORT_MODEL_ENCODER_TYPE". 9 | output_path = "./Depth_Anything_Metric_V2.onnx" 10 | depth_metric_path = "C:/Users/Downloads/Depth-Anything-V2-main/metric_depth/depth_anything_v2" # Specify the path where the Depth-Anything-V2 github project downloaded. 11 | 12 | # Replace the original source code. 13 | shutil.copy("./modeling_modified/dpt.py", depth_metric_path + "/dpt.py") 14 | shutil.copy("./modeling_modified/dinov2.py", depth_metric_path + "/dinov2.py") 15 | shutil.copy("./modeling_modified/mlp.py", depth_metric_path + "/dinov2_layers/mlp.py") 16 | shutil.copy("./modeling_modified/patch_embed.py", depth_metric_path + "/dinov2_layers/patch_embed.py") 17 | shutil.copy("./modeling_modified/attention.py", depth_metric_path + "/dinov2_layers/attention.py") 18 | shutil.copy("./depth_config.py", "./modeling_modified/depth_config.py") 19 | sys.path.append(os.path.dirname(os.path.abspath(depth_metric_path))) 20 | 21 | model_configs = { 22 | 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 23 | 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 24 | 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]}, 25 | 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]} 26 | } 27 | 28 | from depth_anything_v2.dpt import DepthAnythingV2 29 | 30 | model = DepthAnythingV2(**{**model_configs[EXPORT_MODEL_ENCODER_TYPE], 'max_depth': MAX_DEPTH}) 31 | model.load_state_dict(torch.load(model_path, map_location='cpu')) 32 | model.to('cpu').eval() 33 | images = torch.ones(EXPORT_DEPTH_INPUT_SIZE, dtype=torch.uint8) 34 | print("Export Start") 35 | with torch.inference_mode(): 36 | torch.onnx.export( 37 | model, 38 | (images,), 39 | output_path, 40 | input_names=['images'], 41 | output_names=['outputs'], 42 | do_constant_folding=True, 43 | opset_version=17) 44 | print("Export Done!") 45 | 46 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/depth_config.py: -------------------------------------------------------------------------------- 1 | EXPORT_MODEL_ENCODER_TYPE = 'vits' 2 | EXPORT_DEPTH_INPUT_SIZE = [1, 3, 720, 1280] # Input image shape. Batch size must be 1. Should be a multiple of GPU group (e.g., 16) for optimal efficiency. 3 | EXPORT_DEPTH_RESIZE = (294, 518) # Must be a multiple of "14" and less than 518. Resize input to this shape to accelerate Depth inference. Maintain the same W/H ratio as the input images. 4 | MAX_DEPTH = 20 # 20 for indoor model, 80 for outdoor model 5 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/modeling_modified/attention.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # All rights reserved. 3 | # 4 | # This source code is licensed under the license found in the 5 | # LICENSE file in the root directory of this source tree. 6 | 7 | # References: 8 | # https://github.com/facebookresearch/dino/blob/master/vision_transformer.py 9 | # https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py 10 | 11 | import logging 12 | 13 | from torch import Tensor 14 | from torch import nn 15 | 16 | 17 | logger = logging.getLogger("dinov2") 18 | 19 | 20 | try: 21 | from xformers.ops import memory_efficient_attention, unbind, fmha 22 | 23 | XFORMERS_AVAILABLE = True 24 | except ImportError: 25 | logger.warning("xFormers not available") 26 | XFORMERS_AVAILABLE = False 27 | 28 | 29 | class Attention(nn.Module): 30 | def __init__( 31 | self, 32 | dim: int, 33 | num_heads: int = 8, 34 | qkv_bias: bool = False, 35 | proj_bias: bool = True, 36 | attn_drop: float = 0.0, 37 | proj_drop: float = 0.0, 38 | ) -> None: 39 | super().__init__() 40 | self.num_heads = num_heads 41 | head_dim = dim // num_heads 42 | self.scale = head_dim**-0.5 43 | 44 | self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) 45 | self.attn_drop = nn.Dropout(attn_drop) 46 | self.proj = nn.Linear(dim, dim, bias=proj_bias) 47 | self.proj_drop = nn.Dropout(proj_drop) 48 | 49 | def forward(self, x: Tensor) -> Tensor: 50 | B, N, C = x.shape 51 | qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) 52 | q, k, v = qkv[0] * self.scale, qkv[1], qkv[2] 53 | attn = self.attn_drop((q @ k.transpose(-2, -1)).softmax(dim=-1)) 54 | return self.proj_drop(self.proj((attn @ v).transpose(1, 2).reshape(B, N, C))) 55 | 56 | 57 | class MemEffAttention(Attention): 58 | def forward(self, x: Tensor, attn_bias=None) -> Tensor: 59 | return super().forward(x) 60 | 61 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/modeling_modified/dpt.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | from torchvision.transforms import Compose 6 | 7 | from .dinov2 import DINOv2 8 | from .util.blocks import FeatureFusionBlock, _make_scratch 9 | from .util.transform import Resize, NormalizeImage, PrepareForNet 10 | from depth_config import EXPORT_DEPTH_RESIZE 11 | 12 | def _make_fusion_block(features, use_bn, size=None): 13 | return FeatureFusionBlock( 14 | features, 15 | nn.ReLU(False), 16 | deconv=False, 17 | bn=use_bn, 18 | expand=False, 19 | align_corners=True, 20 | size=size, 21 | ) 22 | 23 | 24 | class ConvBlock(nn.Module): 25 | def __init__(self, in_feature, out_feature): 26 | super().__init__() 27 | 28 | self.conv_block = nn.Sequential( 29 | nn.Conv2d(in_feature, out_feature, kernel_size=3, stride=1, padding=1), 30 | nn.BatchNorm2d(out_feature), 31 | nn.ReLU(True) 32 | ) 33 | 34 | def forward(self, x): 35 | return self.conv_block(x) 36 | 37 | 38 | class DPTHead(nn.Module): 39 | def __init__( 40 | self, 41 | in_channels, 42 | features=256, 43 | use_bn=False, 44 | out_channels=[256, 512, 1024, 1024], 45 | use_clstoken=False 46 | ): 47 | super(DPTHead, self).__init__() 48 | 49 | self.use_clstoken = use_clstoken 50 | 51 | self.projects = nn.ModuleList([ 52 | nn.Conv2d( 53 | in_channels=in_channels, 54 | out_channels=out_channel, 55 | kernel_size=1, 56 | stride=1, 57 | padding=0, 58 | ) for out_channel in out_channels 59 | ]) 60 | 61 | self.resize_layers = nn.ModuleList([ 62 | nn.ConvTranspose2d( 63 | in_channels=out_channels[0], 64 | out_channels=out_channels[0], 65 | kernel_size=4, 66 | stride=4, 67 | padding=0), 68 | nn.ConvTranspose2d( 69 | in_channels=out_channels[1], 70 | out_channels=out_channels[1], 71 | kernel_size=2, 72 | stride=2, 73 | padding=0), 74 | nn.Identity(), 75 | nn.Conv2d( 76 | in_channels=out_channels[3], 77 | out_channels=out_channels[3], 78 | kernel_size=3, 79 | stride=2, 80 | padding=1) 81 | ]) 82 | 83 | if use_clstoken: 84 | self.readout_projects = nn.ModuleList() 85 | for _ in range(len(self.projects)): 86 | self.readout_projects.append( 87 | nn.Sequential( 88 | nn.Linear(2 * in_channels, in_channels), 89 | nn.GELU())) 90 | 91 | self.scratch = _make_scratch( 92 | out_channels, 93 | features, 94 | groups=1, 95 | expand=False, 96 | ) 97 | 98 | self.scratch.stem_transpose = None 99 | 100 | self.scratch.refinenet1 = _make_fusion_block(features, use_bn) 101 | self.scratch.refinenet2 = _make_fusion_block(features, use_bn) 102 | self.scratch.refinenet3 = _make_fusion_block(features, use_bn) 103 | self.scratch.refinenet4 = _make_fusion_block(features, use_bn) 104 | 105 | head_features_1 = features 106 | head_features_2 = 32 107 | 108 | self.scratch.output_conv1 = nn.Conv2d(head_features_1, head_features_1 // 2, kernel_size=3, stride=1, padding=1) 109 | self.scratch.output_conv2 = nn.Sequential( 110 | nn.Conv2d(head_features_1 // 2, head_features_2, kernel_size=3, stride=1, padding=1), 111 | nn.ReLU(True), 112 | nn.Conv2d(head_features_2, 1, kernel_size=1, stride=1, padding=0), 113 | nn.Sigmoid() 114 | ) 115 | 116 | def forward(self, out_features, patch_h, patch_w): 117 | out = [] 118 | for i, x in enumerate(out_features): 119 | x = x[0] 120 | out.append(self.resize_layers[i](self.projects[i](x.permute(0, 2, 1).reshape((x.shape[0], x.shape[-1], patch_h, patch_w))))) 121 | 122 | layer_1, layer_2, layer_3, layer_4 = out 123 | 124 | layer_1_rn = self.scratch.layer1_rn(layer_1) 125 | layer_2_rn = self.scratch.layer2_rn(layer_2) 126 | layer_3_rn = self.scratch.layer3_rn(layer_3) 127 | 128 | path_4 = self.scratch.refinenet4(self.scratch.layer4_rn(layer_4), size=layer_3_rn.shape[2:]) 129 | path_3 = self.scratch.refinenet3(path_4, layer_3_rn, size=layer_2_rn.shape[2:]) 130 | path_2 = self.scratch.refinenet2(path_3, layer_2_rn, size=layer_1_rn.shape[2:]) 131 | 132 | return self.scratch.output_conv2(F.interpolate(self.scratch.output_conv1(self.scratch.refinenet1(path_2, layer_1_rn)), EXPORT_DEPTH_RESIZE, mode="bilinear", align_corners=True)) 133 | 134 | 135 | class DepthAnythingV2(nn.Module): 136 | def __init__( 137 | self, 138 | encoder='vitl', 139 | features=256, 140 | out_channels=[256, 512, 1024, 1024], 141 | use_bn=False, 142 | use_clstoken=False, 143 | max_depth=20.0 144 | ): 145 | super(DepthAnythingV2, self).__init__() 146 | 147 | self.intermediate_layer_idx = { 148 | 'vits': [2, 5, 8, 11], 149 | 'vitb': [2, 5, 8, 11], 150 | 'vitl': [4, 11, 17, 23], 151 | 'vitg': [9, 19, 29, 39] 152 | } 153 | 154 | self.max_depth = max_depth 155 | 156 | self.encoder = encoder 157 | self.pretrained = DINOv2(model_name=encoder) 158 | 159 | self.depth_head = DPTHead(self.pretrained.embed_dim, features, use_bn, out_channels=out_channels, use_clstoken=use_clstoken) 160 | self.patch_h = EXPORT_DEPTH_RESIZE[0] // 14 161 | self.patch_w = EXPORT_DEPTH_RESIZE[1] // 14 162 | self.mean = torch.tensor([0.485, 0.456, 0.406], dtype=torch.float32).view(1, 3, 1, 1) 163 | self.inv_std = torch.tensor([1.0 / 0.229, 1.0 / 0.224, 1.0 / 0.225], dtype=torch.float32).view(1, 3, 1, 1) 164 | self.mean_inv_std = self.mean * self.inv_std 165 | self.inv_255_std = self.inv_std / 255.0 166 | 167 | def forward(self, x): 168 | x = nn.functional.interpolate( 169 | x.float(), 170 | EXPORT_DEPTH_RESIZE, 171 | mode="bilinear", 172 | align_corners=True, 173 | ) * self.inv_255_std - self.mean_inv_std 174 | 175 | features = self.pretrained.get_intermediate_layers(x, self.intermediate_layer_idx[self.encoder], return_class_token=True) 176 | 177 | depth = self.depth_head(features, self.patch_h, self.patch_w) * self.max_depth 178 | 179 | return depth.squeeze(1) 180 | 181 | @torch.no_grad() 182 | def infer_image(self, raw_image, input_size=518): 183 | image, (h, w) = self.image2tensor(raw_image, input_size) 184 | 185 | depth = self.forward(image) 186 | 187 | depth = F.interpolate(depth[:, None], (h, w), mode="bilinear", align_corners=True)[0, 0] 188 | 189 | return depth.cpu().numpy() 190 | 191 | def image2tensor(self, raw_image, input_size=518): 192 | transform = Compose([ 193 | Resize( 194 | width=input_size, 195 | height=input_size, 196 | resize_target=False, 197 | keep_aspect_ratio=True, 198 | ensure_multiple_of=14, 199 | resize_method='lower_bound', 200 | image_interpolation_method=cv2.INTER_CUBIC, 201 | ), 202 | NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), 203 | PrepareForNet(), 204 | ]) 205 | 206 | h, w = raw_image.shape[:2] 207 | 208 | image = cv2.cvtColor(raw_image, cv2.COLOR_BGR2RGB) / 255.0 209 | 210 | image = transform({'image': image})['image'] 211 | image = torch.from_numpy(image).unsqueeze(0) 212 | 213 | DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu' 214 | image = image.to(DEVICE) 215 | 216 | return image, (h, w) 217 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/modeling_modified/mlp.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # All rights reserved. 3 | # 4 | # This source code is licensed under the license found in the 5 | # LICENSE file in the root directory of this source tree. 6 | 7 | # References: 8 | # https://github.com/facebookresearch/dino/blob/master/vision_transformer.py 9 | # https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/mlp.py 10 | 11 | 12 | from typing import Callable, Optional 13 | 14 | from torch import Tensor, nn 15 | 16 | 17 | class Mlp(nn.Module): 18 | def __init__( 19 | self, 20 | in_features: int, 21 | hidden_features: Optional[int] = None, 22 | out_features: Optional[int] = None, 23 | act_layer: Callable[..., nn.Module] = nn.GELU, 24 | drop: float = 0.0, 25 | bias: bool = True, 26 | ) -> None: 27 | super().__init__() 28 | out_features = out_features or in_features 29 | hidden_features = hidden_features or in_features 30 | self.fc1 = nn.Linear(in_features, hidden_features, bias=bias) 31 | self.act = act_layer() 32 | self.fc2 = nn.Linear(hidden_features, out_features, bias=bias) 33 | self.drop = nn.Dropout(drop) 34 | 35 | def forward(self, x: Tensor) -> Tensor: 36 | return self.fc2(self.act(self.fc1(x))) 37 | -------------------------------------------------------------------------------- /Export_ONNX/Depth_Anything_Metric_V2/modeling_modified/patch_embed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. 2 | # All rights reserved. 3 | # 4 | # This source code is licensed under the license found in the 5 | # LICENSE file in the root directory of this source tree. 6 | 7 | # References: 8 | # https://github.com/facebookresearch/dino/blob/master/vision_transformer.py 9 | # https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/patch_embed.py 10 | 11 | from typing import Callable, Optional, Tuple, Union 12 | 13 | from torch import Tensor 14 | import torch.nn as nn 15 | 16 | 17 | def make_2tuple(x): 18 | if isinstance(x, tuple): 19 | assert len(x) == 2 20 | return x 21 | 22 | assert isinstance(x, int) 23 | return (x, x) 24 | 25 | 26 | class PatchEmbed(nn.Module): 27 | """ 28 | 2D image to patch embedding: (B,C,H,W) -> (B,N,D) 29 | 30 | Args: 31 | img_size: Image size. 32 | patch_size: Patch token size. 33 | in_chans: Number of input image channels. 34 | embed_dim: Number of linear projection output channels. 35 | norm_layer: Normalization layer. 36 | """ 37 | 38 | def __init__( 39 | self, 40 | img_size: Union[int, Tuple[int, int]] = 224, 41 | patch_size: Union[int, Tuple[int, int]] = 16, 42 | in_chans: int = 3, 43 | embed_dim: int = 768, 44 | norm_layer: Optional[Callable] = None, 45 | flatten_embedding: bool = True, 46 | ) -> None: 47 | super().__init__() 48 | 49 | image_HW = make_2tuple(img_size) 50 | patch_HW = make_2tuple(patch_size) 51 | patch_grid_size = ( 52 | image_HW[0] // patch_HW[0], 53 | image_HW[1] // patch_HW[1], 54 | ) 55 | 56 | self.img_size = image_HW 57 | self.patch_size = patch_HW 58 | self.patches_resolution = patch_grid_size 59 | self.num_patches = patch_grid_size[0] * patch_grid_size[1] 60 | 61 | self.in_chans = in_chans 62 | self.embed_dim = embed_dim 63 | 64 | self.flatten_embedding = flatten_embedding 65 | 66 | self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_HW, stride=patch_HW) 67 | self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity() 68 | 69 | def forward(self, x: Tensor) -> Tensor: 70 | return self.norm(self.proj(x).flatten(2).transpose(1, 2)) 71 | 72 | def flops(self) -> float: 73 | Ho, Wo = self.patches_resolution 74 | flops = Ho * Wo * self.embed_dim * self.in_chans * (self.patch_size[0] * self.patch_size[1]) 75 | if self.norm is not None: 76 | flops += Ho * Wo * self.embed_dim 77 | return flops 78 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/YOLO_NAS_Export.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import shutil 3 | import site 4 | from yolo_config import EXPORT_YOLO_INPUT_SIZE 5 | 6 | output_path = "./yolo_nas.onnx" 7 | 8 | # Replace the original source code file. 9 | python_package_path = site.getsitepackages()[-1] 10 | shutil.copyfile("./modeling_modified/qarepvgg_block.py", 11 | python_package_path + "/super_gradients/modules/qarepvgg_block.py") 12 | shutil.copyfile("./modeling_modified/yolo_stages.py", 13 | python_package_path + "/super_gradients/training/models/detection_models/yolo_nas/yolo_stages.py") 14 | shutil.copyfile("./modeling_modified/customizable_detector.py", 15 | python_package_path + "/super_gradients/training/models/detection_models/customizable_detector.py") 16 | shutil.copyfile("./modeling_modified/dfl_heads.py", 17 | python_package_path + "/super_gradients/training/models/detection_models/yolo_nas/dfl_heads.py") 18 | shutil.copyfile("./modeling_modified/bbox_utils.py", 19 | python_package_path + "/super_gradients/training/utils/bbox_utils.py") 20 | shutil.copyfile("./modeling_modified/panneck.py", 21 | python_package_path + "/super_gradients/training/models/detection_models/yolo_nas/panneck.py") 22 | shutil.copyfile("./yolo_config.py", "./modeling_modified/yolo_config.py") 23 | 24 | from super_gradients.training import models 25 | from super_gradients.common.object_names import Models 26 | 27 | # Load a model 28 | model = models.get(Models.YOLO_NAS_M, pretrained_weights="coco").eval() 29 | 30 | # Export the model 31 | dummy_input = torch.ones(EXPORT_YOLO_INPUT_SIZE, dtype=torch.uint8) 32 | print("Export Start") 33 | with torch.inference_mode(): 34 | torch.onnx.export(model, 35 | (dummy_input,), 36 | output_path, 37 | input_names=['images'], 38 | output_names=['output'], 39 | opset_version=17, 40 | do_constant_folding=True) 41 | print("Export Done!") 42 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/modeling_modified/bbox_utils.py: -------------------------------------------------------------------------------- 1 | from typing import Optional 2 | 3 | import torch 4 | from torch import Tensor 5 | 6 | __all__ = ["batch_distance2bbox"] 7 | 8 | 9 | def batch_distance2bbox(points: Tensor, distance: Tensor, max_shapes: Optional[Tensor] = None) -> Tensor: 10 | """Decode distance prediction to bounding box for batch. 11 | 12 | :param points: [B, ..., 2], "xy" format 13 | :param distance: [B, ..., 4], "ltrb" format 14 | :param max_shapes: [B, 2], "h,w" format, Shape of the image. 15 | :return: Tensor: Decoded bboxes, "x1y1x2y2" format. 16 | """ 17 | lt, rb = torch.split(distance, 2, dim=-1) 18 | return torch.cat([points.unsqueeze(0) + 0.5 * (rb-lt), rb+lt], dim=-1) 19 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/modeling_modified/dfl_heads.py: -------------------------------------------------------------------------------- 1 | import math 2 | from typing import Tuple, Union, List, Callable, Optional 3 | 4 | import torch 5 | from omegaconf import DictConfig 6 | from torch import nn, Tensor 7 | 8 | import super_gradients.common.factories.detection_modules_factory as det_factory 9 | from super_gradients.common.registry import register_detection_module 10 | from super_gradients.modules import ConvBNReLU 11 | from super_gradients.modules.base_modules import BaseDetectionModule 12 | from super_gradients.module_interfaces import SupportsReplaceNumClasses 13 | from super_gradients.modules.utils import width_multiplier 14 | from super_gradients.training.models.detection_models.pp_yolo_e.pp_yolo_head import generate_anchors_for_grid_cell 15 | from super_gradients.training.utils import HpmStruct, torch_version_is_greater_or_equal 16 | from super_gradients.training.utils.bbox_utils import batch_distance2bbox 17 | from super_gradients.training.utils.utils import infer_model_dtype, infer_model_device 18 | 19 | 20 | @register_detection_module() 21 | class YoloNASDFLHead(BaseDetectionModule, SupportsReplaceNumClasses): 22 | def __init__( 23 | self, 24 | in_channels: int, 25 | inter_channels: int, 26 | width_mult: float, 27 | first_conv_group_size: int, 28 | num_classes: int, 29 | stride: int, 30 | reg_max: int, 31 | cls_dropout_rate: float = 0.0, 32 | reg_dropout_rate: float = 0.0, 33 | ): 34 | """ 35 | Initialize the YoloNASDFLHead 36 | :param in_channels: Input channels 37 | :param inter_channels: Intermediate number of channels 38 | :param width_mult: Width multiplier 39 | :param first_conv_group_size: Group size 40 | :param num_classes: Number of detection classes 41 | :param stride: Output stride for this head 42 | :param reg_max: Number of bins in the regression head 43 | :param cls_dropout_rate: Dropout rate for the classification head 44 | :param reg_dropout_rate: Dropout rate for the regression head 45 | """ 46 | super().__init__(in_channels) 47 | 48 | inter_channels = width_multiplier(inter_channels, width_mult, 8) 49 | if first_conv_group_size == 0: 50 | groups = 0 51 | elif first_conv_group_size == -1: 52 | groups = 1 53 | else: 54 | groups = inter_channels // first_conv_group_size 55 | 56 | self.num_classes = num_classes 57 | self.stem = ConvBNReLU(in_channels, inter_channels, kernel_size=1, stride=1, padding=0, bias=False) 58 | 59 | first_cls_conv = [ConvBNReLU(inter_channels, inter_channels, kernel_size=3, stride=1, padding=1, groups=groups, bias=False)] if groups else [] 60 | self.cls_convs = nn.Sequential(*first_cls_conv, ConvBNReLU(inter_channels, inter_channels, kernel_size=3, stride=1, padding=1, bias=False)) 61 | 62 | first_reg_conv = [ConvBNReLU(inter_channels, inter_channels, kernel_size=3, stride=1, padding=1, groups=groups, bias=False)] if groups else [] 63 | self.reg_convs = nn.Sequential(*first_reg_conv, ConvBNReLU(inter_channels, inter_channels, kernel_size=3, stride=1, padding=1, bias=False)) 64 | 65 | self.cls_pred = nn.Conv2d(inter_channels, self.num_classes, 1, 1, 0) 66 | self.reg_pred = nn.Conv2d(inter_channels, 4 * (reg_max + 1), 1, 1, 0) 67 | 68 | self.cls_dropout_rate = nn.Dropout2d(cls_dropout_rate) if cls_dropout_rate > 0 else nn.Identity() 69 | self.reg_dropout_rate = nn.Dropout2d(reg_dropout_rate) if reg_dropout_rate > 0 else nn.Identity() 70 | 71 | self.grid = torch.zeros(1) 72 | self.stride = stride 73 | 74 | self.prior_prob = 1e-2 75 | self._initialize_biases() 76 | 77 | def replace_num_classes(self, num_classes: int, compute_new_weights_fn: Callable[[nn.Module, int], nn.Module]): 78 | self.cls_pred = compute_new_weights_fn(self.cls_pred, num_classes) 79 | self.num_classes = num_classes 80 | 81 | @property 82 | def out_channels(self): 83 | return None 84 | 85 | def forward(self, x): 86 | x = self.stem(x) 87 | 88 | cls_feat = self.cls_convs(x) 89 | cls_feat = self.cls_dropout_rate(cls_feat) 90 | cls_output = self.cls_pred(cls_feat) 91 | 92 | reg_feat = self.reg_convs(x) 93 | reg_feat = self.reg_dropout_rate(reg_feat) 94 | reg_output = self.reg_pred(reg_feat) 95 | 96 | return reg_output, cls_output 97 | 98 | def _initialize_biases(self): 99 | prior_bias = -math.log((1 - self.prior_prob) / self.prior_prob) 100 | torch.nn.init.constant_(self.cls_pred.bias, prior_bias) 101 | 102 | @staticmethod 103 | def _make_grid(nx=20, ny=20): 104 | if torch_version_is_greater_or_equal(1, 10): 105 | # https://github.com/pytorch/pytorch/issues/50276 106 | yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)], indexing="ij") 107 | else: 108 | yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) 109 | return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() 110 | 111 | 112 | @register_detection_module() 113 | class NDFLHeads(BaseDetectionModule, SupportsReplaceNumClasses): 114 | def __init__( 115 | self, 116 | num_classes: int, 117 | in_channels: Tuple[int, int, int], 118 | heads_list: Union[str, HpmStruct, DictConfig], 119 | grid_cell_scale: float = 5.0, 120 | grid_cell_offset: float = 0.5, 121 | reg_max: int = 16, 122 | eval_size: Optional[Tuple[int, int]] = None, 123 | width_mult: float = 1.0, 124 | ): 125 | """ 126 | Initializes the NDFLHeads module. 127 | 128 | :param num_classes: Number of detection classes 129 | :param in_channels: Number of channels for each feature map (See width_mult) 130 | :param grid_cell_scale: 131 | :param grid_cell_offset: 132 | :param reg_max: Number of bins in the regression head 133 | :param eval_size: (rows, cols) Size of the image for evaluation. Setting this value can be beneficial for inference speed, 134 | since anchors will not be regenerated for each forward call. 135 | :param width_mult: A scaling factor applied to in_channels. 136 | """ 137 | super(NDFLHeads, self).__init__(in_channels) 138 | in_channels = [max(round(c * width_mult), 1) for c in in_channels] 139 | 140 | self.in_channels = tuple(in_channels) 141 | self.num_classes = num_classes 142 | self.grid_cell_scale = grid_cell_scale 143 | self.grid_cell_offset = grid_cell_offset 144 | self.reg_max = reg_max 145 | self.eval_size = eval_size 146 | 147 | # Do not apply quantization to this tensor 148 | proj = torch.linspace(0, self.reg_max, self.reg_max + 1).reshape([1, self.reg_max + 1, 1, 1]) 149 | self.register_buffer("proj_conv", proj, persistent=False) 150 | 151 | self._init_weights() 152 | 153 | factory = det_factory.DetectionModulesFactory() 154 | heads_list = self._pass_args(heads_list, factory, num_classes, reg_max) 155 | 156 | self.num_heads = len(heads_list) 157 | fpn_strides: List[int] = [] 158 | for i in range(self.num_heads): 159 | new_head = factory.get(factory.insert_module_param(heads_list[i], "in_channels", in_channels[i])) 160 | fpn_strides.append(new_head.stride) 161 | setattr(self, f"head{i + 1}", new_head) 162 | 163 | self.fpn_strides = tuple(fpn_strides) 164 | 165 | def replace_num_classes(self, num_classes: int, compute_new_weights_fn: Callable[[nn.Module, int], nn.Module]): 166 | for i in range(self.num_heads): 167 | head = getattr(self, f"head{i + 1}") 168 | head.replace_num_classes(num_classes, compute_new_weights_fn) 169 | 170 | self.num_classes = num_classes 171 | 172 | @staticmethod 173 | def _pass_args(heads_list, factory, num_classes, reg_max): 174 | for i in range(len(heads_list)): 175 | heads_list[i] = factory.insert_module_param(heads_list[i], "num_classes", num_classes) 176 | heads_list[i] = factory.insert_module_param(heads_list[i], "reg_max", reg_max) 177 | return heads_list 178 | 179 | @torch.jit.ignore 180 | def cache_anchors(self, input_size: Tuple[int, int]): 181 | self.eval_size = input_size 182 | device = infer_model_device(self) 183 | dtype = infer_model_dtype(self) 184 | 185 | anchor_points, stride_tensor = self._generate_anchors(dtype=dtype, device=device) 186 | self.register_buffer("anchor_points", anchor_points, persistent=False) 187 | self.register_buffer("stride_tensor", stride_tensor, persistent=False) 188 | 189 | @torch.jit.ignore 190 | def _init_weights(self): 191 | if self.eval_size: 192 | device = infer_model_device(self) 193 | dtype = infer_model_dtype(self) 194 | 195 | anchor_points, stride_tensor = self._generate_anchors(dtype=dtype, device=device) 196 | self.anchor_points = anchor_points 197 | self.stride_tensor = stride_tensor 198 | 199 | def forward(self, feats: Tuple[Tensor, ...]) -> Tuple[Tuple[Tensor, Tensor], Tuple[Tensor, ...]]: 200 | cls_score_list, reg_distri_list, reg_dist_reduced_list = [], [], [] 201 | 202 | for i, feat in enumerate(feats): 203 | _, _, h, w = feat.shape 204 | height_mul_width = h * w 205 | reg_distri, cls_logit = getattr(self, f"head{i + 1}")(feat) 206 | reg_distri_list.append(torch.permute(reg_distri.flatten(2), [0, 2, 1])) 207 | 208 | # cls and reg 209 | cls_score_list.append(cls_logit.reshape([1, self.num_classes, height_mul_width])) 210 | reg_dist_reduced_list.append(torch.nn.functional.conv2d(torch.nn.functional.softmax(torch.permute(reg_distri.reshape([-1, 4, self.reg_max + 1, height_mul_width]), [0, 2, 3, 1]), dim=1), weight=self.proj_conv).squeeze(1)) 211 | 212 | anchor_points_inference, stride_tensor = self._generate_anchors(feats) 213 | part_A = batch_distance2bbox(anchor_points_inference, torch.cat(reg_dist_reduced_list, dim=1)) * stride_tensor 214 | part_B = torch.permute(torch.cat(cls_score_list, dim=2), [0, 2, 1]).sigmoid() 215 | max_scores, max_indices = torch.max(part_B, dim=2, keepdim=True) 216 | return torch.cat((part_A, max_scores, max_indices), dim=2) 217 | 218 | @property 219 | def out_channels(self): 220 | return None 221 | 222 | def _generate_anchors(self, feats=None, dtype=None, device=None): 223 | # just use in eval time 224 | anchor_points = [] 225 | stride_tensor = [] 226 | 227 | dtype = dtype or feats[0].dtype 228 | device = device or feats[0].device 229 | 230 | for i, stride in enumerate(self.fpn_strides): 231 | if feats is not None: 232 | _, _, h, w = feats[i].shape 233 | else: 234 | h = int(self.eval_size[0] / stride) 235 | w = int(self.eval_size[1] / stride) 236 | 237 | # ONNX export does not support arange with float16, so it is created as fp32 and then casted to fp16 238 | # This produce correct fp16 weights in ONNX model when exported 239 | shift_x = torch.arange(end=w, dtype=torch.float32, device=device) + self.grid_cell_offset 240 | shift_y = torch.arange(end=h, dtype=torch.float32, device=device) + self.grid_cell_offset 241 | 242 | if torch_version_is_greater_or_equal(1, 10): 243 | shift_y, shift_x = torch.meshgrid(shift_y, shift_x, indexing="ij") 244 | else: 245 | shift_y, shift_x = torch.meshgrid(shift_y, shift_x) 246 | 247 | anchor_point = torch.stack([shift_x, shift_y], dim=-1).to(dtype=dtype) 248 | anchor_points.append(anchor_point.reshape([-1, 2])) 249 | stride_tensor.append(torch.full([h * w, 1], stride, dtype=dtype, device=device)) 250 | 251 | return torch.cat(anchor_points), torch.cat(stride_tensor) 252 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/modeling_modified/panneck.py: -------------------------------------------------------------------------------- 1 | from typing import Union, List, Tuple 2 | 3 | from omegaconf import DictConfig 4 | from torch import Tensor 5 | 6 | from super_gradients.common.registry import register_detection_module 7 | from super_gradients.modules.detection_modules import BaseDetectionModule 8 | from super_gradients.training.utils.utils import HpmStruct 9 | import super_gradients.common.factories.detection_modules_factory as det_factory 10 | 11 | 12 | @register_detection_module("YoloNASPANNeckWithC2") 13 | class YoloNASPANNeckWithC2(BaseDetectionModule): 14 | """ 15 | A PAN (path aggregation network) neck with 4 stages (2 up-sampling and 2 down-sampling stages) 16 | where the up-sampling stages include a higher resolution skip 17 | Returns outputs of neck stage 2, stage 3, stage 4 18 | """ 19 | 20 | def __init__( 21 | self, 22 | in_channels: List[int], 23 | neck1: Union[str, HpmStruct, DictConfig], 24 | neck2: Union[str, HpmStruct, DictConfig], 25 | neck3: Union[str, HpmStruct, DictConfig], 26 | neck4: Union[str, HpmStruct, DictConfig], 27 | ): 28 | """ 29 | Initialize the PAN neck 30 | 31 | :param in_channels: Input channels of the 4 feature maps from the backbone 32 | :param neck1: First neck stage config 33 | :param neck2: Second neck stage config 34 | :param neck3: Third neck stage config 35 | :param neck4: Fourth neck stage config 36 | """ 37 | super().__init__(in_channels) 38 | c2_out_channels, c3_out_channels, c4_out_channels, c5_out_channels = in_channels 39 | 40 | factory = det_factory.DetectionModulesFactory() 41 | self.neck1 = factory.get(factory.insert_module_param(neck1, "in_channels", [c5_out_channels, c4_out_channels, c3_out_channels])) 42 | self.neck2 = factory.get(factory.insert_module_param(neck2, "in_channels", [self.neck1.out_channels[1], c3_out_channels, c2_out_channels])) 43 | self.neck3 = factory.get(factory.insert_module_param(neck3, "in_channels", [self.neck2.out_channels[1], self.neck2.out_channels[0]])) 44 | self.neck4 = factory.get(factory.insert_module_param(neck4, "in_channels", [self.neck3.out_channels, self.neck1.out_channels[0]])) 45 | 46 | self._out_channels = [ 47 | self.neck2.out_channels[1], 48 | self.neck3.out_channels, 49 | self.neck4.out_channels, 50 | ] 51 | 52 | @property 53 | def out_channels(self): 54 | return self._out_channels 55 | 56 | def forward(self, inputs: Tuple[Tensor, Tensor, Tensor, Tensor]) -> Tuple[Tensor, Tensor, Tensor]: 57 | c2, c3, c4, c5 = inputs 58 | 59 | x_n1_inter, x = self.neck1([c5, c4, c3]) 60 | x_n2_inter, p3 = self.neck2([x, c3, c2]) 61 | p4 = self.neck3([p3, x_n2_inter]) 62 | 63 | return p3, p4, self.neck4([p4, x_n1_inter]) 64 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/modeling_modified/yolo_config.py: -------------------------------------------------------------------------------- 1 | EXPORT_YOLO_INPUT_SIZE = [1, 3, 720, 1280] # Input image shape. Batch size must be 1. Should be a multiple of GPU group (e.g., 16) for optimal efficiency. 2 | EXPORT_YOLO_RESIZE = (288, 512) # Resize input to this shape to accelerate YOLO inference. Maintain the same W/H ratio as the input images. 3 | 4 | 5 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_NAS/yolo_config.py: -------------------------------------------------------------------------------- 1 | EXPORT_YOLO_INPUT_SIZE = [1, 3, 720, 1280] # Input image shape. Batch size must be 1. Should be a multiple of GPU group (e.g., 16) for optimal efficiency. 2 | EXPORT_YOLO_RESIZE = (288, 512) # Resize input to this shape to accelerate YOLO inference. Maintain the same W/H ratio as the input images. 3 | 4 | 5 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_v8_v9_v10_v11_v12/YOLO_Export.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | import torch 3 | import site 4 | 5 | # Import the yolo_config 6 | python_package_path = site.getsitepackages()[-1] 7 | shutil.copyfile("./yolo_config.py", python_package_path + "/ultralytics/engine/yolo_config.py") 8 | shutil.copyfile("./yolo_config.py", python_package_path + "/ultralytics/nn/yolo_config.py") 9 | shutil.copyfile("./yolo_config.py", python_package_path + "/ultralytics/nn/modules/yolo_config.py") 10 | 11 | # Replace the original source code file. 12 | shutil.copyfile("./modeling_modified/exporter.py", python_package_path + "/ultralytics/engine/exporter.py") 13 | shutil.copyfile("./modeling_modified/tasks.py", python_package_path + "/ultralytics/nn/tasks.py") 14 | shutil.copyfile("./modeling_modified/head.py", python_package_path + "/ultralytics/nn/modules/head.py") 15 | 16 | from ultralytics import YOLO 17 | 18 | # Load a model, The version number must match EXPORT_YOLO_VERSION. 19 | model = YOLO("yolo12n.pt") # Load an official model, 20 | # model = YOLO("/Users/Downloads/custom_yolo12n.pt") # Or specify your own model path. Currently, do not support yolo12-turbo series. 21 | 22 | # Export the model 23 | with torch.inference_mode(): 24 | model.export(format='onnx') # The exported model will save at the current folder. 25 | -------------------------------------------------------------------------------- /Export_ONNX/YOLO_v8_v9_v10_v11_v12/yolo_config.py: -------------------------------------------------------------------------------- 1 | EXPORT_YOLO_VERSION = 12 # Specify the YOLO version to export. The version must match the loaded model. 2 | EXPORT_YOLO_INPUT_SIZE = [1, 3, 720, 1280] # Input image shape. Batch size must be 1. Should be a multiple of GPU group (e.g., 16) for optimal efficiency. 3 | EXPORT_YOLO_RESIZE = (288, 512) # Resize input to this shape to accelerate YOLO inference. Maintain the same W/H ratio as the input images. Should be a multiple of 32. 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # YOLO-Depth-Drivable Estimation for Android 4 | 5 | ## Overview 6 | 7 | This project enables running YOLO series, monocular depth estimation on Android devices. It is optimized for peak performance using models converted from HuggingFace, ModelScope, and GitHub. 8 | 9 | ## Key Features 10 | 11 | 1. **Demo Models Available**: 12 | - Demo model, yolov12n, now is place in the assets folder. 13 | - Remember unzip `*.zip` files in `libs/arm64-v8a` to get the `*.so`. 14 | 15 | 2. **Supported Models**: 16 | - **YOLO**: Versions 8, 9, 10, 11, 12 and NAS series. 17 | - **Depth Estimation**: Depth Anything V2-Metric-Small. 18 | 19 | 3. **Image Input**: 20 | - Models accept images with resolution h720*w1280. Hold your phone horizontally. 21 | 22 | 4. **ONNX Runtime Compatibility**: 23 | - Models are exported without dynamic axes for better Android compatibility, which may affect x86_64 performance. 24 | 25 | 5. **GPU Utilization**: 26 | - Utilizes OpenGL ES 3.x for efficient camera frame processing and GPU compute shaders for YUV to RGB conversion. 27 | 28 | 6. **Asynchronous Inference**: 29 | - Processes previous camera frame data for inference, improving efficiency. 30 | 31 | 7. **YOLO Bounding Box Rendering**: 32 | - Efficiently rendered using OpenGL ES. 33 | 34 | 8. **Performance Notes**: 35 | - Running both YOLO and depth estimation reduces FPS by about 30%. 36 | 37 | 9. **Export and Configuration**: 38 | - Update model export methods and ensure configuration files match model weights. Use `UInt8` for quantization to avoid ONNX Runtime errors. 39 | 40 | 10. **Qualcomm NPU Support**: 41 | - Only YOLO v9, v12 & NAS series support Qualcomm NPU. NPU libraries must be obtained independently. 42 | 43 | 11. **Quantization Methods**: 44 | - Avoid q4(uint4) due to poor ONNX Runtime performance. 45 | 46 | 12. **Image Preprocessing**: 47 | - Updated on 2024/10/12 for optimized performance. Set `EXPORT_YOLO_INPUT_SIZE` appropriately for high-resolution screens. 48 | 49 | 13. **Custom Parameters**: 50 | - Please adjust the key variables to match the model parameters and `yolo_config.py`. 51 | - `GLRender.java: Around Line 48-53` 52 | - `project.h: Line 11, 12, 13, 31, 32, 34, 35, 37` 53 | 54 | ## Project Resources 55 | 56 | - [More about the project](https://github.com/DakeQQ?tab=repositories) 57 | 58 | ## 演示结果 Demo Results 59 | 60 | - **YOLOv8-n & Depth Anything-Small** 61 | ![Demo Animation](https://github.com/DakeQQ/YOLO-Depth-Estimation-for-Android/blob/main/yolo_depth.gif?raw=true) 62 | 63 | - **YOLOv8-s & Depth Anything-Small** 64 | ![Demo Animation](https://github.com/DakeQQ/YOLO-Depth-Estimation-for-Android/blob/main/yolo_depth2.gif?raw=true) 65 | 66 | --- 67 | 68 | # 安卓本地运行YOLO+深度(距离)+可行驶区域估计 69 | 70 | ## 概述 71 | 72 | 该项目支持在Android设备上运行YOLO系列、单目深度估计。通过从HuggingFace、ModelScope和GitHub转换的模型进行优化以实现最佳性能。 73 | 74 | ## 主要功能 75 | 76 | 1. **演示模型**: 77 | - 演示模型, yolov12n, 已放置在`assets`文件夾中。 78 | - 記得解压`libs/arm64-v8a`中的`.zip`文件來得到`*.so`。 79 | 80 | 2. **支持的模型**: 81 | - **YOLO**: v8, v9, v10, v11, 12, NAS系列。 82 | - **深度估计**: Depth Anything V2-Metric-Small。 83 | 84 | 3. **图像输入**: 85 | - 模型接收分辨率为h720*w1280的图像,需横置手机。 86 | 87 | 4. **ONNX Runtime兼容性**: 88 | - 为了更好地适配Android,导出时未使用dynamic-axes,这可能影响x86_64的性能。 89 | 90 | 5. **GPU利用**: 91 | - 使用OpenGL ES 3.x高效处理相机帧并通过GPU计算着色器进行YUV到RGB的转换。 92 | 93 | 6. **异步推理**: 94 | - 使用前一帧相机数据进行推理,提高效率。 95 | 96 | 7. **YOLO框渲染**: 97 | - 通过OpenGL ES高效渲染YOLO框。 98 | 99 | 8. **性能提示**: 100 | - 同时运行YOLO和深度估计会使FPS降低约30%。 101 | 102 | 9. **导出与配置**: 103 | - 更新模型导出方法并确保配置文件与模型权重匹配。使用`UInt8`进行量化以避免ONNX Runtime错误。 104 | 105 | 10. **高通NPU支持**: 106 | - 只有YOLO v9, v12 & NAS系列支持高通NPU。NPU库需自行获取。 107 | 108 | 11. **量化方法**: 109 | - 由于ONNX Runtime表现不佳,不建议使用q4(uint4)。 110 | 111 | 12. **图像预处理**: 112 | - 2024/10/12更新以优化性能。为高分辨率屏幕设置适当的`EXPORT_YOLO_INPUT_SIZE`。 113 | - 114 | 13. **自定义参数**: 115 | - 请调整关键变量以匹配模型参数和 `yolo_config.py`. 116 | - `GLRender.java: Around Line 48-53` 117 | - `project.h: Line 11, 12, 13, 31, 32, 34, 35, 37` 118 | 119 | ## 项目资源 120 | 121 | - [查看更多项目](https://github.com/DakeQQ?tab=repositories) 122 | 123 | ## GPU Image Preprocess - 图像预处理性能 124 | 125 | | OS | Device | Backend | Pixels: h720*w1280
Time Cost: ms | Pixels: h1088*w1920
Time Cost: ms | 126 | |:-------:|:-------:|:-------:|:-------:|:-------:| 127 | | Android 13 | Nubia Z50 | 8_Gen2-GPU
Adreno-740 | 3.5 | 6.5 | 128 | | Harmony 4 | P40 | Kirin_990_5G-GPU
Mali-G76 MP16 | 9 | 17 | 129 | 130 | ## YOLO - 性能 Performance 131 | 132 | | OS | Device | Backend | Model | FPS
Camera: h720*w1280 | 133 | |:-------:|:-------:|:-------:|:-------:|:-------:| 134 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v12-n
q8f32 | 25 | 135 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | v12-n
f16 | 120+ | 136 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v11-x
q8f32 | 3.5 | 137 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v11-l
q8f32 | 6 | 138 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v11-m
q8f32 | 8 | 139 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v11-s
q8f32 | 18 | 140 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v11-n
q8f32 | 36 | 141 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v10-m
q8f32 | 9.5 | 142 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v10-s
q8f32 | 17.5 | 143 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v10-n
q8f32 | 35 | 144 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v9-C
q8f32 | 7 | 145 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | v9-C
f16 | 50+ | 146 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | v9-M
f16 | 60+ | 147 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | v9-S
f16 | 90+ | 148 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | v9-T
f16 | 110+ | 149 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v8-s
q8f32 | 21 | 150 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | v8-n
q8f32 | 43 | 151 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | NAS-m
q8f32 | 9 | 152 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | NAS-s
q8f32 | 19 | 153 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | NAS-m
f16 | 75+ | 154 | | Android 13 | Nubia Z50 | 8_Gen2-NPU
(HTPv73) | NAS-s
f16 | 95+ | 155 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v12-n
q8f32 | 16.5 | 156 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v12-n
f16 | 18 | 157 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v11-x
q8f32 | 2.5 | 158 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v11-l
q8f32 | 3.5 | 159 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v11-m
q8f32 | 5 | 160 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v11-s
q8f32 | 11.5 | 161 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v11-n
q8f32 | 23 | 162 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v10-m
q8f32 | 5 | 163 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v10-s
q8f32 | 9.5 | 164 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v10-n
q8f32 | 18.5 | 165 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v9-C
q8f32 | 3.5 | 166 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v8-s
q8f32 | 10.5 | 167 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | v8-n
q8f32 | 22 | 168 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | NAS-m
q8f32 | 5 | 169 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | NAS-s
q8f32 | 9.5 | 170 | 171 | ## Depth - 性能 Performance 172 | 173 | | OS | Device | Backend | Model | FPS
Camera: h720*w1280 | 174 | |:-------:|:-------:|:-------:|:-------:|:-------:| 175 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | Depth Anything-Small
q8f32
(previous version) | 22 | 176 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | Depth Anything-Small
q8f32
(previous version) | 11 | 177 | 178 | ## YOLO+Depth - 性能 Performance 179 | 180 | | OS | Device | Backend | Model | YOLO FPS
Camera: h720*w1280 | Depth FPS
Camera: h720*w1280 | 181 | |:-------:|:-------:|:-------:|:-------:|:-------:|:-------:| 182 | | Android 13 | Nubia Z50 | 8_Gen2-CPU
(X3+A715) | YOLOv8-n &
Depth Anything-Small
q8f32
(previous version) | 28 | 16.7 | 183 | | Harmony 4 | P40 | Kirin_990_5G-CPU
(2*A76) | YOLOv8-n &
Depth Anything-Small
q8f32
(previous version) | 16 | 7.7 | 184 | 185 | --- 186 | -------------------------------------------------------------------------------- /YOLO_Depth/app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | } 4 | 5 | android { 6 | namespace 'com.example.myapplication' 7 | compileSdk 34 8 | 9 | defaultConfig { 10 | applicationId "com.example.myapplication" 11 | minSdk 30 12 | targetSdk 35 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | externalNativeBuild { 18 | cmake { 19 | cppFlags '-std=c++17' 20 | abiFilters 'arm64-v8a' 21 | arguments '-DANDROID_STL=c++_shared' 22 | } 23 | ndk{ 24 | //noinspection ChromeOsAbiSupport 25 | abiFilters 'arm64-v8a' 26 | } 27 | } 28 | } 29 | buildTypes { 30 | release { 31 | minifyEnabled true 32 | shrinkResources true 33 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | compileOptions { 37 | sourceCompatibility JavaVersion.VERSION_21 38 | targetCompatibility JavaVersion.VERSION_21 39 | } 40 | externalNativeBuild { 41 | cmake { 42 | path file('src/main/cpp/CMakeLists.txt') 43 | version '3.31.5' 44 | } 45 | } 46 | sourceSets { 47 | main { 48 | jniLibs.srcDirs = ['libs'] 49 | } 50 | } 51 | buildFeatures { 52 | viewBinding true 53 | } 54 | ndkVersion '29.0.13113456 rc1' 55 | } 56 | 57 | dependencies { 58 | implementation 'androidx.appcompat:appcompat:1.7.0' 59 | implementation 'com.google.android.material:material:1.12.0' 60 | implementation 'androidx.constraintlayout:constraintlayout:2.2.0' 61 | testImplementation 'junit:junit:4.13.2' 62 | androidTestImplementation 'androidx.test.ext:junit:1.2.1' 63 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1' 64 | } 65 | -------------------------------------------------------------------------------- /YOLO_Depth/app/libs/arm64-v8a/ort_1.21.0_qnn_2.32.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/libs/arm64-v8a/ort_1.21.0_qnn_2.32.zip -------------------------------------------------------------------------------- /YOLO_Depth/app/local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Mon May 06 14:35:59 CST 2024 8 | sdk.dir=/home/iamj/Android/Sdk 9 | -------------------------------------------------------------------------------- /YOLO_Depth/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 24 | 27 | 28 | 29 | 30 | 31 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/Depth_Anything_Metric_V2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/assets/Depth_Anything_Metric_V2.zip -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/Model_YOLO_v12_n_f16.onnx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/assets/Model_YOLO_v12_n_f16.onnx -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/Model_YOLO_v12_n_f32.ort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/assets/Model_YOLO_v12_n_f32.ort -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/camera_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | #extension GL_OES_EGL_image_external_essl3 : require 3 | precision mediump float; 4 | 5 | in vec2 texCoord;//纹理坐标,图片当中的坐标点 6 | out vec4 outColor; 7 | 8 | uniform samplerExternalOES camera_texture;//图片,采样器 9 | 10 | void main(){ 11 | outColor = texture(camera_texture, texCoord); 12 | } 13 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/camera_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | precision mediump float; 3 | //把顶点坐标给这个变量, 确定要画画的形状 4 | layout (location = 0) in vec4 aPosVertex; 5 | 6 | //接收纹理坐标,接收采样器采样图片的坐标 7 | layout (location = 1) in vec4 aTexVertex; 8 | 9 | //传给片元着色器 像素点 10 | out vec2 texCoord; 11 | 12 | uniform mat4 vMatrix; 13 | 14 | void main() 15 | { 16 | //内置变量 gl_Position ,我们把顶点数据赋值给这个变量 opengl就知道它要画什么形状了 17 | gl_Position = aPosVertex; 18 | texCoord = (vMatrix * aTexVertex).xy; 19 | //texCoord = aTexVertex.xy; 20 | } 21 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/class.txt: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | potted plant 60 | bed 61 | dining table 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/yolo_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | precision mediump float; 3 | out vec4 fragColor; 4 | uniform vec4 box_color; 5 | 6 | void main() { 7 | fragColor = vec4(box_color); 8 | } 9 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/assets/yolo_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | 3 | precision mediump float; 4 | 5 | in vec4 box_position; 6 | 7 | void main() { 8 | 9 | gl_Position = vec4(box_position); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project("myapplication") 2 | set(ANDROID_ABI "arm64-v8a") 3 | cmake_minimum_required(VERSION 3.22.1) 4 | set(CMAKE_BUILD_TYPE Release) 5 | 6 | include_directories(${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}) 7 | 8 | add_compile_options( 9 | # Optimization Flags: Enable aggressive optimizations for performance 10 | -O3 11 | -fno-math-errno 12 | -ffinite-math-only 13 | -ffast-math 14 | -ffp-model=fast 15 | -ffp-contract=fast 16 | -funsafe-math-optimizations 17 | -fvectorize 18 | -funroll-loops 19 | -finline-functions 20 | -fomit-frame-pointer 21 | -fopenmp-simd 22 | -minline-all-stringops 23 | 24 | # CPU-Specific Optimization: Replace with your chip 25 | -march=armv8.7a+fp16fml+i8mm+dotprod+crypto+simd+tme 26 | -mtune=cortex-a76 27 | -mcpu=cortex-a76+cortex-a55 28 | 29 | # Link-Time Optimization (LTO) & Code Size Reduction: Reduce binary size and improve performance 30 | -flto 31 | -ffunction-sections 32 | -fdata-sections 33 | -falign-functions 34 | 35 | # Exception Handling & Runtime Behavior: Disable RTTI, exceptions, and stack protection for faster execution 36 | -fno-sanitize=address 37 | -fno-sanitize=alignment 38 | -fno-exceptions 39 | -fno-rtti 40 | -fno-stack-protector 41 | -fPIC 42 | 43 | # Visibility and Symbol Management: Hide unnecessary symbols to reduce binary size 44 | -fvisibility=hidden 45 | -fvisibility-inlines-hidden 46 | 47 | # Miscellaneous / Compiler-Specific Tweaks 48 | -pipe 49 | 50 | 51 | ) 52 | 53 | add_library(${CMAKE_PROJECT_NAME} SHARED project.cpp) 54 | 55 | add_library( 56 | onnxruntime 57 | SHARED 58 | IMPORTED) 59 | set_target_properties(onnxruntime PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libonnxruntime.so) 60 | 61 | add_library( 62 | omp 63 | SHARED 64 | IMPORTED) 65 | set_target_properties(omp PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libomp.so) 66 | 67 | add_library( 68 | GLESv3 69 | SHARED 70 | IMPORTED) 71 | set_target_properties(GLESv3 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libGLESv3.so) 72 | 73 | find_library( 74 | log 75 | android 76 | ) 77 | 78 | target_link_libraries( 79 | myapplication 80 | android 81 | onnxruntime 82 | omp 83 | GLESv3 84 | log) 85 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/cpp/project.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "onnxruntime_cxx_api.h" 6 | #include 7 | 8 | const char* computeShaderSource = "#version 320 es\n" 9 | "#extension GL_OES_EGL_image_external_essl3 : require\n" 10 | "precision mediump float;\n" 11 | "layout(local_size_x = 16, local_size_y = 16) in;\n" // gpu_num_group=16, Customize it to fit your device's specifications. 12 | "const int camera_width = 1280;\n" // camera_width 13 | "const int camera_height = 720;\n" // camera_height 14 | "layout(binding = 0) uniform samplerExternalOES yuvTex;\n" 15 | "layout(std430, binding = 1) buffer Output {\n" 16 | " int result[camera_height * camera_width];\n" // pixelCount 17 | "} outputData;\n" 18 | "const vec3 bias = vec3(-0.15, -0.5, -0.2);\n" 19 | "const mat3 YUVtoRGBMatrix = mat3(127.5, 0.0, 1.402 * 127.5, " 20 | " 127.5, -0.344136 * 127.5, -0.714136 * 127.5, " 21 | " 127.5, 1.772 * 127.5, 0.0);\n" 22 | "void main() {\n" 23 | " ivec2 texelPos = ivec2(gl_GlobalInvocationID.xy);\n" 24 | " ivec3 rgb = clamp(ivec3(YUVtoRGBMatrix * (texelFetch(yuvTex, texelPos, 0).rgb + bias)), -128, 127) + 128;\n" 25 | // Use uint8 packing the pixels, it would be 1.6 times faster than using float32 buffer. 26 | " outputData.result[texelPos.y * camera_width + texelPos.x] = (rgb.b << 16) | (rgb.r << 8) | (rgb.g);\n" 27 | "}"; 28 | GLuint pbo_A = 0; 29 | GLuint computeProgram = 0; 30 | GLint yuvTexLoc = 0; 31 | const GLsizei camera_width = 1280; 32 | const GLsizei camera_height = 720; 33 | const GLsizei pixelCount = camera_width * camera_height; 34 | const int output_size_A = 6 * 3024; // [x, y, w, h, max_score, max_indices] * yolo_num_boxes 35 | const int output_size_B = 294 * 518; // depth_pixels 36 | const int pixelCount_rgb = 3 * pixelCount; 37 | const int gpu_num_group = 16; // Customize it to fit your device's specifications. 38 | const GLsizei rgbSize = pixelCount_rgb * sizeof(float); 39 | const GLsizei rgbSize_int = pixelCount * sizeof(int); 40 | const GLsizei rgbSize_i8 = pixelCount_rgb * sizeof(uint8_t); 41 | const GLsizei workGroupCountX = camera_width / gpu_num_group; 42 | const GLsizei workGroupCountY = camera_height / gpu_num_group; 43 | 44 | const OrtApi* ort_runtime_A; 45 | OrtSession* session_model_A; 46 | OrtRunOptions *run_options_A; 47 | std::vector input_names_A; 48 | std::vector output_names_A; 49 | std::vector> input_dims_A; 50 | std::vector> output_dims_A; 51 | std::vector input_types_A; 52 | std::vector output_types_A; 53 | std::vector input_tensors_A; 54 | std::vector output_tensors_A; 55 | const OrtApi* ort_runtime_B; 56 | OrtSession* session_model_B; 57 | OrtRunOptions *run_options_B; 58 | std::vector input_names_B; 59 | std::vector output_names_B; 60 | std::vector> input_dims_B; 61 | std::vector> output_dims_B; 62 | std::vector input_types_B; 63 | std::vector output_types_B; 64 | std::vector input_tensors_B; 65 | std::vector output_tensors_B; 66 | const std::string file_name_A = "Model_YOLO_v12_n_f32.ort"; 67 | const std::string file_name_B = "Depth_Anything_Metric_V2.ort"; 68 | const std::string storage_path = "/storage/emulated/0/Android/data/com.example.myapplication/files/"; 69 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/java/com/example/myapplication/Classifier.java: -------------------------------------------------------------------------------- 1 | package com.example.myapplication; 2 | import android.annotation.SuppressLint; 3 | import android.graphics.RectF; 4 | import androidx.annotation.NonNull; 5 | 6 | 7 | /** 8 | * Generic interface for interacting with different recognition engines. 9 | */ 10 | public interface Classifier { 11 | /** 12 | * An immutable result returned by a Classifier describing what was recognized. 13 | */ 14 | class Recognition { 15 | /** 16 | * A unique identifier for what has been recognized. Specific to the class, not the instance of 17 | * the object. 18 | */ 19 | private final String id; 20 | /** 21 | * Display name for the recognition. 22 | */ 23 | private final String title; 24 | /** 25 | * A sortable score for how good the recognition is relative to others. Higher should be better. 26 | */ 27 | private final Float confidence; 28 | /** 29 | * Optional location within the source image for the location of the recognized object. 30 | */ 31 | private final RectF location; 32 | public Recognition(final String id, final String title, final Float confidence, final RectF location) { 33 | this.id = id; 34 | this.title = title; 35 | this.confidence = confidence; 36 | this.location = location; 37 | } 38 | 39 | public Float getConfidence() { 40 | return confidence; 41 | } 42 | 43 | public RectF getLocation() { 44 | return new RectF(location); 45 | } 46 | 47 | public String getTitle() { 48 | return title; 49 | } 50 | 51 | @NonNull 52 | @SuppressLint("DefaultLocale") 53 | @Override 54 | public String toString() { 55 | String resultString = ""; 56 | if (id != null) { 57 | resultString += "[" + id + "] "; 58 | } 59 | 60 | if (title != null) { 61 | resultString += title + " "; 62 | } 63 | 64 | if (confidence != null) { 65 | resultString += String.format("(%.2f%%) ", confidence * 100.f); 66 | } 67 | 68 | if (location != null) { 69 | resultString += location + " "; 70 | } 71 | 72 | return resultString.trim(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/drawable/psyduck.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/drawable/psyduck.png -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 21 | 22 | 38 | 39 | 55 | 56 | 72 | 73 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_picture_snap.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xhdpi/ic_picture_snap.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | 11 | #008577 12 | #00574B 13 | #D81B60 14 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | YOLO_Depth_Drivable 3 | 4 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /YOLO_Depth/app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /YOLO_Depth/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | plugins { 3 | id 'com.android.application' version '8.5.2' apply false 4 | id 'com.android.library' version '8.5.2' apply false 5 | } -------------------------------------------------------------------------------- /YOLO_Depth/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Enables namespacing of each library's R class so that its R class includes only the 19 | # resources declared in the library itself and none from the library's dependencies, 20 | # thereby reducing the size of the R class for that library 21 | android.nonTransitiveRClass=true 22 | android.bundle.enableUncompressesNativeLibs=true -------------------------------------------------------------------------------- /YOLO_Depth/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /YOLO_Depth/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Mar 17 21:55:12 CST 2025 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /YOLO_Depth/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /YOLO_Depth/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /YOLO_Depth/local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Thu Apr 18 17:47:13 CST 2024 8 | sdk.dir=/home/iamj/Android/Sdk 9 | -------------------------------------------------------------------------------- /YOLO_Depth/settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven {url "https://maven.aliyun.com/repository/public/"} 4 | maven {url "https://maven.aliyun.com/repository/google/"} 5 | maven {url "https://maven.aliyun.com/repository/central/"} 6 | maven {url "https://maven.aliyun.com/repository/jcenter/"} 7 | maven {url "https://maven.aliyun.com/repository/gradle-plugin/"} 8 | maven {url "https://maven.aliyun.com/repository/apache-snapshots/"} 9 | google() 10 | mavenCentral() 11 | gradlePluginPortal() 12 | } 13 | } 14 | dependencyResolutionManagement { 15 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 16 | repositories { 17 | maven {url "https://maven.aliyun.com/repository/public/"} 18 | maven {url "https://maven.aliyun.com/repository/google/"} 19 | maven {url "https://maven.aliyun.com/repository/central/"} 20 | maven {url "https://maven.aliyun.com/repository/jcenter/"} 21 | maven {url "https://maven.aliyun.com/repository/gradle-plugin/"} 22 | maven {url "https://maven.aliyun.com/repository/apache-snapshots/"} 23 | google() 24 | mavenCentral() 25 | } 26 | } 27 | 28 | rootProject.name = "My Application" 29 | include ':app' 30 | -------------------------------------------------------------------------------- /YOLO_Depth/v10/project.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "onnxruntime_cxx_api.h" 6 | #include 7 | 8 | const char* computeShaderSource = "#version 320 es\n" 9 | "#extension GL_OES_EGL_image_external_essl3 : require\n" 10 | "precision mediump float;\n" 11 | "layout(local_size_x = 16, local_size_y = 16) in;\n" // gpu_num_group=16, Customize it to fit your device's specifications. 12 | "const int camera_width = 1280;\n" // camera_width 13 | "const int camera_height = 720;\n" // camera_height 14 | "layout(binding = 0) uniform samplerExternalOES yuvTex;\n" 15 | "layout(std430, binding = 1) buffer Output {\n" 16 | " int result[camera_height * camera_width];\n" // pixelCount 17 | "} outputData;\n" 18 | "const vec3 bias = vec3(-0.15, -0.5, -0.2);\n" 19 | "const mat3 YUVtoRGBMatrix = mat3(127.5, 0.0, 1.402 * 127.5, " 20 | " 127.5, -0.344136 * 127.5, -0.714136 * 127.5, " 21 | " 127.5, 1.772 * 127.5, 0.0);\n" 22 | "void main() {\n" 23 | " ivec2 texelPos = ivec2(gl_GlobalInvocationID.xy);\n" 24 | " ivec3 rgb = clamp(ivec3(YUVtoRGBMatrix * (texelFetch(yuvTex, texelPos, 0).rgb + bias)), -128, 127) + 128;\n" 25 | // Use uint8 packing the pixels, it would be 1.6 times faster than using float32 buffer. 26 | " outputData.result[texelPos.y * camera_width + texelPos.x] = (rgb.b << 16) | (rgb.r << 8) | (rgb.g);\n" 27 | "}"; 28 | GLuint pbo_A = 0; 29 | GLuint computeProgram = 0; 30 | GLint yuvTexLoc = 0; 31 | const GLsizei camera_width = 1280; 32 | const GLsizei camera_height = 720; 33 | const GLsizei pixelCount = camera_width * camera_height; 34 | const int output_size_A = 6 * 300; // [left, top, right, bottom, max_score, max_indices] * yolo_num_boxes 35 | const int output_size_B = 294 * 518; // depth_pixels 36 | const int pixelCount_rgb = 3 * pixelCount; 37 | const int gpu_num_group = 16; // Customize it to fit your device's specifications. 38 | const GLsizei rgbSize = pixelCount_rgb * sizeof(float); 39 | const GLsizei rgbSize_int = pixelCount * sizeof(int); 40 | const GLsizei rgbSize_i8 = pixelCount_rgb * sizeof(uint8_t); 41 | const GLsizei workGroupCountX = camera_width / gpu_num_group; 42 | const GLsizei workGroupCountY = camera_height / gpu_num_group; 43 | 44 | const OrtApi *ort_runtime_A; 45 | OrtSession *session_model_A; 46 | OrtRunOptions *run_options_A; 47 | std::vector input_names_A; 48 | std::vector output_names_A; 49 | std::vector> input_dims_A; 50 | std::vector> output_dims_A; 51 | std::vector input_types_A; 52 | std::vector output_types_A; 53 | std::vector input_tensors_A; 54 | std::vector output_tensors_A; 55 | const OrtApi *ort_runtime_B; 56 | OrtSession *session_model_B; 57 | OrtRunOptions *run_options_B; 58 | std::vector input_names_B; 59 | std::vector output_names_B; 60 | std::vector> input_dims_B; 61 | std::vector> output_dims_B; 62 | std::vector input_types_B; 63 | std::vector output_types_B; 64 | std::vector input_tensors_B; 65 | std::vector output_tensors_B; 66 | const std::string file_name_A = "Model_Yolo_v10s.ort"; 67 | const std::string file_name_B = "Depth_Anything_Metric_V2.ort"; 68 | const std::string storage_path = "/storage/emulated/0/Android/data/com.example.myapplication/files/"; 69 | 70 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/README.md: -------------------------------------------------------------------------------- 1 | # Three Steps to Launch the Qualcomm NPU-HTP 2 | 3 | ## Overview 4 | This guide will walk you through the steps to set up the Qualcomm NPU-HTP on your device. The process involves obtaining necessary libraries from your device and the official QNN SDK, and placing them in the appropriate directories. 5 | 6 | --- 7 | 8 | ## Step 1: Get Required Libraries from Your Device 9 | To run the Qualcomm NPU-HTP, connect your mobile device and execute the following commands in the Terminal to retrieve the required libraries. 10 | 11 | **Note:** These libraries are verified for Snapdragon 8Gen1, 8Gen2, 8Gen3; other devices may need different libraries. 12 | 13 | ### Execute the Commands in Terminal: 14 | ```bash 15 | cd ~/Downloads/YOLO_Depth_Drivable_Qualcomm_NPU 16 | 17 | adb pull /vendor/lib64/libcdsprpc.so ./app/libs/arm64-v8a 18 | adb pull /vendor/lib64/vendor.qti.hardware.dsp@1.0.so ./app/libs/arm64-v8a 19 | adb pull /vendor/lib64/libvmmem.so ./app/libs/arm64-v8a 20 | adb pull /system/lib64/libhidlbase.so ./app/libs/arm64-v8a 21 | adb pull /system/lib64/libhardware.so ./app/libs/arm64-v8a 22 | adb pull /system/lib64/libutils.so ./app/libs/arm64-v8a 23 | adb pull /system/lib64/libcutils.so ./app/libs/arm64-v8a 24 | adb pull /system/lib64/libdmabufheap.so ./app/libs/arm64-v8a 25 | adb pull /system/lib64/libc++.so ./app/libs/arm64-v8a 26 | adb pull /system/lib64/libbase.so ./app/libs/arm64-v8a 27 | adb pull /system/lib64/libvndksupport.so ./app/libs/arm64-v8a 28 | adb pull /system/lib64/libdl_android.so ./app/libs/arm64-v8a 29 | adb pull /system/lib64/ld-android.so ./app/libs/arm64-v8a 30 | 31 | adb pull /vendor/lib64/libcdsprpc.so ./app/src/main/assets 32 | adb pull /vendor/lib64/vendor.qti.hardware.dsp@1.0.so ./app/src/main/assets 33 | adb pull /vendor/lib64/libvmmem.so ./app/src/main/assets 34 | adb pull /system/lib64/libhidlbase.so ./app/src/main/assets 35 | adb pull /system/lib64/libhardware.so ./app/src/main/assets 36 | adb pull /system/lib64/libutils.so ./app/src/main/assets 37 | adb pull /system/lib64/libcutils.so ./app/src/main/assets 38 | adb pull /system/lib64/libdmabufheap.so ./app/src/main/assets 39 | adb pull /system/lib64/libc++.so ./app/src/main/assets 40 | adb pull /system/lib64/libbase.so ./app/src/main/assets 41 | adb pull /system/lib64/libvndksupport.so ./app/src/main/assets 42 | adb pull /system/lib64/libdl_android.so ./app/src/main/assets 43 | adb pull /system/lib64/ld-android.so ./app/src/main/assets 44 | ``` 45 | 46 | --- 47 | 48 | ## Step 2: Get Required Libraries from the Official QNN SDK 49 | 50 | You need additional libraries from the official [Qualcomm AI Engine Direct SDK](https://www.qualcomm.com/developer/software/qualcomm-ai-engine-direct-sdk) `(QNN SDK, demo version: 2.32.*)`. Download the SDK from the official website, and locate the libraries in the `2.32.*/lib/aarch64-android` directory. Ensure the SDK version matches the one used for compiling the `libonnxruntime.so` library. 51 | 52 | **Note:** To obtain the target SDK version, you must use the `Qualcomm® Package Manager (QPM3)`; otherwise, you will receive another version via the direct download link.
53 | **Note:** Find your device from [here](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/overview.html#supported-snapdragon-devices). 54 | 55 | ### 8Gen1 Required Libraries: 56 | 1. `libQnnCpu.so` 57 | 2. `libQnnHtp.so` 58 | 3. `libQnnHtpPrepare.so` 59 | 4. `libQnnHtpV69Skel.so` 60 | 5. `libQnnHtpV69Stub.so` 61 | 6. `libQnnSystem.so` 62 | 63 | ### 8Gen2 Required Libraries: 64 | 1. `libQnnCpu.so` 65 | 2. `libQnnHtp.so` 66 | 3. `libQnnHtpPrepare.so` 67 | 4. `libQnnHtpV73Skel.so` 68 | 5. `libQnnHtpV73Stub.so` 69 | 6. `libQnnSystem.so` 70 | 71 | ### 8Gen3 Required Libraries: 72 | 1. `libQnnCpu.so` 73 | 2. `libQnnHtp.so` 74 | 3. `libQnnHtpPrepare.so` 75 | 4. `libQnnHtpV75Skel.so` 76 | 5. `libQnnHtpV75Stub.so` 77 | 6. `libQnnSystem.so` 78 | 79 | 80 | --- 81 | 82 | ## Step 3: Organize Libraries 83 | 84 | Use 8Gen2 as example. Place all the required libraries in both the `assets` and `libs/arm64-v8a` folders. 85 | 86 | ### List of Libraries: 87 | 1. `libQnnCpu.so` 88 | 2. `libQnnHtp.so` 89 | 3. `libQnnHtpPrepare.so` 90 | 4. `libQnnHtpV73Skel.so` 91 | 5. `libQnnHtpV73Stub.so` 92 | 6. `libQnnSystem.so` 93 | 7. `libcdsprpc.so` 94 | 8. `vendor.qti.hardware.dsp@1.0.so` 95 | 9. `libvmmem.so` 96 | 10. `libhidlbase.so` 97 | 11. `libhardware.so` 98 | 12. `libutils.so` 99 | 13. `libcutils.so` 100 | 14. `libdmabufheap.so` 101 | 15. `libc++.so` 102 | 16. `libbase.so` 103 | 17. `libdl_android.so` 104 | 18. `ld-android.so` 105 | 19. `libonnxruntime.so` (libs/arm64-v8a folder only) 106 | 20. `libomp.so` (libs/arm64-v8a folder only) 107 | 108 | --- 109 | 110 | Following these steps will ensure the necessary setup for running the Qualcomm NPU-HTP on a Snapdragon 8Gen2 device. 111 | 112 | # 启动 Qualcomm NPU-HTP 的三个步骤 113 | 114 | ## 概述 115 | 本指南将引导您完成在设备上设置 Qualcomm NPU-HTP 的步骤。该过程涉及从您的设备和官方 QNN SDK 获取必要的库,并将它们放置在适当的目录中。 116 | 117 | --- 118 | 119 | ## 步骤 1:从您的设备获取所需库 120 | 要运行 Qualcomm NPU-HTP,请连接您的移动设备并在终端中执行以下命令以获取所需的库。 121 | 122 | **注意:** 这些库仅针对 Snapdragon 8Gen1, 8Gen2, 8Gen3 确认;其他设备可能需要不同的库。 123 | 124 | ### 在终端中执行以下命令: 125 | ```bash 126 | cd ~/Downloads/YOLO_Depth_Drivable_Qualcomm_NPU 127 | 128 | adb pull /vendor/lib64/libcdsprpc.so ./app/libs/arm64-v8a 129 | adb pull /vendor/lib64/vendor.qti.hardware.dsp@1.0.so ./app/libs/arm64-v8a 130 | adb pull /vendor/lib64/libvmmem.so ./app/libs/arm64-v8a 131 | adb pull /system/lib64/libhidlbase.so ./app/libs/arm64-v8a 132 | adb pull /system/lib64/libhardware.so ./app/libs/arm64-v8a 133 | adb pull /system/lib64/libutils.so ./app/libs/arm64-v8a 134 | adb pull /system/lib64/libcutils.so ./app/libs/arm64-v8a 135 | adb pull /system/lib64/libdmabufheap.so ./app/libs/arm64-v8a 136 | adb pull /system/lib64/libc++.so ./app/libs/arm64-v8a 137 | adb pull /system/lib64/libbase.so ./app/libs/arm64-v8a 138 | adb pull /system/lib64/libvndksupport.so ./app/libs/arm64-v8a 139 | adb pull /system/lib64/libdl_android.so ./app/libs/arm64-v8a 140 | adb pull /system/lib64/ld-android.so ./app/libs/arm64-v8a 141 | 142 | adb pull /vendor/lib64/libcdsprpc.so ./app/src/main/assets 143 | adb pull /vendor/lib64/vendor.qti.hardware.dsp@1.0.so ./app/src/main/assets 144 | adb pull /vendor/lib64/libvmmem.so ./app/src/main/assets 145 | adb pull /system/lib64/libhidlbase.so ./app/src/main/assets 146 | adb pull /system/lib64/libhardware.so ./app/src/main/assets 147 | adb pull /system/lib64/libutils.so ./app/src/main/assets 148 | adb pull /system/lib64/libcutils.so ./app/src/main/assets 149 | adb pull /system/lib64/libdmabufheap.so ./app/src/main/assets 150 | adb pull /system/lib64/libc++.so ./app/src/main/assets 151 | adb pull /system/lib64/libbase.so ./app/src/main/assets 152 | adb pull /system/lib64/libvndksupport.so ./app/src/main/assets 153 | adb pull /system/lib64/libdl_android.so ./app/src/main/assets 154 | adb pull /system/lib64/ld-android.so ./app/src/main/assets 155 | ``` 156 | 157 | --- 158 | 159 | ## 步骤 2:从官方 QNN SDK 获取所需库 160 | 161 | 您需要从官方 [Qualcomm AI Engine Direct SDK](https://www.qualcomm.com/developer/software/qualcomm-ai-engine-direct-sdk) `(QNN SDK,此演示的版本:2.32.*)`获取额外的库。从官网下载 SDK,并在 `2.32.*/lib/aarch64-android` 目录中找到这些库。确保SDK版本与编译 `libonnxruntime.so` 库时使用的版本一致。 162 | 163 | **注意:** 要获取指定的 SDK 版本,您必须使用`Qualcomm® Package Manager(QPM3)`;否则,您将通过直接下载链接收到其他版本。
164 | **注意:** 可以在这查到[型号](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/overview.html#supported-snapdragon-devices)。 165 | 166 | ### 8Gen1 Required Libraries: 167 | 1. `libQnnCpu.so` 168 | 2. `libQnnHtp.so` 169 | 3. `libQnnHtpPrepare.so` 170 | 4. `libQnnHtpV69Skel.so` 171 | 5. `libQnnHtpV69Stub.so` 172 | 6. `libQnnSystem.so` 173 | 174 | ### 8Gen2 Required Libraries: 175 | 1. `libQnnCpu.so` 176 | 2. `libQnnHtp.so` 177 | 3. `libQnnHtpPrepare.so` 178 | 4. `libQnnHtpV73Skel.so` 179 | 5. `libQnnHtpV73Stub.so` 180 | 6. `libQnnSystem.so` 181 | 182 | ### 8Gen3 Required Libraries: 183 | 1. `libQnnCpu.so` 184 | 2. `libQnnHtp.so` 185 | 3. `libQnnHtpPrepare.so` 186 | 4. `libQnnHtpV75Skel.so` 187 | 5. `libQnnHtpV75Stub.so` 188 | 6. `libQnnSystem.so` 189 | --- 190 | 191 | ## 步骤 3:组织库文件 192 | 193 | 使用8Gen2為例子,将所有需要的库文件放置在 `assets` 和 `libs/arm64-v8a` 文件夹中。 194 | 195 | ### 库文件列表: 196 | 1. `libQnnCpu.so` 197 | 2. `libQnnHtp.so` 198 | 3. `libQnnHtpPrepare.so` 199 | 4. `libQnnHtpV73Skel.so` 200 | 5. `libQnnHtpV73Stub.so` 201 | 6. `libQnnSystem.so` 202 | 7. `libcdsprpc.so` 203 | 8. `vendor.qti.hardware.dsp@1.0.so` 204 | 9. `libvmmem.so` 205 | 10. `libhidlbase.so` 206 | 11. `libhardware.so` 207 | 12. `libutils.so` 208 | 13. `libcutils.so` 209 | 14. `libdmabufheap.so` 210 | 15. `libc++.so` 211 | 16. `libbase.so` 212 | 17. `libdl_android.so` 213 | 18. `ld-android.so` 214 | 19. `libonnxruntime.so` (只需放在libs/arm64-v8a文件夹裡) 215 | 20. `libomp.so` (只需放在libs/arm64-v8a文件夹裡) 216 | 217 | --- 218 | 219 | 按照这些步骤操作,将确保在 Snapdragon 8Gen2 设备上运行 Qualcomm NPU-HTP 的必要设置。 220 | 221 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | } 4 | 5 | android { 6 | namespace 'com.example.myapplication' 7 | compileSdk 34 8 | 9 | defaultConfig { 10 | applicationId "com.example.myapplication" 11 | minSdk 29 12 | targetSdk 34 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | externalNativeBuild { 18 | cmake { 19 | cppFlags '-std=c++17' 20 | abiFilters 'arm64-v8a' 21 | arguments '-DANDROID_STL=c++_shared' 22 | } 23 | ndk{ 24 | //noinspection ChromeOsAbiSupport 25 | abiFilters 'arm64-v8a' 26 | } 27 | } 28 | } 29 | buildTypes { 30 | release { 31 | minifyEnabled true 32 | shrinkResources true 33 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | compileOptions { 37 | sourceCompatibility JavaVersion.VERSION_21 38 | targetCompatibility JavaVersion.VERSION_21 39 | } 40 | externalNativeBuild { 41 | cmake { 42 | path file('src/main/cpp/CMakeLists.txt') 43 | version '3.31.6' 44 | } 45 | } 46 | sourceSets { 47 | main { 48 | jniLibs.srcDirs = ['libs'] 49 | } 50 | } 51 | buildFeatures { 52 | viewBinding true 53 | } 54 | ndkVersion '29.0.13113456 rc1' 55 | } 56 | 57 | dependencies { 58 | implementation 'androidx.appcompat:appcompat:1.7.0' 59 | implementation 'com.google.android.material:material:1.12.0' 60 | implementation 'androidx.constraintlayout:constraintlayout:2.2.1' 61 | testImplementation 'junit:junit:4.13.2' 62 | androidTestImplementation 'androidx.test.ext:junit:1.2.1' 63 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1' 64 | } 65 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/libs/arm64-v8a/ort_1.21.0_qnn_2.32.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/libs/arm64-v8a/ort_1.21.0_qnn_2.32.zip -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Mon May 06 14:35:59 CST 2024 8 | sdk.dir=/home/iamj/Android/Sdk 9 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 24 | 27 | 28 | 29 | 30 | 31 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/Model_YOLO_v12_n_f16.onnx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/assets/Model_YOLO_v12_n_f16.onnx -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/camera_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | #extension GL_OES_EGL_image_external_essl3 : require 3 | precision mediump float; 4 | 5 | in vec2 texCoord;//纹理坐标,图片当中的坐标点 6 | out vec4 outColor; 7 | 8 | uniform samplerExternalOES camera_texture;//图片,采样器 9 | 10 | void main(){ 11 | outColor = texture(camera_texture, texCoord); 12 | } 13 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/camera_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | precision mediump float; 3 | //把顶点坐标给这个变量, 确定要画画的形状 4 | layout (location = 0) in vec4 aPosVertex; 5 | 6 | //接收纹理坐标,接收采样器采样图片的坐标 7 | layout (location = 1) in vec4 aTexVertex; 8 | 9 | //传给片元着色器 像素点 10 | out vec2 texCoord; 11 | 12 | uniform mat4 vMatrix; 13 | 14 | void main() 15 | { 16 | //内置变量 gl_Position ,我们把顶点数据赋值给这个变量 opengl就知道它要画什么形状了 17 | gl_Position = aPosVertex; 18 | texCoord = (vMatrix * aTexVertex).xy; 19 | //texCoord = aTexVertex.xy; 20 | } 21 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/class.txt: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | potted plant 60 | bed 61 | dining table 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/yolo_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | precision mediump float; 3 | out vec4 fragColor; 4 | uniform vec4 box_color; 5 | 6 | void main() { 7 | fragColor = vec4(box_color); 8 | } 9 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/assets/yolo_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | #version 320 es 2 | 3 | precision mediump float; 4 | 5 | in vec4 box_position; 6 | 7 | void main() { 8 | 9 | gl_Position = vec4(box_position); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project("myapplication") 2 | set(ANDROID_ABI "arm64-v8a") 3 | cmake_minimum_required(VERSION 3.22.1) 4 | set(CMAKE_BUILD_TYPE Release) 5 | 6 | include_directories(${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}) 7 | 8 | add_compile_options( 9 | # Optimization Flags: Enable aggressive optimizations for performance 10 | -O3 11 | -ffast-math 12 | -ffp-model=fast 13 | -ffp-contract=fast 14 | -funsafe-math-optimizations 15 | -fvectorize 16 | -funroll-loops 17 | -finline-functions 18 | -fomit-frame-pointer 19 | -fopenmp-simd 20 | -minline-all-stringops 21 | 22 | # CPU-Specific Optimization: Replace with your chip 23 | -march=armv8.7a+fp16fml+i8mm+dotprod+crypto+simd+tme 24 | -mtune=cortex-a76 25 | -mcpu=cortex-a76+cortex-a55 26 | 27 | # Link-Time Optimization (LTO) & Code Size Reduction: Reduce binary size and improve performance 28 | -flto 29 | -ffunction-sections 30 | -fdata-sections 31 | -falign-functions 32 | 33 | # Exception Handling & Runtime Behavior: Disable RTTI, exceptions, and stack protection for faster execution 34 | -fno-sanitize=address 35 | -fno-sanitize=alignment 36 | -fno-exceptions 37 | -fno-rtti 38 | -fno-stack-protector 39 | -fPIC 40 | 41 | # Visibility and Symbol Management: Hide unnecessary symbols to reduce binary size 42 | -fvisibility=hidden 43 | -fvisibility-inlines-hidden 44 | 45 | # Miscellaneous / Compiler-Specific Tweaks 46 | -pipe 47 | 48 | ) 49 | 50 | add_library(${CMAKE_PROJECT_NAME} SHARED project.cpp) 51 | 52 | add_library( 53 | onnxruntime 54 | SHARED 55 | IMPORTED) 56 | set_target_properties(onnxruntime PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libonnxruntime.so) 57 | 58 | add_library( 59 | omp 60 | SHARED 61 | IMPORTED) 62 | set_target_properties(omp PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libomp.so) 63 | 64 | add_library( 65 | GLESv3 66 | SHARED 67 | IMPORTED) 68 | set_target_properties(GLESv3 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libGLESv3.so) 69 | 70 | find_library( 71 | log 72 | android 73 | ) 74 | 75 | target_link_libraries( 76 | myapplication 77 | android 78 | onnxruntime 79 | omp 80 | GLESv3 81 | log) 82 | 83 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/cpp/project.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "onnxruntime_cxx_api.h" 6 | #include 7 | 8 | const char* computeShaderSource = "#version 320 es\n" 9 | "#extension GL_OES_EGL_image_external_essl3 : require\n" 10 | "precision mediump float;\n" 11 | "layout(local_size_x = 16, local_size_y = 16) in;\n" // gpu_num_group=16, Customize it to fit your device's specifications. 12 | "const int camera_width = 1280;\n" // camera_width 13 | "const int camera_height = 720;\n" // camera_height 14 | "layout(binding = 0) uniform samplerExternalOES yuvTex;\n" 15 | "layout(std430, binding = 1) buffer Output {\n" 16 | " int result[camera_height * camera_width];\n" // pixelCount 17 | "} outputData;\n" 18 | "const vec3 bias = vec3(-0.15, -0.5, -0.2);\n" 19 | "const mat3 YUVtoRGBMatrix = mat3(127.5, 0.0, 1.402 * 127.5, " 20 | " 127.5, -0.344136 * 127.5, -0.714136 * 127.5, " 21 | " 127.5, 1.772 * 127.5, 0.0);\n" 22 | "void main() {\n" 23 | " ivec2 texelPos = ivec2(gl_GlobalInvocationID.xy);\n" 24 | " ivec3 rgb = clamp(ivec3(YUVtoRGBMatrix * (texelFetch(yuvTex, texelPos, 0).rgb + bias)), -128, 127) + 128;\n" 25 | // Use int8 packing the pixels, it would be 1.6 times faster than using float32 buffer. 26 | " outputData.result[texelPos.y * camera_width + texelPos.x] = (rgb.b << 16) | (rgb.r << 8) | (rgb.g);\n" 27 | "}"; 28 | GLuint pbo_A = 0; 29 | GLuint computeProgram = 0; 30 | GLint yuvTexLoc = 0; 31 | const GLsizei camera_width = 1280; 32 | const GLsizei camera_height = 720; 33 | const GLsizei pixelCount = camera_width * camera_height; 34 | const int output_size_A = 6 * 3024; // [x, y, w, h, max_score, max_indices] * yolo_num_boxes 35 | const int output_size_B = 294 * 518; // depth_pixels 36 | const int pixelCount_rgb = 3 * pixelCount; 37 | const int gpu_num_group = 16; // Customize it to fit your device's specifications. 38 | const GLsizei rgbSize = pixelCount_rgb * sizeof(float); 39 | const GLsizei rgbSize_int = pixelCount * sizeof(int); 40 | const GLsizei rgbSize_i8 = pixelCount_rgb * sizeof(uint8_t); 41 | const GLsizei workGroupCountX = camera_width / gpu_num_group; 42 | const GLsizei workGroupCountY = camera_height / gpu_num_group; 43 | 44 | const OrtApi* ort_runtime_A; 45 | OrtSession* session_model_A; 46 | OrtRunOptions *run_options_A; 47 | std::vector input_names_A; 48 | std::vector output_names_A; 49 | std::vector> input_dims_A; 50 | std::vector> output_dims_A; 51 | std::vector input_types_A; 52 | std::vector output_types_A; 53 | std::vector input_tensors_A; 54 | std::vector output_tensors_A; 55 | const OrtApi* ort_runtime_B; 56 | OrtSession* session_model_B; 57 | OrtRunOptions *run_options_B; 58 | std::vector input_names_B; 59 | std::vector output_names_B; 60 | std::vector> input_dims_B; 61 | std::vector> output_dims_B; 62 | std::vector input_types_B; 63 | std::vector output_types_B; 64 | std::vector input_tensors_B; 65 | std::vector output_tensors_B; 66 | const std::string file_name_A = "Model_YOLO_v12_n_f16.onnx"; 67 | const std::string file_name_B = "Depth_Anything_Metric_V2.ort"; 68 | const std::string storage_path = "/storage/emulated/0/Android/data/com.example.myapplication/"; 69 | const char* ctx_model_A = "/storage/emulated/0/Android/data/com.example.myapplication/ctx_model_A.onnx"; 70 | const char* ctx_model_B = "/storage/emulated/0/Android/data/com.example.myapplication/ctx_model_B.onnx"; 71 | const char* cache_path = "/data/user/0/com.example.myapplication/cache"; 72 | const char* qualcomm_soc_id = "43"; // 0 for unknown, Find your device from here: https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/overview.html#supported-snapdragon-devices 73 | const char* qnn_htp_so = "/data/user/0/com.example.myapplication/cache/libQnnHtp.so"; // If use (std::string + "libQnnHtp.so").c_str() instead, it will open failed. 74 | const char* qnn_cpu_so = "/data/user/0/com.example.myapplication/cache/libQnnCpu.so"; // If use (std::string + "libQnnCpu.so").c_str() instead, it will open failed. 75 | // Just specify the path for qnn_*_so, and the code will automatically locate the other required libraries. 76 | 77 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/java/com/example/myapplication/Classifier.java: -------------------------------------------------------------------------------- 1 | package com.example.myapplication; 2 | import android.annotation.SuppressLint; 3 | import android.graphics.RectF; 4 | import androidx.annotation.NonNull; 5 | 6 | 7 | /** 8 | * Generic interface for interacting with different recognition engines. 9 | */ 10 | public interface Classifier { 11 | /** 12 | * An immutable result returned by a Classifier describing what was recognized. 13 | */ 14 | class Recognition { 15 | /** 16 | * A unique identifier for what has been recognized. Specific to the class, not the instance of 17 | * the object. 18 | */ 19 | private final String id; 20 | /** 21 | * Display name for the recognition. 22 | */ 23 | private final String title; 24 | /** 25 | * A sortable score for how good the recognition is relative to others. Higher should be better. 26 | */ 27 | private final Float confidence; 28 | /** 29 | * Optional location within the source image for the location of the recognized object. 30 | */ 31 | private final RectF location; 32 | public Recognition(final String id, final String title, final Float confidence, final RectF location) { 33 | this.id = id; 34 | this.title = title; 35 | this.confidence = confidence; 36 | this.location = location; 37 | } 38 | 39 | public Float getConfidence() { 40 | return confidence; 41 | } 42 | 43 | public RectF getLocation() { 44 | return new RectF(location); 45 | } 46 | 47 | public String getTitle() { 48 | return title; 49 | } 50 | 51 | @NonNull 52 | @SuppressLint("DefaultLocale") 53 | @Override 54 | public String toString() { 55 | String resultString = ""; 56 | if (id != null) { 57 | resultString += "[" + id + "] "; 58 | } 59 | 60 | if (title != null) { 61 | resultString += title + " "; 62 | } 63 | 64 | if (confidence != null) { 65 | resultString += String.format("(%.2f%%) ", confidence * 100.f); 66 | } 67 | 68 | if (location != null) { 69 | resultString += location + " "; 70 | } 71 | 72 | return resultString.trim(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/drawable/psyduck.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/drawable/psyduck.png -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 21 | 22 | 36 | 37 | 49 | 50 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_picture_snap.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xhdpi/ic_picture_snap.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | 11 | #008577 12 | #00574B 13 | #D81B60 14 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | YOLO_Depth 3 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | plugins { 3 | id 'com.android.application' version '8.5.2' apply false 4 | id 'com.android.library' version '8.5.2' apply false 5 | } -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx16g -XX:MaxMetaspaceSize=16g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Enables namespacing of each library's R class so that its R class includes only the 19 | # resources declared in the library itself and none from the library's dependencies, 20 | # thereby reducing the size of the R class for that library 21 | android.nonTransitiveRClass=true 22 | android.bundle.enableUncompressesNativeLibs=true -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/YOLO_Depth_Qualcomm_NPU/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Mar 17 22:49:45 CST 2025 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Thu Apr 18 17:47:13 CST 2024 8 | sdk.dir=/home/iamj/Android/Sdk 9 | -------------------------------------------------------------------------------- /YOLO_Depth_Qualcomm_NPU/settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven {url "https://maven.aliyun.com/repository/public/"} 4 | maven {url "https://maven.aliyun.com/repository/google/"} 5 | maven {url "https://maven.aliyun.com/repository/central/"} 6 | maven {url "https://maven.aliyun.com/repository/jcenter/"} 7 | maven {url "https://maven.aliyun.com/repository/gradle-plugin/"} 8 | maven {url "https://maven.aliyun.com/repository/apache-snapshots/"} 9 | google() 10 | mavenCentral() 11 | gradlePluginPortal() 12 | } 13 | } 14 | dependencyResolutionManagement { 15 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 16 | repositories { 17 | maven {url "https://maven.aliyun.com/repository/public/"} 18 | maven {url "https://maven.aliyun.com/repository/google/"} 19 | maven {url "https://maven.aliyun.com/repository/central/"} 20 | maven {url "https://maven.aliyun.com/repository/jcenter/"} 21 | maven {url "https://maven.aliyun.com/repository/gradle-plugin/"} 22 | maven {url "https://maven.aliyun.com/repository/apache-snapshots/"} 23 | google() 24 | mavenCentral() 25 | } 26 | } 27 | 28 | rootProject.name = "My Application" 29 | include ':app' 30 | -------------------------------------------------------------------------------- /yolo_depth.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/yolo_depth.gif -------------------------------------------------------------------------------- /yolo_depth2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DakeQQ/YOLO-Depth-Estimation-for-Android/c731ca0e47b709258690e58819cec510094b9b19/yolo_depth2.gif --------------------------------------------------------------------------------