// test new_creat layer
#include<iostream>
#include<string>
#include<vector>
#include<opencv2/opencv.hpp>
#include<caffe/caffe.hpp>
#include"caffe_reg.h"
#include"sin_layer.hpp"
using namespace std;
using namespace cv;
using namespace caffe;
void wrapInputLayer(Net<float>* net, vector<Mat> * input_channels)
{
Blob<float>* input_layer = net->input_blobs()[0];
int width = input_layer->width();
int height = input_layer->height();
float *input_data = input_layer->mutable_cpu_data();
for (int i = 0; i < input_layer->channels(); ++i)
{
Mat channel(height, width, CV_32FC1, input_data);
input_channels->push_back(channel);
input_data += width * height;
}
}
int main(void)
{
// for test
string deploy_proto = "ZOO_VGG16/deploy_sinlayer.prototxt";
//string pre_trained_weight = "ZOO_VGG16/vgg__iter_10000.caffemodel";
Net<float> * net = new Net<float>(deploy_proto, caffe::Phase::TEST, 0);
// use safe pointer
//shared_ptr<Net<float> > net; // warp Net<float> to a pointer
//net.reset(new Net<float>(deploy_proto, TEST));
//net->CopyTrainedLayersFrom(pre_trained_weight);
// create input_data of 1*3*6*6
int rows = 6;
int cols = 6;
vector<Mat> input_channels;
wrapInputLayer(net,&input_channels); // bind net->input_data to vector<mat>
//
input_channels[0] = Mat::ones(rows, cols, CV_32FC1);
input_channels[1] = Mat::ones(rows, cols, CV_32FC1) * 2;
input_channels[2] = Mat::ones(rows, cols, CV_32FC1) * 3;
net->Forward();
const float * data = net->blob_by_name("data")->cpu_data();
const float *sin_data = net->blob_by_name("sin")->cpu_data();
int count_input = net->blob_by_name("data")->count();
int count_output = net->blob_by_name("sin")->count();
assert(count_input == count_output);
for (int i = 0; i < count_input; i++)
{
cout << data[i] << " ";
}
cout << endl;
for (int i = 0; i < count_output; i++)
{
cout << sin_data[i] << " ";
}
cout << endl;
cout << "sin(1): sin(2): sing(3)" << endl;
cout << sin(1) << ";" << sin(2) << ";" <<sin(3)<<endl;
// output class probability
cout << "The probabilites for each classs: " << endl;
const float *prob = net->blob_by_name("prob")->cpu_data();
int nclass = net->blob_by_name("prob")->channels();
for (int i = 0; i < nclass; i++)
{
cout << prob[i] << " ";
}
cout << endl;
//free Net
}
C++ caffe train
#include<iostream>
#include<string>
#include<vector>
#include<opencv2/opencv.hpp>
#include<caffe/caffe.hpp>
#include"caffe_reg.h"
#include"sin_layer.hpp"
#include<memory>
using namespace std;
using namespace cv;
using namespace caffe;
int main()
{
// for train
Caffe::set_mode(Caffe::GPU);
SolverParameter solver_param;
string solver_file = "ZOO_VGG16/solver.prototxt";
//string train_proto = "ZOO_VGG16/train_sinlayer.prototxt";
ReadSolverParamsFromTextFileOrDie(solver_file, &solver_param);
//boost::shared_ptr<Solver<float> > solver(
// SolverRegistry<float>::CreateSolver(solver_param));
Solver<float> * solver = SolverRegistry<float>::CreateSolver(solver_param);//Sover<float> 是纯虚函数,所有不能实例化对象,通过指针的形式指向子类。
boost::shared_ptr<Net<float> > net = solver->net(); //train net
//net->CopyTrainedLayersFrom(pre_trained_weight);
cout << "forward... " << endl;
net->Forward(); // forward once
cout << "backward..." << endl;
net->Backward();// backward once
const float * data = net->blob_by_name("data")->cpu_data();
const float *sin_data = net->blob_by_name("sin")->cpu_data();
cout << endl;
return 0;
}
参考文献:
https://github.com/BVLC/caffe/wiki/Simple-Example:-Sin-Layer