跑通caffe-ssddemo代码(训练测试自己数据集)(代码片段)

Abigbliss Abigbliss     2022-12-21     494

关键词:

跑通caffe-ssd demo代码(训练、测试自己数据集)

ssd网络我就不多介绍了,CSDN上面一搜一大把。这篇主要讲讲如何跑通caffe代码~
github:caffe-ssd

一、代码结构


在caffe-ssd中能用到的文件我全部在上述图片中标出来了,到时候具体的再细说~
caffe-ssd的环境自己百度吧,网上很多安装教程~

二、数据集准备

训练模型,首先第一个事情就是准备数据集。在利用caffe训练分类模型的时候,通常使用lmdb或者hdf5格式的数据,但是在该项目中使用的是lmdb格式的(其他格式的数据肯定也行,但是就是需要自己c++手写数据处理层了~太麻烦了)

1.

首先,准备VOC格式的数据

这是VOC数据集的格式,其中,Annotations里面存放的是所有数据图片相对应的xml标签文件,imagesets里面存放的是一些txt文件,后续用到再详细说,JPEGImages里面存放的就是训练图片,最后两个文件夹是用于实例分割的,在目标检测中用不到,所以就不用管它。(这里,为了简单,所以只使用VOC_trainval_2007中前36张图片)
JPEGImages:

其中,trainval里面存放训练图片,test存放测试图片。这个划分根据你自己项目进行决定就行。一般来说,trainval:test=4:1,这里我在test存放8张随机图片,剩余的存放在trainval文件夹中。值得一提,全部的图片还是放在trainval和test文件夹同级目录中,这样便于后续生成trainval.txt和test.txt。
Annotations:

Imagesets:

2.

下面介绍如何生成Imagesets里面的这四个txt文件~
先说trainval.txt和test.txt这两个文件。

#! /usr/bin/python  
# -*- coding:UTF-8 -*-  
 
import os, sys  
import glob  

trainval_dir = r"D:\\voc\\VOC_test\\JPEGImages\\trainval"  #训练集图片存放地址
test_dir = r"D:\\voc\\VOC_test\\JPEGImages\\test"  #测试图片存放地址
 
trainval_img_lists = glob.glob(trainval_dir + '/*.jpg') #如果你的图片是png格式的,只需要修改最后的后缀
trainval_img_names = []        
for item in trainval_img_lists:  
    temp1, temp2 = os.path.splitext(os.path.basename(item))  
    trainval_img_names.append(temp1)  
 
test_img_lists = glob.glob(test_dir + '/*.jpg') #如果你的图片是png格式的,只需要修改最后的后缀
test_img_names = []  
for item in test_img_lists:  
    temp1, temp2 = os.path.splitext(os.path.basename(item))  
    test_img_names.append(temp1)  
#图片路径和xml路径  
dist_img_dir = r"D:\\voc\\VOC_test\\JPEGImages" #JPEGImages路径
dist_anno_dir = r"D:\\voc\\VOC_test\\Annotations" #存放所有数据的xml文件路径
 
trainval_fd = open(r"D:\\voc\\VOC_test\\ImageSets\\trainval.txt", 'w')  #trainval.txt存放地址
test_fd = open(r"D:\\voc\\VOC_test\\ImageSets\\test.txt", 'w')  #test.txt存放地址
   
for item in trainval_img_names:  
    trainval_fd.write(dist_img_dir + '/' + str(item) + '.jpg' + ' ' + dist_anno_dir + '/' + str(item) + '.xml\\n')  
 
for item in test_img_names:  
    test_fd.write(dist_img_dir + '/' + str(item) + '.jpg' + ' ' + dist_anno_dir + '/' + str(item) + '.xml\\n')

生成的trainval.txt:

test.txt:

注意:这里我建议大家使用绝对路径,到时候在训练的时候比较清楚点

3.

下面介绍labelmap_voc.prototxt:
labelmap_voc.prototxt文件在你下载的caffe-ssd中有一个副本,位置在:you_caffe_root/data/VOC0712/里面,这里面的数据要根据你自己的需求进行修改,因为我这里就是VOC数据集,所以我不用改变的。不过假如我想进行猫狗目标检测算法,那我就得这么改:

