用caffecaffe 停止训练参数cnn怎么调参

用caffe训练cnn怎么调参_百度知道
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。
用caffe训练cnn怎么调参
提示该问题下回答为网友贡献,仅供参考。
我有更好的答案
affe是目前深度学习比较优秀好用的一个开源库,采样c++和CUDA实现,具有速度快,模型定义方便等优点。学习了几天过后,发现也有一个不方便的地方..。Caffe的数据层可以从数据库(支持leveldb,就是在我的程序中调用Caffe做图像分类没有直接的接口
Caffe,全称Convolutional Architecture for Fast Feature Embedding,是一个计算CNN相关算法的框架 我百度的 那么关系就很明了了 一个是主体 一个是说明
为您推荐:
等待您来回答使用caffe训练自己的CNN
时间: 13:56:50
&&&& 阅读:976
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&&
现在有这样的一个场景:给你一张行人的矩形图片, 要你识别出该行人的性别特侦。
(1),行人的姿态各异,变化多端。很难提取图像的特定特征
(2),正常人判别行人的根据是身材比例。(如果是冬天的情况下,行人穿着厚实,性别识别更加难)
solution:
针对难以提取特定特征的图像,可以采用卷积神经网络CNN去自动提取并训练。
数据准备:&
采用 PETA数据集,Pedestrain Attribute Recognition At Far Distance。 该数据集一共包含了19000张标记了行人穿着及性别信息的图片。&
Peta dataset source url: & http://mmlab.ie.cuhk.edu.hk/projects/PETA.html
数据处理:
针对下载解压之后的数据集,采用的流程是:
(1)对每一张图片进行resize, resize到特定的大小(实验中定为50*150),&
(2)对正类负类样本的不均衡情况,进行rebalance处理,实验中对少数类样本进行随机选择n张进行data augmentation之后重新加入到dataset中。&
(3)划分training set和testing set, 根据train/test ratio将整个数据样本随机分为两部分。
(4)对training set 进行data augmentation 处理。 扩大训练数据量。 (操作包括: 翻转,滤波等)
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
## The python code to preprocess the images and resize them into (50, 150)
import os, sys, cv2
import numpy as np
import random
image_cnt = 0
MIN_HEIGHT = 120
MIN_WIDTH = 40
targetLabel = []
positive_cnt = 0
negative_cnt = 0
def readImage( filePath , targetDir ):
global image_cnt, positive_cnt, negative_cnt
global targetLabel
if not os.path.isdir( filePath ):
print(‘{} is not a dir‘.format(filePath))
return None
listFile = os.listdir( filePath )
labelDict = {}
with open( filePath + ‘Label.txt‘, ‘r‘) as reader:
for line in reader:
lines = line.split()
for i in range(1, len(lines)):
if lines[i] == ‘personalMale‘:
elif lines[i] == ‘personalFemale‘:
labelDict[lines[0]] = label
for i in range(len(listFile)):
if len(listFile[i]) & 4 and (listFile[i][-4:] == ‘.bmp‘ or listFile[i][-4:] == ‘.jpg‘ or
listFile[i][-4:] == ‘.png‘ or listFile[i][-5:] == ‘.jpeg‘):
imageName = filePath + listFile[i]
img = cv2.imread( imageName )
if not img.data:
height, width = img.shape[:2]
if height & MIN_HEIGHT or width & MIN_WIDTH:
fileName = str( image_cnt ) + ‘.jpeg‘
identity = listFile[i].find(‘_‘)
identity == -1:
identity = len(listFile[i])
idd = listFile[i][:identity]
if labelDict.has_key( idd ) :
targetLabel.append([ fileName, labelDict[idd]])
if labelDict[idd] == 0:
negative_cnt += 1
positive_cnt += 1
img = cv2.resize(img, (50, 150), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(targetDir + fileName, img)
image_cnt += 1
print(‘file {} do not have label‘.format(listFile[i]) )
####### pyramid operator
def MinAndEnlarge(img, Minus_pixel = 3):
img = img[(3*Minus_pixel):(150 - 3*Minus_pixel), Minus_pixel:(50 - Minus_pixel), :]
img = cv2.resize(img, (50, 150), interpolation = cv2.INTER_CUBIC )
return img
####### rotate operator
def Flip(img, operator = 1):
if operator == 1:
img = cv2.flip(img, 1)
img = cv2.flip(img, 0)
return img
####### median blurring the image
def Blur(img, kernel_size=5):
img = cv2.medianBlur(img, kernel_size)
return img
def EnlargeData( filePath , targetDir ):
global image_cnt, targetLabel
total_sample = len(targetLabel)
for i in range(total_sample):
img = cv2.imread( filePath + targetLabel[i][0] )
fileLabel = targetLabel[i][1]
if not img.data:
print(‘no exits image file {}‘.format( filePath + targetLabel[i][0]) )
img1 = MinAndEnlarge(img, 3)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img1 )
image_cnt += 1
targetLabel.append( [fileName, fileLabel] )
img2 = Flip(img1)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img2 )
image_cnt += 1
targetLabel.append( [fileName, fileLabel] )
img3 = Blur(img, 5)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img3 )
image_cnt += 1
targetLabel.append( [fileName, fileLabel] )
img4 = Blur(img1, 5)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img4 )
image_cnt += 1
targetLabel.append([fileName, fileLabel])
img5 = Blur(img2, 5)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img5 )
image_cnt += 1
targetLabel.append([fileName, fileLabel])
print(‘The total number of images is {}‘.format(image_cnt))
def saveLabel( targetDir ):
global targetLabel
with open(targetDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(targetLabel)):
writer.write( str( targetLabel[i][0] ) + ‘ ‘ + str(targetLabel[i][1]) + ‘\n‘ )
##### ReBalance operator
num (the number of minority class should added)
n_or_p (the label of minority class)
op_chose( 1-- 0-- )
def ReBalance( targetDir, num, n_or_p, op_chose = 0):
global targetLabel, image_cnt
total_sample = len(targetLabel)
Contain = {}
if num &= 0:
key_id = random.randint(0, total_sample-1)
if Contain.has_key( key_id ) or targetLabel[key_id][1] != n_or_p:
img = cv2.imread( targetDir + targetLabel[key_id][0] )
if op_chose == 0:
img = cv2.flip(img, 1)
elif op_chose == 1:
img = cv2.flip(img, 0)
fileName = str(image_cnt) + ‘.jpeg‘
cv2.imwrite(targetDir + fileName, img)
image_cnt += 1
targetLabel.append([fileName, n_or_p])
print(‘Finish add {} images‘.format(image_cnt - total_sample))
print(‘Now the class is balanced and total num is {}‘.format(image_cnt))
print(‘image_cnt is {} and len(_targetLabel_) is {} ‘.format(image_cnt, len(targetLabel)))
def divide( targetDir, trainDir, testDir, test_ratio = 0.20):
global targetLabel
total_sample = len(targetLabel)
assert( test_ratio & 1)
test_num = int(total_sample * test_ratio )
test_half_num = test_num // 2; ml_cnt = 0; fm_cnt = 0
testLabel = [] ; trainLabel = []
for i in range(total_sample):
ml_cnt & test_half_num and targetLabel[i][1] == 1:
ml_cnt += 1
img = cv2.imread( targetDir + targetLabel[i][0] )
cv2.imwrite( testDir +
targetLabel[i][0], img )
testLabel.append(targetLabel[i])
elif fm_cnt & test_half_num and targetLabel[i][1] == 0:
fm_cnt += 1
img = cv2.imread( targetDir + targetLabel[i][0] )
cv2.imwrite( testDir +
targetLabel[i][0], img )
testLabel.append(targetLabel[i])
img = cv2.imread( targetDir + targetLabel[i][0] )
cv2.imwrite( trainDir + targetLabel[i][0], img )
trainLabel.append(targetLabel[i])
with open( trainDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(trainLabel)):
writer.write( str( trainLabel[i][0] ) + ‘ ‘ + str(trainLabel[i][1]) + ‘\n‘ )
with open( testDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(testLabel)):
writer.write( str(testLabel[i][0]) + ‘ ‘ + str(testLabel[i][1]) + ‘\n‘)
print(‘has divide into train with {} samples and test with {} samples‘.format(len(trainLabel), len(testLabel)) )
return trainLabel, testLabel
def DivideSet( targetDir, trainDir, testDir, test_ratio = 0.20):
global targetLabel
total_sample = len(targetLabel)
assert( test_ratio & 1 )
test_num = int(test_ratio * total_sample)
test_half_num = test_num //2 ; ml_cnt = test_half_ fm_cnt = test_half_num
testLabel = [] ; trainLabel = [] ; testDict = {}
while ml_cnt & 0 or fm_cnt & 0:
idd = random.randint(0, total_sample-1)
if testDict.has_key( targetLabel[idd][0] ):
if targetLabel[idd][1] == 1 and ml_cnt & 0:
img = cv2.imread( targetDir + targetLabel[idd][0] )
cv2.imwrite( testDir + targetLabel[idd][0], img )
testLabel.append( targetLabel[idd] )
testDict[targetLabel[idd][0]] = idd
ml_cnt -= 1
if targetLabel[idd][1] == 0 and fm_cnt & 0:
img = cv2.imread( targetDir + targetLabel[idd][0] )
cv2.imwrite( testDir + targetLabel[idd][0], img )
testLabel.append( targetLabel[idd] )
testDict[targetLabel[idd][0]] = idd
fm_cnt -= 1
for i in range(total_sample):
if not testDict.has_key( targetLabel[i][0] ):
trainLabel.append( targetLabel[i] )
img = cv2.imread( targetDir + targetLabel[i][0] )
cv2.imwrite( trainDir + targetLabel[i][0], img )
## save the trainset and testset
with open( trainDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(trainLabel)):
writer.write( str( trainLabel[i][0] ) + ‘ ‘ + str(trainLabel[i][1]) + ‘\n‘ )
with open( testDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(testLabel)):
writer.write( str(testLabel[i][0]) + ‘ ‘ + str(testLabel[i][1]) + ‘\n‘)
print(‘has divide into train with {} samples and test with {} samples‘.format(len(trainLabel), len(testLabel)) )
return trainLabel, testLabel
def EnlargeTrain( fileDir, targetDir, trainLabel , start_cnt):
total_sample = len(trainLabel)
new_cnt = start_cnt
for i in range(total_sample):
img = cv2.imread( fileDir + trainLabel[i][0] )
fileLabel = trainLabel[i][1]
if not img.data:
print(‘no exits image file {}‘.format( fileDir + trainLabel[i][0]) )
img1 = MinAndEnlarge(img, 3)
fileName = str(new_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img1 )
new_cnt += 1
trainLabel.append( [fileName, fileLabel] )
img2 = Flip(img1)
fileName = str(new_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img2 )
new_cnt += 1
trainLabel.append( [fileName, fileLabel] )
img3 = Blur(img, 5)
fileName = str(new_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img3 )
new_cnt += 1
trainLabel.append( [fileName, fileLabel] )
img4 = Blur(img1, 5)
fileName = str(new_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img4 )
new_cnt += 1
trainLabel.append([fileName, fileLabel])
img5 = Blur(img2, 5)
fileName = str(new_cnt) + ‘.jpeg‘
cv2.imwrite( targetDir + fileName, img5 )
new_cnt += 1
trainLabel.append([fileName, fileLabel])
print(‘The total number of training images is {}‘.format(new_cnt))
with open( targetDir + ‘label.txt‘, ‘w‘) as writer:
for i in range(len(trainLabel)):
writer.write( str( trainLabel[i][0] ) + ‘ ‘ + str(trainLabel[i][1]) + ‘\n‘ )
print(‘The trainLabel size is {}‘.format(len(trainLabel)) )
if __name__ == ‘__main__‘:
fileHead = ‘/home/zhangyd/source/PETA_dataset/‘
filePath = [‘3DPeS‘, ‘CAVIAR4REID‘, ‘CUHK‘, ‘GRID‘,‘MIT‘, ‘PRID‘,‘SARC3D‘,‘TownCentre‘, ‘VIPeR‘,‘i-LID‘]
savePath = ‘/home/zhangyd/source/peta/‘
for i in range(len(filePath)):
path = fileHead + filePath[i] + ‘/archive/‘
print (‘runing dataset {}‘.format(filePath[i]) )
readImage( path, savePath )
print (‘The cnt is {}‘.format( image_cnt ))
#EnlargeData( savePath, savePath )
saveLabel( savePath )
print( ‘we have {} positive labels and {} negative labels ‘.format( positive_cnt, negative_cnt ))
if positive_cnt & negative_cnt:
add_num = positive_cnt - negative_cnt
ReBalance( savePath, add_num, 0, 0)
add_num = negative_cnt - positive_cnt
ReBalance( savePath, add_num, 1, 0)
print(‘The total dataset is in {}‘.format(savePath))
TrainsavePath = ‘/home/zhangyd/source/peta_v1/petaTrain/‘
TestsavePath = ‘/home/zhangyd/source/peta_v1/petaTest/‘
trainLabel, testLabel =
DivideSet(savePath, TrainsavePath, TestsavePath, 0.2 )
start_cnt = len(targetLabel)
EnlargeTrain( TrainsavePath, TrainsavePath, trainLabel, start_cnt )
print(‘the end‘)
使用caffe的create_lmdb.sh 转换图像数据 成 imbd数据集。
定义 prototxt 等信息。&
CNN 的结构是:&
训练的参数设置:
# The train/test net protocol buffer definition
net: "examples/peta/petanet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/peta/petanet"
# solver mode: CPU or GPU
solver_mode: GPU
使用论文《Learned vs. Hand-Crafted Features for Pedestrian Gender&Recognition》中的网络结构,取得了较好的训练结果:
I:32.98 solver.cpp:337] Iteration 10000, Testing net (#0)
I:34.98 solver.cpp:404]
Test net output #0: accuracy = 0.8616
I:34.98 solver.cpp:404]
Test net output #1: loss = 0.721973 (* 1 = 0.721973 loss)
I:34.98 solver.cpp:322] Optimization Done.
I:34.98 caffe.cpp:254] Optimization Done.
实验分析:
因为网络不大,网络也比较简单,在GPU下进行训练,消耗的显存大概是几百M,不到1G的显存。网络结构经典,也取得较好的训练结果。
我的拓展: 自己设计的CNN网络
吸取了GoogleNet的网络特征, 引入inception, 重新设计网络。&
是两个 inception 组成, 后面加上一个FC层。&
其中Snapshot的网络结构prototxt文件是:
name: "petaNet"
name: "data"
type: "Input"
top: "data"
input_param {
### ------------
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
lr_mult: 1
lr_mult: 2
convolution_param {
num_output: 20
kernel_size: 3
weight_filler {
type: "xavier"
bias_filler {
type: "constant"
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
# Inception 3a
name: "inc1_conv1"
bottom: "conv1"
top: "inc1_conv1"
type: "Convolution"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 20
kernel_size: 7
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv1_relu"
type: "ReLU"
bottom: "inc1_conv1"
top: "inc1_conv1"
name: "inc1_conv2_1"
type: "Convolution"
bottom: "conv1"
top: "inc1_conv2_1"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 50
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv2_1_relu"
type: "ReLU"
bottom: "inc1_conv2_1"
top: "inc1_conv2_1"
name: "inc1_conv2_2"
type: "Convolution"
bottom: "inc1_conv2_1"
top: "inc1_conv2_2"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 50
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv2_2_relu"
type: "ReLU"
bottom: "inc1_conv2_2"
top: "inc1_conv2_2"
name: "inc1_conv2_3"
type: "Convolution"
bottom: "inc1_conv2_2"
top: "inc1_conv2_3"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 50
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv2_3_relu"
type: "ReLU"
bottom: "inc1_conv2_3"
top: "inc1_conv2_3"
name: "inc1_conv3_1"
type: "Convolution"
bottom: "conv1"
top: "inc1_conv3_1"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 20
kernel_size: 5
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv3_1_relu"
type: "ReLU"
bottom: "inc1_conv3_1"
top: "inc1_conv3_1"
name: "inc1_conv3_2"
type: "Convolution"
bottom: "inc1_conv3_1"
top: "inc1_conv3_2"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 20
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc1_conv3_2_relu"
type: "ReLU"
bottom: "inc1_conv3_2"
top: "inc1_conv3_2"
name: "inc1_concat"
type: "Concat"
bottom: "inc1_conv1"
bottom: "inc1_conv2_3"
bottom: "inc1_conv3_2"
"inc1_concat"
#-----end of Inception 3a
name: "pool1"
type: "Pooling"
bottom: "inc1_concat"
top: "pool1"
pooling_param {
kernel_size: 2
# Inception 2B
name: "inc2_conv1_1"
type: "Convolution"
bottom: "pool1"
top: "inc2_conv1_1"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 120
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc2_conv1_1_relu"
type: "ReLU"
bottom: "inc2_conv1_1"
top: "inc2_conv1_1"
name: "inc2_conv1_2"
type: "Convolution"
bottom: "inc2_conv1_1"
top: "inc2_conv1_2"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 120
kernel_size: 3
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc2_conv1_2_relu"
type: "ReLU"
bottom: "inc2_conv1_2"
top: "inc2_conv1_2"
name: "inc2_conv2"
type: "Convolution"
bottom: "pool1"
top: "inc2_conv2"
param { lr_mult: 1 }
param { lr_mult: 2 }
convolution_param {
num_output: 120
kernel_size: 5
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
name: "inc2_conv2_relu"
type: "ReLU"
bottom: "inc2_conv2"
top: "inc2_conv2"
name: "inc2_concat"
type: "Concat"
bottom: "inc2_conv1_2"
bottom: "inc2_conv2"
top: "inc2_concat"
##----end of Inception 2B
name: "pool2"
type: "Pooling"
bottom: "inc2_concat"
top: "pool2"
pooling_param {
kernel_size: 2
name: "fc1"
type: "InnerProduct"
bottom: "pool2"
top: "fc1"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 2
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
#### ----------
name: "prob"
type: "Softmax"
bottom: "fc1"
top: "prob"
取得的效果比论文中的网络结构差点, 训练结果是:
I:42.95 solver.cpp:317] Iteration 10000, loss = 0.0678897
I:42.95 solver.cpp:337] Iteration 10000, Testing net (#0)
I:06.95 solver.cpp:404]
Test net output #0: accuracy = 0.8448
I:06.95 solver.cpp:404]
Test net output #1: loss = 0.614111 (* 1 = 0.614111 loss)
I:06.95 solver.cpp:322] Optimization Done.
I:06.95 caffe.cpp:254] Optimization Done.
实验分析:
因为该网络的组成较为复杂, inception包含着较大的子网络, 因为训练的时候,需要消耗GPU显存为3G多。训练时间也较长些。
reference: &
Learned vs. Hand-Crafted Features for Pedestrian Gender&Recognition &&Grigory Antipov,Sid-Ahmed Berrani标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/zhang-yd/p/5966293.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!1.配置caffe环境
[请参考此篇博客:]
本篇介绍如何在caffe环境下,实现&图像对图像&的卷积神经网络的训练。
2.文件结构
在配置好的caffe文件夹中,进入examples目录,创建CNN文件夹,并进入文件夹
$ cd caffe-master/examples/
$ mkdir CNN
在CNN文件夹下面创建子文件夹
$ mkdir model snapshot TestPhotos TestLabels TrainPhotos TrainLabels
model用于以后存储卷积核矩阵和偏置向量;
snapshot用于存储训练中备份的caffe模型,每一段时间存储一次,防止断电等一些情况;
TrainPhotos、TrainLabels分别存储训练集输入和监督样本;
TestPhotos、TestLabels分别存储测试集输入和监督样本,不直接参与到训练中。
然后,将训练所用的输入样本和监督样本分别放入到TrainPhotos和TrainLabels中去。注意,样本文件名无所谓,但是排列次序必须一一对应。同样,将测试所用的输入样本和监督样本分别放入到TestPhotos和TestLabels中去。
3.产生训练和测试数据
1.)产生路径文件
在CNN文件夹下面(以下均是在此文件夹下)创建两个路径文件。
$ vim train.txt
输入内容:
examples/CNN/train.h5
:wq保存文档。
$ vim test.txt
输入内容:
examples/CNN/test.h5
:wq保存文档。
2.)产生训练数据
$ vim generate_train.m
输入内容:(把输入图像切成11*11的像素块,监督图像为3*3的像素块(由网络结构和卷积核大小决定),步长为1个像素)
%% settings
folder_input = 'TrainPhotos';
folder_label = 'TrainLabels';
savepath = 'train.h5';
size_input = 11;
size_label = 3; % size_input - 12
stride = 1;
%% initialization
data = zeros(size_input, size_input, 1, 1);
label = zeros(size_label, size_label, 1, 1);
padding = abs(size_input - size_label) / 2;
count = 0;
%% read data
filepaths_input = dir(fullfile(folder_input,'*.jpg'));
filepaths_label = dir(fullfile(folder_label,'*.jpg'));
if (length(filepaths_input)==length(filepaths_label))
length = length(filepaths_input);
error('The Number of Input is NOT equal to the Number of Label.');
%% generate data
for i = 1 : length
im_input = imread(fullfile(folder_input,filepaths_input(i).name));
im_input = rgb2ycbcr(im_input);
im_input = im2double(im_input(:, :, 1));
im_label = imread(fullfile(folder_label,filepaths_label(i).name));
im_label = im2double(im_label(:, :, 1));
if size(im_input) == size(im_label)
[hei,wid] = size(im_input);
error('The size of input and label are not equal.');
for x = 1 : stride : hei-size_input+1
for y = 1 :stride : wid-size_input+1
subim_input = im_input(x : x+size_input-1, y : y+size_input-1);
subim_label = im_label(x+padding : x+padding+size_label-1, y+padding : y+padding+size_label-1);
count = count + 1;
data(:, :, 1, count) = subim_
label(:, :, 1, count) = subim_
%% randomized the data and label
order = randperm(count);
data = data(:, :, 1, order);
label = label(:, :, 1, order);
%% writing to HDF5
chunksz = 128;
created_flag =
totalct = 0;
for batchno = 1:floor(count/chunksz)
last_read=(batchno-1)*
batchdata = data(:,:,1,last_read+1:last_read+chunksz);
batchlabs = label(:,:,1,last_read+1:last_read+chunksz);
startloc = struct('dat',[1,1,1,totalct+1], 'lab', [1,1,1,totalct+1]);
curr_dat_sz = store2hdf5(savepath, batchdata, batchlabs, ~created_flag, startloc, chunksz);
created_flag =
totalct = curr_dat_sz(end);
h5disp(savepath);
终端下输入:
$ matlab -nodesktop -nosplash -logfile generate_train.log
-r generate_train
产生训练数据train.h5。
3.)产生测试数据
$ vim generate_test.m
generate_test.m只需要将generate_test.m文件开头改为:
%% settings
folder_input = 'TestPhotos';
folder_label = 'TestLabels';
savepath = 'test.h5';
size_input = 11;
size_label = 3;
stride = 30;
将最后一段改成:
%% writing to HDF5
chunksz = 2;
created_flag =
totalct = 0;
for batchno = 1:floor(count/chunksz)
last_read=(batchno-1)*
batchdata = data(:,:,1,last_read+1:last_read+chunksz);
batchlabs = label(:,:,1,last_read+1:last_read+chunksz);
startloc = struct('dat',[1,1,1,totalct+1], 'lab', [1,1,1,totalct+1]);
curr_dat_sz = store2hdf5(savepath, batchdata, batchlabs, ~created_flag, startloc, chunksz);
created_flag =
totalct = curr_dat_sz(end);
h5disp(savepath);
终端下输入:
$ matlab -nodesktop -nosplash -logfile generate_test.log -r generate_test
产生测试数据test.h5。仅仅用于判断训练到达什么地步。
4.建立训练文件
1.)建立solver文件
$ vim CNN_solver.prototxt
此为运行的配置文件,输入以下内容:
# The train/test net protocol buffer definition
net: &examples/CNN/CNN_net.prototxt&
test_iter: 556
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.0001
momentum: 0.9
weight_decay: 0
# The learning rate policy
lr_policy: &fixed&
# Display every 100 iterations
display: 100
# The maximum number of iterations
# snapshot intermediate results
snapshot: 500
snapshot_prefix: &examples/CNN/snapshot/CNN&
# solver mode: CPU or GPU
solver_mode: GPU
:wq保存退出。
2.)建立net文件
$ vim CNN_net.prototxt
此为网络结构配置文件,可以配置网络层数,节点数,卷积核等参数。输入以下内容:
name: &CNN&
name: &data&
type: &HDF5Data&
top: &data&
top: &label&
hdf5_data_param {
source: &examples/CNN/train.txt&
batch_size: 128
include: { phase: TRAIN }
name: &data&
type: &HDF5Data&
top: &data&
top: &label&
hdf5_data_param {
source: &examples/CNN/test.txt&
batch_size: 2
include: { phase: TEST }
name: &conv1&
type: &Convolution&
bottom: &data&
top: &conv1&
lr_mult: 1
lr_mult: 0.1
convolution_param {
num_output: 128
kernel_size: 5
weight_filler {
type: &gaussian&
std: 0.001
bias_filler {
type: &constant&
name: &relu1&
type: &ReLU&
bottom: &conv1&
top: &conv1&
name: &conv2&
type: &Convolution&
bottom: &conv1&
top: &conv2&
lr_mult: 1
lr_mult: 0.1
convolution_param {
num_output: 64
kernel_size: 3
weight_filler {
type: &gaussian&
std: 0.001
bias_filler {
type: &constant&
name: &relu2&
type: &ReLU&
bottom: &conv2&
top: &conv2&
name: &conv3&
type: &Convolution&
bottom: &conv2&
top: &conv3&
lr_mult: 0.1
lr_mult: 0.1
convolution_param {
num_output: 1
kernel_size: 3
weight_filler {
type: &gaussian&
std: 0.001
bias_filler {
type: &constant&
name: &loss&
type: &EuclideanLoss&
bottom: &conv3&
bottom: &label&
top: &loss&
:wq保存退出。
$ vim train.sh
输入以下shell:
#!/bin/bash
./build/tools/caffe train --solver examples/CNN/CNN_solver.prototxt 2&&1 | tee examples/CNN/CNN.log
增加运行权限:
$ chmod +x train.sh
运行脚本文件:
$ ./train.sh
时间可能会运行几天,也可以提前退出(Ctrl+C),因为在snapshot中有中间备份存储。
6.保存滤波器
1.)创建mat文件
$ cp CNN_net.prototxt CNN_mat.prototxt
将CNN_mat.prototxt文件开头两个layer段改为:
name: &CNN&
input: &data&
input_dim: 1
input_dim: 1
input_dim: 11
input_dim: 11
input: &label&
input_dim: 1
input_dim: 1
input_dim: 3
input_dim: 3
:wq保存即可。
2.)创建M文件
$ vim saveFilters.m
输入以下内容:(第7行可以更改需要转换的caffemodel文件名)
caffe.reset_all();
%% settings
%folder = 'examples/CNN/';
folder = './';
model = [folder 'CNN_mat.prototxt'];
weights = [folder 'snapshot/CNN_iter_550000.caffemodel'];
savepath = [folder 'model/x.mat'];
layers = 3;
%% load model using mat_caffe
net = caffe.Net(model,weights,'test');
%% reshap parameters
weights_conv = cell(layers,1);
for idx = 1 : layers
conv_filters = net.layers(['conv' num2str(idx)]).params(1).get_data();
[~,fsize,channel,fnum] = size(conv_filters);
if channel == 1
weights = double(ones(fsize^2, fnum));
weights = double(ones(channel, fsize^2, fnum));
for i = 1 : channel
for j = 1 : fnum
temp = conv_filters(:,:,i,j);
if channel == 1
weights(:,j) = temp(:);
weights(i,:,j) = temp(:);
weights_conv{idx} =
%% save parameters
weights_conv1 = weights_conv{1};
weights_conv2 = weights_conv{2};
weights_conv3 = weights_conv{3};
biases_conv1 = double(net.layers('conv1').params(2).get_data());
biases_conv2 = double(net.layers('conv2').params(2).get_data());
biases_conv3 = double(net.layers('conv3').params(2).get_data());
save(savepath,'weights_conv1','biases_conv1','weights_conv2','biases_conv2','weights_conv3','biases_conv3');
3.)运行M文件
matlab -nodesktop -nosplash -logfile saveFilters.log -r
saveFilters
此时,在model中会生成x.mat文件。
已经知道了x.mat文件中,有三层卷积层的卷积核矩阵weights_conv*和偏置向量biases_conv*。
编写一个demo_net.m文件,使用这些参数构建卷积网络结构,对输入图像(矩阵)进行处理,即可得到结果。
不同应用有不同的源码,这里省略该文件源码。
本文可能叙述不全面,如有错误,欢迎指正!
本文已收录于以下专栏:
相关文章推荐
Faster R-CNN是当前目标检测领域内性能最好的算法之一,它将RPN(Region Proposal Network)网络和Fast R-CNN网络结合到了一起,实现了一个端到端的目标检测框架。...
学习的caffe的目的,不是简单的做几个练习,而是最终落实到自己的项目或科研中去。因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测试的整个流程。
一、数据的准备
    有条件的同学,...
SRS(Simple Rtmp Server)单进程能支持9000并发,nginx-rtmp单进程最多支持3000个,单进程的性能SRS(Simple Rtmp Server)是nginx...
转自:http://blog.csdn.net/ws_20100/article/details/
1.配置caffe环境
[请参考此篇博客:http://blog.csd...
实验配置:
曙光I450-G10双路塔式服务器
CPU:Intel Xeon
由于在ubuntu中使用caffe的程序时,都使用.sh文件,该文件中常见的命令为:./build/tools/caffe train --solver=examples/testXXX/solver...
caffe+unbuntu14.04+GPU运行srcnn:
本文有部分转载,地址:http://blog.csdn.net/s/article/details/
欢迎光临资源小阁~
本软件安装教程由“资源小阁”提供,我们会不断的更新和共享最新软件和资料,更多软件和资料请点击文本最下方阅读全文。
获取安装文件可以在公众号回...
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 caffe模型训练参数 的文章

 

随机推荐