适用HDF5shuj数据源。示例:检测人脸眼、鼻位置。
type : 须设定HDF5Data
source : 指定HDF5数据源。
文件夹中:
image文件夹中格式:
label数据文件:
格式: 图片名+数据(空格隔开)
import h5py
#库导入,ubuntu默认安装
import os
import cv2
import math
import numpy as np
import random
import re
root_path = "/home/tyd/caffe_case/HDF5/image"
#图像存储位置
with open("/home/tyd/caffe_case/HDF5/hdf5.txt",'r') as f:
#打开label文件
lines = f.readlines()
num = len(lines)
random.shuffle(lines)
#洗牌
imgAccu = 0
imgs = np.zeros([num,3,224,224])
lables = np.zeros([num,10])
for i in range(num):
line = lines[i]
segments = re.split('ls+',line)[:-1]
print segments[0]
img = cv2.imread(os.path.join(root_path,segments[0]))
#读入图片
img = cv2.resize(img,(224,224))
img = img.transpose(2,0,1)
#caffe:c,h,w ; cv:h,w,c
imgs[i,:,:,:] = img.astype(np.float32)
for j in range(10):
labels[i,j] = float(segments[j+1])*224/256
#label跟随图片resize
batchSize = 1
batchNum = int(math.ceil(1.0*num/batchSize))
imgsMean = np.mean(imgs,axis=0)
#imgs = (imgs - imgsMean)/255.0
labelMean = np.mean(labels,axis=0)
labels = (labels - labelsMean)/10
#0中心化,label减去mean值最后须加回
if os.path.exists('trainlist.txt'):
os.remove('testlist.txt')
if os.path.exists('testlist.txt'):
os.remove('testlist.txt')
comp_kwargs = {'compression':'gzip','compression_opts':1}
for i in range(batchNum):
start = i*batchSize
end min((i+1)*batchSize,num)
if i < batcjhNum - 1:
filename = '/home/tyd/caffe_case/HDF5/h5/train{0}.h5'
else:
filename = '/home/tyd/caffe_case/HDF5/h5/test{0}.h5'
print filename
with h5py.File(filename,'w') as f:
f.create_dataset('data',data = np.array((imgs[start:end]-imgsMean)/255.0)astype(np.float32),**comp_kwargs)
f.create_dataset('label',data = np.array(labels[start:end]).astype(np.float32),**comp_kwars)
#制作。设置名字、data,GPU运算通常要求float32格式。
if i < batchNum - 1:
with open('/home/tyd/caffe_case/HDF5/h5/trainlist.txt','a') as f:
f.write(os.path.join(os.getcwd(),'train{0}.h5').format(i)+'\n')
else:
with open('/home/tyd/caffe_case/HDF5/h5/trainlist.txt','a') as f:
f.write(os.path.join(os.getcwd(),'train{0}.h5').format(i-batchNum+1)+'\n')
#打开文件夹进行写入
imgsMean = np.mean(imgsMean,axis=(1,2))
with open('mean.txt','w') as f:
f.write(str(imgsMean[0])) + '\n' + str(mgsMean[1]) + '\n' + str(imgsMean[2]))
testlist.txt中存储test0.h5路径,trainlist.txt存储train0.h5、train1.h5路径。
指定caffe可执行文件,solver文件路径
time参数,观察时间xiao消耗进行改进。
Iteration:1 forward-backward time: 145 ms
一次迭代前向传播+反向传播消耗145ms
每一层的前向、反向传播分别花费多长时间
前向、反向平均时间,总时间。
打开Makefile.config文件,打开WITH_PYTHON_LAYER开关。
cd caffe
make clean
make all -j8
make pycaffe
网络文件中维度
import sys
caffe root='/home/tydcaffe/'
sys.path.insert(0, caffe_root+'python')
import caffe
import numpy as np
import yaml
import cv2
class MyLayer(caffe.Layer):
#类名与配置文件层名一致
'''四个规定函数'''
def setup(self, bottom, top):
'''准备工作'''
self.num = yaml.load(self.param_str)["num"]
print "Parameter num :",self num
def reshape(self, bottom, top):
pass
def forward(self, bottom, top):
'''前向传播'''
top[0].reshape(*bottom[0].shape)
#输出格式
print bottom[0].data.shape
print bottom[0].data.shape
top[0].data[...] = bottom[0].data + self.num
print top[0].data[...]
def backward(self, top, propagate_down,bottom):
'''反向传播'''
pass
net = caffe.Net('conv.prototxt',caffe.TEST)
#加载网络层
im = np.array(cv2.imread('timg.jpeg'))
im_input = im[np.newaxis,:,:]
#加一维
im_input2 = im_input.transpose((0, 3, 1, 2))
#顺序变换
net.blobs['data'].reshape(*im_input2.shape)
#使用当前shape
net.blobs['data'].data[...] = im_input2
net.forward()