caffe 训练 vgg

使用VGG训练Imagenet

准备数据

具体官网地址,请点击这里

ImageNet官网

训练数据集:ILSVRC2012_img_train.tar

验证数据集:ILSVRC2012_img_val.tar

数据解压

sudo tar –xvf ILSVRC2012_img_train.tar -C ./train 
sudo tar –xvf ILSVRC2012_img_val.tar -C ./val

对于val数据集,解压以后是所有的验证集图片,共50000张,大约6.3G。

对于train数据集,解压后是1000个tar文件,每个tar文件表示1000类里的一个类,共138G,对于1000个子tar,需要再次解压,解压脚本unzip.sh如下

dir=/home/satisfie/imagenet/train #satisfie 是我的用户名
for x in `ls *.tar`
do
    filename=`basename $x .tar`   #注意空格
    mkdir $filename
    tar -xvf $x -C ./$filename
done

原始数据就准备好了,分别放在

  • /home/jhj1/imagenet/train:里面有1000个文件夹,每个文件夹下为JPG图片

  • /home/jhj1/imagenet/val :里面有验证集的50000张图片

接下来下载标签等其他说明数据~~~

下载其他数据

进入大caffe根目录,执行/data/ilsvrc12/get_ilsvrc_aux.sh下载其他数据,包括

  • det_synset_words.txt
  • synset_words.txt— 1000个类别的文件夹名称及真是物体的名称,比如 “n01440764 tench Tinca tinca”,在训练中,这些都当做一个类别。
  • synsets.txt — 1000个类别的文件夹名称,比如”n01440764”…
  • train.txt — 1000个类别每张图片的名字及其标签,比如 “n01440764/n01440764_10026.JPEG 0” 共有1281167张图片
  • val.txt — 同上,总共有50000张。比如“ILSVRC2012_val_00000001.JPEG 65”
  • test.txt — 同上,为测试集合,总有100000张
  • imagenet_mean.binaryproto — 模型图片的各个通道均值
  • imagenet.bet.pickle

模型的训练

训练数据准备(这里我们直接使用图片,数据转换仅为学习参考)

1.转换数据格式为lmdb文件

所有的图片都归一化为256*256的大小,对于一个长方形图片,首先将短边变成256的长度,然后剪裁图片中心的256*256部分。

将examples/imagenet/create_imagenet.sh文件中,将为RESIZE=false更改为RESIZE=true,将所有图片归一化为256*256的大小。注意需将文件中的训练数据集和测试数据集的地址更改为服务器中实际存放的地址,即文件中设置

TRAIN_DATA_ROOT=/dataset/ imagenet/train/

VAL_DATA_ROOT=/dataset/imagenet/val/

执行该文件后生成训练数据和验证数据的lmdb数据库:ilsvrc12_train_lmdb 、ilsvrc12_val_lmdb。

3.得到均值文件

 训练集图片的平均值存储于data/ilsvrc12/imagenet_mean.binaryproto。如果没有该文件,执行

./examples/imagenet/make_imagenet_mean.sh可以生成该文件。

4.这里由于转化为lmdb数据库格式需要耗费较大的空间,且不支持shuffle等操作,所以这里直接读取原图片,使用的类型是ImageData,具体看下面的prototxt

其中的train_new.txt中对每张图片的加上了绝对值路径,这样才能被读取。 
使用sed命令即可,

sed 's/^/\/home\/jhj1\/imagenet\/val\/&/g' val.txt >val_new.txt

VGG_train_val.prototxt