这里,label为0的是背景一类,不管你是检测多少种物体,这一类是不能动的。

4.

下面就是test_name_size.txt生成方式:

#! /usr/bin/python
# -*- coding:UTF-8 -*-
import os, sys
import glob
from PIL import Image #读图

img_dir = r"D:/voc/VOC_test/JPEGImages/test" #测试图片存放路径

#获取制定路径下的所有jpg图片的名称
img_lists = glob.glob(img_dir + '/*.jpg')

test_name_size = open(r"D:/voc/VOC_test/ImageSets/test_name_size.txt", 'w') #test_name_size.txt存放路径

for item in img_lists:
    img = Image.open(item)
    width, height = img.size
    temp1, temp2 = os.path.splitext(os.path.basename(item))
    test_name_size.write(temp1 + ' ' + str(height) + ' ' + str(width) + '\\n')

最后生成的test_name_size.txt:

其中,每一列中第一个表示测试图片名称,第二个和第三个表示的是该测试图片的高和宽。


这样,所有准备工作都做完了~用上述所有文件就可以生成lmdb数据了

5.

将VOC文件夹放在you_caffe_root/data中

定位到you_caffe_root/data/VOC0712,下面应该有两个shell脚本文件:create_list.sh和create_data.sh。前者其实就是生成trainval.txt和test.txt,因为我们已经用python脚本生成好了,所以就可以直接用后者来生成lmdb数据了。
create_data.sh:

cur_dir=$(cd $( dirname $BASH_SOURCE[0] ) && pwd )
root_dir=$cur_dir/../.. #你安装caffe的根目录

cd $root_dir

redo=1
data_root_dir="" #上述VOC总文件夹的位置
mapfile="" #上述生成的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 #该sh脚本本质上是调用的是scripts/create_annoset.py,你找到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 
   # 很多人最后训练代码出错其实就是最后一行没有设置正确
   --check-label 
   $extra_cmd #这个不用管
   $data_root_dir #这个不用管
   $root_dir/data/$dataset_name/$subset.txt #这个位置是上述生成的trainval.txt和test.txt文件路径,这个要设置好
   $data_root_dir/$dataset_name/$db/$dataset_name"_"$subset"_"$db #这一个参数是设置生成lmdb文件的路径,一般来说,最好设置在VOC总文件夹下面,即同JPEGImages、Annotations、Images在同一个路径下,当然,也可以事先自己在VOC总文件夹下面创建一个lmdb的文件夹,该lmdb文件夹下面又有两个子文件夹,分别代表的是训练lmdb数据和测试lmdb数据
   examples/$dataset_name
done

注意点:如果代码运行报错(明明图片和xml标签路径是正确的,但是就是无法生成lmdb),那么找到create_annoset.py第87行:

img_file, anno = line.strip("\\n").split(" ")

改为

img_file, anno = line.strip("\\n").strip("\\r").split(" ")

三、开始训练

与以往caffe实现分类网络不同的是,该项目是通过py脚本进行训练的,而不是直接通过caffe的c++接口进行训练。
该py脚本的位置在:you_caffe_root/examples/ssd/ssd_pascal.py。

上述图片中还有一个ssd_detect.py脚本,该脚本就是用于测试单张图片用的,这个后续再说。
ssd_pascal.py
具体需要修改的地方会在下面标注清楚的

from __future__ import print_function
import caffe
from caffe.model_libs import *
from google.protobuf import text_format

import math
import os
import shutil
import stat
import subprocess
import sys

# Add extra layers on top of a "base" network (e.g. VGGNet or Inception).
def AddExtraLayers(net, use_batchnorm=True, lr_mult=1):
    use_relu = True

    # Add additional convolutional layers.
    # 19 x 19
    from_layer = net.keys()[-1]

    # TODO(weiliu89): Construct the name using the last layer to avoid duplication.
    # 10 x 10
    out_layer = "conv6_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 1, 0, 1,
        lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv6_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 512, 3, 1, 2,
        lr_mult=lr_mult)

    # 5 x 5
    from_layer = out_layer
    out_layer = "conv7_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv7_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 1, 2,
      lr_mult=lr_mult)

    # 3 x 3
    from_layer = out_layer
    out_layer = "conv8_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv8_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1,
      lr_mult=lr_mult)

    # 1 x 1
    from_layer = out_layer
    out_layer = "conv9_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv9_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1,
      lr_mult=lr_mult)

    return net


