1. 程式人生 > >Caffe學習筆記(九)——python介面各網路層構建

Caffe學習筆記(九)——python介面各網路層構建

構建網路

一 資料層
1 基本說明
layer {
  name: "cifar"  //層的名字
  type: "Data"   //層型別  Data表示表示資料來源於LevelDB或LMDB
  top: "data"    //輸出層
  top: "label"
  include {      //該層屬於訓練階段的層
    phase: TRAIN
  }
  transform_param { //資料預處理設定
    scale: 0.00390625 // 輸入資料:sacle*(原始資料-均值檔案)
    mean_file: "examples/cifar10/mean.binaryproto"  // 用一個配置檔案來進行均值操作
    mirror: 1  // 1表示開啟映象,0表示關閉,開啟時,根據隨機數值rand(2)絕對是否左右映象,0不映象 1映象
    crop_size: 227   // 剪裁一個 227*227的圖塊,在訓練階段隨機剪裁,在測試階段從中間裁剪
  }
  data_param {
    source: "examples/cifar10/cifar10_train_lmdb"
    batch_size: 100
    backend: LMDB
  }
}

(1)top和bottom:
	每一層用bottom來輸入資料,用top來輸出資料。如果只有top沒有bottom,則此層只有輸出,沒有輸入。
	反之亦然。如果有多個 top或多個bottom,表示有多個blobs資料的輸入和輸出。
(2)data與label:
	在資料層中,至少有一個命名為data的top。如果有第二個top,一般命名為label。 這種(data,label)配對
	是分類模型所必需的。

2  資料來自資料庫(如LevelDB LMDB)
2.1 相關引數
	層型別(layer type):Data
	必須設定的引數:
	  source: 包含資料庫的目錄名稱,如examples/mnist/mnist_train_lmdb
	  batch_size: 每次處理的資料個數,如64

	可選的引數:
	  rand_skip: 在開始的時候,路過某個資料的輸入。通常對非同步的SGD很有用。
	  backend: 選擇是採用LevelDB還是LMDB, 預設是LevelDB.
2.2 生成函式	

	data,label=L.Data(
        source=lmdb,                             #資料來源,訓練資料LMDB檔案的位置
        backend=P.Data.LMDB,                     #資料型別,本文是lmdb
        batch_size=batch_size,                   #batch大小
        ntop=2,                                  #輸出數量,本文是data和label,所以是2
        transform_param=dict(crop_size=40,       #crop大小
                            mean_file=mean_file, #均值檔案
                            mirror=True          #映象操作
                            )
        )
	
	例子:	
    data, label = L.Data(source=lmdb, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
        transform_param=dict(crop_size=40,mean_file=mean_file,mirror=True))
	layer {
	  name: "Data1"
	  type: "Data"
	  top: "Data1"
	  top: "Data2"
	  transform_param {
		mirror: true
		crop_size: 40
		mean_file: "F:\\TaskProject\\DeepLearning\\caffe_python\\mean.binaryproto"
	  }
	  data_param {
		source: "F:\\TaskProject\\DeepLearning\\caffe_python\\train_db"
		batch_size: 64
		backend: LMDB
	  }
	}	
		
3 資料來自圖片
3.1 相關引數
	層型別:ImageData

	必須設定的引數:
	  source: 一個文字檔案的名字,每一行給定一個圖片檔案的名稱和標籤(label)
	  batch_size: 每一次處理的資料個數,即圖片數

	可選引數:
	  rand_skip: 在開始的時候,路過某個資料的輸入。通常對非同步的SGD很有用。
	  shuffle: 隨機打亂順序,預設值為false

	  new_height,new_width: 如果設定,則將圖片進行resize
	  
3.2 生成函式
    data,label=L.ImageData(source=img_list,batch_size=batch_size,ntop=2, include=dict(phase = 1), //phase = 1 表示TEST
                           transform_param=dict(scale=0.00390625))				   
	layer {
	  name: "ImageData1"
	  type: "ImageData"
	  top: "ImageData1"
	  top: "ImageData2"
	  include {
		phase: TEST
	  }
	  transform_param {
		scale: 0.00390625
	  }
	  image_data_param {
		source: "F:/TaskProject/DeepLearning/MyProject/train.txt"
		batch_size: 64
		new_height: 256
		new_width: 256 //如果設定,則將圖片進行resize
	  }
	}
	
	如果txt中寫的是相對路徑,需要在 image_data_param 中新增:root_folder+txt中路徑=影象路徑
	root_folder: "F:/TaskProject/DeepLearning/MyProject/mnist/"
	data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root,
        transform_param=dict(scale= 0.00390625))