name: "VGG_ILSVRC_16_layers"
layer {
  name: "data"
  type: "ImageData"
  include {
    phase: TRAIN
  }
 transform_param {
    #crop_size: 224
    mean_value: 104
    mean_value: 117
    mean_value: 123
    mirror: true
 }
 image_data_param {
    source: "/home/jhj1/imagenet/train_new.txt"
    batch_size: 8
    new_height: 224
    new_width: 224
  }
  top: "data"
  top: "label"
}
layer {
  name: "data"
  type: "ImageData"
  include {
    phase: TEST
  }
 transform_param {
    #crop_size: 224
    mean_value: 104
    mean_value: 117
    mean_value: 123
    mirror: false
 }
 image_data_param {
    source: "/home/jhj1/imagenet/val_new.txt"
    batch_size: 4
    new_height: 224
    new_width: 224
  }
  top: "data"
  top: "label"
}
layer {
  bottom: "data"
  top: "conv1_1"
  name: "conv1_1"
  type: "Convolution"
  convolution_param {
    num_output: 64
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv1_1"
  top: "conv1_1"
  name: "relu1_1"
  type: "ReLU"
}
layer {
  bottom: "conv1_1"
  top: "conv1_2"
  name: "conv1_2"
  type: "Convolution"
  convolution_param {
    num_output: 64
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv1_2"
  top: "conv1_2"
  name: "relu1_2"
  type: "ReLU"
}
layer {
  bottom: "conv1_2"
  top: "pool1"
  name: "pool1"
  type: "Pooling"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  bottom: "pool1"
  top: "conv2_1"
  name: "conv2_1"
  type: "Convolution"
  convolution_param {
    num_output: 128
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 0
  }
  param {
    lr_mult: 0
  }
}
layer {
  bottom: "conv2_1"
  top: "conv2_1"
  name: "relu2_1"
  type: "ReLU"
}
layer {
  bottom: "conv2_1"
  top: "conv2_2"
  name: "conv2_2"
  type: "Convolution"
  convolution_param {
    num_output: 128
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv2_2"
  top: "conv2_2"
  name: "relu2_2"
  type: "ReLU"
}
layer {
  bottom: "conv2_2"
  top: "pool2"
  name: "pool2"
  type: "Pooling"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  bottom: "pool2"
  top: "conv3_1"
  name: "conv3_1"
  type: "Convolution"
  convolution_param {
    num_output: 256
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv3_1"
  top: "conv3_1"
  name: "relu3_1"
  type: "ReLU"
}
layer {
  bottom: "conv3_1"
  top: "conv3_2"
  name: "conv3_2"
  type: "Convolution"
  convolution_param {
    num_output: 256
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv3_2"
  top: "conv3_2"
  name: "relu3_2"
  type: "ReLU"
}
layer {
  bottom: "conv3_2"
  top: "conv3_3"
  name: "conv3_3"
  type: "Convolution"
  convolution_param {
    num_output: 256
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv3_3"
  top: "conv3_3"
  name: "relu3_3"
  type: "ReLU"
}
layer {
  bottom: "conv3_3"
  top: "pool3"
  name: "pool3"
  type: "Pooling"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  bottom: "pool3"
  top: "conv4_1"
  name: "conv4_1"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv4_1"
  top: "conv4_1"
  name: "relu4_1"
  type: "ReLU"
}
layer {
  bottom: "conv4_1"
  top: "conv4_2"
  name: "conv4_2"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv4_2"
  top: "conv4_2"
  name: "relu4_2"
  type: "ReLU"
}
layer {
  bottom: "conv4_2"
  top: "conv4_3"
  name: "conv4_3"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv4_3"
  top: "conv4_3"
  name: "relu4_3"
  type: "ReLU"
}
layer {
  bottom: "conv4_3"
  top: "pool4"
  name: "pool4"
  type: "Pooling"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  bottom: "pool4"
  top: "conv5_1"
  name: "conv5_1"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv5_1"
  top: "conv5_1"
  name: "relu5_1"
  type: "ReLU"
}
layer {
  bottom: "conv5_1"
  top: "conv5_2"
  name: "conv5_2"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv5_2"
  top: "conv5_2"
  name: "relu5_2"
  type: "ReLU"
}
layer {
  bottom: "conv5_2"
  top: "conv5_3"
  name: "conv5_3"
  type: "Convolution"
  convolution_param {
    num_output: 512
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "conv5_3"
  top: "conv5_3"
  name: "relu5_3"
  type: "ReLU"
}
layer {
  bottom: "conv5_3"
  top: "pool5"
  name: "pool5"
  type: "Pooling"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  bottom: "pool5"
  top: "fc6"
  name: "fc6"
  type: "InnerProduct"
  inner_product_param {
    num_output: 4096
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "fc6"
  top: "fc6"
  name: "relu6"
  type: "ReLU"
}
layer {
  bottom: "fc6"
  top: "fc6"
  name: "drop6"
  type: "Dropout"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  bottom: "fc6"
  top: "fc7"
  name: "fc7"
  type: "InnerProduct"
  inner_product_param {
    num_output: 4096
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  bottom: "fc7"
  top: "fc7"
  name: "relu7"
  type: "ReLU"
}
layer {
  bottom: "fc7"
  top: "fc7"
  name: "drop7"
  type: "Dropout"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "fc8"
  bottom: "fc7"
  top: "fc8"
  type: "InnerProduct"
  inner_product_param {
    num_output: 1000
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  param {
    lr_mult: 1
    decay_mult :1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc8"
  bottom: "label"
  top: "loss/loss"
}
layer {
  name: "accuracy/top1"
  type: "Accuracy"
  bottom: "fc8"
  bottom: "label"
  top: "accuracy@1"
  include: { phase: TEST }
  accuracy_param {
    top_k: 1
  }
}
layer {
  name: "accuracy/top5"
  type: "Accuracy"
  bottom: "fc8"
  bottom: "label"
  top: "accuracy@5"
  include: { phase: TEST }
  accuracy_param {
    top_k: 5
  }
}

solver.prototxt

net: "models/vgg/train_val.prototxt"
test_iter: 10000
test_interval: 40000
test_initialization: false
display: 200
base_lr: 0.0001
lr_policy: "step"
stepsize: 320000
gamma: 0.96
max_iter: 10000000
momentum: 0.9
weight_decay: 0.0005
snapshot: 800000
snapshot_prefix: "models/vgg/vgg"
solver_mode: GPU

开始训练:(保存训练输出到log 并绘制accuracy loss曲线)

设置训练配置文件参数,保存训练时的参数至log文件

二、开始训练后,在log目录里面生成“log-2017-03-22-10-33-20.log”日志文件

三、解析log中的内容 分成train和 test 会在当前文件夹下生成log-data.train 和 log-data.test两个文件

cd  tools/extra

  ./parse_log.sh  log-2017-03-22-10-33-20.log

 

四、调用py程序绘制图形 

cd tools/extra

./plot_training_log.py.example 0 save.png log-data.log

其中0代表曲线类型, save.png 代表保存的图片名称  caffe中支持很多种曲线绘制,通过指定不同的类型参数即可,具体参数如下

Notes:    1. Supporting multiple logs.    

          2. Log file name must end with the lower-cased ".log".

 Supported chart types:    0: Test accuracy  vs. Iters    1: Test accuracy  vs. Seconds    2: Test loss  vs. Iters     3: Test loss  vs. Seconds     4: Train learning rate  vs. Iters    5: Train learning rate  vs. Seconds     6: Train loss  vs. Iters     7: Train loss  vs. Seconds

其结果如下:

./plot_training_log.py.example 0 save.png /home/lw/caffe/examples/testCreateLmDB/log/log-2017-03-22-10-33-20.log 


 

 

 

./plot_training_log.py.example 2 save.png /home/lw/caffe/examples/testCreateLmDB/log/log-2017-03-22-10-33-20.log 

 

 

https://blog.csdn.net/kesonyk/article/details/53819132

https://blog.csdn.net/liuweizj12/article/details/64920428

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值