如何在 Ubuntu 19.10(CPU) 環境中快速安裝 Caffe 1.0 framework及Python 3.7.6並測試Python程式碼

動機:想在 Ubuntu 19.10 (CPU) 中安裝 Caffe,要如何做呢?!

準備環境:如下圖
1.OS: Ubuntu 19.10
2.CPU: Intel(R) Core(TM) i5-3340 @ 3.10GHz
3.Python: 3.7.6
4.Anaconda3: 2019.10 for Linux 64-bit
5.Conda: 4.8.1
6.TensorFlow: 1.15.0
7.OpenCV: 3.4.2
8.Caffe: 1.0

操作步驟
1.下載 Anaconda3 2019.10 for Linux 64-bit Installer: 到官網(https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh)下載並安裝,指令如下:
$ wget https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh
$ sh Anaconda3-2019.10-Linux-x86_64.sh
2.下載 Caffe 源碼: 到官網(https://github.com/BVLC/caffe)下載,指令如下:
$ git clone https://github.com/BVLC/caffe.git

3.建立 Conda 虛擬環境: 指令如下: (多種軟體一條龍一次到位)
$ conda create -n caffe_cpu -c defaults python=3.7 caffe opencv tensorflow=1

4.完成上述環境建置後,就可以測試 Python程式範例碼(-:參攷1.程式並修改:-)...如下:
首先要下載model檔並移動到指定的目錄,指令如下:
(caffe_cpu) user@hostname:~/caffe$ wget http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

(caffe_cpu) user@hostname:~/caffe$ mv bvlc_reference_caffenet.caffemodel models/bvlc_reference_caffenet/

其次,執行程式,如下( $ python imagenet.py ):
# This Python file uses the following encoding: utf-8
import numpy as np
import sys, os
import time
import matplotlib.pyplot as plt
import caffe

# 開始計時
since = time.time()
# 設定目前的工作環境在 caffe 目錄下
caffe_root = '/home/user/caffe/'
# 新增 caffe/python 到目前的環境
sys.path.insert(0, caffe_root + 'python')
# 切換工作目錄
os.chdir(caffe_root)

# 設定Caffe網路結構
net_file = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'

# 新增訓練後的引數
# 請自行下載, 地址:http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel
caffe_model = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
# 均值檔案
mean_file = caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'
# 新增上述兩變數,建構一個Caffe Net
net = caffe.Net(net_file, caffe_model, caffe.TEST)

# 得到data的shape,此圖片是由預設 matplotlib 底層載入的
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# matplotlib 載入的image是[0-1]像素, 圖片的資料格式[weight, high, channels], RGB
# caffe 載入的image是[0-255]像素, 圖片的資料格式[channels, weight, high], BGR
# 需要轉換, channels 放到前面 => (channels=2, weight=0, high=1)
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
# 將圖片像素放大到[0-255]
transformer.set_raw_scale('data', 255)
# 原始圖片RGB格式 => Caffe圖片BGR格式 轉換
transformer.set_channel_swap('data', (2, 1, 0))

# 讀取範例貓圖片
im = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
# 圖片預處理
net.blobs['data'].data[...] = transformer.preprocess('data', im)

# caffe.set_mode_gpu()
# 網路開始向前傳播
out = net.forward()

# 計算耗時
time_elapsed = time.time() - since
minute = time_elapsed / 60
seconds = time_elapsed % 60
# 畫分隔線
print('=' * 55)
print('Elapsed time is: %.4f minute : %.4f seconds' % (minute, seconds))

# 載入標籤label檔
imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')

# 輸出結果: 載入的圖片是屬於那一種分類的機率(列表表示)
output_prob = out['prob'][0]
# 找出其中 最有可能=最大 分類的機率
outmax = output_prob.argmax()
print('Predicted class is: [%d => %s]' % (outmax, labels[outmax]))
print('=' * 55)

最後,預測結果(tabby cat)畫面,如下:
後記
執行上述 python 程式碼時會有一個bug,錯誤訊息為:TypeError: _open() got an unexpected keyword argument 'as_grey',如下圖
只要依照錯誤指示修改其檔案(296行,改為 as_gray)即可解決,如下
$ nano ~/anaconda3/envs/caffe_cpu/lib/python3.7/site-packages/caffe/io.py


心得CaffeUbuntu環境中安裝是容易的(相對於Windows10),對於它的架構及程式練習更是許多機器學習愛好者朝聖之地,畢竟它是華人之光(贾扬清)在Berkeley分校做的~~~


參攷
1.windows10+python3.5下同時安裝caffe和tensorflow, https://www.itread01.com/content/1547846847.html
2.Tabby cat, https://en.wikipedia.org/wiki/Tabby_cat
3.贾扬清, https://baike.baidu.com/item/%E8%B4%BE%E6%89%AC%E6%B8%85/23360837

留言