二 卷積層
	layer {
	  name: "conv1"
	  type: "Convolution"
	  bottom: "data"
	  top: "conv1"
	  param {  //權值學習率倍乘因子
		lr_mult: 1
		decay_mult: 1 //權重衰減係數 防止過擬合
		}
	  param {  //偏置學習率倍乘因子 一般為權值的2倍
		lr_mult: 2
		}
	  convolution_param { //卷積層引數
		num_output: 20  //卷積核個數
		pad: 0          //填充大小
		kernel_size: 5  //卷積核尺寸
		stride: 1       //步長
		weight_filler {  //初始化權值  預設為“constant",值全為0,很多時候我們用"xavier"
		  type: "xavier" //演算法來進行初始化,也可以設定為”gaussian"
		}
		bias_filler { //偏置項的初始化。一般設定為"constant",值全為0。
		  type: "constant"
		  value: 0
		}
	  }
	}

    conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'),bias_filler=dict(type='constant'),
                        param = [dict(lr_mult=1), dict(lr_mult=2)])


三 池化層
	layer {
	  name: "pool1"
	  type: "Pooling"
	  bottom: "norm1"
	  top: "pool1"
	  pooling_param {
		pool: MAX
		kernel_size: 2
		stride: 2
	  }
	}
	pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=2, stride=2)

四 LRN層
	layer {
	  name: "norm1"
	  type: "LRN"
	  bottom: "conv1"
	  top: "norm1"
	  lrn_param {
		local_size: 5
		alpha: 0.0001
		beta: 0.75
	  }
	}
	
	 n.lrn2 = L.LRN(n.pool2, lrn_param = dict(local_size=3))
	
五 啟用層
1 sogmoid 函式
	layer {
	  name: "encode1neuron"
	  bottom: "encode1"
	  top: "encode1neuron"
	  type: "Sigmoid"
	}
2 Relu函式
	layer {
	  name: "relu1"
	  type: "ReLU"
	  bottom: "pool1"
	  top: "pool1"
	}
	relu4=L.ReLU(fc4, in_place=True) //relu支援in_place=True 意味著輸出覆蓋輸出,節省空間
	                                 //這樣relu層輸出與輸入有相同的層名
3 Tanh函式		
	layer {
	  name: "layer"
	  bottom: "in"
	  top: "out"
	  type: "TanH"
	}

   
六 InnerProduct層
	layer {
	  name: "ip1"
	  type: "InnerProduct"
	  bottom: "pool2"
	  top: "ip1"
	  param {
		lr_mult: 1
	  }
	  param {
		lr_mult: 2
	  }
	  inner_product_param {
		num_output: 500  //輸出層個數
		weight_filler {
		  type: "xavier"
		}
		bias_filler {
		  type: "constant"
		}
	  }
	}
	
	fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier'))
	
七 	SoftmaxWithLoss層
	layer {
	  name: "loss"
	  type: "SoftmaxWithLoss"
	  bottom: "ip1"
	  bottom: "label"  //樣本標籤
	  top: "loss"
	}

	loss = L.SoftmaxWithLoss(fc5, label)
	
八 Accuracy
	layer {
	  name: "accuracy"
	  type: "Accuracy"
	  bottom: "ip2"
	  bottom: "label" //樣本標籤
	  top: "accuracy"
	  include {
		phase: TEST
	  }
	}

	L.Accuracy(fc5, label)
九 Dropout層
	layer {
	  name: "drop7"
	  type: "Dropout"
	  bottom: "fc7"
	  top: "fc7"
	  dropout_param {
		dropout_ratio: 0.5
	  }
	}
	
	drop4 = L.Dropout(relu4, in_place=True)
	L.Dropout(n.relu3, in_place=True, dropout_param=dict(dropout_ratio=0.5))