caffe训练网络模型一般直接使用的caffe.bin: caffe train -solver solver.prototxt,其实这个命令的本质也是调用c++的Solver.

本文给出使用纯c++代码,使用mnist数据+多层感知机网络,训练数字分类问题。然后用C++调用训练好的模型测试分类。

solver.prototxt文件内容如下:

net: "/home/xy/caffe_analysis/my_caffe/example/lenet.prototxt"
test_iter: 1000
test_interval: 100
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 5000
display: 1000
max_iter: 20000
momentum: 0.9
weight_decay: 0.0005
snapshot: 10000
snapshot_prefix: "model"
solver_mode: CPU

注:网络名称虽然为lenet.prototxt,实际是多层感知机。

训练网络如下:

name: "LeNet"
layer {
  name: "mnist"
  #name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "/home/xy/caffe-master/examples/mnist/mnist_train_lmdb"
    batch_size: 64
    backend: LMDB
  }
}

layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "/home/xy/caffe-master/examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}


layer {
  name: "fc1"
  type: "InnerProduct"
  # learning rate and decay multipliers for the weights
  param { lr_mult: 1 decay_mult: 1 }
  # learning rate and decay multipliers for the biases
  param { lr_mult: 2 decay_mult: 0 }
  inner_product_param {
    num_output: 300
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  bottom: "data"
  top: "fc1"
}

layer {
  name: "relu1"
  type: "ReLU"
  bottom: "fc1"
  top: "fc1"
}

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

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "fc2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc2"
  bottom: "label"
  top: "loss"
}

训练代码如下:

google::InitGoogleLogging("test_solver");
FLAGS_alsologtostderr = 1;

string base_dir = "/home/xy/caffe_analysis/my_caffe/example/";
string path = base_dir + "mlp_solver.prototxt";
SGDSolver<float> solver(path);

// resuming from trained weights, 也可以加载已经训练好的模型
string trained_weight_path = base_dir + "model_iter_20000.caffemodel";
solver.net()->CopyTrainedLayersFrom(trained_weight_path);
// 开始优化
solver.Solve();

训练过程如下:

caffe c++示例(mnist 多层感知机c++训练,测试)-LMLPHP

接下来测试训练模型的效果的,代码如下:

void test_net(){
    Caffe::set_mode(Caffe::CPU);
    string base_dir = "/home/xy/caffe_analysis/my_caffe/example/";
    string net_path = base_dir + "lenet_deploy.prototxt";
    string trained_path = base_dir + "model_iter_20000.caffemodel";

    Net<float> net(net_path, Phase::TEST);
    net.CopyTrainedLayersFrom(trained_path);

    CHECK_EQ(net.num_inputs(), 1) << "Network should have exactly one input.";
    CHECK_EQ(net.num_outputs(), 1) << "Network should have exactly one output.";

    // read Datum from file
    string datum_path = base_dir + "0.proto";
    Datum datum;
    ReadProtoFromBinaryFileOrDie(datum_path, &datum);

    // Datum convert
    TransformationParameter parameter;
    parameter.set_scale(0.00390625);
    DataTransformer<float> dataTransformer(parameter, Phase::TEST);

    Blob<float>* input_layer = net.input_blobs()[0];
//    input_layer->Reshape(1, 1, 28, 28);//no need
    dataTransformer.Transform(datum, input_layer);

//    net.Reshape();
    net.Forward();
    Blob<float>* output_layer = net.output_blobs()[0];

    const float* begin = output_layer->cpu_data();
    const float* end = begin + output_layer->channels();

    std::vector<float> ret(begin, end);

    for(auto item:ret)cout<<item<<" ";
    cout<<endl;

}

运行输出如下:可以看到0的概率最大。

caffe c++示例(mnist 多层感知机c++训练,测试)-LMLPHP

测试inference的网络定义如下:

name: "LeNet"

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}

layer {
  name: "fc1"
  type: "InnerProduct"
  # learning rate and decay multipliers for the weights
  param { lr_mult: 1 decay_mult: 1 }
  # learning rate and decay multipliers for the biases
  param { lr_mult: 2 decay_mult: 0 }
  inner_product_param {
    num_output: 300
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
  bottom: "data"
  top: "fc1"
}

layer {
  name: "relu1"
  type: "ReLU"
  bottom: "fc1"
  top: "fc1"
}

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

layer {
  name: "prob"
  type: "Softmax"
  bottom: "fc2"
  top: "prob"
}
10-07 15:42