【caffe-Windows】基于Python多标签方法——VOC2012数据集
前言
按照上一篇博客所遺留的話題:多標簽分類,進行初步探索,此篇博客針對caffe官網(wǎng)的多分類進行配置,只不過是Python接口的,不過官網(wǎng)在開頭說明可以使用HDF5或者LMDB進行操作,只不過Python更加方便罷了
國際慣例,貼參考網(wǎng)址:
官網(wǎng)教程:Multilabel classification on PASCAL using python data-layers
數(shù)據(jù)集(VOC2012)官網(wǎng):Visual Object Classes Challenge 2012 (VOC2012) 或者直接下載壓縮包
【注】雖然大部分和Linux下Python的配置代碼方法一樣,但是Windows下跑此程序還是有些地方需要改動的。
準備工作
【注】這一步主要是為了指定我們的Python是從GPU版本下拷貝過來的,如果會玩Python的同學,請直接指定當前調用的caffe是E:\CaffeDev-GPU\caffe-master\Build\x64\Release\pycaffe這個文件夾編譯好的即可,不會的話按照我的操作,這樣應該會默認調用Anaconda2里面site-packages中的caffe
程序運行
寫在前面,以下程序最好一句一句運行調試,為了方便,我適當寫成了代碼塊運行
在E:\CaffeDev-GPU\caffe-master\examples下新建一個空文件夾multilabel用于存儲我們后面的代碼,在此文件夾目錄下打開jupyter notebook,即cmd命令如下:
E:\CaffeDev-GPU\caffe-master\examples\multilabel>jupyter notebook在打開的頁面中New->Python2,然后逐條語句運行以下程序:
首先導入一些必要的包
import sys import osimport numpy as np import os.path as osp import matplotlib.pyplot as pltfrom copy import copy% matplotlib inline plt.rcParams['figure.figsize'] = (6, 6)然后設置一下caffe相關路徑并導入caffe,問題就在這里,感覺這個import進來的caffe并不是設置的路徑里面的caffe,所以有準備工作中的3操作
caffe_root = '../../' # this file is expected to be in {caffe_root}/examples sys.path.append(caffe_root + 'python') sys.path.append('../../../') import caffe from caffe import layers as L, params as P先看看E:\CaffeDev-GPU\caffe-master\examples\pycaffe下有無tools.py這個文件,并import近來,我在這里卡了一會,主要是一直不會用python代碼引入一個py文件,不過最后還是瞎折騰引入了
sys.path.append('../../examples/pycaffe/layers') # the datalayers we will use are in this directory. sys.path.append('../../examples/pycaffe') # the tools file is in this folder import tools設置數(shù)據(jù)集位置以及待會微調所需要的模型bvlc_reference_caffenet.caffemodel,如果你是按照我的博客學caffe,這個文件應該是已經(jīng)存在于E:\CaffeDev-GPU\caffe-master\models\bvlc_reference_caffenet,如果存在了,以下代碼還是在下載,那你得核對一下上面各種路徑了
# set data root directory, e.g:pascal_root = osp.join(caffe_root, 'data/pascal/VOC2012')# these are the PASCAL classes, we'll need them later.classes = np.asarray(['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'])# make sure we have the caffenet weight downloaded.if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):print("Downloading pre-trained CaffeNet model...")!../scripts/download_model_binary.py ../models/bvlc_reference_caffenet設置caffe的GPU運行模式,這里我也卡了一下,Python一直崩潰,結果發(fā)現(xiàn)是自動調用的caffe是我原來在site-packages編譯的CPU模式caffe,所以這一步出問題,你也得琢磨一下這個細節(jié)
caffe.set_mode_gpu() caffe.set_device(0)以下便是設計網(wǎng)絡結構了,不可能出錯
# helper function for common structuresdef conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):conv = L.Convolution(bottom, kernel_size=ks, stride=stride,num_output=nout, pad=pad, group=group)return conv, L.ReLU(conv, in_place=True)# another helper functiondef fc_relu(bottom, nout):fc = L.InnerProduct(bottom, num_output=nout)return fc, L.ReLU(fc, in_place=True)# yet another helper functiondef max_pool(bottom, ks, stride=1):return L.Pooling(bottom, pool=P.Pooling.MAX, kernel_size=ks, stride=stride)# main netspec wrapperdef caffenet_multilabel(data_layer_params, datalayer):# setup the python data layer n = caffe.NetSpec()n.data, n.label = L.Python(module = 'pascal_multilabel_datalayers', layer = datalayer, ntop = 2, param_str=str(data_layer_params))# the net itselfn.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4)n.pool1 = max_pool(n.relu1, 3, stride=2)n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2)n.pool2 = max_pool(n.relu2, 3, stride=2)n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1)n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2)n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2)n.pool5 = max_pool(n.relu5, 3, stride=2)n.fc6, n.relu6 = fc_relu(n.pool5, 4096)n.drop6 = L.Dropout(n.relu6, in_place=True)n.fc7, n.relu7 = fc_relu(n.drop6, 4096)n.drop7 = L.Dropout(n.relu7, in_place=True)n.score = L.InnerProduct(n.drop7, num_output=20)n.loss = L.SigmoidCrossEntropyLoss(n.score, n.label)return str(n.to_proto())將網(wǎng)絡結構寫入prototxt里面,這里有一個坑就是/和\的問題,我們在程序中盡量用/,因為\經(jīng)常用于做轉義字符使用,所以在prototxt中遇到類似\train\XXXX的文件,將會讀取rain文件夾,因為\t被當成制表符了,這里也卡了我一下,解決方法如下:
workdir = './pascal_multilabel_with_datalayer//' if not os.path.isdir(workdir):os.makedirs(workdir)solverprototxt = tools.CaffeSolver(trainnet_prototxt_path = osp.join(workdir, "trainnet.prototxt"), testnet_prototxt_path = osp.join(workdir, "valnet.prototxt")) solverprototxt.sp['display'] = "1" solverprototxt.sp['base_lr'] = "0.0001" solverprototxt.write(osp.join(workdir, 'solver.prototxt'))# write train net.with open(osp.join(workdir, 'trainnet.prototxt'), 'w') as f:# provide parameters to the data layer as a python dictionary. Easy as pie!data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'train', pascal_root = pascal_root)f.write(caffenet_multilabel(data_layer_params, 'PascalMultilabelDataLayerSync'))# write validation net.with open(osp.join(workdir, 'valnet.prototxt'), 'w') as f:data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'val', pascal_root = pascal_root)f.write(caffenet_multilabel(data_layer_params, 'PascalMultilabelDataLayerSync'))載入網(wǎng)絡結構
solver = caffe.SGDSolver(osp.join(workdir, 'solver.prototxt'))應該會出現(xiàn)下面這個輸出
BatchLoader initialized with 5717 images PascalMultilabelDataLayerSync initialized for split: train, with bs: 128, im_shape: [227, 227]. BatchLoader initialized with 5823 images PascalMultilabelDataLayerSync initialized for split: val, with bs: 128, im_shape: [227, 227].比較奇怪的是下面這幾句話我執(zhí)行的結果是輸出一張啥都沒有的圖,但是官網(wǎng)有圖片被取出來,奇怪。
transformer = tools.SimpleTransformer() # This is simply to add back the bias, re-shuffle the color channels to RGB, and so on... image_index = 100 # First image in the batch. plt.figure() plt.imshow(transformer.deprocess(copy(solver.net.blobs['data'].data[image_index, ...]))) gtlist = solver.net.blobs['label'].data[image_index, ...].astype(np.int) plt.title('GT: {}'.format(classes[np.where(gtlist)])) plt.axis('off'); print(classes)這是我的程序輸出,自行對照官網(wǎng)的輸出
依據(jù)官網(wǎng)所說,必須用一個方法去度量準確率,而在多標簽中常用的方法是海明距離Hamming distance,僅僅需要一個簡單的循環(huán)操作,如下:
def hamming_distance(gt, est):return sum([1 for (g, e) in zip(gt, est) if g == e]) / float(len(gt))def check_accuracy(net, num_batches, batch_size = 128):acc = 0.0for t in range(num_batches):net.forward()gts = net.blobs['label'].dataests = net.blobs['score'].data > 0for gt, est in zip(gts, ests): #for each ground truth and estimated label vectoracc += hamming_distance(gt, est)return acc / (num_batches * batch_size)訓練模型
for itt in range(6):solver.step(100)print 'itt:{:3d}'.format((itt + 1) * 100), 'accuracy:{0:.4f}'.format(check_accuracy(solver.test_nets[0], 50))這里慢慢等,會有輸出的,而且cmd窗口可以看到具體迭代了多少次,Python窗口100次提示一次
itt:100 accuracy:0.9239 itt:200 accuracy:0.9236 itt:300 accuracy:0.9239 itt:400 accuracy:0.9240 itt:500 accuracy:0.9237 itt:600 accuracy:0.9241檢查一下基本準確率
def check_baseline_accuracy(net, num_batches, batch_size = 128):acc = 0.0for t in range(num_batches):net.forward()gts = net.blobs['label'].dataests = np.zeros((batch_size, len(gts)))for gt, est in zip(gts, ests): #for each ground truth and estimated label vectoracc += hamming_distance(gt, est)return acc / (num_batches * batch_size)print 'Baseline accuracy:{0:.4f}'.format(check_baseline_accuracy(solver.test_nets[0], 5823/128))輸出
Baseline accuracy:0.9238可視化一些結果看看
test_net = solver.test_nets[0] for image_index in range(5):plt.figure()plt.imshow(transformer.deprocess(copy(test_net.blobs['data'].data[image_index, ...])))gtlist = test_net.blobs['label'].data[image_index, ...].astype(np.int)estlist = test_net.blobs['score'].data[image_index, ...] > 0plt.title('GT: {} \n EST: {}'.format(classes[np.where(gtlist)], classes[np.where(estlist)]))plt.axis('off')輸出結果分別有
整個程序至此結束,容易出錯的就在于各種路徑的書寫上,請自行核對,必要時使用bat核對錯誤,因為bat調試很少崩潰。下一步研究研究文章開頭說的使用HDF5格式制作多標簽數(shù)據(jù)集并訓練測試看看。
附件
包含代碼,以及對應產(chǎn)出的各種文件(主要是prototxt文件)的程序打包:鏈接:http://pan.baidu.com/s/1nvofWJf 密碼:m1h0
數(shù)據(jù)集請自行去官網(wǎng)下載,可以使用迅雷好像,速度蠻快的。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的【caffe-Windows】基于Python多标签方法——VOC2012数据集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安随享金怎么还款?利息是多少?
- 下一篇: 【theano-windows】学习笔记