- layer {
- name: “data”
- type: “Data”
- top: “data”
- top: “label”
- include {
- phase: TRAIN
- }
- transform_param {
- mirror: true
- crop_size: 600
- mean_file: “examples/images/imagenet_mean.binaryproto”
- }
- data_param {
- source: “examples/images/train_lmdb”
- batch_size: 256
- backend: LMDB
- }
- }
- layer {
- name: “data”
- type: “Data”
- top: “data”
- top: “label”
- include {
- phase: TEST
- }
- transform_param {
- mirror: false
- crop_size: 600
- mean_file: “examples/images/imagenet_mean.binaryproto”
- }
- data_param {
- source: “examples/images/val_lmdb”
- batch_size: 50
- backend: LMDB
- }
- }
從上面的 數據層的定義,看得出用了鏡像和crop_size,還定義了 mean_file。
利用crop_size這種方式可以剪裁中心關注點和邊角特徵,mirror可以產生鏡像,彌補小數據集的不足.
這裏要重點講一下crop_size在訓練層與測試層的區別:
首先我們需要了解mean_file和crop_size沒什麼大關係。mean_file是根據訓練集圖片製作出來的,crop_size是對訓練集圖像進行裁剪,兩個都是對原始的訓練集圖像進行處理。如果原始訓練圖像的尺寸大小爲800*800,crop_size的圖片爲600*600,則mean_file與crop_size的圖片均爲800*800的圖像集。
在caffe中,如果定義了crop_size,那麼在train時會對大於crop_size的圖片進行隨機裁剪,而在test時只是截取中間部分(詳見/caffe/src/caffe/data_transformer.cpp):
- //We only do random crop when we do training.
- if (phase_ == TRAIN) {
- h_off = Rand(datum_height - crop_size + 1);
- w_off = Rand(datum_width - crop_size + 1);
- } else {
- h_off = (datum_height - crop_size) / 2;
- w_off = (datum_width - crop_size) / 2;
- }
- }
從上述的代碼可以看出,如果我們輸入的圖片尺寸大於crop_size,那麼圖片會被裁剪。當 phase 模式爲 TRAIN 時,裁剪是隨機進行裁剪,而當爲TEST 模式時,其裁剪方式則只是裁剪圖像的中間區域。
下面是我在網上找到的自己進行圖像裁剪的程序:
可對照給出的網址進行詳細閱讀:http://blog.csdn.net/u011762313/article/details/48343799
我們可以手動將圖片裁剪並導入pycaffe中,這樣能夠提高識別率(pycaffe利用caffemodel進行分類中:進行分類
這一步改爲如下):
- #記錄分類概率分佈
- pridects = np.zeros((1, CLASS_NUM))
-
- # 圖片維度(高、寬)
- img_shape = np.array(img.shape)
- # 裁剪的大小(高、寬)
- crop_dims = (32, 96)
- crop_dims = np.array(crop_dims)
- # 這裏使用的圖片高度全部固定爲32,長度可變,最小爲96
- # 裁剪起點爲0,終點爲w_range
- w_range = img_shape[1] - crop_dims[1]
- # 從左往右剪一遍,再從右往左剪一遍,步長爲96/4=24
- for k in range(0, w_range + 1, crop_dims[1] / 4) + range(w_range, 1, -crop_dims[1] / 4):
- # 裁剪圖片
- crop_img = img[:, k:k + crop_dims[1], :]
- # 數據輸入、預處理
- net.blobs[‘data’].data[…] = transformer.preprocess(‘data’, crop_img)
- # 前向迭代,即分類
- out = net.forward()
- # 每一次分類,概率分佈疊加
- pridects += out[‘prob’]
-
- # 取最大的概率分佈爲最終結果
- pridect = pridects.argmax()
- caffe中提供了過採樣的方法(oversample),詳見/caffe/python/caffe/io.py,裁剪的是圖片中央、4個角以及鏡像共10張圖片。