建设网站的心得体会,免费建站哪个最好,wordpress页面更新失败,免费推广网站推荐前言
按照上一篇博客所遗留的话题#xff1a;多标签分类#xff0c;进行初步探索#xff0c;此篇博客针对caffe官网的多分类进行配置#xff0c;只不过是Python接口的#xff0c;不过官网在开头说明可以使用HDF5或者LMDB进行操作#xff0c;只不过Python更加方便罢了
国…前言
按照上一篇博客所遗留的话题多标签分类进行初步探索此篇博客针对caffe官网的多分类进行配置只不过是Python接口的不过官网在开头说明可以使用HDF5或者LMDB进行操作只不过Python更加方便罢了
国际惯例贴参考网址
官网教程Multilabel classification on PASCAL using python data-layers
数据集(VOC2012)官网Visual Object Classes Challenge 2012 (VOC2012) 或者直接下载压缩包
【注】虽然大部分和Linux下Python的配置代码方法一样但是Windows下跑此程序还是有些地方需要改动的。
准备工作
此电脑上caffe的路径默认为E:\CaffeDev-GPU\caffe-master\同时在E:\CaffeDev-GPU\caffe-master\data下新建一个文件夹pascal。下载完数据集以后是VOCtrainval_11-May-2012.tar这个压缩文件把此压缩包里VOCdevkit里面的文件夹VOC2012直接拖到上面说的E:\CaffeDev-GPU\caffe-master\data\pascal文件夹中然后就有了这样几个文件夹 确保Python接口已经完全配置成功按照此博客配置即可。由于还不会玩Python(会玩Python的看下面的注即可)所以我把编译好的E:\CaffeDev-GPU\caffe-master\Build\x64\Release\pycaffe\caffe在C:\Users\Bingo\Anaconda2\Lib\site-packages拷贝了一份的同时也将E:\CaffeDev-GPU\caffe-master\Build\x64\Release\pycaffe整个复制到E:\CaffeDev-GPU\caffe-master\python该替换的直接点替换即可。 【注】这一步主要是为了指定我们的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\multilabeljupyter 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设置数据集位置以及待会微调所需要的模型bvlc_reference_caffenet.caffemodel如果你是按照我的博客学caffe这个文件应该是已经存在于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, well 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一直崩溃结果发现是自动调用的caffe是我原来在site-packages编译的CPU模式caffe所以这一步出问题你也得琢磨一下这个细节 caffe.set_mode_gpu()
caffe.set_device(0)以下便是设计网络结构了不可能出错
# helper function for common structuresdef conv_relu(bottom, ks, nout, stride1, pad0, group1):conv L.Convolution(bottom, kernel_sizeks, stridestride,num_outputnout, padpad, groupgroup)return conv, L.ReLU(conv, in_placeTrue)# another helper functiondef fc_relu(bottom, nout):fc L.InnerProduct(bottom, num_outputnout)return fc, L.ReLU(fc, in_placeTrue)# yet another helper functiondef max_pool(bottom, ks, stride1):return L.Pooling(bottom, poolP.Pooling.MAX, kernel_sizeks, stridestride)# 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_strstr(data_layer_params))# the net itselfn.conv1, n.relu1 conv_relu(n.data, 11, 96, stride4)n.pool1 max_pool(n.relu1, 3, stride2)n.norm1 L.LRN(n.pool1, local_size5, alpha1e-4, beta0.75)n.conv2, n.relu2 conv_relu(n.norm1, 5, 256, pad2, group2)n.pool2 max_pool(n.relu2, 3, stride2)n.norm2 L.LRN(n.pool2, local_size5, alpha1e-4, beta0.75)n.conv3, n.relu3 conv_relu(n.norm2, 3, 384, pad1)n.conv4, n.relu4 conv_relu(n.relu3, 3, 384, pad1, group2)n.conv5, n.relu5 conv_relu(n.relu4, 3, 256, pad1, group2)n.pool5 max_pool(n.relu5, 3, stride2)n.fc6, n.relu6 fc_relu(n.pool5, 4096)n.drop6 L.Dropout(n.relu6, in_placeTrue)n.fc7, n.relu7 fc_relu(n.drop6, 4096)n.drop7 L.Dropout(n.relu7, in_placeTrue)n.score L.InnerProduct(n.drop7, num_output20)n.loss L.SigmoidCrossEntropyLoss(n.score, n.label)return str(n.to_proto())将网络结构写入prototxt里面这里有一个坑就是/和\的问题我们在程序中尽量用/因为\经常用于做转义字符使用所以在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))载入网络结构 solver caffe.SGDSolver(osp.join(workdir, solver.prototxt)) 应该会出现下面这个输出 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].比较奇怪的是下面这几句话我执行的结果是输出一张啥都没有的图但是官网有图片被取出来奇怪。 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) 这是我的程序输出自行对照官网的输出 依据官网所说必须用一个方法去度量准确率而在多标签中常用的方法是海明距离Hamming distance仅仅需要一个简单的循环操作如下 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格式制作多标签数据集并训练测试看看。
附件
包含代码以及对应产出的各种文件主要是prototxt文件的程序打包链接http://pan.baidu.com/s/1nvofWJf 密码m1h0
数据集请自行去官网下载可以使用迅雷好像速度蛮快的。