caffe制作数据集

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")  

test.py运行后可生成两个txt文件

准备生成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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值