• 百度Apollo自定义安装第三方库(以libtorch为例)


    百度Apollo是一个非常优秀的自动驾驶框架,但我们平时在开发中也会遇到各种原repo没有处理的问题。笔者近期想用pytorch的C++前端推理模型,但是遇到了libtorch版本与pytorch版本不匹配的问题,因此想自己安装一个新版本的libtorch。

    首先找到Apollo的docker是怎么安装libtorch或其他第三方库的。在/apollo/docker/build/installers文件夹下面,可以看到非常多的install_***.sh,这些就是安装第三方库的脚本。原始的install_libtorch.sh如下

    1. set -e
    2. CURR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
    3. . ${CURR_DIR}/installer_base.sh
    4. # TODO(build): Docs on how to build libtorch on Jetson boards
    5. # References:
    6. # https://github.com/ApolloAuto/apollo/blob/pre6/docker/build/installers/install_libtorch.sh
    7. # https://github.com/dusty-nv/jetson-containers/blob/master/Dockerfile.pytorch
    8. # https://forums.developer.nvidia.com/t/pytorch-for-jetson-version-1-6-0-now-available
    9. # https://github.com/pytorch/pytorch/blob/master/docker/caffe2/ubuntu-16.04-cpu-all-options/Dockerfile
    10. bash ${CURR_DIR}/install_mkl.sh
    11. TARGET_ARCH="$(uname -m)"
    12. ##============================================================##
    13. # libtorch_cpu
    14. if [[ "${TARGET_ARCH}" == "x86_64" ]]; then
    15. # https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.5.0%2Bcpu.zip
    16. VERSION="1.7.0-2"
    17. CHECKSUM="02fd4f30e97ce8911ef933d0516660892392e95e6768b50f591f4727f6224390"
    18. elif [[ "${TARGET_ARCH}" == "aarch64" ]]; then
    19. VERSION="1.6.0-1"
    20. CHECKSUM="6d1fba522e746213c209fbf6275fa6bac68e360bcd11cbd4d3bdbddb657bee82"
    21. else
    22. error "libtorch for ${TARGET_ARCH} not ready. Exiting..."
    23. exit 1
    24. fi
    25. PKG_NAME="libtorch_cpu-${VERSION}-linux-${TARGET_ARCH}.tar.gz"
    26. DOWNLOAD_LINK="https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME}"
    27. download_if_not_cached "${PKG_NAME}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
    28. tar xzf "${PKG_NAME}"
    29. mv "${PKG_NAME%.tar.gz}" /usr/local/libtorch_cpu
    30. rm -f "${PKG_NAME}"
    31. ok "Successfully installed libtorch_cpu ${VERSION}"
    32. ##============================================================##
    33. # libtorch_gpu
    34. if [[ "${TARGET_ARCH}" == "x86_64" ]]; then
    35. VERSION="1.7.0-2"
    36. CHECKSUM="b64977ca4a13ab41599bac8a846e8782c67ded8d562fdf437f0e606cd5a3b588"
    37. PKG_NAME="libtorch_gpu-${VERSION}-cu111-linux-x86_64.tar.gz"
    38. else # AArch64
    39. VERSION="1.6.0-1"
    40. PKG_NAME="libtorch_gpu-1.6.0-1-linux-aarch64.tar.gz"
    41. CHECKSUM="eeb5a223d9dbe40fe96f16e6711c49a3777cea2c0a8da2445d63e117fdad0385"
    42. fi
    43. DOWNLOAD_LINK="https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME}"
    44. download_if_not_cached "${PKG_NAME}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
    45. tar xzf "${PKG_NAME}"
    46. mv "${PKG_NAME%.tar.gz}" /usr/local/libtorch_gpu
    47. # Cleanup
    48. rm -f "${PKG_NAME}"
    49. ok "Successfully installed libtorch_gpu ${VERSION}"

     可以看到这一段

    PKG_NAME="libtorch_cpu-${VERSION}-linux-${TARGET_ARCH}.tar.gz"

    DOWNLOAD_LINK="https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME}"

    download_if_not_cached "${PKG_NAME}" "${CHECKSUM}" "${DOWNLOAD_LINK}"

    tar xzf "${PKG_NAME}"
    mv "${PKG_NAME%.tar.gz}" /usr/local/libtorch_cpu
    rm -f "${PKG_NAME}"
    ok "Successfully installed libtorch_cpu ${VERSION}"

    实际上就是找到Apollo指定版本的libtorch然后交给Apollo编译,因此想要替换版本的话只需要自己把这几句下载解压之类的东西替换就行。

    接下来看笔者自己写的新的install_libtorch_new.sh:

    1. set -e
    2. CURR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
    3. . ${CURR_DIR}/installer_base.sh
    4. # TODO(build): Docs on how to build libtorch on Jetson boards
    5. # References:
    6. # https://github.com/ApolloAuto/apollo/blob/pre6/docker/build/installers/install_libtorch.sh
    7. # https://github.com/dusty-nv/jetson-containers/blob/master/Dockerfile.pytorch
    8. # https://forums.developer.nvidia.com/t/pytorch-for-jetson-version-1-6-0-now-available
    9. # https://github.com/pytorch/pytorch/blob/master/docker/caffe2/ubuntu-16.04-cpu-all-options/Dockerfile
    10. bash ${CURR_DIR}/install_mkl.sh
    11. TARGET_ARCH="$(uname -m)"
    12. ##============================================================##
    13. # libtorch_cpu
    14. if [[ "${TARGET_ARCH}" == "x86_64" ]]; then
    15. # https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.5.0%2Bcpu.zip
    16. VERSION="1.7.0-2"
    17. CHECKSUM="02fd4f30e97ce8911ef933d0516660892392e95e6768b50f591f4727f6224390"
    18. elif [[ "${TARGET_ARCH}" == "aarch64" ]]; then
    19. VERSION="1.6.0-1"
    20. CHECKSUM="6d1fba522e746213c209fbf6275fa6bac68e360bcd11cbd4d3bdbddb657bee82"
    21. else
    22. error "libtorch for ${TARGET_ARCH} not ready. Exiting..."
    23. exit 1
    24. fi
    25. # PKG_NAME="libtorch_cpu-${VERSION}-linux-${TARGET_ARCH}.tar.gz"
    26. # DOWNLOAD_LINK="https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME}"
    27. # download_if_not_cached "${PKG_NAME}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
    28. PKG_NAME="/apollo/libtorch-cxx11-abi-shared-with-deps-1.10.2+cpu.zip"
    29. unzip "${PKG_NAME}" -d /usr/local/
    30. mv /usr/local/libtorch/ /usr/local/libtorch_cpu
    31. ok "Successfully installed libtorch_cpu ${VERSION}"
    32. ##============================================================##
    33. # libtorch_gpu
    34. if [[ "${TARGET_ARCH}" == "x86_64" ]]; then
    35. VERSION="1.7.0-2"
    36. CHECKSUM="b64977ca4a13ab41599bac8a846e8782c67ded8d562fdf437f0e606cd5a3b588"
    37. PKG_NAME="libtorch_gpu-${VERSION}-cu111-linux-x86_64.tar.gz"
    38. else # AArch64
    39. VERSION="1.6.0-1"
    40. PKG_NAME="libtorch_gpu-1.6.0-1-linux-aarch64.tar.gz"
    41. CHECKSUM="eeb5a223d9dbe40fe96f16e6711c49a3777cea2c0a8da2445d63e117fdad0385"
    42. fi
    43. # DOWNLOAD_LINK="https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME}"
    44. # download_if_not_cached "${PKG_NAME}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
    45. PKG_NAME="/apollo/libtorch-cxx11-abi-shared-with-deps-1.10.2+cu111.zip"
    46. unzip "${PKG_NAME}" -d /usr/local/
    47. mv /usr/local/libtorch/ /usr/local/libtorch_gpu
    48. # Cleanup
    49. rm -f "${PKG_NAME}"
    50. ok "Successfully installed libtorch_gpu ${VERSION}"

    可以看到首先把下载的命令注释掉了,然后自己写了一个PKG_NAME,这个地方填的是我自己下载的libtorch的zip文件,然后解压到/usr/local目录下,它自己解压出来的是libtorch文件夹,后面我自己分别重命名为了libtorch_cpu和libtorch_gpu文件夹,然后直接

    bash docker/build/installer/install_libtorch_new.sh

    就会把原来的/usr/local/下面的libtorch_cpu和libtorch_gpu替换成我自己的版本,这里用的是libtorch-cxx11-abi-shared-with-deps-1.10.2+cpu.zip和libtorch-cxx11-abi-shared-with-deps-1.10.2+cu111.zip。

    PS:

    再说一下pytorch的弱智指引,去他官网上下载的话实际上不能找到所有版本的libtorch,只有这么个结果

     你如果想要跟自己的pytorch版本匹配的话只能自己去下载,我用自己下载的举例。

    CPU版本:

    https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.10.2+cpu.zip

    这个命名方式很简单,直接把版本号替换就行。 

    GPU版本: 

    https://download.pytorch.org/libtorch/cu111/libtorch-cxx11-abi-shared-with-deps-1.10.2+cu111.zip gpu版本要先看自己的cuda版本,我的是11.1,所以上面两处都是cu111,然后在改pytorch版本号1.10.2。这么操作就可以下载自己需要的libtorch版本了。

  • 相关阅读:
    django上传excel并读入数据库
    HMI-Board上手指南
    深入浅出排序算法之归并排序
    机器学习(11)---降维PCA
    仿美团外卖微信小程序源码/美团外卖优惠券领劵小程序-自带流量主模式
    个股期权、商品期权、股指期权开户攻略(全网最全)
    两个点的距离
    SpringBoot-Web开发-拦截器
    win11系统点开图片几秒后就显示“此处没有任何要显示的内容
    js基本类型和引用类型
  • 原文地址:https://blog.csdn.net/qq_26751117/article/details/125623063