### Modify the following parameters accordingly ###
# The directory which contains the caffe code.
# We assume you are running the script at the CAFFE_ROOT.
caffe_root = os.getcwd()

# Set true if you want to start training right after generating all files.
run_soon = True
# Set true if you want to load from most recently saved snapshot.
# Otherwise, we will load from the pretrain_model defined below.
resume_training = True
# If true, Remove old model files.
remove_old_models = False

# The database file for training data. Created by data/VOC0712/create_data.sh
train_data = "examples/VOC0712/VOC0712_trainval_lmdb"  #该地址为上述生成的lmdb格式的训练数据,注意的是,该地址为存放lmdb数据的上一级文件夹
# The database file for testing data. Created by data/VOC0712/create_data.sh
test_data = "examples/VOC0712/VOC0712_test_lmdb" #该地址为上述生成的lmdb格式的测试数据,注意的是,该地址为存放lmdb数据的上一级文件夹
# Specify the batch sampler.
resize_width = 300 #该模型接收输入图片的宽
resize_height = 300 #该模型接收输入图片的高
resize = "x".format(resize_width, resize_height)
batch_sampler = [
        
                'sampler': 
                        ,
                'max_trials': 1,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'min_jaccard_overlap': 0.1,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'min_jaccard_overlap': 0.3,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'min_jaccard_overlap': 0.5,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'min_jaccard_overlap': 0.7,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'min_jaccard_overlap': 0.9,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        
                'sampler': 
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        ,
                'sample_constraint': 
                        'max_jaccard_overlap': 1.0,
                        ,
                'max_trials': 50,
                'max_sample': 1,
        ,
        ]
train_transform_param =   #这个字典里面就是进行数据增强的操作,其实我感觉可以不要该操作,但是没有尝试过注释后代码还能不能跑通
        'mirror': True,
        'mean_value': [104, 117, 123], #图片均值
        'resize_param': 
                'prob': 1,
                'resize_mode': P.Resize.WARP,
                'height': resize_height,
                'width': resize_width,
                'interp_mode': [
                        P.Resize.LINEAR,
                        P.Resize.AREA,
                        P.Resize.NEAREST,
                        P.Resize.CUBIC,
                        P.Resize.LANCZOS4,
                        ],
                ,
        'distort_param': 
                'brightness_prob': 0.5,
                'brightness_delta': 32,
                'contrast_prob': 0.5,
                'contrast_lower': 0.5,
                'contrast_upper': 1.5,
                'hue_prob': 0.5,
                'hue_delta': 18,
                'saturation_prob': 0.5,
                'saturation_lower': 0.5,
                'saturation_upper': 1.5,
                'random_order_prob': 0.0,
                ,
        'expand_param': 
                'prob': 0.5,
                'max_expand_ratio': 4.0,
                ,
        'emit_constraint': 
            'emit_type': caffe_pb2.EmitConstraint.CENTER,
            
        
test_transform_param = 
        'mean_value': [104, 117, 123],
        'resize_param': 
                'prob': 1,
                'resize_mode': P.Resize.WARP,
                'height': resize_height,
                'width': resize_width,
                'interp_mode': [P.Resize.LINEAR],
                ,
        

# If true, use batch norm for all newly added layers.
# Currently only the non batch norm version has been tested.
use_batchnorm = False
lr_mult = 1
# Use different initial learning rate.
if use_batchnorm:
    base_lr = 0.0004
else:
    # A learning rate for batch_size = 1, num_gpus = 1.
    base_lr = 0.00004  #一般来说,我们会调用这个学习率,但是实际在训练的时候的学习率应该为base_lr * 25,所以说如果想增减学习率,只需要修改此处就可以

# Modify the job name if you want.
job_name = "SSD_".format(resize)
# The name of the model. Modify it if you want.
model_name = "VGG_VOC0712_".format(job_name)

