如何用PaddlePaddle在短時間內P圖一個畢業照片

本篇文章是基於新冠疫情下,畢業照怎麼拍?10行Python代碼搞定的學習記錄。繼手繪PS雲畢業照片走紅網絡,身爲預備役程序員的我卻把好好的青春回憶變成了暗黑系恐怖片之後,終於找到了P圖神器,orz,orz。

依託於PaddlePaddle深度學習底層框架實現對照片的快速P圖

開源深度學習平臺飛槳下載,詳細的安裝步驟在

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ which python3
/home/warmtree/anaconda3/bin/python3
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ python3 --version
Python 3.7.3
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ python3 -m ensurepip
Looking in links: /tmp/tmpbk5oru46
Requirement already satisfied: setuptools in ./anaconda3/lib/python3.7/site-packages (46.1.3)
Requirement already satisfied: pip in ./anaconda3/lib/python3.7/site-packages (19.0.3)
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ python3 -m pip --versionpip 19.0.3 from /home/warmtree/anaconda3/lib/python3.7/site-packages/pip (python 3.7)
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ python3 -c "import platform;print(platform.architecture()[0]);print(platform.machine())"
64bit
x86_64

值得注意的是 python3 -c "import platform;print(platform.architecture()[0]);print(platform.machine())"可以確認 Python 和 pip 是 64 bit,並且處理器架構是x86_64架構,PaddlePaddle不支持arm64架構。然後,我們輸入下面的指令開始安裝PaddlePaddle:

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

安裝完出現了一個小問題,不知道會不會影響後續的工作,記住incompatible這個詞。

Successfully built prettytable pathlib rarfile
tensorflow 2.0.0 has requirement gast==0.2.2, but you'll have gast 0.3.3 which is incompatible.
Installing collected packages: prettytable, objgraph, pathlib, gast, funcsigs, rarfile, opencv-python, paddlepaddle
  Found existing installation: gast 0.2.2
    Uninstalling gast-0.2.2:
      Successfully uninstalled gast-0.2.2
Successfully installed funcsigs-1.0.2 gast-0.3.3 objgraph-3.4.1 opencv-python-4.2.0.34 paddlepaddle-1.8.1 pathlib-1.0.1 prettytable-0.7.2 rarfile-3.1

安裝deeplabv3p_xception65_humanseg模型,畢業照設計的實質是扣圖,該功能主要採用PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)實現

由於我們的是Linux系統,Linux環境下如果deeplabv3p_xception65_humanseg模型未安裝,我們加載模型時則會被自動安裝(如下圖顯示)。

>>> humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')
[2020-06-05 11:21:36,662] [    INFO] - Installing deeplabv3p_xception65_humanseg module

再繼續安裝paddlehub模塊

pip install paddlehub

還是有安裝問題沒有解決,版本不兼容問題。

Successfully built distlib
spyder 3.3.3 requires pyqt5<=5.12; python_version >= "3", which is not installed.
virtualenv 20.0.21 has requirement importlib-metadata<2,>=0.12; python_version < "3.8", but you'll have importlib-metadata 0.0.0 which is incompatible.
visualdl 2.0.0b4 has requirement six>=1.14.0, but you'll have six 1.12.0 which is incompatible.
Installing collected packages: Jinja2, Werkzeug, flask, Flask-Babel, pyflakes, pycodestyle, flake8, toml, appdirs, distlib, virtualenv, cfgv, identify, nodeenv, pre-commit, Pillow, visualdl, yapf, cma, sentencepiece, colorlog, gunicorn, paddlehub

我們檢查是否安裝好則在python3環境下運行

>>> import paddle.fluid
>>> paddle.fluid.install_check.run_check()
Running Verify Fluid Program ... 
Your Paddle Fluid works well on SINGLE GPU or CPU.
W0605 11:16:24.953212  9766 build_strategy.cc:170] fusion_group is not enabled for Windows/MacOS now, and only effective when running with CUDA GPU.
W0605 11:16:24.954181  9766 fuse_all_reduce_op_pass.cc:74] Find all_reduce operators: 2. To make the speed faster, some all_reduce ops are fused during training, after fusion, the number of all_reduce ops is 1.
Your Paddle Fluid works well on MUTIPLE GPU or CPU.
Your Paddle Fluid is installed successfully! Let's start deep Learning with Paddle Fluid now
>>> 

如上證明我們已經安裝好了之後,然後我們開始準備人物素材和背景素材。

humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')
student = humanseg.segmentation(data={'image':[imgs]})

別再用PS了,我用五行Python代碼就實現了批量摳圖

關於python圖片的輸入問題,其實有蠻多的庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章