一、首先准备好训练数据和测试数据,每一类图片放到一个文件夹中

生成lmdb文件和均值文件

生成lmdb文件和均值文件

生成lmdb文件和均值文件

二、生成标签文件:这个代码对应的是七分类

"<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('/home/ths/6_operation_32_data/database/train/train_bilinear/')
for img in imgfile:
    str1=img+' '+'0'+'\n'       
    train_txt.writelines(str1)
    
imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_constract/')
for img in imgfile:
    str2=img+' '+'1'+'\n'

    train_txt.writelines(str2)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/trian/train_gaussion/')
for img in imgfile:
    str3=img+' '+'2'+'\n'
    train_txt.writelines(str3)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_jpeg/')
for img in imgfile:
    str4=img+' '+'3'+'\n'
    train_txt.writelines(str4)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_mean/')
for img in imgfile:
    str5=img+' '+'4'+'\n'
    train_txt.writelines(str5)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_med/')
for img in imgfile:
    str6=img+' '+'5'+'\n'
    train_txt.writelines(str6)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_org/')
for img in imgfile:
    str7=img+' '+'6'+'\n'
    train_txt.writelines(str7)
train_txt.close()

test_txt=open('val.txt','w')

imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_bilinear/')
for img in imgfile:
    str8=img+' '+'0'+'\n'
    test_txt.writelines(str8)
    
imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_constract/')
for img in imgfile:
    str9=img+' '+'1'+'\n'
    test_txt.writelines(str9)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_gaussion/')
for img in imgfile:
    str10=img+' '+'2'+'\n'
    test_txt.writelines(str10)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/train/train_jpeg/')
for img in imgfile:
    str11=img+' '+'3'+'\n'
    test_txt.writelines(str11)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_mean/')
for img in imgfile:
    str12=img+' '+'4'+'\n'
    test_txt.writelines(str12)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_med/')
for img in imgfile:
    str13=img+' '+'5'+'\n'
    test_txt.writelines(str13)

imgfile=GetFileList('/home/ths/6_operation_32_data/database/val/val_org/')
for img in imgfile:
    str14=img+' '+'6'+'\n'
    test_txt.writelines(str14)
test_txt.close()

print("success")

生成lmdb文件和均值文件

生成lmdb文件和均值文件

生成的标签文件前面有路径,把路径用查找替换功能删掉,只保留上图中的信息即可。因为生成lmdb文件的时候可以自动读出路径信息。


三、生成lmdb文件

在caffe-master下新建My_Files文件夹,将caffe-master/examples/imagenet文件夹下的create_imagenet.sh复制过去

要是用服务器运行这个代码的时候记得删掉所有汉字哦

#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs

EXAMPLE=/home/ths/caffe-master/My_Files   #生成模型训练数据文件,即create_imagenet.sh文件的位置
DATA=/home/ths/6_operation_32_data/database #python脚本处理数据路径,即生成的文件列表.txt文件所在的文件夹
TOOLS=/home/ths/caffe-master/.build_release/tools #caffe所在的文件夹

TRAIN_DATA_ROOT=/home/ths/6_operation_32_data/database/train/ #待处理的训练数据所在的文件夹
VAL_DATA_ROOT=/home/ths/6_operation_32_data/database/val/ #待处理的训练数据所在的文件夹

# 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."

生成lmdb文件和均值文件

四、生成均值文件

用服务器跑要删掉汉字哦!!!

将caffe-master/examples/imagenet文件夹下的make_imagenet_mean.sh到My_Files文件夹

#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

EXAMPLE=/home/ths/caffe-master/My_Files #lmdb文件所在的文件夹
DATA=/home/ths/6_operation_32_data/database #生成的imagenet_mean.binaryproto的存放路径
TOOLS=/home/ths/caffe-master/.build_release/tools #caffe所在的文件夹

$TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb \
  $DATA/imagenet_mean.binaryproto

echo "Done."

生成lmdb文件和均值文件


相关文章:

  • 2021-09-21
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
  • 2021-06-03
  • 2021-12-02
  • 2021-09-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
相关资源
相似解决方案