# Directory which stores the model .prototxt file.
save_dir = "models/VGGNet/VOC0712/".format(job_name) #最后生成caffemodel的位置
# Directory which stores the snapshot of models.
snapshot_dir = "models/VGGNet/VOC0712/".format(job_name) 
# Directory which stores the job script and log file.
job_dir = "jobs/VGGNet/VOC0712/".format(job_name)
# Directory which stores the detection results.
output_result_dir = "/data/VOCdevkit/results/VOC2007//Main".format(os.environ['HOME'], job_name)

# model definition files.
train_net_file = "/train.prototxt".format(save_dir) #train.prototxt位置
test_net_file = "/test.prototxt".format(save_dir) #test.prototxt位置
deploy_net_file = "/deploy.prototxt".format(save_dir)
solver_file = "/solver.prototxt".format(save_dir)
# snapshot prefix.
snapshot_prefix = "/".format(snapshot_dir, model_name)
# job script path.
job_file = "/.sh".format(job_dir, model_name)

# Stores the test image names and sizes. Created by data/VOC0712/create_list.sh
name_size_file = "data/VOC0712/test_name_size.txt" #上述生成的test_name_size.txt位置,最后用绝对路径
# The pretrained model. We use the Fully convolutional reduced (atrous) VGGNet.
pretrain_model = "models/VGGNet/VGG_ILSVRC_16_layers_fc_reduced.caffemodel" #预训练模型,会很大程度减少自己项目的训练时间
# Stores LabelMapItem.
label_map_file = "data/VOC0712/labelmap_voc.prototxt" #上述生成的labelmap_voc.prototxt位置,最后用绝对路径

# MultiBoxLoss parameters.
num_classes = 21 #该位置要换成你自己项目的物体类别个数,别忘了要加上背景
share_location = True
background_label_id=0
train_on_diff_gt = True
normalization_

深度学习之初识篇——小白也能跑通的深度学习万能框架交通标识牌检测(代码片段)

目录环境下载;点击即可数据集下载;点击即可深度学习环境配置点击下载深度学习环境数据集准备使用自己标注的数据集使用标注软件数据准备VOC标签格式转yolo格式并划分训练集和测试集部署和训练深度学习项目克隆... 查看详情

pytorch预训练集进行训练测试

...行训练测试;安装配置环境:直接在GPU上跟着先跑通一遍,能当成一个调包侠。主要是安装一些库,其中最基础的库:mmcv来做底层的运算。下载1000多个类别分类数据集。B1是核心:导入基础包;找计算... 查看详情

自己训练卷积模型实现猫狗(代码片段)

原数据集:包含25000张猫狗图像,两个类别各有12500新数据集:猫、狗(照片大小不一样)训练集:各1000个样本验证集:各500个样本测试集:各500个样本#将图像复制到训练、验证和测试的目录importos,shutilorginal_dataset_dir=‘kaggle_ori... 查看详情

机器学习基石训练与测试(代码片段)

目录写在前面1.回顾和预览(RecapandPreview)2.有效的直线数(PLA中)3.有效的假设数4.BreakPoint写在前面本节首先对前面的内容进行了回顾,然后提出两个核心问题,接着讨论了对hypothesis的分类,然后引出... 查看详情

swinunet官方代码测试自己的数据集(已训练完毕)(代码片段)

...件名称测试集的npz文件1.2检查模型权重文件2、修改部分代码2.1修改dataset_synapse.pyslice_name=self.sample_list[idx].strip('\\n')data_path=os.path.join(self.data_dir,slice_name+'.npz')data=np.load(data_path)image,label=data['image'],d... 查看详情

caffe上用ssd训练和测试自己的数据

... 我的根目录$caffe_root为/home/gpu/ljy/caffe一、运行SSD示例代码1.到https://github.com/weiliu89/caffe.git下载caffe-ssd代码,是一个caffe文件夹2.从已经配置好的caffe目录下拷贝一个Makefile.config放到$ 查看详情

使用cyclegan训练自己制作的数据集,通俗教程,快速上手(代码片段)

总结了使用CycleGAN训练自己制作的数据集,这里的教程例子主要就是官网给出的斑马变马,马变斑马,两个不同域之间的相互转换。教程中提供了官网给的源码包和我自己调试优化好的源码包,大家根据自己的情... 查看详情

