import sys
import osimport numpy as np
import os.path as osp
import matplotlib.pyplot as plt
from copy import copy% matplotlib inline
plt.rcParams['figure.figsize'] = (6, 6)# 改成自己的caffe路徑
caffe_root = '/home/xhb/caffe/caffe/'# this file is expected to be in {caffe_root}/examples
sys.path.append(caffe_root + 'python')
import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.from caffe import layers as L, params as P # Shortcuts to define the net prototxt.# 修改一下當前路徑,修改到caffe/examples路徑下,才能找到pycaffe文件夾
os.chdir(os.path.join(caffe_root, 'examples'))sys.path.append("pycaffe/layers") # the datalayers we will use are in this directory.
sys.path.append("pycaffe") # the tools file is in this folderimport tools #this contains some tools that we need
# print os.path.join(workdir, 'solver.prototxt')# solver_path = os.path.join(caffe_root, 'examples', 'pascal_multilabel_with_datalayer', 'solver.prototxt')
solver = caffe.SGDSolver(os.path.join(workdir, 'solver.prototxt'))
# solver = caffe.SGDSolver(solver_path)
solver.net.copy_from(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')
solver.test_nets[0].share_with(solver.net)
solver.step(1)
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 = 0
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')
(-0.5, 226.5, 226.5, -0.5)
defhamming_distance(gt, est):return sum([1for (g, e) in zip(gt, est) if g == e]) / float(len(gt))defcheck_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 it in range(6):solver.step(100)print'iter:{:3d}'.format((it+1) * 100), 'accuracy:{0:.4f}'.format(check_accuracy(solver.test_nets[0], 50))
iter:100 accuracy:0.9523
iter:200 accuracy:0.9569
iter:300 accuracy:0.9580
iter:400 accuracy:0.9586
iter:500 accuracy:0.9591
iter:600 accuracy:0.9593