主要關於linux下export、echo、pwd的用法。
《21個項目玩轉深度學習:基於Tensorflow的時間詳解》5.2.1安裝TensorFlow Object Detection API的一個操作,失敗了,寫給碰到同樣問題的人。
照着這個教程做,發現運行一個python3 shell,然後import slim也確實沒問題。
但是!!!
這容易有誤解和干擾項,因爲你可能就是在research目錄下直接運行python shell,而不是其他地方,在research目錄下,本來就有slim,怎麼可能導入不成功呢?什麼變量都別設置,import一樣成功。所以這個判斷方法不準確,至少,應該換個目錄去運行python shell。
然後,運行下邊那個py文件還是會提示找不到nets:
Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 21, in <module>
from object_detection.builders import model_builder
File "/home/qw/Documents/Deep-Learning-21-Examples/chapter_5/research/object_detection/builders/model_builder.py", line 30, in <module>
from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
File "/home/qw/Documents/Deep-Learning-21-Examples/chapter_5/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py", line 28, in <module>
from nets import inception_resnet_v2
ImportError: No module named 'nets'
解決方法一:去那個要執行的文件所在目錄去export,再回到research目錄下執行,可過。
../Deep-Learning-21-Examples/chapter_5/research/object_detection/builders#export PYTHONPATH=$PYTHONPATH:'pwd'../../slim
問題好像出在那個'pwd'上。
解決方法二:使用絕對路徑
解決方案三:
沒錯~!
我是說,原文沒錯,注意pwd兩邊的不是'是`,鍵盤左上角那個按鍵。
# export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
linux的反單引號是命令替換,能直接把pwd的輸出放到這裏,也就等於方案二的手寫絕對路徑。
還有$(pwd)
# export PYTHONPATH=$PYTHONPATH:$(pwd):$(pwd)/slim
總之,是一個變量替換,類似字符串中的%s。
root@ubuntu:/home/qw/Documents/Deep-Learning-21-Examples/chapter_5/research# str="hello world `date`"
root@ubuntu:/home/qw/Documents/Deep-Learning-21-Examples/chapter_5/research# echo $strhello world Thu Oct 11 20:05:30 PDT 2018