小言_互联网的博客

在M1 Macbook上配置OpenCV与Tensorflow等机器学习环境(Apple)

1225人阅读  评论(0)


点击链接观看安装视频:https://youtu.be/6835OZT0Y5Y

设置Xcode

打开终端并执行

sudo xcode-select --install

安装HomeBrew(原生Apple Silicon M1)

打开终端,逐个写入

这将为M1芯片安装最新的Brew


   
  1. /bin/bash -c  "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. echo “export PATH=/opt/homebrew/bin:$PATH” >> ~/.zshrc
  3.  
  4. //Restart The Terminal
  5. brew install gcc
  6. brew install cmake
  7. brew install wget

安装Miniforge,设置Conda环境

点击下面的链接下载(Apple Silicon)版本

https://github.com/conda-forge/miniforge

打开终端并执行以下操作


   
  1. // If the Downloaded File Stored in Download
  2. cd Downloads
  3. bash Miniforge3-MacOSX-arm64.sh
  4. //After Installation Completes Restart Terminal
  5. //Creating Conda Environment named ml You can use any name in place           of "ml"
  6. conda create --name ml
  7. conda install -y python== 3.8 .6
  8. conda install -y pandas matplotlib scikit-learn jupyterlab

安装Tensorflow

单击下面的链接并下载文件

https://github.com/apple/tensorflow_macos/releases

Am在2021年3月3日为M1使用最新的TF alpha 2版本。


   
  1. //if Download Directory is Downloads
  2. cd Downloads
  3. tar xvf tensorflow_macos -0.1alpha2.tar.gz
  4. cd tensorflow_macos/arm64
  5. //Dont Forget To Activate Conda Environment 
  6. conda activate ml
  7. // Install specific pip version and some other base packages
  8. pip install --force pip== 20.2 .4 wheel setuptools cached-property six
  9. // Install all the packages provided by Apple but TensorFlow
  10. pip install --upgrade --no-dependencies --force numpy -1.18 .5-cp38-cp38-macosx_11_0_arm64.whl grpcio -1.33 .2-cp38-cp38-macosx_11_0_arm64.whl h5py -2.10 .0-cp38-cp38-macosx_11_0_arm64.whl tensorflow_addons_macos -0.1a2-cp38-cp38-macosx_11_0_arm64.whl
  11. // Install additional packages
  12. pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
  13. // Install TensorFlow
  14. pip install --upgrade --force --no-dependencies tensorflow_macos -0.1a2-cp38-cp38-macosx_11_0_arm64.whl

安装额外的包


   
  1. pip install matplotlib
  2. conda install -c conda-forge scikit-learn
  3. pip install keras
  4. pip install notebook

编译和安装OpenCV


   
  1. //I Suggest To Do all this Inside miniforge3 dir for that
  2. //  cd miniforge3
  3.  wget -O opencv.zip https: //github.com/opencv/opencv/archive/4.5.0.zip
  4.  
  5.  wget -O opencv_contrib.zip https: //github.com/opencv/opencv_contrib/archive/4.5.0.zip
  6.  
  7.  unzip opencv.zip
  8.  
  9.  unzip opencv_contrib.zip
  10.  
  11.  cd opencv -4.5 .0
  12. mkdir build && cd build
  13. //Here Take Care Of Paths of OPENCV_EXTRA_MODULES_PATH and   
  14. //    PYTHON3_EXECUTABLE If you're Beginner watch the YouTube  video
  15. //And If Inside miniforge3 just place your <username>.
  16. cmake \
  17. -DCMAKE_SYSTEM_PROCESSOR=arm64 \
  18. -DCMAKE_OSX_ARCHITECTURES=arm64 \
  19. -DWITH_OPENJPEG=OFF \
  20. -DWITH_IPP=OFF \
  21. -D CMAKE_BUILD_TYPE=RELEASE \
  22. -D CMAKE_INSTALL_PREFIX=/usr/local \
  23. -D OPENCV_EXTRA_MODULES_PATH=/Users/<username>/miniforge3/opencv_contrib -4.5 .0/modules \
  24. -D PYTHON3_EXECUTABLE=/Users/<username>/miniforge3/envs/ml/bin/python3 \
  25. -D BUILD_opencv_python2=OFF \
  26. -D BUILD_opencv_python3=ON \
  27. -D INSTALL_PYTHON_EXAMPLES=ON \
  28. -D INSTALL_C_EXAMPLES=OFF \
  29. -D OPENCV_ENABLE_NONFREE=ON \
  30. -D BUILD_EXAMPLES=ON ..
  31. make -j8
  32. //"8" is the number of cores To be used(This Step Takes Time)
  33. sudo make install
  34. //Linking OpenCV To Conda Environment 
  35. mdfind cv2.cpython
  36. //From the output Copy the Path similar to the below one 
  37. "/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so cv2.so"
  38. cd 
  39. cd miniforge3/envs/dev/lib/python3 .8/site-packages
  40. ln -s PasteYourCopiedPathHere

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 mthler」,每日朋友圈更新一篇高质量博文。

扫描二维码添加小编↓


转载:https://blog.csdn.net/woshicver/article/details/114994435
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场