Instructions to use aa-studio/aa_studio_data with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use aa-studio/aa_studio_data with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("aa-studio/aa_studio_data", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+1
- ComfyUI/models/midas/intel-isl_MiDaS_master/.gitignoreAdded+110
- ComfyUI/models/midas/intel-isl_MiDaS_master/DockerfileAdded+29
- ComfyUI/models/midas/intel-isl_MiDaS_master/LICENSEAdded+21
- ComfyUI/models/midas/intel-isl_MiDaS_master/README.mdAdded+300
- ComfyUI/models/midas/intel-isl_MiDaS_master/__pycache__/hubconf.cpython-310.pycAdded
- ComfyUI/models/midas/intel-isl_MiDaS_master/environment.yamlAdded+16
- ComfyUI/models/midas/intel-isl_MiDaS_master/figures/Comparison.pngAdded
- ComfyUI/models/midas/intel-isl_MiDaS_master/figures/Improvement_vs_FPS.pngAdded
- ComfyUI/models/midas/intel-isl_MiDaS_master/hubconf.pyAdded+435
- ComfyUI/models/midas/intel-isl_MiDaS_master/input/.placeholderAdded
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/beit.pyAdded+196
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/levit.pyAdded+106
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/next_vit.pyAdded+39
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin.pyAdded+13
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin2.pyAdded+34
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin_common.pyAdded+52
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/utils.pyAdded+249
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/vit.pyAdded+221
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/base_model.pyAdded+16
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/blocks.pyAdded+439
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/dpt_depth.pyAdded+166
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/midas_net.pyAdded+76
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/midas_net_custom.pyAdded+128
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/model_loader.pyAdded+242
- ComfyUI/models/midas/intel-isl_MiDaS_master/midas/transforms.pyAdded+234
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/README.mdAdded+70
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/.gitignoreAdded+13
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/EXPLORE_THE_CODE.mdAdded+414
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/LICENSEAdded+21
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/README.mdAdded+21
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/.gitignoreAdded+3
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/build.gradleAdded+56
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/proguard-rules.proAdded+21
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/assets/fox-mobilenet_v1_1.0_224_support.txtAdded+3
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/assets/fox-mobilenet_v1_1.0_224_task_api.txtAdded+3
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/java/AndroidManifest.xmlAdded+5
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/java/org/tensorflow/lite/examples/classification/ClassifierTest.javaAdded+121
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/AndroidManifest.xmlAdded+28
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/CameraActivity.javaAdded+717
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/CameraConnectionFragment.javaAdded+575
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/ClassifierActivity.javaAdded+238
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/LegacyCameraConnectionFragment.javaAdded+203
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/AutoFitTextureView.javaAdded+72
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/OverlayView.javaAdded+48
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/RecognitionScoreView.javaAdded+67
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/ResultsView.javaAdded+23
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/BorderedText.javaAdded+115
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/ImageUtils.javaAdded+152
- ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/Logger.javaAdded+186
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textComfyUI/models/midas/intel-isl_MiDaS_master/figures/Comparison.png filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/*.egg-info/.installed.cfg*.eggMANIFEST
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover.hypothesis/.pytest_cache/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuildertarget/
# Jupyter Notebook.ipynb_checkpoints
# pyenv.python-version
# celery beat schedule filecelerybeat-schedule
# SageMath parsed files*.sage.py
# Environments.env.venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/
*.png*.pfm*.jpg*.jpeg*.pt1234567891011121314151617181920212223242526272829# enables cuda support in dockerFROM nvidia/cuda:10.2-cudnn7-runtime-ubuntu18.04
# install python 3.6, pip and requirements for opencv-python # (see https://github.com/NVIDIA/nvidia-docker/issues/864)RUN apt-get update && apt-get -y install \ python3 \ python3-pip \ libsm6 \ libxext6 \ libxrender-dev \ curl \ && rm -rf /var/lib/apt/lists/*
# install python dependenciesRUN pip3 install --upgrade pipRUN pip3 install torch~=1.8 torchvision opencv-python-headless~=3.4 timm
# copy inference codeWORKDIR /opt/MiDaSCOPY ./midas ./midasCOPY ./*.py ./
# download model weights so the docker image can be used offlineRUN cd weights && {curl -OL https://github.com/isl-org/MiDaS/releases/download/v3/dpt_hybrid_384.pt; cd -; }RUN python3 run.py --model_type dpt_hybrid; exit 0
# entrypoint (dont forget to mount input and output directories)CMD python3 run.py --model_type dpt_hybrid123456789101112131415161718192021MIT License
Copyright (c) 2019 Intel ISL (Intel Intelligent Systems Lab)
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300## Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer
This repository contains code to compute depth from a single image. It accompanies our [paper](https://arxiv.org/abs/1907.01341v3):
>Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer René Ranftl, Katrin Lasinger, David Hafner, Konrad Schindler, Vladlen Koltun
and our [preprint](https://arxiv.org/abs/2103.13413):
> Vision Transformers for Dense Prediction > René Ranftl, Alexey Bochkovskiy, Vladlen Koltun
For the latest release MiDaS 3.1, a [technical report](https://arxiv.org/pdf/2307.14460.pdf) and [video](https://www.youtube.com/watch?v=UjaeNNFf9sE&t=3s) are available.
MiDaS was trained on up to 12 datasets (ReDWeb, DIML, Movies, MegaDepth, WSVD, TartanAir, HRWSI, ApolloScape, BlendedMVS, IRS, KITTI, NYU Depth V2) withmulti-objective optimization. The original model that was trained on 5 datasets (`MIX 5` in the paper) can be found [here](https://github.com/isl-org/MiDaS/releases/tag/v2).The figure below shows an overview of the different MiDaS models; the bubble size scales with number of parameters.

### Setup
1) Pick one or more models and download the corresponding weights to the `weights` folder:
MiDaS 3.1- For highest quality: [dpt_beit_large_512](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_512.pt)- For moderately less quality, but better speed-performance trade-off: [dpt_swin2_large_384](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_large_384.pt)- For embedded devices: [dpt_swin2_tiny_256](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_tiny_256.pt), [dpt_levit_224](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_levit_224.pt)- For inference on Intel CPUs, OpenVINO may be used for the small legacy model: openvino_midas_v21_small [.xml](https://github.com/isl-org/MiDaS/releases/download/v3_1/openvino_midas_v21_small_256.xml), [.bin](https://github.com/isl-org/MiDaS/releases/download/v3_1/openvino_midas_v21_small_256.bin)
MiDaS 3.0: Legacy transformer models [dpt_large_384](https://github.com/isl-org/MiDaS/releases/download/v3/dpt_large_384.pt) and [dpt_hybrid_384](https://github.com/isl-org/MiDaS/releases/download/v3/dpt_hybrid_384.pt)
MiDaS 2.1: Legacy convolutional models [midas_v21_384](https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_384.pt) and [midas_v21_small_256](https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_small_256.pt)
1) Set up dependencies:
```shell conda env create -f environment.yaml conda activate midas-py310 ```
#### optional
For the Next-ViT model, execute
```shellgit submodule add https://github.com/isl-org/Next-ViT midas/external/next_vit```
For the OpenVINO model, install
```shellpip install openvino``` ### Usage
1) Place one or more input images in the folder `input`.
2) Run the model with
```shell python run.py --model_type <model_type> --input_path input --output_path output ``` where ```<model_type>``` is chosen from [dpt_beit_large_512](#model_type), [dpt_beit_large_384](#model_type), [dpt_beit_base_384](#model_type), [dpt_swin2_large_384](#model_type), [dpt_swin2_base_384](#model_type), [dpt_swin2_tiny_256](#model_type), [dpt_swin_large_384](#model_type), [dpt_next_vit_large_384](#model_type), [dpt_levit_224](#model_type), [dpt_large_384](#model_type), [dpt_hybrid_384](#model_type), [midas_v21_384](#model_type), [midas_v21_small_256](#model_type), [openvino_midas_v21_small_256](#model_type). 3) The resulting depth maps are written to the `output` folder.
#### optional
1) By default, the inference resizes the height of input images to the size of a model to fit into the encoder. This size is given by the numbers in the model names of the [accuracy table](#accuracy). Some models do not only support a single inference height but a range of different heights. Feel free to explore different heights by appending the extra command line argument `--height`. Unsupported height values will throw an error. Note that using this argument may decrease the model accuracy.2) By default, the inference keeps the aspect ratio of input images when feeding them into the encoder if this is supported by a model (all models except for Swin, Swin2, LeViT). In order to resize to a square resolution, disregarding the aspect ratio while preserving the height, use the command line argument `--square`.
#### via Camera
If you want the input images to be grabbed from the camera and shown in a window, leave the input and output paths away and choose a model type as shown above:
```shell python run.py --model_type <model_type> --side ```
The argument `--side` is optional and causes both the input RGB image and the output depth map to be shown side-by-side for comparison.
#### via Docker
1) Make sure you have installed Docker and the [NVIDIA Docker runtime](https://github.com/NVIDIA/nvidia-docker/wiki/Installation-\(Native-GPU-Support\)).
2) Build the Docker image:
```shell docker build -t midas . ```
3) Run inference:
```shell docker run --rm --gpus all -v $PWD/input:/opt/MiDaS/input -v $PWD/output:/opt/MiDaS/output -v $PWD/weights:/opt/MiDaS/weights midas ```
This command passes through all of your NVIDIA GPUs to the container, mounts the `input` and `output` directories and then runs the inference.
#### via PyTorch Hub
The pretrained model is also available on [PyTorch Hub](https://pytorch.org/hub/intelisl_midas_v2/)
#### via TensorFlow or ONNX
See [README](https://github.com/isl-org/MiDaS/tree/master/tf) in the `tf` subdirectory.
Currently only supports MiDaS v2.1.
#### via Mobile (iOS / Android)
See [README](https://github.com/isl-org/MiDaS/tree/master/mobile) in the `mobile` subdirectory.
#### via ROS1 (Robot Operating System)
See [README](https://github.com/isl-org/MiDaS/tree/master/ros) in the `ros` subdirectory.
Currently only supports MiDaS v2.1. DPT-based models to be added.
### Accuracy
We provide a **zero-shot error** $\epsilon_d$ which is evaluated for 6 different datasets(see [paper](https://arxiv.org/abs/1907.01341v3)). **Lower error values are better**. $\color{green}{\textsf{Overall model quality is represented by the improvement}}$ ([Imp.](#improvement)) with respect toMiDaS 3.0 DPT<sub>L-384</sub>. The models are grouped by the height used for inference, whereas the square training resolution is given by the numbers in the model names. The table also shows the **number of parameters** (in millions) and the **frames per second** for inference at the training resolution (for GPU RTX 3090):
| MiDaS Model | DIW </br><sup>WHDR</sup> | Eth3d </br><sup>AbsRel</sup> | Sintel </br><sup>AbsRel</sup> | TUM </br><sup>δ1</sup> | KITTI </br><sup>δ1</sup> | NYUv2 </br><sup>δ1</sup> | $\color{green}{\textsf{Imp.}}$ </br><sup>%</sup> | Par.</br><sup>M</sup> | FPS</br><sup> </sup> ||-----------------------------------------------------------------------------------------------------------------------|-------------------------:|-----------------------------:|------------------------------:|-------------------------:|-------------------------:|-------------------------:|-------------------------------------------------:|----------------------:|--------------------------:|| **Inference height 512** | | | | | | | | | || [v3.1 BEiT<sub>L-512</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_512.pt) | 0.1137 | 0.0659 | 0.2366 | **6.13** | 11.56* | **1.86*** | $\color{green}{\textsf{19}}$ | **345** | **5.7** || [v3.1 BEiT<sub>L-512</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_512.pt)$\tiny{\square}$ | **0.1121** | **0.0614** | **0.2090** | 6.46 | **5.00*** | 1.90* | $\color{green}{\textsf{34}}$ | **345** | **5.7** || | | | | | | | | | || **Inference height 384** | | | | | | | | | || [v3.1 BEiT<sub>L-512</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_512.pt) | 0.1245 | 0.0681 | **0.2176** | **6.13** | 6.28* | **2.16*** | $\color{green}{\textsf{28}}$ | 345 | 12 || [v3.1 Swin2<sub>L-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_large_384.pt)$\tiny{\square}$ | 0.1106 | 0.0732 | 0.2442 | 8.87 | **5.84*** | 2.92* | $\color{green}{\textsf{22}}$ | 213 | 41 || [v3.1 Swin2<sub>B-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_base_384.pt)$\tiny{\square}$ | 0.1095 | 0.0790 | 0.2404 | 8.93 | 5.97* | 3.28* | $\color{green}{\textsf{22}}$ | 102 | 39 || [v3.1 Swin<sub>L-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin_large_384.pt)$\tiny{\square}$ | 0.1126 | 0.0853 | 0.2428 | 8.74 | 6.60* | 3.34* | $\color{green}{\textsf{17}}$ | 213 | 49 || [v3.1 BEiT<sub>L-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_384.pt) | 0.1239 | **0.0667** | 0.2545 | 7.17 | 9.84* | 2.21* | $\color{green}{\textsf{17}}$ | 344 | 13 || [v3.1 Next-ViT<sub>L-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_next_vit_large_384.pt) | **0.1031** | 0.0954 | 0.2295 | 9.21 | 6.89* | 3.47* | $\color{green}{\textsf{16}}$ | **72** | 30 || [v3.1 BEiT<sub>B-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_base_384.pt) | 0.1159 | 0.0967 | 0.2901 | 9.88 | 26.60* | 3.91* | $\color{green}{\textsf{-31}}$ | 112 | 31 || [v3.0 DPT<sub>L-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3/dpt_large_384.pt) | 0.1082 | 0.0888 | 0.2697 | 9.97 | 8.46 | 8.32 | $\color{green}{\textsf{0}}$ | 344 | **61** || [v3.0 DPT<sub>H-384</sub>](https://github.com/isl-org/MiDaS/releases/download/v3/dpt_hybrid_384.pt) | 0.1106 | 0.0934 | 0.2741 | 10.89 | 11.56 | 8.69 | $\color{green}{\textsf{-10}}$ | 123 | 50 || [v2.1 Large<sub>384</sub>](https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_384.pt) | 0.1295 | 0.1155 | 0.3285 | 12.51 | 16.08 | 8.71 | $\color{green}{\textsf{-32}}$ | 105 | 47 || | | | | | | | | | || **Inference height 256** | | | | | | | | | || [v3.1 Swin2<sub>T-256</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_tiny_256.pt)$\tiny{\square}$ | **0.1211** | **0.1106** | **0.2868** | **13.43** | **10.13*** | **5.55*** | $\color{green}{\textsf{-11}}$ | 42 | 64 || [v2.1 Small<sub>256</sub>](https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_small_256.pt) | 0.1344 | 0.1344 | 0.3370 | 14.53 | 29.27 | 13.43 | $\color{green}{\textsf{-76}}$ | **21** | **90** || | | | | | | | | | || **Inference height 224** | | | | | | | | | || [v3.1 LeViT<sub>224</sub>](https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_levit_224.pt)$\tiny{\square}$ | **0.1314** | **0.1206** | **0.3148** | **18.21** | **15.27*** | **8.64*** | $\color{green}{\textsf{-40}}$ | **51** | **73** |
* No zero-shot error, because models are also trained on KITTI and NYU Depth V2\$\square$ Validation performed at **square resolution**, either because the transformer encoder backbone of a model does not support non-square resolutions (Swin, Swin2, LeViT) or for comparison with these models. All other validations keep the aspect ratio. A difference in resolution limits the comparability of the zero-shot error and theimprovement, because these quantities are averages over the pixels of an image and do not take into account the advantage of more details due to a higher resolution.\Best values per column and same validation height in bold
#### Improvement
The improvement in the above table is defined as the relative zero-shot error with respect to MiDaS v3.0 DPT<sub>L-384</sub> and averaging over the datasets. So, if $\epsilon_d$ is the zero-shot error for dataset $d$, thenthe $\color{green}{\textsf{improvement}}$ is given by $100(1-(1/6)\sum_d\epsilon_d/\epsilon_{d,\rm{DPT_{L-384}}})$%.
Note that the improvements of 10% for MiDaS v2.0 → v2.1 and 21% for MiDaS v2.1 → v3.0 are not visible from theimprovement column (Imp.) in the table but would require an evaluation with respect to MiDaS v2.1 Large<sub>384</sub>and v2.0 Large<sub>384</sub> respectively instead of v3.0 DPT<sub>L-384</sub>.
### Depth map comparison
Zoom in for better visibility
### Speed on Camera Feed
Test configuration - Windows 10 - 11th Gen Intel Core i7-1185G7 3.00GHz - 16GB RAM - Camera resolution 640x480 - openvino_midas_v21_small_256
Speed: 22 FPS
### Applications
MiDaS is used in the following other projects from Intel Labs:
- [ZoeDepth](https://arxiv.org/pdf/2302.12288.pdf) (code available [here](https://github.com/isl-org/ZoeDepth)): MiDaS computes the relative depth map given an image. For metric depth estimation, ZoeDepth can be used, which combines MiDaS with a metric depth binning module appended to the decoder.- [LDM3D](https://arxiv.org/pdf/2305.10853.pdf) (Hugging Face model available [here](https://huggingface.co/Intel/ldm3d-4c)): LDM3D is an extension of vanilla stable diffusion designed to generate joint image and depth data from a text prompt. The depth maps used for supervision when training LDM3D have been computed using MiDaS.
### Changelog
* [Dec 2022] Released [MiDaS v3.1](https://arxiv.org/pdf/2307.14460.pdf): - New models based on 5 different types of transformers ([BEiT](https://arxiv.org/pdf/2106.08254.pdf), [Swin2](https://arxiv.org/pdf/2111.09883.pdf), [Swin](https://arxiv.org/pdf/2103.14030.pdf), [Next-ViT](https://arxiv.org/pdf/2207.05501.pdf), [LeViT](https://arxiv.org/pdf/2104.01136.pdf)) - Training datasets extended from 10 to 12, including also KITTI and NYU Depth V2 using [BTS](https://github.com/cleinc/bts) split - Best model, BEiT<sub>Large 512</sub>, with resolution 512x512, is on average about [28% more accurate](#Accuracy) than MiDaS v3.0 - Integrated live depth estimation from camera feed* [Sep 2021] Integrated to [Huggingface Spaces](https://huggingface.co/spaces) with [Gradio](https://github.com/gradio-app/gradio). See [Gradio Web Demo](https://huggingface.co/spaces/akhaliq/DPT-Large).* [Apr 2021] Released MiDaS v3.0: - New models based on [Dense Prediction Transformers](https://arxiv.org/abs/2103.13413) are on average [21% more accurate](#Accuracy) than MiDaS v2.1 - Additional models can be found [here](https://github.com/isl-org/DPT)* [Nov 2020] Released MiDaS v2.1: - New model that was trained on 10 datasets and is on average about [10% more accurate](#Accuracy) than [MiDaS v2.0](https://github.com/isl-org/MiDaS/releases/tag/v2) - New light-weight model that achieves [real-time performance](https://github.com/isl-org/MiDaS/tree/master/mobile) on mobile platforms. - Sample applications for [iOS](https://github.com/isl-org/MiDaS/tree/master/mobile/ios) and [Android](https://github.com/isl-org/MiDaS/tree/master/mobile/android) - [ROS package](https://github.com/isl-org/MiDaS/tree/master/ros) for easy deployment on robots* [Jul 2020] Added TensorFlow and ONNX code. Added [online demo](http://35.202.76.57/).* [Dec 2019] Released new version of MiDaS - the new model is significantly more accurate and robust* [Jul 2019] Initial release of MiDaS ([Link](https://github.com/isl-org/MiDaS/releases/tag/v1))
### Citation
Please cite our paper if you use this code or any of the models:```@ARTICLE {Ranftl2022, author = "Ren\'{e} Ranftl and Katrin Lasinger and David Hafner and Konrad Schindler and Vladlen Koltun", title = "Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-Shot Cross-Dataset Transfer", journal = "IEEE Transactions on Pattern Analysis and Machine Intelligence", year = "2022", volume = "44", number = "3"}```
If you use a DPT-based model, please also cite:
```@article{Ranftl2021, author = {Ren\'{e} Ranftl and Alexey Bochkovskiy and Vladlen Koltun}, title = {Vision Transformers for Dense Prediction}, journal = {ICCV}, year = {2021},}```
Please cite the technical report for MiDaS 3.1 models:
```@article{birkl2023midas, title={MiDaS v3.1 -- A Model Zoo for Robust Monocular Relative Depth Estimation}, author={Reiner Birkl and Diana Wofk and Matthias M{\"u}ller}, journal={arXiv preprint arXiv:2307.14460}, year={2023}}```
For ZoeDepth, please use
```@article{bhat2023zoedepth, title={Zoedepth: Zero-shot transfer by combining relative and metric depth}, author={Bhat, Shariq Farooq and Birkl, Reiner and Wofk, Diana and Wonka, Peter and M{\"u}ller, Matthias}, journal={arXiv preprint arXiv:2302.12288}, year={2023}}```
and for LDM3D
```@article{stan2023ldm3d, title={LDM3D: Latent Diffusion Model for 3D}, author={Stan, Gabriela Ben Melech and Wofk, Diana and Fox, Scottie and Redden, Alex and Saxton, Will and Yu, Jean and Aflalo, Estelle and Tseng, Shao-Yen and Nonato, Fabio and Muller, Matthias and others}, journal={arXiv preprint arXiv:2305.10853}, year={2023}}```
### Acknowledgements
Our work builds on and uses code from [timm](https://github.com/rwightman/pytorch-image-models) and [Next-ViT](https://github.com/bytedance/Next-ViT). We'd like to thank the authors for making these libraries available.
### License
MIT License 12345678910111213141516name: midas-py310channels: - pytorch - defaultsdependencies: - nvidia::cudatoolkit=11.7 - python=3.10.8 - pytorch::pytorch=1.13.0 - torchvision=0.14.0 - pip=22.3.1 - numpy=1.23.4 - pip: - opencv-python==4.6.0.66 - imutils==0.5.4 - timm==0.6.12 - einops==0.6.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435dependencies = ["torch"]
import torch
from midas.dpt_depth import DPTDepthModelfrom midas.midas_net import MidasNetfrom midas.midas_net_custom import MidasNet_small
def DPT_BEiT_L_512(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_BEiT_L_512 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="beitl16_512", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_512.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_BEiT_L_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_BEiT_L_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="beitl16_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_large_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_BEiT_B_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_BEiT_B_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="beitb16_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_beit_base_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_SwinV2_L_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_SwinV2_L_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="swin2l24_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_large_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_SwinV2_B_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_SwinV2_B_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="swin2b24_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_base_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_SwinV2_T_256(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_SwinV2_T_256 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="swin2t16_256", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin2_tiny_256.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_Swin_L_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_Swin_L_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="swinl12_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_swin_large_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_Next_ViT_L_384(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_Next_ViT_L_384 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="next_vit_large_6m", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_next_vit_large_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_LeViT_224(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT_LeViT_224 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="levit_384", non_negative=True, head_features_1=64, head_features_2=8, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3_1/dpt_levit_224.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def DPT_Large(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT-Large model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="vitl16_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3/dpt_large_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model def DPT_Hybrid(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS DPT-Hybrid model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = DPTDepthModel( path=None, backbone="vitb_rn50_384", non_negative=True, )
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v3/dpt_hybrid_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model def MiDaS(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS v2.1 model for monocular depth estimation pretrained (bool): load pretrained weights into model """
model = MidasNet()
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_384.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def MiDaS_small(pretrained=True, **kwargs): """ # This docstring shows up in hub.help() MiDaS v2.1 small model for monocular depth estimation on resource-constrained devices pretrained (bool): load pretrained weights into model """
model = MidasNet_small(None, features=64, backbone="efficientnet_lite3", exportable=True, non_negative=True, blocks={'expand': True})
if pretrained: checkpoint = ( "https://github.com/isl-org/MiDaS/releases/download/v2_1/midas_v21_small_256.pt" ) state_dict = torch.hub.load_state_dict_from_url( checkpoint, map_location=torch.device('cpu'), progress=True, check_hash=True ) model.load_state_dict(state_dict)
return model
def transforms(): import cv2 from torchvision.transforms import Compose from midas.transforms import Resize, NormalizeImage, PrepareForNet from midas import transforms
transforms.default_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 384, 384, resize_target=None, keep_aspect_ratio=True, ensure_multiple_of=32, resize_method="upper_bound", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.small_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 256, 256, resize_target=None, keep_aspect_ratio=True, ensure_multiple_of=32, resize_method="upper_bound", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.dpt_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 384, 384, resize_target=None, keep_aspect_ratio=True, ensure_multiple_of=32, resize_method="minimal", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.beit512_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 512, 512, resize_target=None, keep_aspect_ratio=True, ensure_multiple_of=32, resize_method="minimal", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.swin384_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 384, 384, resize_target=None, keep_aspect_ratio=False, ensure_multiple_of=32, resize_method="minimal", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.swin256_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 256, 256, resize_target=None, keep_aspect_ratio=False, ensure_multiple_of=32, resize_method="minimal", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
transforms.levit_transform = Compose( [ lambda img: {"image": img / 255.0}, Resize( 224, 224, resize_target=None, keep_aspect_ratio=False, ensure_multiple_of=32, resize_method="minimal", image_interpolation_method=cv2.INTER_CUBIC, ), NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), PrepareForNet(), lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0), ] )
return transforms123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import timmimport torchimport types
import numpy as npimport torch.nn.functional as F
from .utils import forward_adapted_unflatten, make_backbone_defaultfrom timm.models.beit import gen_relative_position_indexfrom torch.utils.checkpoint import checkpointfrom typing import Optional
def forward_beit(pretrained, x): return forward_adapted_unflatten(pretrained, x, "forward_features")
def patch_embed_forward(self, x): """ Modification of timm.models.layers.patch_embed.py: PatchEmbed.forward to support arbitrary window sizes. """ x = self.proj(x) if self.flatten: x = x.flatten(2).transpose(1, 2) x = self.norm(x) return x
def _get_rel_pos_bias(self, window_size): """ Modification of timm.models.beit.py: Attention._get_rel_pos_bias to support arbitrary window sizes. """ old_height = 2 * self.window_size[0] - 1 old_width = 2 * self.window_size[1] - 1
new_height = 2 * window_size[0] - 1 new_width = 2 * window_size[1] - 1
old_relative_position_bias_table = self.relative_position_bias_table
old_num_relative_distance = self.num_relative_distance new_num_relative_distance = new_height * new_width + 3
old_sub_table = old_relative_position_bias_table[:old_num_relative_distance - 3]
old_sub_table = old_sub_table.reshape(1, old_width, old_height, -1).permute(0, 3, 1, 2) new_sub_table = F.interpolate(old_sub_table, size=(new_height, new_width), mode="bilinear") new_sub_table = new_sub_table.permute(0, 2, 3, 1).reshape(new_num_relative_distance - 3, -1)
new_relative_position_bias_table = torch.cat( [new_sub_table, old_relative_position_bias_table[old_num_relative_distance - 3:]])
key = str(window_size[1]) + "," + str(window_size[0]) if key not in self.relative_position_indices.keys(): self.relative_position_indices[key] = gen_relative_position_index(window_size)
relative_position_bias = new_relative_position_bias_table[ self.relative_position_indices[key].view(-1)].view( window_size[0] * window_size[1] + 1, window_size[0] * window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww return relative_position_bias.unsqueeze(0)
def attention_forward(self, x, resolution, shared_rel_pos_bias: Optional[torch.Tensor] = None): """ Modification of timm.models.beit.py: Attention.forward to support arbitrary window sizes. """ B, N, C = x.shape
qkv_bias = torch.cat((self.q_bias, self.k_bias, self.v_bias)) if self.q_bias is not None else None qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias) qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) # make torchscript happy (cannot use tensor as tuple)
q = q * self.scale attn = (q @ k.transpose(-2, -1))
if self.relative_position_bias_table is not None: window_size = tuple(np.array(resolution) // 16) attn = attn + self._get_rel_pos_bias(window_size) if shared_rel_pos_bias is not None: attn = attn + shared_rel_pos_bias
attn = attn.softmax(dim=-1) attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, -1) x = self.proj(x) x = self.proj_drop(x) return x
def block_forward(self, x, resolution, shared_rel_pos_bias: Optional[torch.Tensor] = None): """ Modification of timm.models.beit.py: Block.forward to support arbitrary window sizes. """ if self.gamma_1 is None: x = x + self.drop_path(self.attn(self.norm1(x), resolution, shared_rel_pos_bias=shared_rel_pos_bias)) x = x + self.drop_path(self.mlp(self.norm2(x))) else: x = x + self.drop_path(self.gamma_1 * self.attn(self.norm1(x), resolution, shared_rel_pos_bias=shared_rel_pos_bias)) x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x))) return x
def beit_forward_features(self, x): """ Modification of timm.models.beit.py: Beit.forward_features to support arbitrary window sizes. """ resolution = x.shape[2:]
x = self.patch_embed(x) x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) if self.pos_embed is not None: x = x + self.pos_embed x = self.pos_drop(x)
rel_pos_bias = self.rel_pos_bias() if self.rel_pos_bias is not None else None for blk in self.blocks: if self.grad_checkpointing and not torch.jit.is_scripting(): x = checkpoint(blk, x, shared_rel_pos_bias=rel_pos_bias) else: x = blk(x, resolution, shared_rel_pos_bias=rel_pos_bias) x = self.norm(x) return x
def _make_beit_backbone( model, features=[96, 192, 384, 768], size=[384, 384], hooks=[0, 4, 8, 11], vit_features=768, use_readout="ignore", start_index=1, start_index_readout=1,): backbone = make_backbone_default(model, features, size, hooks, vit_features, use_readout, start_index, start_index_readout)
backbone.model.patch_embed.forward = types.MethodType(patch_embed_forward, backbone.model.patch_embed) backbone.model.forward_features = types.MethodType(beit_forward_features, backbone.model)
for block in backbone.model.blocks: attn = block.attn attn._get_rel_pos_bias = types.MethodType(_get_rel_pos_bias, attn) attn.forward = types.MethodType(attention_forward, attn) attn.relative_position_indices = {}
block.forward = types.MethodType(block_forward, block)
return backbone
def _make_pretrained_beitl16_512(pretrained, use_readout="ignore", hooks=None): model = timm.create_model("beit_large_patch16_512", pretrained=pretrained)
hooks = [5, 11, 17, 23] if hooks is None else hooks
features = [256, 512, 1024, 1024]
return _make_beit_backbone( model, features=features, size=[512, 512], hooks=hooks, vit_features=1024, use_readout=use_readout, )
def _make_pretrained_beitl16_384(pretrained, use_readout="ignore", hooks=None): model = timm.create_model("beit_large_patch16_384", pretrained=pretrained)
hooks = [5, 11, 17, 23] if hooks is None else hooks return _make_beit_backbone( model, features=[256, 512, 1024, 1024], hooks=hooks, vit_features=1024, use_readout=use_readout, )
def _make_pretrained_beitb16_384(pretrained, use_readout="ignore", hooks=None): model = timm.create_model("beit_base_patch16_384", pretrained=pretrained)
hooks = [2, 5, 8, 11] if hooks is None else hooks return _make_beit_backbone( model, features=[96, 192, 384, 768], hooks=hooks, use_readout=use_readout, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import timmimport torchimport torch.nn as nnimport numpy as np
from .utils import activations, get_activation, Transpose
def forward_levit(pretrained, x): pretrained.model.forward_features(x)
layer_1 = pretrained.activations["1"] layer_2 = pretrained.activations["2"] layer_3 = pretrained.activations["3"]
layer_1 = pretrained.act_postprocess1(layer_1) layer_2 = pretrained.act_postprocess2(layer_2) layer_3 = pretrained.act_postprocess3(layer_3)
return layer_1, layer_2, layer_3
def _make_levit_backbone( model, hooks=[3, 11, 21], patch_grid=[14, 14]): pretrained = nn.Module()
pretrained.model = model pretrained.model.blocks[hooks[0]].register_forward_hook(get_activation("1")) pretrained.model.blocks[hooks[1]].register_forward_hook(get_activation("2")) pretrained.model.blocks[hooks[2]].register_forward_hook(get_activation("3"))
pretrained.activations = activations
patch_grid_size = np.array(patch_grid, dtype=int)
pretrained.act_postprocess1 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size(patch_grid_size.tolist())) ) pretrained.act_postprocess2 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size((np.ceil(patch_grid_size / 2).astype(int)).tolist())) ) pretrained.act_postprocess3 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size((np.ceil(patch_grid_size / 4).astype(int)).tolist())) )
return pretrained
class ConvTransposeNorm(nn.Sequential): """ Modification of https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/levit.py: ConvNorm such that ConvTranspose2d is used instead of Conv2d. """
def __init__( self, in_chs, out_chs, kernel_size=1, stride=1, pad=0, dilation=1, groups=1, bn_weight_init=1): super().__init__() self.add_module('c', nn.ConvTranspose2d(in_chs, out_chs, kernel_size, stride, pad, dilation, groups, bias=False)) self.add_module('bn', nn.BatchNorm2d(out_chs))
nn.init.constant_(self.bn.weight, bn_weight_init)
@torch.no_grad() def fuse(self): c, bn = self._modules.values() w = bn.weight / (bn.running_var + bn.eps) ** 0.5 w = c.weight * w[:, None, None, None] b = bn.bias - bn.running_mean * bn.weight / (bn.running_var + bn.eps) ** 0.5 m = nn.ConvTranspose2d( w.size(1), w.size(0), w.shape[2:], stride=self.c.stride, padding=self.c.padding, dilation=self.c.dilation, groups=self.c.groups) m.weight.data.copy_(w) m.bias.data.copy_(b) return m
def stem_b4_transpose(in_chs, out_chs, activation): """ Modification of https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/levit.py: stem_b16 such that ConvTranspose2d is used instead of Conv2d and stem is also reduced to the half. """ return nn.Sequential( ConvTransposeNorm(in_chs, out_chs, 3, 2, 1), activation(), ConvTransposeNorm(out_chs, out_chs // 2, 3, 2, 1), activation())
def _make_pretrained_levit_384(pretrained, hooks=None): model = timm.create_model("levit_384", pretrained=pretrained)
hooks = [3, 11, 21] if hooks == None else hooks return _make_levit_backbone( model, hooks=hooks )123456789101112131415161718192021222324252627282930313233343536373839import timm
import torch.nn as nn
from pathlib import Pathfrom .utils import activations, forward_default, get_activation
from ..external.next_vit.classification.nextvit import *
def forward_next_vit(pretrained, x): return forward_default(pretrained, x, "forward")
def _make_next_vit_backbone( model, hooks=[2, 6, 36, 39],): pretrained = nn.Module()
pretrained.model = model pretrained.model.features[hooks[0]].register_forward_hook(get_activation("1")) pretrained.model.features[hooks[1]].register_forward_hook(get_activation("2")) pretrained.model.features[hooks[2]].register_forward_hook(get_activation("3")) pretrained.model.features[hooks[3]].register_forward_hook(get_activation("4"))
pretrained.activations = activations
return pretrained
def _make_pretrained_next_vit_large_6m(hooks=None): model = timm.create_model("nextvit_large")
hooks = [2, 6, 36, 39] if hooks == None else hooks return _make_next_vit_backbone( model, hooks=hooks, )12345678910111213import timm
from .swin_common import _make_swin_backbone
def _make_pretrained_swinl12_384(pretrained, hooks=None): model = timm.create_model("swin_large_patch4_window12_384", pretrained=pretrained)
hooks = [1, 1, 17, 1] if hooks == None else hooks return _make_swin_backbone( model, hooks=hooks )12345678910111213141516171819202122232425262728293031323334import timm
from .swin_common import _make_swin_backbone
def _make_pretrained_swin2l24_384(pretrained, hooks=None): model = timm.create_model("swinv2_large_window12to24_192to384_22kft1k", pretrained=pretrained)
hooks = [1, 1, 17, 1] if hooks == None else hooks return _make_swin_backbone( model, hooks=hooks )
def _make_pretrained_swin2b24_384(pretrained, hooks=None): model = timm.create_model("swinv2_base_window12to24_192to384_22kft1k", pretrained=pretrained)
hooks = [1, 1, 17, 1] if hooks == None else hooks return _make_swin_backbone( model, hooks=hooks )
def _make_pretrained_swin2t16_256(pretrained, hooks=None): model = timm.create_model("swinv2_tiny_window16_256", pretrained=pretrained)
hooks = [1, 1, 5, 1] if hooks == None else hooks return _make_swin_backbone( model, hooks=hooks, patch_grid=[64, 64] )12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import torch
import torch.nn as nnimport numpy as np
from .utils import activations, forward_default, get_activation, Transpose
def forward_swin(pretrained, x): return forward_default(pretrained, x)
def _make_swin_backbone( model, hooks=[1, 1, 17, 1], patch_grid=[96, 96]): pretrained = nn.Module()
pretrained.model = model pretrained.model.layers[0].blocks[hooks[0]].register_forward_hook(get_activation("1")) pretrained.model.layers[1].blocks[hooks[1]].register_forward_hook(get_activation("2")) pretrained.model.layers[2].blocks[hooks[2]].register_forward_hook(get_activation("3")) pretrained.model.layers[3].blocks[hooks[3]].register_forward_hook(get_activation("4"))
pretrained.activations = activations
if hasattr(model, "patch_grid"): used_patch_grid = model.patch_grid else: used_patch_grid = patch_grid
patch_grid_size = np.array(used_patch_grid, dtype=int)
pretrained.act_postprocess1 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size(patch_grid_size.tolist())) ) pretrained.act_postprocess2 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size((patch_grid_size // 2).tolist())) ) pretrained.act_postprocess3 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size((patch_grid_size // 4).tolist())) ) pretrained.act_postprocess4 = nn.Sequential( Transpose(1, 2), nn.Unflatten(2, torch.Size((patch_grid_size // 8).tolist())) )
return pretrained123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249import torch
import torch.nn as nn
class Slice(nn.Module): def __init__(self, start_index=1): super(Slice, self).__init__() self.start_index = start_index
def forward(self, x): return x[:, self.start_index:]
class AddReadout(nn.Module): def __init__(self, start_index=1): super(AddReadout, self).__init__() self.start_index = start_index
def forward(self, x): if self.start_index == 2: readout = (x[:, 0] + x[:, 1]) / 2 else: readout = x[:, 0] return x[:, self.start_index:] + readout.unsqueeze(1)
class ProjectReadout(nn.Module): def __init__(self, in_features, start_index=1): super(ProjectReadout, self).__init__() self.start_index = start_index
self.project = nn.Sequential(nn.Linear(2 * in_features, in_features), nn.GELU())
def forward(self, x): readout = x[:, 0].unsqueeze(1).expand_as(x[:, self.start_index:]) features = torch.cat((x[:, self.start_index:], readout), -1)
return self.project(features)
class Transpose(nn.Module): def __init__(self, dim0, dim1): super(Transpose, self).__init__() self.dim0 = dim0 self.dim1 = dim1
def forward(self, x): x = x.transpose(self.dim0, self.dim1) return x
activations = {}
def get_activation(name): def hook(model, input, output): activations[name] = output
return hook
def forward_default(pretrained, x, function_name="forward_features"): exec(f"pretrained.model.{function_name}(x)")
layer_1 = pretrained.activations["1"] layer_2 = pretrained.activations["2"] layer_3 = pretrained.activations["3"] layer_4 = pretrained.activations["4"]
if hasattr(pretrained, "act_postprocess1"): layer_1 = pretrained.act_postprocess1(layer_1) if hasattr(pretrained, "act_postprocess2"): layer_2 = pretrained.act_postprocess2(layer_2) if hasattr(pretrained, "act_postprocess3"): layer_3 = pretrained.act_postprocess3(layer_3) if hasattr(pretrained, "act_postprocess4"): layer_4 = pretrained.act_postprocess4(layer_4)
return layer_1, layer_2, layer_3, layer_4
def forward_adapted_unflatten(pretrained, x, function_name="forward_features"): b, c, h, w = x.shape
exec(f"glob = pretrained.model.{function_name}(x)")
layer_1 = pretrained.activations["1"] layer_2 = pretrained.activations["2"] layer_3 = pretrained.activations["3"] layer_4 = pretrained.activations["4"]
layer_1 = pretrained.act_postprocess1[0:2](layer_1) layer_2 = pretrained.act_postprocess2[0:2](layer_2) layer_3 = pretrained.act_postprocess3[0:2](layer_3) layer_4 = pretrained.act_postprocess4[0:2](layer_4)
unflatten = nn.Sequential( nn.Unflatten( 2, torch.Size( [ h // pretrained.model.patch_size[1], w // pretrained.model.patch_size[0], ] ), ) )
if layer_1.ndim == 3: layer_1 = unflatten(layer_1) if layer_2.ndim == 3: layer_2 = unflatten(layer_2) if layer_3.ndim == 3: layer_3 = unflatten(layer_3) if layer_4.ndim == 3: layer_4 = unflatten(layer_4)
layer_1 = pretrained.act_postprocess1[3: len(pretrained.act_postprocess1)](layer_1) layer_2 = pretrained.act_postprocess2[3: len(pretrained.act_postprocess2)](layer_2) layer_3 = pretrained.act_postprocess3[3: len(pretrained.act_postprocess3)](layer_3) layer_4 = pretrained.act_postprocess4[3: len(pretrained.act_postprocess4)](layer_4)
return layer_1, layer_2, layer_3, layer_4
def get_readout_oper(vit_features, features, use_readout, start_index=1): if use_readout == "ignore": readout_oper = [Slice(start_index)] * len(features) elif use_readout == "add": readout_oper = [AddReadout(start_index)] * len(features) elif use_readout == "project": readout_oper = [ ProjectReadout(vit_features, start_index) for out_feat in features ] else: assert ( False ), "wrong operation for readout token, use_readout can be 'ignore', 'add', or 'project'"
return readout_oper
def make_backbone_default( model, features=[96, 192, 384, 768], size=[384, 384], hooks=[2, 5, 8, 11], vit_features=768, use_readout="ignore", start_index=1, start_index_readout=1,): pretrained = nn.Module()
pretrained.model = model pretrained.model.blocks[hooks[0]].register_forward_hook(get_activation("1")) pretrained.model.blocks[hooks[1]].register_forward_hook(get_activation("2")) pretrained.model.blocks[hooks[2]].register_forward_hook(get_activation("3")) pretrained.model.blocks[hooks[3]].register_forward_hook(get_activation("4"))
pretrained.activations = activations
readout_oper = get_readout_oper(vit_features, features, use_readout, start_index_readout)
# 32, 48, 136, 384 pretrained.act_postprocess1 = nn.Sequential( readout_oper[0], Transpose(1, 2), nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])), nn.Conv2d( in_channels=vit_features, out_channels=features[0], kernel_size=1, stride=1, padding=0, ), nn.ConvTranspose2d( in_channels=features[0], out_channels=features[0], kernel_size=4, stride=4, padding=0, bias=True, dilation=1, groups=1, ), )
pretrained.act_postprocess2 = nn.Sequential( readout_oper[1], Transpose(1, 2), nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])), nn.Conv2d( in_channels=vit_features, out_channels=features[1], kernel_size=1, stride=1, padding=0, ), nn.ConvTranspose2d( in_channels=features[1], out_channels=features[1], kernel_size=2, stride=2, padding=0, bias=True, dilation=1, groups=1, ), )
pretrained.act_postprocess3 = nn.Sequential( readout_oper[2], Transpose(1, 2), nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])), nn.Conv2d( in_channels=vit_features, out_channels=features[2], kernel_size=1, stride=1, padding=0, ), )
pretrained.act_postprocess4 = nn.Sequential( readout_oper[3], Transpose(1, 2), nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])), nn.Conv2d( in_channels=vit_features, out_channels=features[3], kernel_size=1, stride=1, padding=0, ), nn.Conv2d( in_channels=features[3], out_channels=features[3], kernel_size=3, stride=2, padding=1, ), )
pretrained.model.start_index = start_index pretrained.model.patch_size = [16, 16]
return pretrained123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221import torchimport torch.nn as nnimport timmimport typesimport mathimport torch.nn.functional as F
from .utils import (activations, forward_adapted_unflatten, get_activation, get_readout_oper, make_backbone_default, Transpose)
def forward_vit(pretrained, x): return forward_adapted_unflatten(pretrained, x, "forward_flex")
def _resize_pos_embed(self, posemb, gs_h, gs_w): posemb_tok, posemb_grid = ( posemb[:, : self.start_index], posemb[0, self.start_index:], )
gs_old = int(math.sqrt(len(posemb_grid)))
posemb_grid = posemb_grid.reshape(1, gs_old, gs_old, -1).permute(0, 3, 1, 2) posemb_grid = F.interpolate(posemb_grid, size=(gs_h, gs_w), mode="bilinear") posemb_grid = posemb_grid.permute(0, 2, 3, 1).reshape(1, gs_h * gs_w, -1)
posemb = torch.cat([posemb_tok, posemb_grid], dim=1)
return posemb
def forward_flex(self, x): b, c, h, w = x.shape
pos_embed = self._resize_pos_embed( self.pos_embed, h // self.patch_size[1], w // self.patch_size[0] )
B = x.shape[0]
if hasattr(self.patch_embed, "backbone"): x = self.patch_embed.backbone(x) if isinstance(x, (list, tuple)): x = x[-1] # last feature if backbone outputs list/tuple of features
x = self.patch_embed.proj(x).flatten(2).transpose(1, 2)
if getattr(self, "dist_token", None) is not None: cls_tokens = self.cls_token.expand( B, -1, -1 ) # stole cls_tokens impl from Phil Wang, thanks dist_token = self.dist_token.expand(B, -1, -1) x = torch.cat((cls_tokens, dist_token, x), dim=1) else: if self.no_embed_class: x = x + pos_embed cls_tokens = self.cls_token.expand( B, -1, -1 ) # stole cls_tokens impl from Phil Wang, thanks x = torch.cat((cls_tokens, x), dim=1)
if not self.no_embed_class: x = x + pos_embed x = self.pos_drop(x)
for blk in self.blocks: x = blk(x)
x = self.norm(x)
return x
def _make_vit_b16_backbone( model, features=[96, 192, 384, 768], size=[384, 384], hooks=[2, 5, 8, 11], vit_features=768, use_readout="ignore", start_index=1, start_index_readout=1,): pretrained = make_backbone_default(model, features, size, hooks, vit_features, use_readout, start_index, start_index_readout)
# We inject this function into the VisionTransformer instances so that # we can use it with interpolated position embeddings without modifying the library source. pretrained.model.forward_flex = types.MethodType(forward_flex, pretrained.model) pretrained.model._resize_pos_embed = types.MethodType( _resize_pos_embed, pretrained.model )
return pretrained
def _make_pretrained_vitl16_384(pretrained, use_readout="ignore", hooks=None): model = timm.create_model("vit_large_patch16_384", pretrained=pretrained)
hooks = [5, 11, 17, 23] if hooks == None else hooks return _make_vit_b16_backbone( model, features=[256, 512, 1024, 1024], hooks=hooks, vit_features=1024, use_readout=use_readout, )
def _make_pretrained_vitb16_384(pretrained, use_readout="ignore", hooks=None): model = timm.create_model("vit_base_patch16_384", pretrained=pretrained)
hooks = [2, 5, 8, 11] if hooks == None else hooks return _make_vit_b16_backbone( model, features=[96, 192, 384, 768], hooks=hooks, use_readout=use_readout )
def _make_vit_b_rn50_backbone( model, features=[256, 512, 768, 768], size=[384, 384], hooks=[0, 1, 8, 11], vit_features=768, patch_size=[16, 16], number_stages=2, use_vit_only=False, use_readout="ignore", start_index=1,): pretrained = nn.Module()
pretrained.model = model
used_number_stages = 0 if use_vit_only else number_stages for s in range(used_number_stages): pretrained.model.patch_embed.backbone.stages[s].register_forward_hook( get_activation(str(s + 1)) ) for s in range(used_number_stages, 4): pretrained.model.blocks[hooks[s]].register_forward_hook(get_activation(str(s + 1)))
pretrained.activations = activations
readout_oper = get_readout_oper(vit_features, features, use_readout, start_index)
for s in range(used_number_stages): value = nn.Sequential(nn.Identity(), nn.Identity(), nn.Identity()) exec(f"pretrained.act_postprocess{s + 1}=value") for s in range(used_number_stages, 4): if s < number_stages: final_layer = nn.ConvTranspose2d( in_channels=features[s], out_channels=features[s], kernel_size=4 // (2 ** s), stride=4 // (2 ** s), padding=0, bias=True, dilation=1, groups=1, ) elif s > number_stages: final_layer = nn.Conv2d( in_channels=features[3], out_channels=features[3], kernel_size=3, stride=2, padding=1, ) else: final_layer = None
layers = [ readout_oper[s], Transpose(1, 2), nn.Unflatten(2, torch.Size([size[0] // 16, size[1] // 16])), nn.Conv2d( in_channels=vit_features, out_channels=features[s], kernel_size=1, stride=1, padding=0, ), ] if final_layer is not None: layers.append(final_layer)
value = nn.Sequential(*layers) exec(f"pretrained.act_postprocess{s + 1}=value")
pretrained.model.start_index = start_index pretrained.model.patch_size = patch_size
# We inject this function into the VisionTransformer instances so that # we can use it with interpolated position embeddings without modifying the library source. pretrained.model.forward_flex = types.MethodType(forward_flex, pretrained.model)
# We inject this function into the VisionTransformer instances so that # we can use it with interpolated position embeddings without modifying the library source. pretrained.model._resize_pos_embed = types.MethodType( _resize_pos_embed, pretrained.model )
return pretrained
def _make_pretrained_vitb_rn50_384( pretrained, use_readout="ignore", hooks=None, use_vit_only=False): model = timm.create_model("vit_base_resnet50_384", pretrained=pretrained)
hooks = [0, 1, 8, 11] if hooks == None else hooks return _make_vit_b_rn50_backbone( model, features=[256, 512, 768, 768], size=[384, 384], hooks=hooks, use_vit_only=use_vit_only, use_readout=use_readout, )12345678910111213141516import torch
class BaseModel(torch.nn.Module): def load(self, path): """Load model from file.
Args: path (str): file path """ parameters = torch.load(path, map_location=torch.device('cpu'))
if "optimizer" in parameters: parameters = parameters["model"]
self.load_state_dict(parameters)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439import torchimport torch.nn as nn
from .backbones.beit import ( _make_pretrained_beitl16_512, _make_pretrained_beitl16_384, _make_pretrained_beitb16_384, forward_beit,)from .backbones.swin_common import ( forward_swin,)from .backbones.swin2 import ( _make_pretrained_swin2l24_384, _make_pretrained_swin2b24_384, _make_pretrained_swin2t16_256,)from .backbones.swin import ( _make_pretrained_swinl12_384,)from .backbones.levit import ( _make_pretrained_levit_384, forward_levit,)from .backbones.vit import ( _make_pretrained_vitb_rn50_384, _make_pretrained_vitl16_384, _make_pretrained_vitb16_384, forward_vit,)
def _make_encoder(backbone, features, use_pretrained, groups=1, expand=False, exportable=True, hooks=None, use_vit_only=False, use_readout="ignore", in_features=[96, 256, 512, 1024]): if backbone == "beitl16_512": pretrained = _make_pretrained_beitl16_512( use_pretrained, hooks=hooks, use_readout=use_readout ) scratch = _make_scratch( [256, 512, 1024, 1024], features, groups=groups, expand=expand ) # BEiT_512-L (backbone) elif backbone == "beitl16_384": pretrained = _make_pretrained_beitl16_384( use_pretrained, hooks=hooks, use_readout=use_readout ) scratch = _make_scratch( [256, 512, 1024, 1024], features, groups=groups, expand=expand ) # BEiT_384-L (backbone) elif backbone == "beitb16_384": pretrained = _make_pretrained_beitb16_384( use_pretrained, hooks=hooks, use_readout=use_readout ) scratch = _make_scratch( [96, 192, 384, 768], features, groups=groups, expand=expand ) # BEiT_384-B (backbone) elif backbone == "swin2l24_384": pretrained = _make_pretrained_swin2l24_384( use_pretrained, hooks=hooks ) scratch = _make_scratch( [192, 384, 768, 1536], features, groups=groups, expand=expand ) # Swin2-L/12to24 (backbone) elif backbone == "swin2b24_384": pretrained = _make_pretrained_swin2b24_384( use_pretrained, hooks=hooks ) scratch = _make_scratch( [128, 256, 512, 1024], features, groups=groups, expand=expand ) # Swin2-B/12to24 (backbone) elif backbone == "swin2t16_256": pretrained = _make_pretrained_swin2t16_256( use_pretrained, hooks=hooks ) scratch = _make_scratch( [96, 192, 384, 768], features, groups=groups, expand=expand ) # Swin2-T/16 (backbone) elif backbone == "swinl12_384": pretrained = _make_pretrained_swinl12_384( use_pretrained, hooks=hooks ) scratch = _make_scratch( [192, 384, 768, 1536], features, groups=groups, expand=expand ) # Swin-L/12 (backbone) elif backbone == "next_vit_large_6m": from .backbones.next_vit import _make_pretrained_next_vit_large_6m pretrained = _make_pretrained_next_vit_large_6m(hooks=hooks) scratch = _make_scratch( in_features, features, groups=groups, expand=expand ) # Next-ViT-L on ImageNet-1K-6M (backbone) elif backbone == "levit_384": pretrained = _make_pretrained_levit_384( use_pretrained, hooks=hooks ) scratch = _make_scratch( [384, 512, 768], features, groups=groups, expand=expand ) # LeViT 384 (backbone) elif backbone == "vitl16_384": pretrained = _make_pretrained_vitl16_384( use_pretrained, hooks=hooks, use_readout=use_readout ) scratch = _make_scratch( [256, 512, 1024, 1024], features, groups=groups, expand=expand ) # ViT-L/16 - 85.0% Top1 (backbone) elif backbone == "vitb_rn50_384": pretrained = _make_pretrained_vitb_rn50_384( use_pretrained, hooks=hooks, use_vit_only=use_vit_only, use_readout=use_readout, ) scratch = _make_scratch( [256, 512, 768, 768], features, groups=groups, expand=expand ) # ViT-H/16 - 85.0% Top1 (backbone) elif backbone == "vitb16_384": pretrained = _make_pretrained_vitb16_384( use_pretrained, hooks=hooks, use_readout=use_readout ) scratch = _make_scratch( [96, 192, 384, 768], features, groups=groups, expand=expand ) # ViT-B/16 - 84.6% Top1 (backbone) elif backbone == "resnext101_wsl": pretrained = _make_pretrained_resnext101_wsl(use_pretrained) scratch = _make_scratch([256, 512, 1024, 2048], features, groups=groups, expand=expand) # efficientnet_lite3 elif backbone == "efficientnet_lite3": pretrained = _make_pretrained_efficientnet_lite3(use_pretrained, exportable=exportable) scratch = _make_scratch([32, 48, 136, 384], features, groups=groups, expand=expand) # efficientnet_lite3 else: print(f"Backbone '{backbone}' not implemented") assert False return pretrained, scratch
def _make_scratch(in_shape, out_shape, groups=1, expand=False): scratch = nn.Module()
out_shape1 = out_shape out_shape2 = out_shape out_shape3 = out_shape if len(in_shape) >= 4: out_shape4 = out_shape
if expand: out_shape1 = out_shape out_shape2 = out_shape*2 out_shape3 = out_shape*4 if len(in_shape) >= 4: out_shape4 = out_shape*8
scratch.layer1_rn = nn.Conv2d( in_shape[0], out_shape1, kernel_size=3, stride=1, padding=1, bias=False, groups=groups ) scratch.layer2_rn = nn.Conv2d( in_shape[1], out_shape2, kernel_size=3, stride=1, padding=1, bias=False, groups=groups ) scratch.layer3_rn = nn.Conv2d( in_shape[2], out_shape3, kernel_size=3, stride=1, padding=1, bias=False, groups=groups ) if len(in_shape) >= 4: scratch.layer4_rn = nn.Conv2d( in_shape[3], out_shape4, kernel_size=3, stride=1, padding=1, bias=False, groups=groups )
return scratch
def _make_pretrained_efficientnet_lite3(use_pretrained, exportable=False): efficientnet = torch.hub.load( "rwightman/gen-efficientnet-pytorch", "tf_efficientnet_lite3", pretrained=use_pretrained, exportable=exportable ) return _make_efficientnet_backbone(efficientnet)
def _make_efficientnet_backbone(effnet): pretrained = nn.Module()
pretrained.layer1 = nn.Sequential( effnet.conv_stem, effnet.bn1, effnet.act1, *effnet.blocks[0:2] ) pretrained.layer2 = nn.Sequential(*effnet.blocks[2:3]) pretrained.layer3 = nn.Sequential(*effnet.blocks[3:5]) pretrained.layer4 = nn.Sequential(*effnet.blocks[5:9])
return pretrained
def _make_resnet_backbone(resnet): pretrained = nn.Module() pretrained.layer1 = nn.Sequential( resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool, resnet.layer1 )
pretrained.layer2 = resnet.layer2 pretrained.layer3 = resnet.layer3 pretrained.layer4 = resnet.layer4
return pretrained
def _make_pretrained_resnext101_wsl(use_pretrained): resnet = torch.hub.load("facebookresearch/WSL-Images", "resnext101_32x8d_wsl") return _make_resnet_backbone(resnet)
class Interpolate(nn.Module): """Interpolation module. """
def __init__(self, scale_factor, mode, align_corners=False): """Init.
Args: scale_factor (float): scaling mode (str): interpolation mode """ super(Interpolate, self).__init__()
self.interp = nn.functional.interpolate self.scale_factor = scale_factor self.mode = mode self.align_corners = align_corners
def forward(self, x): """Forward pass.
Args: x (tensor): input
Returns: tensor: interpolated data """
x = self.interp( x, scale_factor=self.scale_factor, mode=self.mode, align_corners=self.align_corners )
return x
class ResidualConvUnit(nn.Module): """Residual convolution module. """
def __init__(self, features): """Init.
Args: features (int): number of features """ super().__init__()
self.conv1 = nn.Conv2d( features, features, kernel_size=3, stride=1, padding=1, bias=True )
self.conv2 = nn.Conv2d( features, features, kernel_size=3, stride=1, padding=1, bias=True )
self.relu = nn.ReLU(inplace=True)
def forward(self, x): """Forward pass.
Args: x (tensor): input
Returns: tensor: output """ out = self.relu(x) out = self.conv1(out) out = self.relu(out) out = self.conv2(out)
return out + x
class FeatureFusionBlock(nn.Module): """Feature fusion block. """
def __init__(self, features): """Init.
Args: features (int): number of features """ super(FeatureFusionBlock, self).__init__()
self.resConfUnit1 = ResidualConvUnit(features) self.resConfUnit2 = ResidualConvUnit(features)
def forward(self, *xs): """Forward pass.
Returns: tensor: output """ output = xs[0]
if len(xs) == 2: output += self.resConfUnit1(xs[1])
output = self.resConfUnit2(output)
output = nn.functional.interpolate( output, scale_factor=2, mode="bilinear", align_corners=True )
return output
class ResidualConvUnit_custom(nn.Module): """Residual convolution module. """
def __init__(self, features, activation, bn): """Init.
Args: features (int): number of features """ super().__init__()
self.bn = bn
self.groups=1
self.conv1 = nn.Conv2d( features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups ) self.conv2 = nn.Conv2d( features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups )
if self.bn==True: self.bn1 = nn.BatchNorm2d(features) self.bn2 = nn.BatchNorm2d(features)
self.activation = activation
self.skip_add = nn.quantized.FloatFunctional()
def forward(self, x): """Forward pass.
Args: x (tensor): input
Returns: tensor: output """ out = self.activation(x) out = self.conv1(out) if self.bn==True: out = self.bn1(out) out = self.activation(out) out = self.conv2(out) if self.bn==True: out = self.bn2(out)
if self.groups > 1: out = self.conv_merge(out)
return self.skip_add.add(out, x)
# return out + x
class FeatureFusionBlock_custom(nn.Module): """Feature fusion block. """
def __init__(self, features, activation, deconv=False, bn=False, expand=False, align_corners=True, size=None): """Init.
Args: features (int): number of features """ super(FeatureFusionBlock_custom, self).__init__()
self.deconv = deconv self.align_corners = align_corners
self.groups=1
self.expand = expand out_features = features if self.expand==True: out_features = features//2 self.out_conv = nn.Conv2d(features, out_features, kernel_size=1, stride=1, padding=0, bias=True, groups=1)
self.resConfUnit1 = ResidualConvUnit_custom(features, activation, bn) self.resConfUnit2 = ResidualConvUnit_custom(features, activation, bn) self.skip_add = nn.quantized.FloatFunctional()
self.size=size
def forward(self, *xs, size=None): """Forward pass.
Returns: tensor: output """ output = xs[0]
if len(xs) == 2: res = self.resConfUnit1(xs[1]) output = self.skip_add.add(output, res) # output += res
output = self.resConfUnit2(output)
if (size is None) and (self.size is None): modifier = {"scale_factor": 2} elif size is None: modifier = {"size": self.size} else: modifier = {"size": size}
output = nn.functional.interpolate( output, **modifier, mode="bilinear", align_corners=self.align_corners )
output = self.out_conv(output)
return output
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import torchimport torch.nn as nn
from .base_model import BaseModelfrom .blocks import ( FeatureFusionBlock_custom, Interpolate, _make_encoder, forward_beit, forward_swin, forward_levit, forward_vit,)from .backbones.levit import stem_b4_transposefrom timm.models.layers import get_act_layer
def _make_fusion_block(features, use_bn, size = None): return FeatureFusionBlock_custom( features, nn.ReLU(False), deconv=False, bn=use_bn, expand=False, align_corners=True, size=size, )
class DPT(BaseModel): def __init__( self, head, features=256, backbone="vitb_rn50_384", readout="project", channels_last=False, use_bn=False, **kwargs ):
super(DPT, self).__init__()
self.channels_last = channels_last
# For the Swin, Swin 2, LeViT and Next-ViT Transformers, the hierarchical architectures prevent setting the # hooks freely. Instead, the hooks have to be chosen according to the ranges specified in the comments. hooks = { "beitl16_512": [5, 11, 17, 23], "beitl16_384": [5, 11, 17, 23], "beitb16_384": [2, 5, 8, 11], "swin2l24_384": [1, 1, 17, 1], # Allowed ranges: [0, 1], [0, 1], [ 0, 17], [ 0, 1] "swin2b24_384": [1, 1, 17, 1], # [0, 1], [0, 1], [ 0, 17], [ 0, 1] "swin2t16_256": [1, 1, 5, 1], # [0, 1], [0, 1], [ 0, 5], [ 0, 1] "swinl12_384": [1, 1, 17, 1], # [0, 1], [0, 1], [ 0, 17], [ 0, 1] "next_vit_large_6m": [2, 6, 36, 39], # [0, 2], [3, 6], [ 7, 36], [37, 39] "levit_384": [3, 11, 21], # [0, 3], [6, 11], [14, 21] "vitb_rn50_384": [0, 1, 8, 11], "vitb16_384": [2, 5, 8, 11], "vitl16_384": [5, 11, 17, 23], }[backbone]
if "next_vit" in backbone: in_features = { "next_vit_large_6m": [96, 256, 512, 1024], }[backbone] else: in_features = None
# Instantiate backbone and reassemble blocks self.pretrained, self.scratch = _make_encoder( backbone, features, False, # Set to true of you want to train from scratch, uses ImageNet weights groups=1, expand=False, exportable=False, hooks=hooks, use_readout=readout, in_features=in_features, )
self.number_layers = len(hooks) if hooks is not None else 4 size_refinenet3 = None self.scratch.stem_transpose = None
if "beit" in backbone: self.forward_transformer = forward_beit elif "swin" in backbone: self.forward_transformer = forward_swin elif "next_vit" in backbone: from .backbones.next_vit import forward_next_vit self.forward_transformer = forward_next_vit elif "levit" in backbone: self.forward_transformer = forward_levit size_refinenet3 = 7 self.scratch.stem_transpose = stem_b4_transpose(256, 128, get_act_layer("hard_swish")) else: self.forward_transformer = forward_vit
self.scratch.refinenet1 = _make_fusion_block(features, use_bn) self.scratch.refinenet2 = _make_fusion_block(features, use_bn) self.scratch.refinenet3 = _make_fusion_block(features, use_bn, size_refinenet3) if self.number_layers >= 4: self.scratch.refinenet4 = _make_fusion_block(features, use_bn)
self.scratch.output_conv = head
def forward(self, x): if self.channels_last == True: x.contiguous(memory_format=torch.channels_last)
layers = self.forward_transformer(self.pretrained, x) if self.number_layers == 3: layer_1, layer_2, layer_3 = layers else: layer_1, layer_2, layer_3, layer_4 = layers
layer_1_rn = self.scratch.layer1_rn(layer_1) layer_2_rn = self.scratch.layer2_rn(layer_2) layer_3_rn = self.scratch.layer3_rn(layer_3) if self.number_layers >= 4: layer_4_rn = self.scratch.layer4_rn(layer_4)
if self.number_layers == 3: path_3 = self.scratch.refinenet3(layer_3_rn, size=layer_2_rn.shape[2:]) else: path_4 = self.scratch.refinenet4(layer_4_rn, size=layer_3_rn.shape[2:]) path_3 = self.scratch.refinenet3(path_4, layer_3_rn, size=layer_2_rn.shape[2:]) path_2 = self.scratch.refinenet2(path_3, layer_2_rn, size=layer_1_rn.shape[2:]) path_1 = self.scratch.refinenet1(path_2, layer_1_rn)
if self.scratch.stem_transpose is not None: path_1 = self.scratch.stem_transpose(path_1)
out = self.scratch.output_conv(path_1)
return out
class DPTDepthModel(DPT): def __init__(self, path=None, non_negative=True, **kwargs): features = kwargs["features"] if "features" in kwargs else 256 head_features_1 = kwargs["head_features_1"] if "head_features_1" in kwargs else features head_features_2 = kwargs["head_features_2"] if "head_features_2" in kwargs else 32 kwargs.pop("head_features_1", None) kwargs.pop("head_features_2", None)
head = nn.Sequential( nn.Conv2d(head_features_1, head_features_1 // 2, kernel_size=3, stride=1, padding=1), Interpolate(scale_factor=2, mode="bilinear", align_corners=True), nn.Conv2d(head_features_1 // 2, head_features_2, kernel_size=3, stride=1, padding=1), nn.ReLU(True), nn.Conv2d(head_features_2, 1, kernel_size=1, stride=1, padding=0), nn.ReLU(True) if non_negative else nn.Identity(), nn.Identity(), )
super().__init__(head, **kwargs)
if path is not None: self.load(path)
def forward(self, x): return super().forward(x).squeeze(dim=1)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576"""MidashNet: Network for monocular depth estimation trained by mixing several datasets.This file contains code that is adapted fromhttps://github.com/thomasjpfan/pytorch_refinenet/blob/master/pytorch_refinenet/refinenet/refinenet_4cascade.py"""import torchimport torch.nn as nn
from .base_model import BaseModelfrom .blocks import FeatureFusionBlock, Interpolate, _make_encoder
class MidasNet(BaseModel): """Network for monocular depth estimation. """
def __init__(self, path=None, features=256, non_negative=True): """Init.
Args: path (str, optional): Path to saved model. Defaults to None. features (int, optional): Number of features. Defaults to 256. backbone (str, optional): Backbone network for encoder. Defaults to resnet50 """ print("Loading weights: ", path)
super(MidasNet, self).__init__()
use_pretrained = False if path is None else True
self.pretrained, self.scratch = _make_encoder(backbone="resnext101_wsl", features=features, use_pretrained=use_pretrained)
self.scratch.refinenet4 = FeatureFusionBlock(features) self.scratch.refinenet3 = FeatureFusionBlock(features) self.scratch.refinenet2 = FeatureFusionBlock(features) self.scratch.refinenet1 = FeatureFusionBlock(features)
self.scratch.output_conv = nn.Sequential( nn.Conv2d(features, 128, kernel_size=3, stride=1, padding=1), Interpolate(scale_factor=2, mode="bilinear"), nn.Conv2d(128, 32, kernel_size=3, stride=1, padding=1), nn.ReLU(True), nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0), nn.ReLU(True) if non_negative else nn.Identity(), )
if path: self.load(path)
def forward(self, x): """Forward pass.
Args: x (tensor): input data (image)
Returns: tensor: depth """
layer_1 = self.pretrained.layer1(x) layer_2 = self.pretrained.layer2(layer_1) layer_3 = self.pretrained.layer3(layer_2) layer_4 = self.pretrained.layer4(layer_3)
layer_1_rn = self.scratch.layer1_rn(layer_1) layer_2_rn = self.scratch.layer2_rn(layer_2) layer_3_rn = self.scratch.layer3_rn(layer_3) layer_4_rn = self.scratch.layer4_rn(layer_4)
path_4 = self.scratch.refinenet4(layer_4_rn) path_3 = self.scratch.refinenet3(path_4, layer_3_rn) path_2 = self.scratch.refinenet2(path_3, layer_2_rn) path_1 = self.scratch.refinenet1(path_2, layer_1_rn)
out = self.scratch.output_conv(path_1)
return torch.squeeze(out, dim=1)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128"""MidashNet: Network for monocular depth estimation trained by mixing several datasets.This file contains code that is adapted fromhttps://github.com/thomasjpfan/pytorch_refinenet/blob/master/pytorch_refinenet/refinenet/refinenet_4cascade.py"""import torchimport torch.nn as nn
from .base_model import BaseModelfrom .blocks import FeatureFusionBlock, FeatureFusionBlock_custom, Interpolate, _make_encoder
class MidasNet_small(BaseModel): """Network for monocular depth estimation. """
def __init__(self, path=None, features=64, backbone="efficientnet_lite3", non_negative=True, exportable=True, channels_last=False, align_corners=True, blocks={'expand': True}): """Init.
Args: path (str, optional): Path to saved model. Defaults to None. features (int, optional): Number of features. Defaults to 256. backbone (str, optional): Backbone network for encoder. Defaults to resnet50 """ print("Loading weights: ", path)
super(MidasNet_small, self).__init__()
use_pretrained = False if path else True self.channels_last = channels_last self.blocks = blocks self.backbone = backbone
self.groups = 1
features1=features features2=features features3=features features4=features self.expand = False if "expand" in self.blocks and self.blocks['expand'] == True: self.expand = True features1=features features2=features*2 features3=features*4 features4=features*8
self.pretrained, self.scratch = _make_encoder(self.backbone, features, use_pretrained, groups=self.groups, expand=self.expand, exportable=exportable) self.scratch.activation = nn.ReLU(False)
self.scratch.refinenet4 = FeatureFusionBlock_custom(features4, self.scratch.activation, deconv=False, bn=False, expand=self.expand, align_corners=align_corners) self.scratch.refinenet3 = FeatureFusionBlock_custom(features3, self.scratch.activation, deconv=False, bn=False, expand=self.expand, align_corners=align_corners) self.scratch.refinenet2 = FeatureFusionBlock_custom(features2, self.scratch.activation, deconv=False, bn=False, expand=self.expand, align_corners=align_corners) self.scratch.refinenet1 = FeatureFusionBlock_custom(features1, self.scratch.activation, deconv=False, bn=False, align_corners=align_corners)
self.scratch.output_conv = nn.Sequential( nn.Conv2d(features, features//2, kernel_size=3, stride=1, padding=1, groups=self.groups), Interpolate(scale_factor=2, mode="bilinear"), nn.Conv2d(features//2, 32, kernel_size=3, stride=1, padding=1), self.scratch.activation, nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0), nn.ReLU(True) if non_negative else nn.Identity(), nn.Identity(), ) if path: self.load(path)
def forward(self, x): """Forward pass.
Args: x (tensor): input data (image)
Returns: tensor: depth """ if self.channels_last==True: print("self.channels_last = ", self.channels_last) x.contiguous(memory_format=torch.channels_last)
layer_1 = self.pretrained.layer1(x) layer_2 = self.pretrained.layer2(layer_1) layer_3 = self.pretrained.layer3(layer_2) layer_4 = self.pretrained.layer4(layer_3) layer_1_rn = self.scratch.layer1_rn(layer_1) layer_2_rn = self.scratch.layer2_rn(layer_2) layer_3_rn = self.scratch.layer3_rn(layer_3) layer_4_rn = self.scratch.layer4_rn(layer_4)
path_4 = self.scratch.refinenet4(layer_4_rn) path_3 = self.scratch.refinenet3(path_4, layer_3_rn) path_2 = self.scratch.refinenet2(path_3, layer_2_rn) path_1 = self.scratch.refinenet1(path_2, layer_1_rn) out = self.scratch.output_conv(path_1)
return torch.squeeze(out, dim=1)
def fuse_model(m): prev_previous_type = nn.Identity() prev_previous_name = '' previous_type = nn.Identity() previous_name = '' for name, module in m.named_modules(): if prev_previous_type == nn.Conv2d and previous_type == nn.BatchNorm2d and type(module) == nn.ReLU: # print("FUSED ", prev_previous_name, previous_name, name) torch.quantization.fuse_modules(m, [prev_previous_name, previous_name, name], inplace=True) elif prev_previous_type == nn.Conv2d and previous_type == nn.BatchNorm2d: # print("FUSED ", prev_previous_name, previous_name) torch.quantization.fuse_modules(m, [prev_previous_name, previous_name], inplace=True) # elif previous_type == nn.Conv2d and type(module) == nn.ReLU: # print("FUSED ", previous_name, name) # torch.quantization.fuse_modules(m, [previous_name, name], inplace=True)
prev_previous_type = previous_type prev_previous_name = previous_name previous_type = type(module) previous_name = name123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242import cv2import torch
from midas.dpt_depth import DPTDepthModelfrom midas.midas_net import MidasNetfrom midas.midas_net_custom import MidasNet_smallfrom midas.transforms import Resize, NormalizeImage, PrepareForNet
from torchvision.transforms import Compose
default_models = { "dpt_beit_large_512": "weights/dpt_beit_large_512.pt", "dpt_beit_large_384": "weights/dpt_beit_large_384.pt", "dpt_beit_base_384": "weights/dpt_beit_base_384.pt", "dpt_swin2_large_384": "weights/dpt_swin2_large_384.pt", "dpt_swin2_base_384": "weights/dpt_swin2_base_384.pt", "dpt_swin2_tiny_256": "weights/dpt_swin2_tiny_256.pt", "dpt_swin_large_384": "weights/dpt_swin_large_384.pt", "dpt_next_vit_large_384": "weights/dpt_next_vit_large_384.pt", "dpt_levit_224": "weights/dpt_levit_224.pt", "dpt_large_384": "weights/dpt_large_384.pt", "dpt_hybrid_384": "weights/dpt_hybrid_384.pt", "midas_v21_384": "weights/midas_v21_384.pt", "midas_v21_small_256": "weights/midas_v21_small_256.pt", "openvino_midas_v21_small_256": "weights/openvino_midas_v21_small_256.xml",}
def load_model(device, model_path, model_type="dpt_large_384", optimize=True, height=None, square=False): """Load the specified network.
Args: device (device): the torch device used model_path (str): path to saved model model_type (str): the type of the model to be loaded optimize (bool): optimize the model to half-integer on CUDA? height (int): inference encoder image height square (bool): resize to a square resolution?
Returns: The loaded network, the transform which prepares images as input to the network and the dimensions of the network input """ if "openvino" in model_type: from openvino.runtime import Core
keep_aspect_ratio = not square
if model_type == "dpt_beit_large_512": model = DPTDepthModel( path=model_path, backbone="beitl16_512", non_negative=True, ) net_w, net_h = 512, 512 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_beit_large_384": model = DPTDepthModel( path=model_path, backbone="beitl16_384", non_negative=True, ) net_w, net_h = 384, 384 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_beit_base_384": model = DPTDepthModel( path=model_path, backbone="beitb16_384", non_negative=True, ) net_w, net_h = 384, 384 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_swin2_large_384": model = DPTDepthModel( path=model_path, backbone="swin2l24_384", non_negative=True, ) net_w, net_h = 384, 384 keep_aspect_ratio = False resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_swin2_base_384": model = DPTDepthModel( path=model_path, backbone="swin2b24_384", non_negative=True, ) net_w, net_h = 384, 384 keep_aspect_ratio = False resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_swin2_tiny_256": model = DPTDepthModel( path=model_path, backbone="swin2t16_256", non_negative=True, ) net_w, net_h = 256, 256 keep_aspect_ratio = False resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_swin_large_384": model = DPTDepthModel( path=model_path, backbone="swinl12_384", non_negative=True, ) net_w, net_h = 384, 384 keep_aspect_ratio = False resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_next_vit_large_384": model = DPTDepthModel( path=model_path, backbone="next_vit_large_6m", non_negative=True, ) net_w, net_h = 384, 384 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
# We change the notation from dpt_levit_224 (MiDaS notation) to levit_384 (timm notation) here, where the 224 refers # to the resolution 224x224 used by LeViT and 384 is the first entry of the embed_dim, see _cfg and model_cfgs of # https://github.com/rwightman/pytorch-image-models/blob/main/timm/models/levit.py # (commit id: 927f031293a30afb940fff0bee34b85d9c059b0e) elif model_type == "dpt_levit_224": model = DPTDepthModel( path=model_path, backbone="levit_384", non_negative=True, head_features_1=64, head_features_2=8, ) net_w, net_h = 224, 224 keep_aspect_ratio = False resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_large_384": model = DPTDepthModel( path=model_path, backbone="vitl16_384", non_negative=True, ) net_w, net_h = 384, 384 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_hybrid_384": model = DPTDepthModel( path=model_path, backbone="vitb_rn50_384", non_negative=True, ) net_w, net_h = 384, 384 resize_mode = "minimal" normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "midas_v21_384": model = MidasNet(model_path, non_negative=True) net_w, net_h = 384, 384 resize_mode = "upper_bound" normalization = NormalizeImage( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] )
elif model_type == "midas_v21_small_256": model = MidasNet_small(model_path, features=64, backbone="efficientnet_lite3", exportable=True, non_negative=True, blocks={'expand': True}) net_w, net_h = 256, 256 resize_mode = "upper_bound" normalization = NormalizeImage( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] )
elif model_type == "openvino_midas_v21_small_256": ie = Core() uncompiled_model = ie.read_model(model=model_path) model = ie.compile_model(uncompiled_model, "CPU") net_w, net_h = 256, 256 resize_mode = "upper_bound" normalization = NormalizeImage( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] )
else: print(f"model_type '{model_type}' not implemented, use: --model_type large") assert False
if not "openvino" in model_type: print("Model loaded, number of parameters = {:.0f}M".format(sum(p.numel() for p in model.parameters()) / 1e6)) else: print("Model loaded, optimized with OpenVINO")
if "openvino" in model_type: keep_aspect_ratio = False
if height is not None: net_w, net_h = height, height
transform = Compose( [ Resize( net_w, net_h, resize_target=None, keep_aspect_ratio=keep_aspect_ratio, ensure_multiple_of=32, resize_method=resize_mode, image_interpolation_method=cv2.INTER_CUBIC, ), normalization, PrepareForNet(), ] )
if not "openvino" in model_type: model.eval()
if optimize and (device == torch.device("cuda")): if not "openvino" in model_type: model = model.to(memory_format=torch.channels_last) model = model.half() else: print("Error: OpenVINO models are already optimized. No optimization to half-float possible.") exit()
if not "openvino" in model_type: model.to(device)
return model, transform, net_w, net_h123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234import numpy as npimport cv2import math
def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA): """Rezise the sample to ensure the given size. Keeps aspect ratio.
Args: sample (dict): sample size (tuple): image size
Returns: tuple: new size """ shape = list(sample["disparity"].shape)
if shape[0] >= size[0] and shape[1] >= size[1]: return sample
scale = [0, 0] scale[0] = size[0] / shape[0] scale[1] = size[1] / shape[1]
scale = max(scale)
shape[0] = math.ceil(scale * shape[0]) shape[1] = math.ceil(scale * shape[1])
# resize sample["image"] = cv2.resize( sample["image"], tuple(shape[::-1]), interpolation=image_interpolation_method )
sample["disparity"] = cv2.resize( sample["disparity"], tuple(shape[::-1]), interpolation=cv2.INTER_NEAREST ) sample["mask"] = cv2.resize( sample["mask"].astype(np.float32), tuple(shape[::-1]), interpolation=cv2.INTER_NEAREST, ) sample["mask"] = sample["mask"].astype(bool)
return tuple(shape)
class Resize(object): """Resize sample to given size (width, height). """
def __init__( self, width, height, resize_target=True, keep_aspect_ratio=False, ensure_multiple_of=1, resize_method="lower_bound", image_interpolation_method=cv2.INTER_AREA, ): """Init.
Args: width (int): desired output width height (int): desired output height resize_target (bool, optional): True: Resize the full sample (image, mask, target). False: Resize image only. Defaults to True. keep_aspect_ratio (bool, optional): True: Keep the aspect ratio of the input sample. Output sample might not have the given width and height, and resize behaviour depends on the parameter 'resize_method'. Defaults to False. ensure_multiple_of (int, optional): Output width and height is constrained to be multiple of this parameter. Defaults to 1. resize_method (str, optional): "lower_bound": Output will be at least as large as the given size. "upper_bound": Output will be at max as large as the given size. (Output size might be smaller than given size.) "minimal": Scale as least as possible. (Output size might be smaller than given size.) Defaults to "lower_bound". """ self.__width = width self.__height = height
self.__resize_target = resize_target self.__keep_aspect_ratio = keep_aspect_ratio self.__multiple_of = ensure_multiple_of self.__resize_method = resize_method self.__image_interpolation_method = image_interpolation_method
def constrain_to_multiple_of(self, x, min_val=0, max_val=None): y = (np.round(x / self.__multiple_of) * self.__multiple_of).astype(int)
if max_val is not None and y > max_val: y = (np.floor(x / self.__multiple_of) * self.__multiple_of).astype(int)
if y < min_val: y = (np.ceil(x / self.__multiple_of) * self.__multiple_of).astype(int)
return y
def get_size(self, width, height): # determine new height and width scale_height = self.__height / height scale_width = self.__width / width
if self.__keep_aspect_ratio: if self.__resize_method == "lower_bound": # scale such that output size is lower bound if scale_width > scale_height: # fit width scale_height = scale_width else: # fit height scale_width = scale_height elif self.__resize_method == "upper_bound": # scale such that output size is upper bound if scale_width < scale_height: # fit width scale_height = scale_width else: # fit height scale_width = scale_height elif self.__resize_method == "minimal": # scale as least as possbile if abs(1 - scale_width) < abs(1 - scale_height): # fit width scale_height = scale_width else: # fit height scale_width = scale_height else: raise ValueError( f"resize_method {self.__resize_method} not implemented" )
if self.__resize_method == "lower_bound": new_height = self.constrain_to_multiple_of( scale_height * height, min_val=self.__height ) new_width = self.constrain_to_multiple_of( scale_width * width, min_val=self.__width ) elif self.__resize_method == "upper_bound": new_height = self.constrain_to_multiple_of( scale_height * height, max_val=self.__height ) new_width = self.constrain_to_multiple_of( scale_width * width, max_val=self.__width ) elif self.__resize_method == "minimal": new_height = self.constrain_to_multiple_of(scale_height * height) new_width = self.constrain_to_multiple_of(scale_width * width) else: raise ValueError(f"resize_method {self.__resize_method} not implemented")
return (new_width, new_height)
def __call__(self, sample): width, height = self.get_size( sample["image"].shape[1], sample["image"].shape[0] )
# resize sample sample["image"] = cv2.resize( sample["image"], (width, height), interpolation=self.__image_interpolation_method, )
if self.__resize_target: if "disparity" in sample: sample["disparity"] = cv2.resize( sample["disparity"], (width, height), interpolation=cv2.INTER_NEAREST, )
if "depth" in sample: sample["depth"] = cv2.resize( sample["depth"], (width, height), interpolation=cv2.INTER_NEAREST )
sample["mask"] = cv2.resize( sample["mask"].astype(np.float32), (width, height), interpolation=cv2.INTER_NEAREST, ) sample["mask"] = sample["mask"].astype(bool)
return sample
class NormalizeImage(object): """Normlize image by given mean and std. """
def __init__(self, mean, std): self.__mean = mean self.__std = std
def __call__(self, sample): sample["image"] = (sample["image"] - self.__mean) / self.__std
return sample
class PrepareForNet(object): """Prepare sample for usage as network input. """
def __init__(self): pass
def __call__(self, sample): image = np.transpose(sample["image"], (2, 0, 1)) sample["image"] = np.ascontiguousarray(image).astype(np.float32)
if "mask" in sample: sample["mask"] = sample["mask"].astype(np.float32) sample["mask"] = np.ascontiguousarray(sample["mask"])
if "disparity" in sample: disparity = sample["disparity"].astype(np.float32) sample["disparity"] = np.ascontiguousarray(disparity)
if "depth" in sample: depth = sample["depth"].astype(np.float32) sample["depth"] = np.ascontiguousarray(depth)
return sample12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970## Mobile version of MiDaS for iOS / Android - Monocular Depth Estimation
### Accuracy
* Old small model - ResNet50 default-decoder 384x384* New small model - EfficientNet-Lite3 small-decoder 256x256
**Zero-shot error** (the lower - the better):
| Model | DIW WHDR | Eth3d AbsRel | Sintel AbsRel | Kitti δ>1.25 | NyuDepthV2 δ>1.25 | TUM δ>1.25 ||---|---|---|---|---|---|---|| Old small model 384x384 | **0.1248** | 0.1550 | **0.3300** | **21.81** | 15.73 | 17.00 || New small model 256x256 | 0.1344 | **0.1344** | 0.3370 | 29.27 | **13.43** | **14.53** || Relative improvement, % | -8 % | **+13 %** | -2 % | -34 % | **+15 %** | **+15 %** |
None of Train/Valid/Test subsets of datasets (DIW, Eth3d, Sintel, Kitti, NyuDepthV2, TUM) were not involved in Training or Fine Tuning.
### Inference speed (FPS) on iOS / Android
**Frames Per Second** (the higher - the better):
| Model | iPhone CPU | iPhone GPU | iPhone NPU | OnePlus8 CPU | OnePlus8 GPU | OnePlus8 NNAPI ||---|---|---|---|---|---|---|| Old small model 384x384 | 0.6 | N/A | N/A | 0.45 | 0.50 | 0.50 || New small model 256x256 | 8 | 22 | **30** | 6 | **22** | 4 || SpeedUp, X times | **12.8x** | - | - | **13.2x** | **44x** | **8x** |
N/A - run-time error (no data available)
#### Models:
* Old small model - ResNet50 default-decoder 1x384x384x3, batch=1 FP32 (converters: Pytorch -> ONNX - [onnx_tf](https://github.com/onnx/onnx-tensorflow) -> (saved model) PB -> TFlite)
(Trained on datasets: RedWeb, MegaDepth, WSVD, 3D Movies, DIML indoor)
* New small model - EfficientNet-Lite3 small-decoder 1x256x256x3, batch=1 FP32 (custom converter: Pytorch -> TFlite)
(Trained on datasets: RedWeb, MegaDepth, WSVD, 3D Movies, DIML indoor, HRWSI, IRS, TartanAir, BlendedMVS, ApolloScape)
#### Frameworks for training and conversions:```pip install torch==1.6.0 torchvision==0.7.0pip install tf-nightly-gpu==2.5.0.dev20201031 tensorflow-addons==0.11.2 numpy==1.18.0git clone --depth 1 --branch v1.6.0 https://github.com/onnx/onnx-tensorflow```
#### SoC - OS - Library:
* iPhone 11 (A13 Bionic) - iOS 13.7 - TensorFlowLiteSwift 0.0.1-nightly* OnePlus 8 (Snapdragon 865) - Andoird 10 - org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly
### Citation
This repository contains code to compute depth from a single image. It accompanies our [paper](https://arxiv.org/abs/1907.01341v3):
>Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer René Ranftl, Katrin Lasinger, David Hafner, Konrad Schindler, Vladlen Koltun
Please cite our paper if you use this code or any of the models:```@article{Ranftl2020, author = {Ren\'{e} Ranftl and Katrin Lasinger and David Hafner and Konrad Schindler and Vladlen Koltun}, title = {Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer}, journal = {IEEE Transactions on Pattern Analysis and Machine Intelligence (TPAMI)}, year = {2020},}```
12345678910111213*.iml.gradle/local.properties/.idea/libraries/.idea/modules.xml/.idea/workspace.xml.DS_Store/build/captures.externalNativeBuild
/.gradle//.idea/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414# TensorFlow Lite Android image classification example
This document walks through the code of a simple Android mobile application thatdemonstrates[image classification](https://www.tensorflow.org/lite/models/image_classification/overview)using the device camera.
## Explore the code
We're now going to walk through the most important parts of the sample code.
### Get camera input
This mobile application gets the camera input using the functions defined in thefile[`CameraActivity.java`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/app/src/main/java/org/tensorflow/lite/examples/classification/CameraActivity.java).This file depends on[`AndroidManifest.xml`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/app/src/main/AndroidManifest.xml)to set the camera orientation.
`CameraActivity` also contains code to capture user preferences from the UI andmake them available to other classes via convenience methods.
```javamodel = Model.valueOf(modelSpinner.getSelectedItem().toString().toUpperCase());device = Device.valueOf(deviceSpinner.getSelectedItem().toString());numThreads = Integer.parseInt(threadsTextView.getText().toString().trim());```
### Classifier
This Image Classification Android reference app demonstrates two implementationsolutions,[`lib_task_api`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/lib_task_api)that leverages the out-of-box API from the[TensorFlow Lite Task Library](https://www.tensorflow.org/lite/inference_with_metadata/task_library/image_classifier),and[`lib_support`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/lib_support)that creates the custom inference pipleline using the[TensorFlow Lite Support Library](https://www.tensorflow.org/lite/inference_with_metadata/lite_support).
Both solutions implement the file `Classifier.java` (see[the one in lib_task_api](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/lib_task_api/src/main/java/org/tensorflow/lite/examples/classification/tflite/Classifier.java)and[the one in lib_support](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/lib_support/src/main/java/org/tensorflow/lite/examples/classification/tflite/Classifier.java))that contains most of the complex logic for processing the camera input andrunning inference.
Two subclasses of the `Classifier` exist, as in `ClassifierFloatMobileNet.java`and `ClassifierQuantizedMobileNet.java`, which contain settings for bothfloating point and[quantized](https://www.tensorflow.org/lite/performance/post_training_quantization)models.
The `Classifier` class implements a static method, `create`, which is used toinstantiate the appropriate subclass based on the supplied model type (quantizedvs floating point).
#### Using the TensorFlow Lite Task Library
Inference can be done using just a few lines of code with the[`ImageClassifier`](https://www.tensorflow.org/lite/inference_with_metadata/task_library/image_classifier)in the TensorFlow Lite Task Library.
##### Load model and create ImageClassifier
`ImageClassifier` expects a model populated with the[model metadata](https://www.tensorflow.org/lite/convert/metadata) and the labelfile. See the[model compatibility requirements](https://www.tensorflow.org/lite/inference_with_metadata/task_library/image_classifier#model_compatibility_requirements)for more details.
`ImageClassifierOptions` allows manipulation on various inference options, suchas setting the maximum number of top scored results to return using`setMaxResults(MAX_RESULTS)`, and setting the score threshold using`setScoreThreshold(scoreThreshold)`.
```java// Create the ImageClassifier instance.ImageClassifierOptions options = ImageClassifierOptions.builder().setMaxResults(MAX_RESULTS).build();imageClassifier = ImageClassifier.createFromFileAndOptions(activity, getModelPath(), options);```
`ImageClassifier` currently does not support configuring delegates andmultithread, but those are on our roadmap. Please stay tuned!
##### Run inference
`ImageClassifier` contains builtin logic to preprocess the input image, such asrotating and resizing an image. Processing options can be configured through`ImageProcessingOptions`. In the following example, input images are rotated tothe up-right angle and cropped to the center as the model expects a square input(`224x224`). See the[Java doc of `ImageClassifier`](https://github.com/tensorflow/tflite-support/blob/195b574f0aa9856c618b3f1ad87bd185cddeb657/tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/core/vision/ImageProcessingOptions.java#L22)for more details about how the underlying image processing is performed.
```javaTensorImage inputImage = TensorImage.fromBitmap(bitmap);int width = bitmap.getWidth();int height = bitmap.getHeight();int cropSize = min(width, height);ImageProcessingOptions imageOptions = ImageProcessingOptions.builder() .setOrientation(getOrientation(sensorOrientation)) // Set the ROI to the center of the image. .setRoi( new Rect( /*left=*/ (width - cropSize) / 2, /*top=*/ (height - cropSize) / 2, /*right=*/ (width + cropSize) / 2, /*bottom=*/ (height + cropSize) / 2)) .build();
List<Classifications> results = imageClassifier.classify(inputImage, imageOptions);```
The output of `ImageClassifier` is a list of `Classifications` instance, whereeach `Classifications` element is a single head classification result. All thedemo models are single head models, therefore, `results` only contains one`Classifications` object. Use `Classifications.getCategories()` to get a list oftop-k categories as specified with `MAX_RESULTS`. Each `Category` objectcontains the srting label and the score of that category.
To match the implementation of[`lib_support`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/lib_support),`results` is converted into `List<Recognition>` in the method,`getRecognitions`.
#### Using the TensorFlow Lite Support Library
##### Load model and create interpreter
To perform inference, we need to load a model file and instantiate an`Interpreter`. This happens in the constructor of the `Classifier` class, alongwith loading the list of class labels. Information about the device type andnumber of threads is used to configure the `Interpreter` via the`Interpreter.Options` instance passed into its constructor. Note that if a GPU,DSP (Digital Signal Processor) or NPU (Neural Processing Unit) is available, a[`Delegate`](https://www.tensorflow.org/lite/performance/delegates) can be usedto take full advantage of these hardware.
Please note that there are performance edge cases and developers are adviced totest with a representative set of devices prior to production.
```javaprotected Classifier(Activity activity, Device device, int numThreads) throws IOException { tfliteModel = FileUtil.loadMappedFile(activity, getModelPath()); switch (device) { case NNAPI: nnApiDelegate = new NnApiDelegate(); tfliteOptions.addDelegate(nnApiDelegate); break; case GPU: gpuDelegate = new GpuDelegate(); tfliteOptions.addDelegate(gpuDelegate); break; case CPU: break; } tfliteOptions.setNumThreads(numThreads); tflite = new Interpreter(tfliteModel, tfliteOptions); labels = FileUtil.loadLabels(activity, getLabelPath());...```
For Android devices, we recommend pre-loading and memory mapping the model fileto offer faster load times and reduce the dirty pages in memory. The method`FileUtil.loadMappedFile` does this, returning a `MappedByteBuffer` containingthe model.
The `MappedByteBuffer` is passed into the `Interpreter` constructor, along withan `Interpreter.Options` object. This object can be used to configure theinterpreter, for example by setting the number of threads (`.setNumThreads(1)`)or enabling [NNAPI](https://developer.android.com/ndk/guides/neuralnetworks)(`.addDelegate(nnApiDelegate)`).
##### Pre-process bitmap image
Next in the `Classifier` constructor, we take the input camera bitmap image,convert it to a `TensorImage` format for efficient processing and pre-processit. The steps are shown in the private 'loadImage' method:
```java/** Loads input image, and applys preprocessing. */private TensorImage loadImage(final Bitmap bitmap, int sensorOrientation) { // Loads bitmap into a TensorImage. image.load(bitmap);
// Creates processor for the TensorImage. int cropSize = Math.min(bitmap.getWidth(), bitmap.getHeight()); int numRoration = sensorOrientation / 90; ImageProcessor imageProcessor = new ImageProcessor.Builder() .add(new ResizeWithCropOrPadOp(cropSize, cropSize)) .add(new ResizeOp(imageSizeX, imageSizeY, ResizeMethod.BILINEAR)) .add(new Rot90Op(numRoration)) .add(getPreprocessNormalizeOp()) .build(); return imageProcessor.process(inputImageBuffer);}```
The pre-processing is largely the same for quantized and float models with oneexception: Normalization.
In `ClassifierFloatMobileNet`, the normalization parameters are defined as:
```javaprivate static final float IMAGE_MEAN = 127.5f;private static final float IMAGE_STD = 127.5f;```
In `ClassifierQuantizedMobileNet`, normalization is not required. Thus thenomalization parameters are defined as:
```javaprivate static final float IMAGE_MEAN = 0.0f;private static final float IMAGE_STD = 1.0f;```
##### Allocate output object
Initiate the output `TensorBuffer` for the output of the model.
```java/** Output probability TensorBuffer. */private final TensorBuffer outputProbabilityBuffer;
//...// Get the array size for the output buffer from the TensorFlow Lite model fileint probabilityTensorIndex = 0;int[] probabilityShape = tflite.getOutputTensor(probabilityTensorIndex).shape(); // {1, 1001}DataType probabilityDataType = tflite.getOutputTensor(probabilityTensorIndex).dataType();
// Creates the output tensor and its processor.outputProbabilityBuffer = TensorBuffer.createFixedSize(probabilityShape, probabilityDataType);
// Creates the post processor for the output probability.probabilityProcessor = new TensorProcessor.Builder().add(getPostprocessNormalizeOp()).build();```
For quantized models, we need to de-quantize the prediction with the NormalizeOp(as they are all essentially linear transformation). For float model,de-quantize is not required. But to uniform the API, de-quantize is added tofloat model too. Mean and std are set to 0.0f and 1.0f, respectively. To be morespecific,
In `ClassifierQuantizedMobileNet`, the normalized parameters are defined as:
```javaprivate static final float PROBABILITY_MEAN = 0.0f;private static final float PROBABILITY_STD = 255.0f;```
In `ClassifierFloatMobileNet`, the normalized parameters are defined as:
```javaprivate static final float PROBABILITY_MEAN = 0.0f;private static final float PROBABILITY_STD = 1.0f;```
##### Run inference
Inference is performed using the following in `Classifier` class:
```javatflite.run(inputImageBuffer.getBuffer(), outputProbabilityBuffer.getBuffer().rewind());```
##### Recognize image
Rather than call `run` directly, the method `recognizeImage` is used. It acceptsa bitmap and sensor orientation, runs inference, and returns a sorted `List` of`Recognition` instances, each corresponding to a label. The method will return anumber of results bounded by `MAX_RESULTS`, which is 3 by default.
`Recognition` is a simple class that contains information about a specificrecognition result, including its `title` and `confidence`. Using thepost-processing normalization method specified, the confidence is converted tobetween 0 and 1 of a given class being represented by the image.
```java/** Gets the label to probability map. */Map<String, Float> labeledProbability = new TensorLabel(labels, probabilityProcessor.process(outputProbabilityBuffer)) .getMapWithFloatValue();```
A `PriorityQueue` is used for sorting.
```java/** Gets the top-k results. */private static List<Recognition> getTopKProbability( Map<String, Float> labelProb) { // Find the best classifications. PriorityQueue<Recognition> pq = new PriorityQueue<>( MAX_RESULTS, new Comparator<Recognition>() { @Override public int compare(Recognition lhs, Recognition rhs) { // Intentionally reversed to put high confidence at the head of // the queue. return Float.compare(rhs.getConfidence(), lhs.getConfidence()); } });
for (Map.Entry<String, Float> entry : labelProb.entrySet()) { pq.add(new Recognition("" + entry.getKey(), entry.getKey(), entry.getValue(), null)); }
final ArrayList<Recognition> recognitions = new ArrayList<>(); int recognitionsSize = Math.min(pq.size(), MAX_RESULTS); for (int i = 0; i < recognitionsSize; ++i) { recognitions.add(pq.poll()); } return recognitions;}```
### Display results
The classifier is invoked and inference results are displayed by the`processImage()` function in[`ClassifierActivity.java`](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android/app/src/main/java/org/tensorflow/lite/examples/classification/ClassifierActivity.java).
`ClassifierActivity` is a subclass of `CameraActivity` that contains methodimplementations that render the camera image, run classification, and displaythe results. The method `processImage()` runs classification on a backgroundthread as fast as possible, rendering information on the UI thread to avoidblocking inference and creating latency.
```java@Overrideprotected void processImage() { rgbFrameBitmap.setPixels(getRgbBytes(), 0, previewWidth, 0, 0, previewWidth, previewHeight); final int imageSizeX = classifier.getImageSizeX(); final int imageSizeY = classifier.getImageSizeY();
runInBackground( new Runnable() { @Override public void run() { if (classifier != null) { final long startTime = SystemClock.uptimeMillis(); final List<Classifier.Recognition> results = classifier.recognizeImage(rgbFrameBitmap, sensorOrientation); lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime; LOGGER.v("Detect: %s", results);
runOnUiThread( new Runnable() { @Override public void run() { showResultsInBottomSheet(results); showFrameInfo(previewWidth + "x" + previewHeight); showCropInfo(imageSizeX + "x" + imageSizeY); showCameraResolution(imageSizeX + "x" + imageSizeY); showRotationInfo(String.valueOf(sensorOrientation)); showInference(lastProcessingTimeMs + "ms"); } }); } readyForNextImage(); } });}```
Another important role of `ClassifierActivity` is to determine user preferences(by interrogating `CameraActivity`), and instantiate the appropriatelyconfigured `Classifier` subclass. This happens when the video feed begins (via`onPreviewSizeChosen()`) and when options are changed in the UI (via`onInferenceConfigurationChanged()`).
```javaprivate void recreateClassifier(Model model, Device device, int numThreads) { if (classifier != null) { LOGGER.d("Closing classifier."); classifier.close(); classifier = null; } if (device == Device.GPU && model == Model.QUANTIZED) { LOGGER.d("Not creating classifier: GPU doesn't support quantized models."); runOnUiThread( () -> { Toast.makeText(this, "GPU does not yet supported quantized models.", Toast.LENGTH_LONG) .show(); }); return; } try { LOGGER.d( "Creating classifier (model=%s, device=%s, numThreads=%d)", model, device, numThreads); classifier = Classifier.create(this, model, device, numThreads); } catch (IOException e) { LOGGER.e(e, "Failed to create classifier."); }}```123456789101112131415161718192021MIT License
Copyright (c) 2020 Alexey
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021# MiDaS on Android smartphone by using TensorFlow-lite (TFLite)
* Either use Android Studio for compilation.
* Or use ready to install apk-file: * Or use URL: https://i.diawi.com/CVb8a9 * Or use QR-code: Scan QR-code or open URL -> Press `Install application` -> Press `Download` and wait for download -> Open -> Install -> Open -> Press: Allow MiDaS to take photo and video from the camera While using the APP

----
To use another model, you should convert it to `model_opt.tflite` and place it to the directory: `models\src\main\assets`
----
Original repository: https://github.com/isl-org/MiDaS123/build
/build/1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556apply plugin: 'com.android.application'
android { compileSdkVersion 28 defaultConfig { applicationId "org.tensorflow.lite.examples.classification" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } aaptOptions { noCompress "tflite" } compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' } lintOptions { abortOnError false } flavorDimensions "tfliteInference" productFlavors { // The TFLite inference is built using the TFLite Support library. support { dimension "tfliteInference" } // The TFLite inference is built using the TFLite Task library. taskApi { dimension "tfliteInference" } }
}
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) supportImplementation project(":lib_support") taskApiImplementation project(":lib_task_api") implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0' implementation 'com.google.android.material:material:1.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'com.google.truth:truth:1.0.1' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:rules:1.1.0'}123456789101112131415161718192021# Add project specific ProGuard rules here.# You can control the set of applied configuration files using the# proguardFiles setting in build.gradle.## For more details, see# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {# public *;#}
# Uncomment this to preserve the line number information for# debugging stack traces.#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to# hide the original source file name.#-renamesourcefileattribute SourceFile123red_fox 0.79403335kit_fox 0.16753247grey_fox 0.03619214123red_fox 0.85kit_fox 0.13grey_fox 0.0212345<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.tensorflow.lite.examples.classification"> <uses-sdk /></manifest>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.tensorflow.lite.examples.classification;
import static com.google.common.truth.Truth.assertThat;
import android.content.res.AssetManager;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import androidx.test.ext.junit.runners.AndroidJUnit4;import androidx.test.platform.app.InstrumentationRegistry;import androidx.test.rule.ActivityTestRule;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Scanner;import org.junit.Assert;import org.junit.Rule;import org.junit.Test;import org.junit.runner.RunWith;import org.tensorflow.lite.examples.classification.tflite.Classifier;import org.tensorflow.lite.examples.classification.tflite.Classifier.Device;import org.tensorflow.lite.examples.classification.tflite.Classifier.Model;import org.tensorflow.lite.examples.classification.tflite.Classifier.Recognition;
/** Golden test for Image Classification Reference app. */@RunWith(AndroidJUnit4.class)public class ClassifierTest {
@Rule public ActivityTestRule<ClassifierActivity> rule = new ActivityTestRule<>(ClassifierActivity.class);
private static final String[] INPUTS = {"fox.jpg"}; private static final String[] GOLDEN_OUTPUTS_SUPPORT = {"fox-mobilenet_v1_1.0_224_support.txt"}; private static final String[] GOLDEN_OUTPUTS_TASK = {"fox-mobilenet_v1_1.0_224_task_api.txt"};
@Test public void classificationResultsShouldNotChange() throws IOException { ClassifierActivity activity = rule.getActivity(); Classifier classifier = Classifier.create(activity, Model.FLOAT_MOBILENET, Device.CPU, 1); for (int i = 0; i < INPUTS.length; i++) { String imageFileName = INPUTS[i]; String goldenOutputFileName; // TODO(b/169379396): investigate the impact of the resize algorithm on accuracy. // This is a temporary workaround to set different golden rest results as the preprocessing // of lib_support and lib_task_api are different. Will merge them once the above TODO is // resolved. if (Classifier.TAG.equals("ClassifierWithSupport")) { goldenOutputFileName = GOLDEN_OUTPUTS_SUPPORT[i]; } else { goldenOutputFileName = GOLDEN_OUTPUTS_TASK[i]; } Bitmap input = loadImage(imageFileName); List<Recognition> goldenOutput = loadRecognitions(goldenOutputFileName);
List<Recognition> result = classifier.recognizeImage(input, 0); Iterator<Recognition> goldenOutputIterator = goldenOutput.iterator();
for (Recognition actual : result) { Assert.assertTrue(goldenOutputIterator.hasNext()); Recognition expected = goldenOutputIterator.next(); assertThat(actual.getTitle()).isEqualTo(expected.getTitle()); assertThat(actual.getConfidence()).isWithin(0.01f).of(expected.getConfidence()); } } }
private static Bitmap loadImage(String fileName) { AssetManager assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets(); InputStream inputStream = null; try { inputStream = assetManager.open(fileName); } catch (IOException e) { Log.e("Test", "Cannot load image from assets"); } return BitmapFactory.decodeStream(inputStream); }
private static List<Recognition> loadRecognitions(String fileName) { AssetManager assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets(); InputStream inputStream = null; try { inputStream = assetManager.open(fileName); } catch (IOException e) { Log.e("Test", "Cannot load probability results from assets"); } Scanner scanner = new Scanner(inputStream); List<Recognition> result = new ArrayList<>(); while (scanner.hasNext()) { String category = scanner.next(); category = category.replace('_', ' '); if (!scanner.hasNextFloat()) { break; } float probability = scanner.nextFloat(); Recognition recognition = new Recognition(null, category, probability, null); result.add(recognition); } return result; }}12345678910111213141516171819202122232425262728<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.tensorflow.lite.examples.classification">
<uses-sdk />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/tfe_ic_app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.ImageClassification"> <activity android:name=".ClassifierActivity" android:label="@string/tfe_ic_app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.tensorflow.lite.examples.classification;
import android.Manifest;import android.app.Fragment;import android.content.Context;import android.content.pm.PackageManager;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.hardware.Camera;import android.hardware.camera2.CameraAccessException;import android.hardware.camera2.CameraCharacteristics;import android.hardware.camera2.CameraManager;import android.hardware.camera2.params.StreamConfigurationMap;import android.media.Image;import android.media.Image.Plane;import android.media.ImageReader;import android.media.ImageReader.OnImageAvailableListener;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Trace;import androidx.annotation.NonNull;import androidx.annotation.UiThread;import androidx.appcompat.app.AppCompatActivity;import android.util.Size;import android.view.Surface;import android.view.TextureView;import android.view.View;import android.view.ViewTreeObserver;import android.view.WindowManager;import android.widget.AdapterView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Spinner;import android.widget.TextView;import android.widget.Toast;import com.google.android.material.bottomsheet.BottomSheetBehavior;import java.nio.ByteBuffer;import java.util.List;import org.tensorflow.lite.examples.classification.env.ImageUtils;import org.tensorflow.lite.examples.classification.env.Logger;import org.tensorflow.lite.examples.classification.tflite.Classifier.Device;import org.tensorflow.lite.examples.classification.tflite.Classifier.Model;import org.tensorflow.lite.examples.classification.tflite.Classifier.Recognition;
public abstract class CameraActivity extends AppCompatActivity implements OnImageAvailableListener, Camera.PreviewCallback, View.OnClickListener, AdapterView.OnItemSelectedListener { private static final Logger LOGGER = new Logger();
private static final int PERMISSIONS_REQUEST = 1;
private static final String PERMISSION_CAMERA = Manifest.permission.CAMERA; protected int previewWidth = 0; protected int previewHeight = 0; private Handler handler; private HandlerThread handlerThread; private boolean useCamera2API; private boolean isProcessingFrame = false; private byte[][] yuvBytes = new byte[3][]; private int[] rgbBytes = null; private int yRowStride; private Runnable postInferenceCallback; private Runnable imageConverter; private LinearLayout bottomSheetLayout; private LinearLayout gestureLayout; private BottomSheetBehavior<LinearLayout> sheetBehavior; protected TextView recognitionTextView, recognition1TextView, recognition2TextView, recognitionValueTextView, recognition1ValueTextView, recognition2ValueTextView; protected TextView frameValueTextView, cropValueTextView, cameraResolutionTextView, rotationTextView, inferenceTimeTextView; protected ImageView bottomSheetArrowImageView; private ImageView plusImageView, minusImageView; private Spinner modelSpinner; private Spinner deviceSpinner; private TextView threadsTextView;
//private Model model = Model.QUANTIZED_EFFICIENTNET; //private Device device = Device.CPU; private Model model = Model.FLOAT_EFFICIENTNET; private Device device = Device.GPU; private int numThreads = -1;
@Override protected void onCreate(final Bundle savedInstanceState) { LOGGER.d("onCreate " + this); super.onCreate(null); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tfe_ic_activity_camera);
if (hasPermission()) { setFragment(); } else { requestPermission(); }
threadsTextView = findViewById(R.id.threads); plusImageView = findViewById(R.id.plus); minusImageView = findViewById(R.id.minus); modelSpinner = findViewById(R.id.model_spinner); deviceSpinner = findViewById(R.id.device_spinner); bottomSheetLayout = findViewById(R.id.bottom_sheet_layout); gestureLayout = findViewById(R.id.gesture_layout); sheetBehavior = BottomSheetBehavior.from(bottomSheetLayout); bottomSheetArrowImageView = findViewById(R.id.bottom_sheet_arrow);
ViewTreeObserver vto = gestureLayout.getViewTreeObserver(); vto.addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { gestureLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { gestureLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); } // int width = bottomSheetLayout.getMeasuredWidth(); int height = gestureLayout.getMeasuredHeight();
sheetBehavior.setPeekHeight(height); } }); sheetBehavior.setHideable(false);
sheetBehavior.setBottomSheetCallback( new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { switch (newState) { case BottomSheetBehavior.STATE_HIDDEN: break; case BottomSheetBehavior.STATE_EXPANDED: { bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_down); } break; case BottomSheetBehavior.STATE_COLLAPSED: { bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_up); } break; case BottomSheetBehavior.STATE_DRAGGING: break; case BottomSheetBehavior.STATE_SETTLING: bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_up); break; } }
@Override public void onSlide(@NonNull View bottomSheet, float slideOffset) {} });
recognitionTextView = findViewById(R.id.detected_item); recognitionValueTextView = findViewById(R.id.detected_item_value); recognition1TextView = findViewById(R.id.detected_item1); recognition1ValueTextView = findViewById(R.id.detected_item1_value); recognition2TextView = findViewById(R.id.detected_item2); recognition2ValueTextView = findViewById(R.id.detected_item2_value);
frameValueTextView = findViewById(R.id.frame_info); cropValueTextView = findViewById(R.id.crop_info); cameraResolutionTextView = findViewById(R.id.view_info); rotationTextView = findViewById(R.id.rotation_info); inferenceTimeTextView = findViewById(R.id.inference_info);
modelSpinner.setOnItemSelectedListener(this); deviceSpinner.setOnItemSelectedListener(this);
plusImageView.setOnClickListener(this); minusImageView.setOnClickListener(this);
model = Model.valueOf(modelSpinner.getSelectedItem().toString().toUpperCase()); device = Device.valueOf(deviceSpinner.getSelectedItem().toString()); numThreads = Integer.parseInt(threadsTextView.getText().toString().trim()); }
protected int[] getRgbBytes() { imageConverter.run(); return rgbBytes; }
protected int getLuminanceStride() { return yRowStride; }
protected byte[] getLuminance() { return yuvBytes[0]; }
/** Callback for android.hardware.Camera API */ @Override public void onPreviewFrame(final byte[] bytes, final Camera camera) { if (isProcessingFrame) { LOGGER.w("Dropping frame!"); return; }
try { // Initialize the storage bitmaps once when the resolution is known. if (rgbBytes == null) { Camera.Size previewSize = camera.getParameters().getPreviewSize(); previewHeight = previewSize.height; previewWidth = previewSize.width; rgbBytes = new int[previewWidth * previewHeight]; onPreviewSizeChosen(new Size(previewSize.width, previewSize.height), 90); } } catch (final Exception e) { LOGGER.e(e, "Exception!"); return; }
isProcessingFrame = true; yuvBytes[0] = bytes; yRowStride = previewWidth;
imageConverter = new Runnable() { @Override public void run() { ImageUtils.convertYUV420SPToARGB8888(bytes, previewWidth, previewHeight, rgbBytes); } };
postInferenceCallback = new Runnable() { @Override public void run() { camera.addCallbackBuffer(bytes); isProcessingFrame = false; } }; processImage(); }
/** Callback for Camera2 API */ @Override public void onImageAvailable(final ImageReader reader) { // We need wait until we have some size from onPreviewSizeChosen if (previewWidth == 0 || previewHeight == 0) { return; } if (rgbBytes == null) { rgbBytes = new int[previewWidth * previewHeight]; } try { final Image image = reader.acquireLatestImage();
if (image == null) { return; }
if (isProcessingFrame) { image.close(); return; } isProcessingFrame = true; Trace.beginSection("imageAvailable"); final Plane[] planes = image.getPlanes(); fillBytes(planes, yuvBytes); yRowStride = planes[0].getRowStride(); final int uvRowStride = planes[1].getRowStride(); final int uvPixelStride = planes[1].getPixelStride();
imageConverter = new Runnable() { @Override public void run() { ImageUtils.convertYUV420ToARGB8888( yuvBytes[0], yuvBytes[1], yuvBytes[2], previewWidth, previewHeight, yRowStride, uvRowStride, uvPixelStride, rgbBytes); } };
postInferenceCallback = new Runnable() { @Override public void run() { image.close(); isProcessingFrame = false; } };
processImage(); } catch (final Exception e) { LOGGER.e(e, "Exception!"); Trace.endSection(); return; } Trace.endSection(); }
@Override public synchronized void onStart() { LOGGER.d("onStart " + this); super.onStart(); }
@Override public synchronized void onResume() { LOGGER.d("onResume " + this); super.onResume();
handlerThread = new HandlerThread("inference"); handlerThread.start(); handler = new Handler(handlerThread.getLooper()); }
@Override public synchronized void onPause() { LOGGER.d("onPause " + this);
handlerThread.quitSafely(); try { handlerThread.join(); handlerThread = null; handler = null; } catch (final InterruptedException e) { LOGGER.e(e, "Exception!"); }
super.onPause(); }
@Override public synchronized void onStop() { LOGGER.d("onStop " + this); super.onStop(); }
@Override public synchronized void onDestroy() { LOGGER.d("onDestroy " + this); super.onDestroy(); }
protected synchronized void runInBackground(final Runnable r) { if (handler != null) { handler.post(r); } }
@Override public void onRequestPermissionsResult( final int requestCode, final String[] permissions, final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSIONS_REQUEST) { if (allPermissionsGranted(grantResults)) { setFragment(); } else { requestPermission(); } } }
private static boolean allPermissionsGranted(final int[] grantResults) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { return false; } } return true; }
private boolean hasPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return checkSelfPermission(PERMISSION_CAMERA) == PackageManager.PERMISSION_GRANTED; } else { return true; } }
private void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (shouldShowRequestPermissionRationale(PERMISSION_CAMERA)) { Toast.makeText( CameraActivity.this, "Camera permission is required for this demo", Toast.LENGTH_LONG) .show(); } requestPermissions(new String[] {PERMISSION_CAMERA}, PERMISSIONS_REQUEST); } }
// Returns true if the device supports the required hardware level, or better. private boolean isHardwareLevelSupported( CameraCharacteristics characteristics, int requiredLevel) { int deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (deviceLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) { return requiredLevel == deviceLevel; } // deviceLevel is not LEGACY, can use numerical sort return requiredLevel <= deviceLevel; }
private String chooseCamera() { final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { for (final String cameraId : manager.getCameraIdList()) { final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
// We don't use a front facing camera in this sample. final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { continue; }
final StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) { continue; }
// Fallback to camera1 API for internal cameras that don't have full support. // This should help with legacy situations where using the camera2 API causes // distorted or otherwise broken previews. useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL) || isHardwareLevelSupported( characteristics, CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL); LOGGER.i("Camera API lv2?: %s", useCamera2API); return cameraId; } } catch (CameraAccessException e) { LOGGER.e(e, "Not allowed to access camera"); }
return null; }
protected void setFragment() { String cameraId = chooseCamera();
Fragment fragment; if (useCamera2API) { CameraConnectionFragment camera2Fragment = CameraConnectionFragment.newInstance( new CameraConnectionFragment.ConnectionCallback() { @Override public void onPreviewSizeChosen(final Size size, final int rotation) { previewHeight = size.getHeight(); previewWidth = size.getWidth(); CameraActivity.this.onPreviewSizeChosen(size, rotation); } }, this, getLayoutId(), getDesiredPreviewFrameSize());
camera2Fragment.setCamera(cameraId); fragment = camera2Fragment; } else { fragment = new LegacyCameraConnectionFragment(this, getLayoutId(), getDesiredPreviewFrameSize()); }
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); }
protected void fillBytes(final Plane[] planes, final byte[][] yuvBytes) { // Because of the variable row stride it's not possible to know in // advance the actual necessary dimensions of the yuv planes. for (int i = 0; i < planes.length; ++i) { final ByteBuffer buffer = planes[i].getBuffer(); if (yuvBytes[i] == null) { LOGGER.d("Initializing buffer %d at size %d", i, buffer.capacity()); yuvBytes[i] = new byte[buffer.capacity()]; } buffer.get(yuvBytes[i]); } }
protected void readyForNextImage() { if (postInferenceCallback != null) { postInferenceCallback.run(); } }
protected int getScreenOrientation() { switch (getWindowManager().getDefaultDisplay().getRotation()) { case Surface.ROTATION_270: return 270; case Surface.ROTATION_180: return 180; case Surface.ROTATION_90: return 90; default: return 0; } }
@UiThread protected void showResultsInTexture(float[] img_array, int imageSizeX, int imageSizeY) { float maxval = Float.NEGATIVE_INFINITY; float minval = Float.POSITIVE_INFINITY; for (float cur : img_array) { maxval = Math.max(maxval, cur); minval = Math.min(minval, cur); } float multiplier = 0; if ((maxval - minval) > 0) multiplier = 255 / (maxval - minval);
int[] img_normalized = new int[img_array.length]; for (int i = 0; i < img_array.length; ++i) { float val = (float) (multiplier * (img_array[i] - minval)); img_normalized[i] = (int) val; }
TextureView textureView = findViewById(R.id.textureView3); //AutoFitTextureView textureView = (AutoFitTextureView) findViewById(R.id.texture);
if(textureView.isAvailable()) { int width = imageSizeX; int height = imageSizeY;
Canvas canvas = textureView.lockCanvas(); canvas.drawColor(Color.BLUE); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setARGB(255, 150, 150, 150);
int canvas_size = Math.min(canvas.getWidth(), canvas.getHeight());
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int ii = 0; ii < width; ii++) //pass the screen pixels in 2 directions { for (int jj = 0; jj < height; jj++) { //int val = img_normalized[ii + jj * width]; int index = (width - ii - 1) + (height - jj - 1) * width; if(index < img_array.length) { int val = img_normalized[index]; bitmap.setPixel(ii, jj, Color.rgb(val, val, val)); } } }
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvas_size, canvas_size), null);
textureView.unlockCanvasAndPost(canvas);
}
}
protected void showResultsInBottomSheet(List<Recognition> results) { if (results != null && results.size() >= 3) { Recognition recognition = results.get(0); if (recognition != null) { if (recognition.getTitle() != null) recognitionTextView.setText(recognition.getTitle()); if (recognition.getConfidence() != null) recognitionValueTextView.setText( String.format("%.2f", (100 * recognition.getConfidence())) + "%"); }
Recognition recognition1 = results.get(1); if (recognition1 != null) { if (recognition1.getTitle() != null) recognition1TextView.setText(recognition1.getTitle()); if (recognition1.getConfidence() != null) recognition1ValueTextView.setText( String.format("%.2f", (100 * recognition1.getConfidence())) + "%"); }
Recognition recognition2 = results.get(2); if (recognition2 != null) { if (recognition2.getTitle() != null) recognition2TextView.setText(recognition2.getTitle()); if (recognition2.getConfidence() != null) recognition2ValueTextView.setText( String.format("%.2f", (100 * recognition2.getConfidence())) + "%"); } } }
protected void showFrameInfo(String frameInfo) { frameValueTextView.setText(frameInfo); }
protected void showCropInfo(String cropInfo) { cropValueTextView.setText(cropInfo); }
protected void showCameraResolution(String cameraInfo) { cameraResolutionTextView.setText(cameraInfo); }
protected void showRotationInfo(String rotation) { rotationTextView.setText(rotation); }
protected void showInference(String inferenceTime) { inferenceTimeTextView.setText(inferenceTime); }
protected Model getModel() { return model; }
private void setModel(Model model) { if (this.model != model) { LOGGER.d("Updating model: " + model); this.model = model; onInferenceConfigurationChanged(); } }
protected Device getDevice() { return device; }
private void setDevice(Device device) { if (this.device != device) { LOGGER.d("Updating device: " + device); this.device = device; final boolean threadsEnabled = device == Device.CPU; plusImageView.setEnabled(threadsEnabled); minusImageView.setEnabled(threadsEnabled); threadsTextView.setText(threadsEnabled ? String.valueOf(numThreads) : "N/A"); onInferenceConfigurationChanged(); } }
protected int getNumThreads() { return numThreads; }
private void setNumThreads(int numThreads) { if (this.numThreads != numThreads) { LOGGER.d("Updating numThreads: " + numThreads); this.numThreads = numThreads; onInferenceConfigurationChanged(); } }
protected abstract void processImage();
protected abstract void onPreviewSizeChosen(final Size size, final int rotation);
protected abstract int getLayoutId();
protected abstract Size getDesiredPreviewFrameSize();
protected abstract void onInferenceConfigurationChanged();
@Override public void onClick(View v) { if (v.getId() == R.id.plus) { String threads = threadsTextView.getText().toString().trim(); int numThreads = Integer.parseInt(threads); if (numThreads >= 9) return; setNumThreads(++numThreads); threadsTextView.setText(String.valueOf(numThreads)); } else if (v.getId() == R.id.minus) { String threads = threadsTextView.getText().toString().trim(); int numThreads = Integer.parseInt(threads); if (numThreads == 1) { return; } setNumThreads(--numThreads); threadsTextView.setText(String.valueOf(numThreads)); } }
@Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { if (parent == modelSpinner) { setModel(Model.valueOf(parent.getItemAtPosition(pos).toString().toUpperCase())); } else if (parent == deviceSpinner) { setDevice(Device.valueOf(parent.getItemAtPosition(pos).toString())); } }
@Override public void onNothingSelected(AdapterView<?> parent) { // Do nothing. }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.tensorflow.lite.examples.classification;
import android.annotation.SuppressLint;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.app.Fragment;import android.content.Context;import android.content.DialogInterface;import android.content.res.Configuration;import android.graphics.ImageFormat;import android.graphics.Matrix;import android.graphics.RectF;import android.graphics.SurfaceTexture;import android.hardware.camera2.CameraAccessException;import android.hardware.camera2.CameraCaptureSession;import android.hardware.camera2.CameraCharacteristics;import android.hardware.camera2.CameraDevice;import android.hardware.camera2.CameraManager;import android.hardware.camera2.CaptureRequest;import android.hardware.camera2.CaptureResult;import android.hardware.camera2.TotalCaptureResult;import android.hardware.camera2.params.StreamConfigurationMap;import android.media.ImageReader;import android.media.ImageReader.OnImageAvailableListener;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.text.TextUtils;import android.util.Size;import android.util.SparseIntArray;import android.view.LayoutInflater;import android.view.Surface;import android.view.TextureView;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;import org.tensorflow.lite.examples.classification.customview.AutoFitTextureView;import org.tensorflow.lite.examples.classification.env.Logger;
/** * Camera Connection Fragment that captures images from camera. * * <p>Instantiated by newInstance.</p> */@SuppressWarnings("FragmentNotInstantiable")public class CameraConnectionFragment extends Fragment { private static final Logger LOGGER = new Logger();
/** * The camera preview size will be chosen to be the smallest frame by pixel size capable of * containing a DESIRED_SIZE x DESIRED_SIZE square. */ private static final int MINIMUM_PREVIEW_SIZE = 320;
/** Conversion from screen rotation to JPEG orientation. */ private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
private static final String FRAGMENT_DIALOG = "dialog";
static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); }
/** A {@link Semaphore} to prevent the app from exiting before closing the camera. */ private final Semaphore cameraOpenCloseLock = new Semaphore(1); /** A {@link OnImageAvailableListener} to receive frames as they are available. */ private final OnImageAvailableListener imageListener; /** The input size in pixels desired by TensorFlow (width and height of a square bitmap). */ private final Size inputSize; /** The layout identifier to inflate for this Fragment. */ private final int layout;
private final ConnectionCallback cameraConnectionCallback; private final CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureProgressed( final CameraCaptureSession session, final CaptureRequest request, final CaptureResult partialResult) {}
@Override public void onCaptureCompleted( final CameraCaptureSession session, final CaptureRequest request, final TotalCaptureResult result) {} }; /** ID of the current {@link CameraDevice}. */ private String cameraId; /** An {@link AutoFitTextureView} for camera preview. */ private AutoFitTextureView textureView; /** A {@link CameraCaptureSession } for camera preview. */ private CameraCaptureSession captureSession; /** A reference to the opened {@link CameraDevice}. */ private CameraDevice cameraDevice; /** The rotation in degrees of the camera sensor from the display. */ private Integer sensorOrientation; /** The {@link Size} of camera preview. */ private Size previewSize; /** An additional thread for running tasks that shouldn't block the UI. */ private HandlerThread backgroundThread; /** A {@link Handler} for running tasks in the background. */ private Handler backgroundHandler; /** * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a {@link * TextureView}. */ private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable( final SurfaceTexture texture, final int width, final int height) { openCamera(width, height); }
@Override public void onSurfaceTextureSizeChanged( final SurfaceTexture texture, final int width, final int height) { configureTransform(width, height); }
@Override public boolean onSurfaceTextureDestroyed(final SurfaceTexture texture) { return true; }
@Override public void onSurfaceTextureUpdated(final SurfaceTexture texture) {} }; /** An {@link ImageReader} that handles preview frame capture. */ private ImageReader previewReader; /** {@link CaptureRequest.Builder} for the camera preview */ private CaptureRequest.Builder previewRequestBuilder; /** {@link CaptureRequest} generated by {@link #previewRequestBuilder} */ private CaptureRequest previewRequest; /** {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state. */ private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(final CameraDevice cd) { // This method is called when the camera is opened. We start camera preview here. cameraOpenCloseLock.release(); cameraDevice = cd; createCameraPreviewSession(); }
@Override public void onDisconnected(final CameraDevice cd) { cameraOpenCloseLock.release(); cd.close(); cameraDevice = null; }
@Override public void onError(final CameraDevice cd, final int error) { cameraOpenCloseLock.release(); cd.close(); cameraDevice = null; final Activity activity = getActivity(); if (null != activity) { activity.finish(); } } };
@SuppressLint("ValidFragment") private CameraConnectionFragment( final ConnectionCallback connectionCallback, final OnImageAvailableListener imageListener, final int layout, final Size inputSize) { this.cameraConnectionCallback = connectionCallback; this.imageListener = imageListener; this.layout = layout; this.inputSize = inputSize; }
/** * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose * width and height are at least as large as the minimum of both, or an exact match if possible. * * @param choices The list of sizes that the camera supports for the intended output class * @param width The minimum desired width * @param height The minimum desired height * @return The optimal {@code Size}, or an arbitrary one if none were big enough */ protected static Size chooseOptimalSize(final Size[] choices, final int width, final int height) { final int minSize = Math.max(Math.min(width, height), MINIMUM_PREVIEW_SIZE); final Size desiredSize = new Size(width, height);
// Collect the supported resolutions that are at least as big as the preview Surface boolean exactSizeFound = false; final List<Size> bigEnough = new ArrayList<Size>(); final List<Size> tooSmall = new ArrayList<Size>(); for (final Size option : choices) { if (option.equals(desiredSize)) { // Set the size but don't return yet so that remaining sizes will still be logged. exactSizeFound = true; }
if (option.getHeight() >= minSize && option.getWidth() >= minSize) { bigEnough.add(option); } else { tooSmall.add(option); } }
LOGGER.i("Desired size: " + desiredSize + ", min size: " + minSize + "x" + minSize); LOGGER.i("Valid preview sizes: [" + TextUtils.join(", ", bigEnough) + "]"); LOGGER.i("Rejected preview sizes: [" + TextUtils.join(", ", tooSmall) + "]");
if (exactSizeFound) { LOGGER.i("Exact size match found."); return desiredSize; }
// Pick the smallest of those, assuming we found any if (bigEnough.size() > 0) { final Size chosenSize = Collections.min(bigEnough, new CompareSizesByArea()); LOGGER.i("Chosen size: " + chosenSize.getWidth() + "x" + chosenSize.getHeight()); return chosenSize; } else { LOGGER.e("Couldn't find any suitable preview size"); return choices[0]; } }
public static CameraConnectionFragment newInstance( final ConnectionCallback callback, final OnImageAvailableListener imageListener, final int layout, final Size inputSize) { return new CameraConnectionFragment(callback, imageListener, layout, inputSize); }
/** * Shows a {@link Toast} on the UI thread. * * @param text The message to show */ private void showToast(final String text) { final Activity activity = getActivity(); if (activity != null) { activity.runOnUiThread( new Runnable() { @Override public void run() { Toast.makeText(activity, text, Toast.LENGTH_SHORT).show(); } }); } }
@Override public View onCreateView( final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { return inflater.inflate(layout, container, false); }
@Override public void onViewCreated(final View view, final Bundle savedInstanceState) { textureView = (AutoFitTextureView) view.findViewById(R.id.texture); }
@Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); }
@Override public void onResume() { super.onResume(); startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open // a camera and start preview from here (otherwise, we wait until the surface is ready in // the SurfaceTextureListener). if (textureView.isAvailable()) { openCamera(textureView.getWidth(), textureView.getHeight()); } else { textureView.setSurfaceTextureListener(surfaceTextureListener); } }
@Override public void onPause() { closeCamera(); stopBackgroundThread(); super.onPause(); }
public void setCamera(String cameraId) { this.cameraId = cameraId; }
/** Sets up member variables related to camera. */ private void setUpCameraOutputs() { final Activity activity = getActivity(); final CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
final StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera // bus' bandwidth limitation, resulting in gorgeous previews but the storage of // garbage capture data. previewSize = chooseOptimalSize( map.getOutputSizes(SurfaceTexture.class), inputSize.getWidth(), inputSize.getHeight());
// We fit the aspect ratio of TextureView to the size of preview we picked. final int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight()); } else { textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth()); } } catch (final CameraAccessException e) { LOGGER.e(e, "Exception!"); } catch (final NullPointerException e) { // Currently an NPE is thrown when the Camera2API is used but not supported on the // device this code runs. ErrorDialog.newInstance(getString(R.string.tfe_ic_camera_error)) .show(getChildFragmentManager(), FRAGMENT_DIALOG); throw new IllegalStateException(getString(R.string.tfe_ic_camera_error)); }
cameraConnectionCallback.onPreviewSizeChosen(previewSize, sensorOrientation); }
/** Opens the camera specified by {@link CameraConnectionFragment#cameraId}. */ private void openCamera(final int width, final int height) { setUpCameraOutputs(); configureTransform(width, height); final Activity activity = getActivity(); final CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { throw new RuntimeException("Time out waiting to lock camera opening."); } manager.openCamera(cameraId, stateCallback, backgroundHandler); } catch (final CameraAccessException e) { LOGGER.e(e, "Exception!"); } catch (final InterruptedException e) { throw new RuntimeException("Interrupted while trying to lock camera opening.", e); } }
/** Closes the current {@link CameraDevice}. */ private void closeCamera() { try { cameraOpenCloseLock.acquire(); if (null != captureSession) { captureSession.close(); captureSession = null; } if (null != cameraDevice) { cameraDevice.close(); cameraDevice = null; } if (null != previewReader) { previewReader.close(); previewReader = null; } } catch (final InterruptedException e) { throw new RuntimeException("Interrupted while trying to lock camera closing.", e); } finally { cameraOpenCloseLock.release(); } }
/** Starts a background thread and its {@link Handler}. */ private void startBackgroundThread() { backgroundThread = new HandlerThread("ImageListener"); backgroundThread.start(); backgroundHandler = new Handler(backgroundThread.getLooper()); }
/** Stops the background thread and its {@link Handler}. */ private void stopBackgroundThread() { backgroundThread.quitSafely(); try { backgroundThread.join(); backgroundThread = null; backgroundHandler = null; } catch (final InterruptedException e) { LOGGER.e(e, "Exception!"); } }
/** Creates a new {@link CameraCaptureSession} for camera preview. */ private void createCameraPreviewSession() { try { final SurfaceTexture texture = textureView.getSurfaceTexture(); assert texture != null;
// We configure the size of default buffer to be the size of camera preview we want. texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
// This is the output Surface we need to start preview. final Surface surface = new Surface(texture);
// We set up a CaptureRequest.Builder with the output Surface. previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewRequestBuilder.addTarget(surface);
LOGGER.i("Opening camera preview: " + previewSize.getWidth() + "x" + previewSize.getHeight());
// Create the reader for the preview frames. previewReader = ImageReader.newInstance( previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, 2);
previewReader.setOnImageAvailableListener(imageListener, backgroundHandler); previewRequestBuilder.addTarget(previewReader.getSurface());
// Here, we create a CameraCaptureSession for camera preview. cameraDevice.createCaptureSession( Arrays.asList(surface, previewReader.getSurface()), new CameraCaptureSession.StateCallback() {
@Override public void onConfigured(final CameraCaptureSession cameraCaptureSession) { // The camera is already closed if (null == cameraDevice) { return; }
// When the session is ready, we start displaying the preview. captureSession = cameraCaptureSession; try { // Auto focus should be continuous for camera preview. previewRequestBuilder.set( CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // Flash is automatically enabled when necessary. previewRequestBuilder.set( CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
// Finally, we start displaying the camera preview. previewRequest = previewRequestBuilder.build(); captureSession.setRepeatingRequest( previewRequest, captureCallback, backgroundHandler); } catch (final CameraAccessException e) { LOGGER.e(e, "Exception!"); } }
@Override public void onConfigureFailed(final CameraCaptureSession cameraCaptureSession) { showToast("Failed"); } }, null); } catch (final CameraAccessException e) { LOGGER.e(e, "Exception!"); } }
/** * Configures the necessary {@link Matrix} transformation to `mTextureView`. This method should be * called after the camera preview size is determined in setUpCameraOutputs and also the size of * `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` */ private void configureTransform(final int viewWidth, final int viewHeight) { final Activity activity = getActivity(); if (null == textureView || null == previewSize || null == activity) { return; } final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); final Matrix matrix = new Matrix(); final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); final float centerX = viewRect.centerX(); final float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); final float scale = Math.max( (float) viewHeight / previewSize.getHeight(), (float) viewWidth / previewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } textureView.setTransform(matrix); }
/** * Callback for Activities to use to initialize their data once the selected preview size is * known. */ public interface ConnectionCallback { void onPreviewSizeChosen(Size size, int cameraRotation); }
/** Compares two {@code Size}s based on their areas. */ static class CompareSizesByArea implements Comparator<Size> { @Override public int compare(final Size lhs, final Size rhs) { // We cast here to ensure the multiplications won't overflow return Long.signum( (long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight()); } }
/** Shows an error message dialog. */ public static class ErrorDialog extends DialogFragment { private static final String ARG_MESSAGE = "message";
public static ErrorDialog newInstance(final String message) { final ErrorDialog dialog = new ErrorDialog(); final Bundle args = new Bundle(); args.putString(ARG_MESSAGE, message); dialog.setArguments(args); return dialog; }
@Override public Dialog onCreateDialog(final Bundle savedInstanceState) { final Activity activity = getActivity(); return new AlertDialog.Builder(activity) .setMessage(getArguments().getString(ARG_MESSAGE)) .setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialogInterface, final int i) { activity.finish(); } }) .create(); } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.tensorflow.lite.examples.classification;
import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Typeface;import android.media.ImageReader.OnImageAvailableListener;import android.os.SystemClock;import android.util.Size;import android.util.TypedValue;import android.view.TextureView;import android.view.ViewStub;import android.widget.TextView;import android.widget.Toast;import java.io.IOException;import java.util.List;import java.util.ArrayList;
import org.tensorflow.lite.examples.classification.customview.AutoFitTextureView;import org.tensorflow.lite.examples.classification.env.BorderedText;import org.tensorflow.lite.examples.classification.env.Logger;import org.tensorflow.lite.examples.classification.tflite.Classifier;import org.tensorflow.lite.examples.classification.tflite.Classifier.Device;import org.tensorflow.lite.examples.classification.tflite.Classifier.Model;
import android.widget.ImageView;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.PixelFormat;import java.nio.ByteBuffer;
public class ClassifierActivity extends CameraActivity implements OnImageAvailableListener { private static final Logger LOGGER = new Logger(); private static final Size DESIRED_PREVIEW_SIZE = new Size(640, 480); private static final float TEXT_SIZE_DIP = 10; private Bitmap rgbFrameBitmap = null; private long lastProcessingTimeMs; private Integer sensorOrientation; private Classifier classifier; private BorderedText borderedText; /** Input image size of the model along x axis. */ private int imageSizeX; /** Input image size of the model along y axis. */ private int imageSizeY;
@Override protected int getLayoutId() { return R.layout.tfe_ic_camera_connection_fragment; }
@Override protected Size getDesiredPreviewFrameSize() { return DESIRED_PREVIEW_SIZE; }
@Override public void onPreviewSizeChosen(final Size size, final int rotation) { final float textSizePx = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE_DIP, getResources().getDisplayMetrics()); borderedText = new BorderedText(textSizePx); borderedText.setTypeface(Typeface.MONOSPACE);
recreateClassifier(getModel(), getDevice(), getNumThreads()); if (classifier == null) { LOGGER.e("No classifier on preview!"); return; }
previewWidth = size.getWidth(); previewHeight = size.getHeight();
sensorOrientation = rotation - getScreenOrientation(); LOGGER.i("Camera orientation relative to screen canvas: %d", sensorOrientation);
LOGGER.i("Initializing at size %dx%d", previewWidth, previewHeight); rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888); }
@Override protected void processImage() { rgbFrameBitmap.setPixels(getRgbBytes(), 0, previewWidth, 0, 0, previewWidth, previewHeight); final int cropSize = Math.min(previewWidth, previewHeight);
runInBackground( new Runnable() { @Override public void run() { if (classifier != null) { final long startTime = SystemClock.uptimeMillis(); //final List<Classifier.Recognition> results = // classifier.recognizeImage(rgbFrameBitmap, sensorOrientation); final List<Classifier.Recognition> results = new ArrayList<>();
float[] img_array = classifier.recognizeImage(rgbFrameBitmap, sensorOrientation);
/* float maxval = Float.NEGATIVE_INFINITY; float minval = Float.POSITIVE_INFINITY; for (float cur : img_array) { maxval = Math.max(maxval, cur); minval = Math.min(minval, cur); } float multiplier = 0; if ((maxval - minval) > 0) multiplier = 255 / (maxval - minval);
int[] img_normalized = new int[img_array.length]; for (int i = 0; i < img_array.length; ++i) { float val = (float) (multiplier * (img_array[i] - minval)); img_normalized[i] = (int) val; }
TextureView textureView = findViewById(R.id.textureView3); //AutoFitTextureView textureView = (AutoFitTextureView) findViewById(R.id.texture);
if(textureView.isAvailable()) { int width = imageSizeX; int height = imageSizeY;
Canvas canvas = textureView.lockCanvas(); canvas.drawColor(Color.BLUE); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setARGB(255, 150, 150, 150);
int canvas_size = Math.min(canvas.getWidth(), canvas.getHeight());
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int ii = 0; ii < width; ii++) //pass the screen pixels in 2 directions { for (int jj = 0; jj < height; jj++) { //int val = img_normalized[ii + jj * width]; int index = (width - ii - 1) + (height - jj - 1) * width; if(index < img_array.length) { int val = img_normalized[index]; bitmap.setPixel(ii, jj, Color.rgb(val, val, val)); } } }
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvas_size, canvas_size), null);
textureView.unlockCanvasAndPost(canvas);
} */
lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime; LOGGER.v("Detect: %s", results);
runOnUiThread( new Runnable() { @Override public void run() { //showResultsInBottomSheet(results); showResultsInTexture(img_array, imageSizeX, imageSizeY); showFrameInfo(previewWidth + "x" + previewHeight); showCropInfo(imageSizeX + "x" + imageSizeY); showCameraResolution(cropSize + "x" + cropSize); showRotationInfo(String.valueOf(sensorOrientation)); showInference(lastProcessingTimeMs + "ms"); } }); } readyForNextImage(); } }); }
@Override protected void onInferenceConfigurationChanged() { if (rgbFrameBitmap == null) { // Defer creation until we're getting camera frames. return; } final Device device = getDevice(); final Model model = getModel(); final int numThreads = getNumThreads(); runInBackground(() -> recreateClassifier(model, device, numThreads)); }
private void recreateClassifier(Model model, Device device, int numThreads) { if (classifier != null) { LOGGER.d("Closing classifier."); classifier.close(); classifier = null; } if (device == Device.GPU && (model == Model.QUANTIZED_MOBILENET || model == Model.QUANTIZED_EFFICIENTNET)) { LOGGER.d("Not creating classifier: GPU doesn't support quantized models."); runOnUiThread( () -> { Toast.makeText(this, R.string.tfe_ic_gpu_quant_error, Toast.LENGTH_LONG).show(); }); return; } try { LOGGER.d( "Creating classifier (model=%s, device=%s, numThreads=%d)", model, device, numThreads); classifier = Classifier.create(this, model, device, numThreads); } catch (IOException | IllegalArgumentException e) { LOGGER.e(e, "Failed to create classifier."); runOnUiThread( () -> { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); }); return; }
// Updates the input image size. imageSizeX = classifier.getImageSizeX(); imageSizeY = classifier.getImageSizeY(); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203package org.tensorflow.lite.examples.classification;
/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
import android.annotation.SuppressLint;import android.app.Fragment;import android.graphics.SurfaceTexture;import android.hardware.Camera;import android.hardware.Camera.CameraInfo;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.util.Size;import android.util.SparseIntArray;import android.view.LayoutInflater;import android.view.Surface;import android.view.TextureView;import android.view.View;import android.view.ViewGroup;import java.io.IOException;import java.util.List;import org.tensorflow.lite.examples.classification.customview.AutoFitTextureView;import org.tensorflow.lite.examples.classification.env.ImageUtils;import org.tensorflow.lite.examples.classification.env.Logger;
public class LegacyCameraConnectionFragment extends Fragment { private static final Logger LOGGER = new Logger(); /** Conversion from screen rotation to JPEG orientation. */ private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); }
private Camera camera; private Camera.PreviewCallback imageListener; private Size desiredSize; /** The layout identifier to inflate for this Fragment. */ private int layout; /** An {@link AutoFitTextureView} for camera preview. */ private AutoFitTextureView textureView; /** * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a {@link * TextureView}. */ private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable( final SurfaceTexture texture, final int width, final int height) {
int index = getCameraId(); camera = Camera.open(index);
try { Camera.Parameters parameters = camera.getParameters(); List<String> focusModes = parameters.getSupportedFocusModes(); if (focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } List<Camera.Size> cameraSizes = parameters.getSupportedPreviewSizes(); Size[] sizes = new Size[cameraSizes.size()]; int i = 0; for (Camera.Size size : cameraSizes) { sizes[i++] = new Size(size.width, size.height); } Size previewSize = CameraConnectionFragment.chooseOptimalSize( sizes, desiredSize.getWidth(), desiredSize.getHeight()); parameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight()); camera.setDisplayOrientation(90); camera.setParameters(parameters); camera.setPreviewTexture(texture); } catch (IOException exception) { camera.release(); }
camera.setPreviewCallbackWithBuffer(imageListener); Camera.Size s = camera.getParameters().getPreviewSize(); camera.addCallbackBuffer(new byte[ImageUtils.getYUVByteSize(s.height, s.width)]);
textureView.setAspectRatio(s.height, s.width);
camera.startPreview(); }
@Override public void onSurfaceTextureSizeChanged( final SurfaceTexture texture, final int width, final int height) {}
@Override public boolean onSurfaceTextureDestroyed(final SurfaceTexture texture) { return true; }
@Override public void onSurfaceTextureUpdated(final SurfaceTexture texture) {} }; /** An additional thread for running tasks that shouldn't block the UI. */ private HandlerThread backgroundThread;
@SuppressLint("ValidFragment") public LegacyCameraConnectionFragment( final Camera.PreviewCallback imageListener, final int layout, final Size desiredSize) { this.imageListener = imageListener; this.layout = layout; this.desiredSize = desiredSize; }
@Override public View onCreateView( final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { return inflater.inflate(layout, container, false); }
@Override public void onViewCreated(final View view, final Bundle savedInstanceState) { textureView = (AutoFitTextureView) view.findViewById(R.id.texture); }
@Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); }
@Override public void onResume() { super.onResume(); startBackgroundThread(); // When the screen is turned off and turned back on, the SurfaceTexture is already // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open // a camera and start preview from here (otherwise, we wait until the surface is ready in // the SurfaceTextureListener).
if (textureView.isAvailable()) { if (camera != null) { camera.startPreview(); } } else { textureView.setSurfaceTextureListener(surfaceTextureListener); } }
@Override public void onPause() { stopCamera(); stopBackgroundThread(); super.onPause(); }
/** Starts a background thread and its {@link Handler}. */ private void startBackgroundThread() { backgroundThread = new HandlerThread("CameraBackground"); backgroundThread.start(); }
/** Stops the background thread and its {@link Handler}. */ private void stopBackgroundThread() { backgroundThread.quitSafely(); try { backgroundThread.join(); backgroundThread = null; } catch (final InterruptedException e) { LOGGER.e(e, "Exception!"); } }
protected void stopCamera() { if (camera != null) { camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; } }
private int getCameraId() { CameraInfo ci = new CameraInfo(); for (int i = 0; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, ci); if (ci.facing == CameraInfo.CAMERA_FACING_BACK) return i; } return -1; // No camera found }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172/* * Copyright 2019 The TensorFlow Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.tensorflow.lite.examples.classification.customview;
import android.content.Context;import android.util.AttributeSet;import android.view.TextureView;
/** A {@link TextureView} that can be adjusted to a specified aspect ratio. */public class AutoFitTextureView extends TextureView { private int ratioWidth = 0; private int ratioHeight = 0;
public AutoFitTextureView(final Context context) { this(context, null); }
public AutoFitTextureView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); }
public AutoFitTextureView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); }
/** * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio * calculated from the parameters. Note that the actual sizes of parameters don't matter, that is, * calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. * * @param width Relative horizontal size * @param height Relative vertical size */ public void setAspectRatio(final int width, final int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } ratioWidth = width; ratioHeight = height; requestLayout(); }
@Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int height = MeasureSpec.getSize(heightMeasureSpec); if (0 == ratioWidth || 0 == ratioHeight) { setMeasuredDimension(width, height); } else { if (width < height * ratioWidth / ratioHeight) { setMeasuredDimension(width, width * ratioHeight / ratioWidth); } else { setMeasuredDimension(height * ratioWidth / ratioHeight, height); } } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.customview;
import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.View;import java.util.LinkedList;import java.util.List;
/** A simple View providing a render callback to other classes. */public class OverlayView extends View { private final List<DrawCallback> callbacks = new LinkedList<DrawCallback>();
public OverlayView(final Context context, final AttributeSet attrs) { super(context, attrs); }
public void addCallback(final DrawCallback callback) { callbacks.add(callback); }
@Override public synchronized void draw(final Canvas canvas) { for (final DrawCallback callback : callbacks) { callback.drawCallback(canvas); } }
/** Interface defining the callback for client classes. */ public interface DrawCallback { public void drawCallback(final Canvas canvas); }}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.customview;
import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.util.TypedValue;import android.view.View;import java.util.List;import org.tensorflow.lite.examples.classification.tflite.Classifier.Recognition;
public class RecognitionScoreView extends View implements ResultsView { private static final float TEXT_SIZE_DIP = 16; private final float textSizePx; private final Paint fgPaint; private final Paint bgPaint; private List<Recognition> results;
public RecognitionScoreView(final Context context, final AttributeSet set) { super(context, set);
textSizePx = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE_DIP, getResources().getDisplayMetrics()); fgPaint = new Paint(); fgPaint.setTextSize(textSizePx);
bgPaint = new Paint(); bgPaint.setColor(0xcc4285f4); }
@Override public void setResults(final List<Recognition> results) { this.results = results; postInvalidate(); }
@Override public void onDraw(final Canvas canvas) { final int x = 10; int y = (int) (fgPaint.getTextSize() * 1.5f);
canvas.drawPaint(bgPaint);
if (results != null) { for (final Recognition recog : results) { canvas.drawText(recog.getTitle() + ": " + recog.getConfidence(), x, y, fgPaint); y += (int) (fgPaint.getTextSize() * 1.5f); } } }}1234567891011121314151617181920212223/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.customview;
import java.util.List;import org.tensorflow.lite.examples.classification.tflite.Classifier.Recognition;
public interface ResultsView { public void setResults(final List<Recognition> results);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.env;
import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Align;import android.graphics.Paint.Style;import android.graphics.Rect;import android.graphics.Typeface;import java.util.Vector;
/** A class that encapsulates the tedious bits of rendering legible, bordered text onto a canvas. */public class BorderedText { private final Paint interiorPaint; private final Paint exteriorPaint;
private final float textSize;
/** * Creates a left-aligned bordered text object with a white interior, and a black exterior with * the specified text size. * * @param textSize text size in pixels */ public BorderedText(final float textSize) { this(Color.WHITE, Color.BLACK, textSize); }
/** * Create a bordered text object with the specified interior and exterior colors, text size and * alignment. * * @param interiorColor the interior text color * @param exteriorColor the exterior text color * @param textSize text size in pixels */ public BorderedText(final int interiorColor, final int exteriorColor, final float textSize) { interiorPaint = new Paint(); interiorPaint.setTextSize(textSize); interiorPaint.setColor(interiorColor); interiorPaint.setStyle(Style.FILL); interiorPaint.setAntiAlias(false); interiorPaint.setAlpha(255);
exteriorPaint = new Paint(); exteriorPaint.setTextSize(textSize); exteriorPaint.setColor(exteriorColor); exteriorPaint.setStyle(Style.FILL_AND_STROKE); exteriorPaint.setStrokeWidth(textSize / 8); exteriorPaint.setAntiAlias(false); exteriorPaint.setAlpha(255);
this.textSize = textSize; }
public void setTypeface(Typeface typeface) { interiorPaint.setTypeface(typeface); exteriorPaint.setTypeface(typeface); }
public void drawText(final Canvas canvas, final float posX, final float posY, final String text) { canvas.drawText(text, posX, posY, exteriorPaint); canvas.drawText(text, posX, posY, interiorPaint); }
public void drawLines(Canvas canvas, final float posX, final float posY, Vector<String> lines) { int lineNum = 0; for (final String line : lines) { drawText(canvas, posX, posY - getTextSize() * (lines.size() - lineNum - 1), line); ++lineNum; } }
public void setInteriorColor(final int color) { interiorPaint.setColor(color); }
public void setExteriorColor(final int color) { exteriorPaint.setColor(color); }
public float getTextSize() { return textSize; }
public void setAlpha(final int alpha) { interiorPaint.setAlpha(alpha); exteriorPaint.setAlpha(alpha); }
public void getTextBounds( final String line, final int index, final int count, final Rect lineBounds) { interiorPaint.getTextBounds(line, index, count, lineBounds); }
public void setTextAlign(final Align align) { interiorPaint.setTextAlign(align); exteriorPaint.setTextAlign(align); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.env;
import android.graphics.Bitmap;import android.os.Environment;import java.io.File;import java.io.FileOutputStream;
/** Utility class for manipulating images. */public class ImageUtils { // This value is 2 ^ 18 - 1, and is used to clamp the RGB values before their ranges // are normalized to eight bits. static final int kMaxChannelValue = 262143;
@SuppressWarnings("unused") private static final Logger LOGGER = new Logger();
/** * Utility method to compute the allocated size in bytes of a YUV420SP image of the given * dimensions. */ public static int getYUVByteSize(final int width, final int height) { // The luminance plane requires 1 byte per pixel. final int ySize = width * height;
// The UV plane works on 2x2 blocks, so dimensions with odd size must be rounded up. // Each 2x2 block takes 2 bytes to encode, one each for U and V. final int uvSize = ((width + 1) / 2) * ((height + 1) / 2) * 2;
return ySize + uvSize; }
/** * Saves a Bitmap object to disk for analysis. * * @param bitmap The bitmap to save. */ public static void saveBitmap(final Bitmap bitmap) { saveBitmap(bitmap, "preview.png"); }
/** * Saves a Bitmap object to disk for analysis. * * @param bitmap The bitmap to save. * @param filename The location to save the bitmap to. */ public static void saveBitmap(final Bitmap bitmap, final String filename) { final String root = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "tensorflow"; LOGGER.i("Saving %dx%d bitmap to %s.", bitmap.getWidth(), bitmap.getHeight(), root); final File myDir = new File(root);
if (!myDir.mkdirs()) { LOGGER.i("Make dir failed"); }
final String fname = filename; final File file = new File(myDir, fname); if (file.exists()) { file.delete(); } try { final FileOutputStream out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 99, out); out.flush(); out.close(); } catch (final Exception e) { LOGGER.e(e, "Exception!"); } }
public static void convertYUV420SPToARGB8888(byte[] input, int width, int height, int[] output) { final int frameSize = width * height; for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width; int u = 0; int v = 0;
for (int i = 0; i < width; i++, yp++) { int y = 0xff & input[yp]; if ((i & 1) == 0) { v = 0xff & input[uvp++]; u = 0xff & input[uvp++]; }
output[yp] = YUV2RGB(y, u, v); } } }
private static int YUV2RGB(int y, int u, int v) { // Adjust and check YUV values y = (y - 16) < 0 ? 0 : (y - 16); u -= 128; v -= 128;
// This is the floating point equivalent. We do the conversion in integer // because some Android devices do not have floating point in hardware. // nR = (int)(1.164 * nY + 2.018 * nU); // nG = (int)(1.164 * nY - 0.813 * nV - 0.391 * nU); // nB = (int)(1.164 * nY + 1.596 * nV); int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u);
// Clipping RGB values to be inside boundaries [ 0 , kMaxChannelValue ] r = r > kMaxChannelValue ? kMaxChannelValue : (r < 0 ? 0 : r); g = g > kMaxChannelValue ? kMaxChannelValue : (g < 0 ? 0 : g); b = b > kMaxChannelValue ? kMaxChannelValue : (b < 0 ? 0 : b);
return 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); }
public static void convertYUV420ToARGB8888( byte[] yData, byte[] uData, byte[] vData, int width, int height, int yRowStride, int uvRowStride, int uvPixelStride, int[] out) { int yp = 0; for (int j = 0; j < height; j++) { int pY = yRowStride * j; int pUV = uvRowStride * (j >> 1);
for (int i = 0; i < width; i++) { int uv_offset = pUV + (i >> 1) * uvPixelStride;
out[yp++] = YUV2RGB(0xff & yData[pY + i], 0xff & uData[uv_offset], 0xff & vData[uv_offset]); } } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/
package org.tensorflow.lite.examples.classification.env;
import android.util.Log;import java.util.HashSet;import java.util.Set;
/** Wrapper for the platform log function, allows convenient message prefixing and log disabling. */public final class Logger { private static final String DEFAULT_TAG = "tensorflow"; private static final int DEFAULT_MIN_LOG_LEVEL = Log.DEBUG;
// Classes to be ignored when examining the stack trace private static final Set<String> IGNORED_CLASS_NAMES;
static { IGNORED_CLASS_NAMES = new HashSet<String>(3); IGNORED_CLASS_NAMES.add("dalvik.system.VMStack"); IGNORED_CLASS_NAMES.add("java.lang.Thread"); IGNORED_CLASS_NAMES.add(Logger.class.getCanonicalName()); }
private final String tag; private final String messagePrefix; private int minLogLevel = DEFAULT_MIN_LOG_LEVEL;
/** * Creates a Logger using the class name as the message prefix. * * @param clazz the simple name of this class is used as the message prefix. */ public Logger(final Class<?> clazz) { this(clazz.getSimpleName()); }
/** * Creates a Logger using the specified message prefix. * * @param messagePrefix is prepended to the text of every message. */ public Logger(final String messagePrefix) { this(DEFAULT_TAG, messagePrefix); }
/** * Creates a Logger with a custom tag and a custom message prefix. If the message prefix is set to * * <pre>null</pre> * * , the caller's class name is used as the prefix. * * @param tag identifies the source of a log message. * @param messagePrefix prepended to every message if non-null. If null, the name of the caller is * being used */ public Logger(final String tag, final String messagePrefix) { this.tag = tag; final String prefix = messagePrefix == null ? getCallerSimpleName() : messagePrefix; this.messagePrefix = (prefix.length() > 0) ? prefix + ": " : prefix; }
/** Creates a Logger using the caller's class name as the message prefix. */ public Logger() { this(DEFAULT_TAG, null); }
/** Creates a Logger using the caller's class name as the message prefix. */ public Logger(final int minLogLevel) { this(DEFAULT_TAG, null); this.minLogLevel = minLogLevel; }
/** * Return caller's simple name. * * <p>Android getStackTrace() returns an array that looks like this: stackTrace[0]: * dalvik.system.VMStack stackTrace[1]: java.lang.Thread stackTrace[2]: * com.google.android.apps.unveil.env.UnveilLogger stackTrace[3]: * com.google.android.apps.unveil.BaseApplication * * <p>This function returns the simple version of the first non-filtered name. * * @return caller's simple name */ private static String getCallerSimpleName() { // Get the current callstack so we can pull the class of the caller off of it. final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (final StackTraceElement elem : stackTrace) { final String className = elem.getClassName(); if (!IGNORED_CLASS_NAMES.contains(className)) { // We're only interested in the simple name of the class, not the complete package. final String[] classParts = className.split("\\."); return classParts[classParts.length - 1]; } }
return Logger.class.getSimpleName(); }
public void setMinLogLevel(final int minLogLevel) { this.minLogLevel = minLogLevel; }
public boolean isLoggable(final int logLevel) { return logLevel >= minLogLevel || Log.isLoggable(tag, logLevel); }
private String toMessage(final String format, final Object... args) { return messagePrefix + (args.length > 0 ? String.format(format, args) : format); }
public void v(final String format, final Object... args) { if (isLoggable(Log.VERBOSE)) { Log.v(tag, toMessage(format, args)); } }
public void v(final Throwable t, final String format, final Object... args) { if (isLoggable(Log.VERBOSE)) { Log.v(tag, toMessage(format, args), t); } }
public void d(final String format, final Object... args) { if (isLoggable(Log.DEBUG)) { Log.d(tag, toMessage(format, args)); } }
public void d(final Throwable t, final String format, final Object... args) { if (isLoggable(Log.DEBUG)) { Log.d(tag, toMessage(format, args), t); } }
public void i(final String format, final Object... args) { if (isLoggable(Log.INFO)) { Log.i(tag, toMessage(format, args)); } }
public void i(final Throwable t, final String format, final Object... args) { if (isLoggable(Log.INFO)) { Log.i(tag, toMessage(format, args), t); } }
public void w(final String format, final Object... args) { if (isLoggable(Log.WARN)) { Log.w(tag, toMessage(format, args)); } }
public void w(final Throwable t, final String format, final Object... args) { if (isLoggable(Log.WARN)) { Log.w(tag, toMessage(format, args), t); } }
public void e(final String format, final Object... args) { if (isLoggable(Log.ERROR)) { Log.e(tag, toMessage(format, args)); } }
public void e(final Throwable t, final String format, final Object... args) { if (isLoggable(Log.ERROR)) { Log.e(tag, toMessage(format, args), t); } }}.gitattributes+1
ComfyUI/models/midas/intel-isl_MiDaS_master/.gitignoreAdded+110
ComfyUI/models/midas/intel-isl_MiDaS_master/DockerfileAdded+29
ComfyUI/models/midas/intel-isl_MiDaS_master/LICENSEAdded+21
ComfyUI/models/midas/intel-isl_MiDaS_master/README.mdAdded+300
ComfyUI/models/midas/intel-isl_MiDaS_master/__pycache__/hubconf.cpython-310.pycAdded
ComfyUI/models/midas/intel-isl_MiDaS_master/environment.yamlAdded+16
ComfyUI/models/midas/intel-isl_MiDaS_master/figures/Comparison.pngAdded

Large File Pointer Details
- SHA256: df5f85755c2a261c0f95855467f65843e813c11ee2723a9dfe7b26113be53248
- Pointer size: 132 Bytes
- Size of remote file: 1.7 MB
ComfyUI/models/midas/intel-isl_MiDaS_master/figures/Improvement_vs_FPS.pngAdded

ComfyUI/models/midas/intel-isl_MiDaS_master/hubconf.pyAdded+435
ComfyUI/models/midas/intel-isl_MiDaS_master/input/.placeholderAdded
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/beit.pyAdded+196
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/levit.pyAdded+106
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/next_vit.pyAdded+39
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin.pyAdded+13
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin2.pyAdded+34
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/swin_common.pyAdded+52
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/utils.pyAdded+249
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/backbones/vit.pyAdded+221
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/base_model.pyAdded+16
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/blocks.pyAdded+439
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/dpt_depth.pyAdded+166
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/midas_net.pyAdded+76
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/midas_net_custom.pyAdded+128
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/model_loader.pyAdded+242
ComfyUI/models/midas/intel-isl_MiDaS_master/midas/transforms.pyAdded+234
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/README.mdAdded+70
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/.gitignoreAdded+13
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/EXPLORE_THE_CODE.mdAdded+414
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/LICENSEAdded+21
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/README.mdAdded+21
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/.gitignoreAdded+3
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/build.gradleAdded+56
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/proguard-rules.proAdded+21
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/assets/fox-mobilenet_v1_1.0_224_support.txtAdded+3
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/assets/fox-mobilenet_v1_1.0_224_task_api.txtAdded+3
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/java/AndroidManifest.xmlAdded+5
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/androidTest/java/org/tensorflow/lite/examples/classification/ClassifierTest.javaAdded+121
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/AndroidManifest.xmlAdded+28
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/CameraActivity.javaAdded+717
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/CameraConnectionFragment.javaAdded+575
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/ClassifierActivity.javaAdded+238
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/LegacyCameraConnectionFragment.javaAdded+203
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/AutoFitTextureView.javaAdded+72
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/OverlayView.javaAdded+48
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/RecognitionScoreView.javaAdded+67
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/customview/ResultsView.javaAdded+23
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/BorderedText.javaAdded+115
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/ImageUtils.javaAdded+152
ComfyUI/models/midas/intel-isl_MiDaS_master/mobile/android/app/src/main/java/org/tensorflow/lite/examples/classification/env/Logger.javaAdded+186