caffe根目录下新建myself文件夹,作为存放地址。
新建test.py,生成train.txt和val.txt,作为数据集图片的说明。
以下为test.py,和train文件夹、val文件夹放在同一路径下。train文件夹中有Neg_train文件夹和Pos_train文件夹。分别存放正负样本。val文件夹中有Neg_val文件夹和Pos_val文件夹。(使用时注意修改py文件中的路径)
'''''
Created on Jul 29, 2016
@author: sgg
'''
"<span style=""font-family:Arial;font-size:18px;"">"
"<span style=""font-size:18px;"">"
"<span style=""font-size:18px;"">"
import os
def IsSubString(SubStrList,Str):
flag=True
for substr in SubStrList:
if not(substr in Str):
flag=False
return flag
def GetFileList(FindPath,FlagStr=[]):
FileList=[]
FileNames=os.listdir(FindPath)
if len(FileNames)>0:
for fn in FileNames:
if len(FlagStr)>0:
if IsSubString(FlagStr,fn):
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
else:
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
if len(FileList)>0:
FileList.sort()
return FileList
train_txt=open('train.txt','w')
imgfile=GetFileList('train/Pos_train')
for img in imgfile:
str1=img+' '+'1'+'\n'
train_txt.writelines(str1)
imgfile=GetFileList('train/Neg_train')
for img in imgfile:
str2=img+' '+'0'+'\n'
train_txt.writelines(str2)
train_txt.close()
test_txt=open('val.txt','w')
imgfile=GetFileList('val/Pos_val')
for img in imgfile:
str3=img+' '+'1'+'\n'
test_txt.writelines(str3)
imgfile=GetFileList('val/Neg_val')
for img in imgfile:
str4=img+' '+'0'+'\n'
test_txt.writelines(str4)
test_txt.close()
print("success")
准备生成lmbd文件。在myself文件夹下新建文件夹Build_lmbd.
从/home/seu/caffe/examples/imagenet路径下 拷贝create_imagenet.sh,修改使用。拷贝到Build_lmbd中。
修改路径后如下:
#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
set -e
EXAMPLE=myself/Build_lmbd
DATA=myself/Sample
TOOLS=build/tools
TRAIN_DATA_ROOT=/home/seu/caffe/myself/
VAL_DATA_ROOT=/home/seu/caffe/myself/
# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=false
if $RESIZE; then
RESIZE_HEIGHT=256
RESIZE_WIDTH=256
else
RESIZE_HEIGHT=0
RESIZE_WIDTH=0
fi
if [ ! -d "$TRAIN_DATA_ROOT" ]; then
echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
"where the ImageNet training data is stored."
exit 1
fi
if [ ! -d "$VAL_DATA_ROOT" ]; then
echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
"where the ImageNet validation data is stored."
exit 1
fi
echo "Creating train lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$TRAIN_DATA_ROOT \
$DATA/train.txt \
$EXAMPLE/ilsvrc12_train_lmdb
echo "Creating val lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$VAL_DATA_ROOT \
$DATA/val.txt \
$EXAMPLE/ilsvrc12_val_lmdb
echo "Done."
在caffe根目录下运行后。
seu@my-pc:~/caffe$ sudo ./myself/Build_lmbd/create_imagenet.sh
可以看到生成结果:
参考博客:http://blog.youkuaiyun.com/liuweizj12/article/details/52149743