train yourself ssd network

1、新建目录:caffeSSD/data/VOCdevkit/callsmoke

2、将标注图片和标注文件放到目录 caffeSSD/data/VOCdevkit/callsmoke 下面,对应Annotations和JPEGImages两个文件夹

3、目录 caffeSSD/data/VOCdevkit/callsmoke 下,建立ImageSets文件夹,在ImageSets文件夹下面建立Main文件夹

4、制作训练、测试文本目录,在caffeSSD/data/VOCdevkit/callsmoke目录下面建立dir.py

import os  
import random  

trainval_percent = 0.9995  
train_percent = 0.9995
xmlfilepath = 'Annotations'  
#xmlfilepath='1jpgTest'
txtsavepath = 'ImageSets\Main'  
total_xml = os.listdir(xmlfilepath)  

num=len(total_xml)  
list=range(num)  
tv=int(num*trainval_percent)  
tr=int(tv*train_percent)  
trainval= random.sample(list,tv)  
train=random.sample(trainval,tr)  

ftrainval = open('ImageSets/Main/trainval.txt', 'w')  
ftest = open('ImageSets/Main/test.txt', 'w')  
ftrain = open('ImageSets/Main/train.txt', 'w')  
fval = open('ImageSets/Main/val.txt', 'w')  

for i  in list:  
    name=total_xml[i][:-4]+'\n'  
    if i in trainval:  
        ftrainval.write(name)  
        if i in train:  
            ftrain.write(name)  
        else:  
            fval.write(name)  
    else:  
        ftest.write(name)  

ftrainval.close()  
ftrain.close()  
fval.close()  
ftest .close()  

5、caffeSSD/data 目录下建立callsmoke文件夹,callsmoke文件夹下面建立create_data.sh、create_list.sh、labelmap_voc.prototxt三个文件

(1)运行create_list.sh

#!/bin/bash

root_dir=/自己目录/workspace/caffeSSD/data/VOCdevkit/
sub_dir=ImageSets/Main
bash_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
for dataset in trainval test
do
  dst_file=$bash_dir/$dataset.txt
  if [ -f $dst_file ]
  then
    rm -f $dst_file
  fi
  for name in callsmoke
  do
    #if [[ $dataset == "test" && $name == "VOC2012" ]]
    #then
      #continue
    #fi
    echo "Create list for $name $dataset..."
    dataset_file=$root_dir/$name/$sub_dir/$dataset.txt

    img_file=$bash_dir/$dataset"_img.txt"
    cp $dataset_file $img_file
    sed -i "s/^/$name\/JPEGImages\//g" $img_file
    sed -i "s/$/.jpg/g" $img_file

    label_file=$bash_dir/$dataset"_label.txt"
    cp $dataset_file $label_file
    sed -i "s/^/$name\/Annotations\//g" $label_file
    sed -i "s/$/.xml/g" $label_file

    paste -d' ' $img_file $label_file >> $dst_file

    rm -f $label_file
    rm -f $img_file
  done

  # Generate image name and size infomation.
  if [ $dataset == "test" ]
  then
    $bash_dir/../../build/tools/get_image_size $root_dir $dst_file $bash_dir/$dataset"_name_size.txt"
  fi

  # Shuffle trainval file.
  if [ $dataset == "trainval" ]
  then
    rand_file=$dst_file.random
    cat $dst_file | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);' > $rand_file
    mv $rand_file $dst_file
  fi
done

(2)、create_data.sh

cur_dir=$(cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
#root_dir=$cur_dir/../..


root_dir=/home/zyw/workspace/caffeSSD

cd $root_dir

redo=1
data_root_dir="/自己目录/workspace/caffeSSD/data/VOCdevkit"
dataset_name="callsmoke"
mapfile="$root_dir/data/$dataset_name/labelmap_voc.prototxt"
anno_type="detection"
db="lmdb"
min_dim=0
max_dim=0
width=0
height=0

extra_cmd="--encode-type=jpg --encoded"
if [ $redo ]
then
  extra_cmd="$extra_cmd --redo"
fi
for subset in test trainval
do
  python $root_dir/scripts/create_annoset.py --anno-type=$anno_type --label-map-file=$mapfile --min-dim=$min_dim --max-dim=$max_dim --resize-width=$width --resize-height=$height --shuffle --check-label $extra_cmd $data_root_dir $root_dir/data/$dataset_name/$subset.txt $data_root_dir/$dataset_name/$db/$dataset_name"_"$subset"_"$db examples/$dataset_name
done

 (3)、labelmap_voc.prototxt

写入自己要检测的物体类别和背景

item {
  name: "none_of_the_above"
  label: 0
  display_name: "background"
}
item {
  name: "hand"
  label: 1
  display_name: "hand"
}
item {
  name: "head"
  label: 2
  display_name: "head"
}

 

posted @ 2020-05-21 15:01  crazybird123  阅读(138)  评论(0编辑  收藏  举报