使用Google Cloud Platform構建機器學習項目-寵物識別

   寵物識別咱們使用到了tensorflow object-detection API  (https://github.com/tensorflow/models/tree/master/research/object_detection)python

  其中的Quick Start 2向咱們介紹了這個項目(https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md)linux

  安裝建議直接下載一份tensorflow/models文件夾的所有內容git

  爲了下降機器學習的時間成本和訓練須要的金錢成本(畢竟不是全部人都會爲機器學習購買昂貴的的顯卡和內存),各家公司都推出了雲機器學習的服務,如阿里雲的機器學習PAI(華東2是免費的,可是我確實看不懂怎麼用......)和GOOGLE的ML Engine等,這裏咱們使用到了Google的ML Engine,試用Google Cloud Platform(如下簡稱GCP)須要Visa或者Master Card的認證,認證完成能夠得到300美圓的試用機會,可在谷歌雲平臺的全部產品中使用。這裏過驗證我申請了一張建設銀行的e付卡(Visa版)成功經過了驗證,下面開始正式的ML Engine使用(詳細的過程能夠跟隨running_pets.md中的步驟進行):github

  1.建立GCP項目 這裏我建立了名爲My First Project的項目shell

  2.安裝Cloud SDK,這裏我選擇安裝了Windows版並全選了全部選項,以後使用gcloud auth login在瀏覽器中登陸谷歌帳號或使用gcloud init完成初始化,即可以在console中使用google SDKapi

  不過在設置中我遇到了設置代理錯誤的問題,提示瀏覽器

Network diagnostic detects and fixes local network connection issues.
Checking network connection...failed.
ERROR: gcloud crashed (TypeError): setproxy() takes at most 7 arguments (8 given)

If you would like to report this issue, please run the following command:
  gcloud feedback

To check gcloud for common problems, please run the following command:
  gcloud info --run-diagnostics

 

  gcloud version機器學習

Google Cloud SDK 186.0.0
bq 2.0.28
core 2018.01.22
gsutil 4.28

  查了下Stack Overflow ,發現是谷歌本身的問題.......解決方案是下載185版本(https://console.cloud.google.com/storage/browser/cloud-sdk-release?authuser=0&prefix=google-cloud-sdk-185)學習

  這周的187版本中會修復本問題,程序要求Python2.7環境,因此先下了一個捆綁python的版本湊合用吧,須要手動配置環境變量才能在powershell中使用,我相信這應該不是問題。測試

  (由於後面要配置本地環境,又換回了linux,安裝過程大同小異,使用交互式的安裝後再下載185版本安裝便可)

  3.啓用機器學習API,在打開的網頁中選擇須要啓用API的項目 點繼續等待完成。

  4.建立用於存儲的Bucket,這裏我建立了takefetter_pets_detector的Bucket,以後即可以向此Bucket上傳文件,記住這個Bucket的名字,以後咱們會使用屢次。

  或者是使用以下方法進行定義(相似win下使用set命令)

export YOUR_GCS_BUCKET=${takefetter_pets_detector}

   5.接下來配置本地的環境過程(這裏我一開始使用到了WSL Ubuntu進行配置,分區目錄掛載在/mnt/下,更換國內源......而後發現竟然連不上網,github上的issue裏的解決方案用了也沒用,又回到了機械硬盤安裝的deepin上)

   根據https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md的步驟安裝所需的庫和配置環境並測試

   6.接下來在tensorflow/models/research/目錄下執行

wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz
tar -xvf images.tar.gz
tar -xvf annotations.tar.gz

  7.運行

python object_detection/dataset_tools/create_pet_tf_record.py \
    --label_map_path=object_detection/data/pet_label_map.pbtxt \
    --data_dir=`pwd` \
    --output_dir=`pwd`

    這裏很容易遇到

Traceback (most recent call last):
  File "object_detection/dataset_tools/create_pet_tf_record.py", line 41, in <module>
    from object_detection.utils import dataset_util
ModuleNotFoundError: No module named 'object_detection'

    緣由是中間配置環境過程當中其實輸入了這麼一步,可是重開shell以後便失效了

    解決方法是輸入 以下命令

    Linux:

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

    Windows:

set PYTHONPATH=<PATH_TO_MODELS>;<PATH_TO_MODELS>\slim

    再次執行python就行了,執行中可能會遇到一些warnings,直接忽略就行.

    在research目錄下能夠獲得如下兩個文件,如圖所示,在object_detection/data下會生成pb.txt文件

 

   8.以後即是將文件上傳到bucket中,不要忘記

export YOUR_GCS_BUCKET=${takefetter_pets_detector}

   運行

1
2
3
gsutil cp pet_train.record gs: / / ${YOUR_GCS_BUCKET} / data / pet_train.record
gsutil cp pet_val.record gs: / / ${YOUR_GCS_BUCKET} / data / pet_val.record
gsutil cp object_detection / data / pet_label_map.pbtxt gs: / / ${YOUR_GCS_BUCKET} / data / pet_label_map.pbtxt

   等了半天沒有相應,因此我經過瀏覽器建了data文件夾把這三個文件傳上去了.....200多k的上傳和下載真的難受

  9.從頭開始培訓最早進的物體探測器可能須要數天時間,即便使用多個GPU也是如此! 爲了加速訓練,咱們將在不一樣的數據集(COCO)上訓練一個對象檢測器,並從新使用它的一些參數來初始化咱們的新模型。   

wget http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz
tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz
gsutil cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* gs://${YOUR_GCS_BUCKET}/data/

  下載tar文件並上傳其中model.ckpt開頭的三個文件到data目錄下.

  10.以後需將sample中的文件faster_rcnn_resnet101_pets.config文件中帶有gs::///data/的部分所有替換爲gs://${YOUR_GCS_BUCKET}/data/,如

gs://takefetter_pets_detector/data/

   並上傳

至此,咱們的bucket/data目錄下文件如圖所示

接下來就能夠訓練數據集了

  11.在research目錄下使用

python setup.py sdist
(cd slim && python setup.py sdist)

打包tfslim和object detection API,以後運行

複製代碼

gcloud ml-engine jobs submit training `whoami`_object_detection_`date +%s` \
--runtime-version 1.4 \
--job-dir=gs://takefetter_pets_detector/train \
--packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz \
--module-name object_detection.train \
--region us-central1 \
--config object_detection/samples/cloud/cloud.yml \
-- \
--train_dir=gs://takefetter_pets_detector/train \
--pipeline_config_path=gs://takefetter_pets_detector/data/faster_rcnn_resnet101_pets.config

複製代碼

開始訓練,訓練過程當中會遇到日誌中顯示ImportError: No module named matplotlib.pyplot的錯誤,須要在research目錄下生成的setup.py中的REQUIRED_PACKAGES中添加'matplotlib',(方法來自https://stackoverflow.com/questions/47196673/import-error-matplotlib-pyplot)可是又出現了其餘的錯誤,最終在

https://github.com/tensorflow/models/issues/2739下找到了andersskog的方法,改了好多文件終因而跑起來了.

經過

tensorboard --logdir=gs://takefetter_pets_detector

啓動tensorboard服務,能夠打開127.0.0.1:6006查看相關的信息.

 

最終通過2個小時的訓練..... tensorboard上並無顯示和例程中同樣的結果...... 所以我決定先作本身的數據集吧,改天再作 畢竟Google ML Engine收費仍是很貴的.......

確實不知道錯在哪,歡迎在評論中指教。

相關文章
相關標籤/搜索