Caffe - 从train_val.prototxt创建deploy.prototxt

时间:2016-01-12 18:06:08

标签: matlab input hdf5 caffe conv-neural-network

我已经对我的数据集上的imagenet预训练模型进行了微调,以下是train_val.prototxt中的相关更改(我还没有进行过采样,只是准备hdf5时的中心裁剪)

name: "MyCaffeNet"
layer {
  type: "HDF5Data"
  name: "data"
  top: "X"  
  top: "Meta" 
  top: "Labels"
  hdf5_data_param {
    source: "/path/to/hdf5_train.txt"
    batch_size: 50
  }
  include { phase: TRAIN }
}
layer {
  type: "HDF5Data"
  name: "data"
  top: "X"    
  top: "Meta" 
  top: "Labels"
  hdf5_data_param {
    source: "/path/to/hdf5_test.txt"
    batch_size: 50
 }
  include { phase: TEST }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "X"

其余部分一样直到

layer {
  name: "concat"
  bottom: "fc7"
  bottom: "Meta"
  top: "combined"
  type: "Concat"
  concat_param {
    concat_dim: 1
  }
}
layer {
  name: "my-fc8"
  type: "InnerProduct"
  bottom: "combined"
  top: "my-fc8"
  # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 4098
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
 }
}
layer {
  name: "my-fc9"
  type: "InnerProduct"
  bottom: "my-fc8"
  top: "my-fc9"
  # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 1
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "loss"
  type: "EuclideanLoss"
  bottom: "my-fc9"
  bottom: "Labels"
}
layer {
  name: "loss"
  type: "EuclideanLoss"
  bottom: "my-fc9"
  bottom: "Labels"
  top: "loss"
  include {
    phase: TEST
  }
}

问题是我没有得到如何修改我的deploy.prototxt和我的特征提取代码(下面给出)来测试这个模型,现在使用额外的元信息作为输入功能

for i=1:n
    im = fgetl(file_list);
    im = imread(im);
    input_data = {prepare_image(im)};
    scores = caffe('forward', input_data);
    scores_original=scores;
    scores = scores{1};
    scores = squeeze(scores);
end

这里prepare_image对bgr,permute和中心裁剪的东西做了rgb,基本上是预处理。

所以,归结为我应该如何修改'caffe('forward',input_data)';并制作一个deploy.prototxt,以便在测试caffe时也可以获得Meta(n * 2)功能。感谢您的耐心和帮助!

我认为deploy.prototxt的开头应该是

name: "MyCaffeNet"
input: "X"
input_dim: 1
input_dim: 3
input_dim: 227
input_dim: 227
input: "Meta"
input_dim: 1
input_dim: 1
input_dim: 1
input_dim: 2 # two additional 'features' per image

1 个答案:

答案 0 :(得分:0)

变量input_data可以是4-D blob的向量。函数prepare_data给出了4D blob的图像数据,类似地,可以对4D blob的meta或任何其他类型的输入进行充分重塑,然后将其传递给forward函数。

input_data = {prepare_data(im),prepare_meta()};

scores = caffe('forward', input_data');