Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / ml / fetch_and_convert.sh
fetch_and_convert.sh146 lines · 7.1 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Refresh the bundled `.maml` weights that `:library:ml` runs, from the ONNX exports |
| 4 | # they are converted from. |
| 5 | # |
| 6 | # camera/src/main/assets/selfie_segmentation.maml MediaPipe Selfie Segmentation |
| 7 | # camera/src/main/assets/modnet.maml MODNet photographic portrait matting |
| 8 | # photos/src/main/assets/u2netp.maml U^2-Net portable (saliency) |
| 9 | # photos/src/main/assets/scrfd_500m.maml SCRFD 500M (face detection) |
| 10 | # photos/src/main/assets/w600k_mbf.maml MobileFaceNet (face embedding) |
| 11 | # library/ocr/src/main/assets/ppocr_det.maml PP-OCRv5 mobile (text detection) |
| 12 | # library/ocr/src/main/assets/ppocr_rec.maml PP-OCRv5 mobile latin (text recognition) |
| 13 | # |
| 14 | # The first two are Apache-2.0. The two face models are InsightFace's buffalo_s pack |
| 15 | # under InsightFace's own licence, which permits **non-commercial research use only** — |
| 16 | # see photos/src/main/assets/README.md. They are the same weights the repo already |
| 17 | # shipped as ncnn `.param`/`.bin`, re-sourced from a licensed ONNX export so the |
| 18 | # provenance is recorded rather than absent. |
| 19 | # |
| 20 | # No ONNX is vendored: it is 2x the size of the fp16 `.maml` we actually ship and |
| 21 | # nothing reads it at run time. So this is the only record of where the weights came |
| 22 | # from, alongside the README.md next to each asset and the source ONNX SHA-256 baked |
| 23 | # into each `.maml` header. |
| 24 | # |
| 25 | # The conversion is deliberately not a graph import. `:library:ml` hardcodes each |
| 26 | # forward pass in Rust, so `.maml` carries ordered tensors and nothing else, and |
| 27 | # maml_convert.py pins a digest over the whole ordered layer table - an upstream |
| 28 | # re-export that reorders or re-pads one layer fails here rather than shipping a |
| 29 | # net that infers nonsense. See scripts/ml/maml_convert.py. |
| 30 | # |
| 31 | # The digest pins *structure*, not weight values. For the two folded models it is worth |
| 32 | # also running scripts/ml/onnx_parity.py, which checks the numbers against onnxruntime; |
| 33 | # it is how two silently-wrong folds were found. See that script's header. |
| 34 | # net that infers nonsense. See scripts/ml/maml_convert.py. |
| 35 | # |
| 36 | # ./scripts/ml/fetch_and_convert.sh # verify pinned hashes, rebuild assets |
| 37 | # ./scripts/ml/fetch_and_convert.sh --update # re-download and rewrite hashes |
| 38 | # ./scripts/ml/fetch_and_convert.sh --layers # also dump the per-layer tables |
| 39 | # |
| 40 | set -euo pipefail |
| 41 | |
| 42 | # repo-relative-dest graph sha256(onnx) url [converter] |
| 43 | # |
| 44 | # The converter defaults to maml_convert.py. PP-OCRv5 uses ppocr_fold.py instead: its |
| 45 | # export spells every convolution as a chain of constant Add/Mul/HardSigmoid nodes that |
| 46 | # has to be folded first, and doing that inside maml_convert.py would make "the tensors |
| 47 | # verbatim" stop being true for every other model. See that script's header. |
| 48 | # |
| 49 | # Pinned to a commit rather than to `main`: the file SHA-256 below would catch a |
| 50 | # silent upstream change anyway, but a pinned revision means --update tells you the |
| 51 | # upstream *revision* that moved, not just that some bytes differ. |
| 52 | # |
| 53 | # MODNet is the exception: no pinned upstream commit exists for the mirror file, |
| 54 | # so its row records the mirror URL and the SHA-256 of the copy the `.maml` was |
| 55 | # built from (see camera/src/main/assets/README.md). A moved mirror fails the |
| 56 | # hash check the same way. |
| 57 | MODELS=( |
| 58 | "camera/src/main/assets/selfie_segmentation.maml selfie 3241ac4ad8aa35bdaf33946776db29f7c283a413aa0b0dacb9483594b4531aad https://huggingface.co/onnx-community/mediapipe_selfie_segmentation/resolve/be49485c8e027524be38591817fc5cd31bd9d00e/onnx/model.onnx" |
| 59 | "camera/src/main/assets/modnet.maml modnet 07c308cf0fc7e6e8b2065a12ed7fc07e1de8feb37dc7839d7b7f15dd66584df9 https://huggingface.co/DavG25/modnet-pretrained-models/resolve/main/models/modnet_photographic_portrait_matting.onnx modnet_fold.py" |
| 60 | "photos/src/main/assets/u2netp.maml u2netp 309c8469258dda742793dce0ebea8e6dd393174f89934733ecc8b14c76f4ddd8 https://huggingface.co/BritishWerewolf/U-2-Netp/resolve/7112208dbac3a3642496c8d54e2f0f9bb3dc1dc8/onnx/model.onnx" |
| 61 | "photos/src/main/assets/scrfd_500m.maml scrfd 5e4447f50245bbd7966bd6c0fa52938c61474a04ec7def48753668a9d8b4ea3a https://huggingface.co/immich-app/buffalo_s/resolve/0ff1751885575e62e084dff70549ce24a11fa5dc/detection/model.onnx" |
| 62 | "photos/src/main/assets/w600k_mbf.maml mobilefacenet 9cc6e4a75f0e2bf0b1aed94578f144d15175f357bdc05e815e5c4a02b319eb4f https://huggingface.co/immich-app/buffalo_s/resolve/0ff1751885575e62e084dff70549ce24a11fa5dc/recognition/model.onnx" |
| 63 | "library/ocr/src/main/assets/ppocr_det.maml ppocr_det a431985659dc921974177a95adcfbb90fd9e51989a5e04d70d0b75f597b6e61d https://huggingface.co/PaddlePaddle/PP-OCRv5_mobile_det_onnx/resolve/e6f4fa85f00e168c862bc462aebca69eef9b3d3d/inference.onnx ppocr_fold.py" |
| 64 | "library/ocr/src/main/assets/ppocr_rec.maml ppocr_rec 7888113072263cb471b93f66dd5e2ad70548dc526fa1ace760d0d973dd121498 https://huggingface.co/PaddlePaddle/latin_PP-OCRv5_mobile_rec_onnx/resolve/89d3a50e2c27e2e7cceeab0e944c25c807d5db4f/inference.onnx ppocr_fold.py" |
| 65 | ) |
| 66 | |
| 67 | if [ ! -f settings.gradle.kts ]; then |
| 68 | echo "Run this from the repo root (where settings.gradle.kts is)." >&2 |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | sha256() { |
| 73 | if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 |
| 74 | else shasum -a 256 "$1" | cut -d' ' -f1 |
| 75 | fi |
| 76 | } |
| 77 | |
| 78 | update=0 |
| 79 | layers=0 |
| 80 | for arg in "$@"; do |
| 81 | case "$arg" in |
| 82 | --update) update=1 ;; |
| 83 | --layers) layers=1 ;; |
| 84 | *) echo "unknown argument: $arg" >&2; exit 1 ;; |
| 85 | esac |
| 86 | done |
| 87 | |
| 88 | # The ONNX is a build-time input only, so it goes to a scratch dir rather than into |
| 89 | # the tree, and is re-fetched rather than committed. |
| 90 | work="$(mktemp -d)" |
| 91 | trap 'rm -rf "$work"' EXIT |
| 92 | |
| 93 | python="${PYTHON:-python3}" |
| 94 | if ! "$python" -c 'import onnx, numpy' >/dev/null 2>&1; then |
| 95 | echo "Needs the 'onnx' and 'numpy' packages: $python -m pip install onnx numpy" >&2 |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | fail=0 |
| 100 | for entry in "${MODELS[@]}"; do |
| 101 | # shellcheck disable=SC2086 |
| 102 | set -- $entry |
| 103 | dest="$1" |
| 104 | graph="$2" |
| 105 | want="$3" |
| 106 | url="$4" |
| 107 | converter="${5:-maml_convert.py}" |
| 108 | onnx="${work}/${graph}.onnx" |
| 109 | |
| 110 | echo "Downloading ${graph} from ${url}..." |
| 111 | curl -fL --retry 3 "$url" -o "$onnx" |
| 112 | |
| 113 | got="$(sha256 "$onnx")" |
| 114 | if [ "$got" = "$want" ]; then |
| 115 | printf ' ok %-8s %s\n' "$graph" "$got" |
| 116 | elif [ "$update" = 1 ]; then |
| 117 | printf ' CHANGED %-8s\n was %s\n now %s\n' "$graph" "$want" "$got" |
| 118 | echo " ^ update the pin for ${graph} in this script, then re-pin the" |
| 119 | echo " layer digest with maml_convert.py --print-digest" |
| 120 | fail=1 |
| 121 | continue |
| 122 | else |
| 123 | printf ' MISMATCH %-8s\n want %s\n got %s\n' "$graph" "$want" "$got" |
| 124 | echo " Upstream moved. Re-run with --update to accept it." >&2 |
| 125 | fail=1 |
| 126 | continue |
| 127 | fi |
| 128 | |
| 129 | extra="" |
| 130 | [ "$layers" = 1 ] && extra="--print-layers" |
| 131 | mkdir -p "$(dirname "$dest")" |
| 132 | # shellcheck disable=SC2086 |
| 133 | "$python" "scripts/ml/${converter}" "$onnx" --graph "$graph" -o "$dest" $extra |
| 134 | done |
| 135 | |
| 136 | echo |
| 137 | if [ "$fail" != 0 ]; then |
| 138 | echo "FAILED — see above." >&2 |
| 139 | exit 1 |
| 140 | fi |
| 141 | for entry in "${MODELS[@]}"; do |
| 142 | # shellcheck disable=SC2086 |
| 143 | set -- $entry |
| 144 | ls -l "$1" | awk '{ printf " %10d %s\n", $5, $NF }' |
| 145 | done |
| 146 | echo "All ${#MODELS[@]} models converted." |