深度学习之Caffe完全掌握:添加新的网络层(训练非图像纯数据)

深度学习之Caffe完全掌握:添加新的网络层


这里写图片描述


什么是caffe

Caffe,全称Convolutional Architecture for Fast Feature Embedding。是一种常用的深度学习框架,在视频、图像处理方面应用较多。作者是贾扬清,加州大学伯克利的ph.D。Caffe用C++编写,但可以用python调用。


关于caffe的使用

你完全可以把python看作它的UI,并不涉及算法具体实现。
你完全可以把prototxt文件看作它的配置,只是模型和任务流程的一种文本描述。
你完全可以把.caffemodel文件视为出产物,模型实体。
(很勉强的排比修辞手法)(1分)


下载示例程序

root@master:# git clone https://github.com/cbelth/irisCaffe.git
root@master:# cd irisCaffe/iris
root@master:# python iris_tuto.py
... ...

关于配置环境

注意必须配置好关于pycaffe接口的地址,
我在iris_tuto.py中添加了:

import sys
sys.path.append("/download/caffe/python/")
import caffe

就可以使用其接口了


我们先看看现在的网络结构

这里写图片描述
其结构文件如下:

name: "IrisNet"
layer {
  name: "iris"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  hdf5_data_param {
    source: "iris_train_data.txt"
    batch_size: 1

  }
}

layer {
  name: "iris"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  hdf5_data_param {
    source: "iris_test_data.txt"
    batch_size: 1

  }
}

layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "data"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 50
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "drop1"
  type: "Dropout"
  bottom: "ip1"
  top: "ip1"
  dropout_param {
    dropout_ratio: 0.5
  }
}


layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 50
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "drop2"
  type: "Dropout"
  bottom: "ip2"
  top: "ip2"
  dropout_param {
    dropout_ratio: 0.4
  }
}

layer {
  name: "ip3"
  type: "InnerProduct"
  bottom: "ip2"
  top: "ip3"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}

layer {
  name: "drop3"
  type: "Dropout"
  bottom: "ip3"
  top: "ip3"
  dropout_param {
    dropout_ratio: 0.3
  }
}


layer {
  name: "loss"
  type: "SigmoidCrossEntropyLoss" 
  # type: "EuclideanLoss" 
  # type: "HingeLoss"  
  bottom: "ip3"
  bottom: "label"
  top: "loss"
}

现在我想在ip1层后添加一层新的名为”newLayer”的新层,其结构和ip1一样,那么,就改写为:

name: "IrisNet"
layer {
  name: "iris"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  hdf5_data_param {
    source: "iris_train_data.txt"
    batch_size: 1

  }
}

layer {
  name: "iris"
  type: "HDF5Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  hdf5_data_param {
    source: "iris_test_data.txt"
    batch_size: 1

  }
}

layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "data"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 50
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "drop1"
  type: "Dropout"
  bottom: "ip1"
  top: "ip1"
  dropout_param {
    dropout_ratio: 0.5
  }
}

layer {
  name: "newLayer"
  type: "InnerProduct"
  bottom: "ip1"
  top: "newLayer"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 50
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu_newLayer"
  type: "ReLU"
  bottom: "newLayer"
  top: "newLayer"
}
layer {
  name: "drop_newLayer"
  type: "Dropout"
  bottom: "newLayer"
  top: "newLayer"
  dropout_param {
    dropout_ratio: 0.5
  }
}

layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "newLayer"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 50
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "drop2"
  type: "Dropout"
  bottom: "ip2"
  top: "ip2"
  dropout_param {
    dropout_ratio: 0.4
  }
}

layer {
  name: "ip3"
  type: "InnerProduct"
  bottom: "ip2"
  top: "ip3"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 3
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}

layer {
  name: "drop3"
  type: "Dropout"
  bottom: "ip3"
  top: "ip3"
  dropout_param {
    dropout_ratio: 0.3
  }
}

layer {
  name: "loss"
  type: "SigmoidCrossEntropyLoss" 
  # type: "EuclideanLoss" 
  # type: "HingeLoss"  
  bottom: "ip3"
  bottom: "label"
  top: "loss"
}

网络模型结构:
这里写图片描述

然后运行:

root@master:/App/Caffe_Iris/iris# python iris_tuto.py 
I1217 21:32:06.172330  6814 layer_factory.hpp:77] Creating layer iris
I1217 21:32:06.172345  6814 net.cpp:84] Creating Layer iris
I1217 21:32:06.172353  6814 net.cpp:380] iris -> data
I1217 21:32:06.172364  6814 net.cpp:380] iris -> label
I1217 21:32:06.172392  6814 hdf5_data_layer.cpp:80] Loading list of HDF5 filenames from: iris_test_data.txt
I1217 21:32:06.172417  6814 hdf5_data_layer.cpp:94] Number of HDF5 files: 1
I1217 21:32:06.173000  6814 net.cpp:122] Setting up iris
I1217 21:32:06.173017  6814 net.cpp:129] Top shape: 1 1 1 4 (4)
I1217 21:32:06.173024  6814 net.cpp:129] Top shape: 1 3 (3)
I1217 21:32:06.173028  6814 net.cpp:137] Memory required for data: 28
I1217 21:32:06.173033  6814 layer_factory.hpp:77] Creating layer ip1
I1217 21:32:06.173075  6814 net.cpp:84] Creating Layer ip1
... ...

猜你喜欢

转载自blog.csdn.net/hanss2/article/details/78828452