图像识别之yolov5训练自己的模型(代码片段)

图像识别之Yolov5训练自己的模型文章目录图像识别之Yolov5训练自己的模型一、前言二、对图像进行标注三、数据集的划分四、配置训练的文件1、修改yolov5l.yaml配置文件2、修改coco128.yaml配置文件五、开始训练六、训练结果呈现1、... 查看详情

yolov7训练自己的数据集(口罩检测)(代码片段)

YOLOv7训练自己的数据集(口罩检测)前言前提条件实验环境项目结构制作自己的数据集数据集目录结构训练自己的数据集VOC格式数据集转换成YOLO格式数据集修改cfg配置新建一个myyolov7.yaml配置文件myyolov7.yaml内容创建自己... 查看详情

openvino+yolov5自己训练模型并测试(代码片段)

https://github.com/ultralytics/yolov5/releases/tag/v6.1condacreate-nopenvino_yolov6python=3.7-ycondaactivateopenvino_yolov6pipinstall-rrequirements.txtexport.pyexport_onnx(model,im,file,10,train,False,simplify)#opset1210pythonexport.py--weightsyolov5n.pt--img640--batch1*设置一个临时的环... 查看详情

如何在 caffe 中训练/测试我自己的数据集?

】如何在caffe中训练/测试我自己的数据集?【英文标题】:Howtotraining/testingmyowndatasetincaffe?【发布时间】:2016-03-1820:53:55【问题描述】:我从Caffe开始,mnist示例运行良好。我的火车和标签数据为data.mat。(我有300个训练数据,其... 查看详情

如何在 caffe 中训练/测试我自己的数据集?

】如何在caffe中训练/测试我自己的数据集?【英文标题】:Howtotraining/testingmyowndatasetincaffe?【发布时间】:2016-10-1614:36:34【问题描述】:我从Caffe开始,mnist示例运行良好。我的火车和标签数据为data.mat。(我有300个训练数据,其... 查看详情

MNIST 训练的网络用我自己的样本进行了测试

】MNIST训练的网络用我自己的样本进行了测试【英文标题】:MNISTtrainednetworktestedwithmyownsamples【发布时间】:2021-12-0602:01:40【问题描述】:我使用MNIST数据集训练了一个密集神经网络,以便对28x28的数字图像进行分类。现在我试图... 查看详情

gpt2训练自己的对话问答机器人(代码片段)

...模型实现基于HuggingFace的transformers,精读GPT2-Chinese的论文和代码,获益匪浅。3.模型训练与测试data/train.txt:默认的原 查看详情

训练与测试自己的图片

1)数据集准备家装风格图片共500张,分为5个类,每类各100张图片。80张作为train,20张作为val。2)图片统一大小所有图片统一大小为256*256大小。3)生成train.txt与val.txt文件类标签最好从0开始。 下面开始使用caffe1)使用convert_... 查看详情

使用c版yolov4在自己的数据集上训练测试(代码片段)

...行训练与测试。论文:YOLOv4:OptimalSpeedandAccuracyofObjectDetection代码:https://github.com/AlexeyAB/darknet环境配置建议使用docker容器配置环境,docker的安装不在此赘述,docker安装好后拉取一个nvidia/cuda镜像,docker的一些用法可以看这里。如果你... 查看详情

跑通一篇博客代码假装自己会vue(代码片段)

本文是《跑通一篇博客代码假装自己会Java》的母亲篇,Vue是目前比较流行的前端框架。博主本人在两个月前对Java一窍不通,发配帝都出差一个月后,对Java,包括前端和后端都稍有熟悉。这篇博文主要介绍Vue的快... 查看详情

如何在 TensorFlow 中使用我自己的数据将图像拆分为测试和训练集

...如何在TensorFlow中使用我自己的数据将图像拆分为测试和训练集【英文标题】:HowtosplitimagesintotestandtrainsetusingmyowndatainTensorFlow【发布时间】:2020-05-2414:54:50【问题描述】:我在这里有点困惑...我刚刚花了一个小时阅读有关如何在T... 查看详情