How to modify num_ouput in C++?

29 views
Skip to first unread message

laurent...@univ-lemans.fr

unread,
Mar 14, 2018, 4:59:02 PM3/14/18
to Caffe Users
Hi,

My program in C++ read a le_net_train_test :

        caffe::ReadSolverParamsFromTextFileOrDie(solverParamName, &solver_param);
        boost::shared_ptr<caffe::Solver<float> > solver(caffe::SolverRegistry<float>::CreateSolver(solver_param));

before training I want to set num_output in conv1 layer. How can I set this value in C++?

Thanks for yours answers

Przemek D

unread,
Mar 20, 2018, 8:41:10 AM3/20/18
to Caffe Users
I would go with modifying the network prototxt. When the solver is instantiated, so is the net - layers are constructed and configured according to the prototxt, and modifying it after they're set up might not be easy. You would probably have to gain access to some private attributes, manually call LayerSetUp etc. - I don't think it's the right way to go about it. Definitely not when all you want to do is change the value once for the whole process.

laurent...@univ-lemans.fr

unread,
Mar 20, 2018, 12:16:12 PM3/20/18
to Caffe Users

Thanks for yours answers

I solve my problem using @Charles West post

I have to create a net using an existing net. Now i can  modify all layer parameters :

template <typename T>
caffe
::NetParameter ModifyNet(string nomFichier,T &y,string layerName,int numOutput)
{
    caffe
::NetParameter param;
    caffe
::NetParameter param2;

    fstream fd
(nomFichier.c_str(), ios::binary | ios::in);
    google
::protobuf::io::IstreamInputStream input1(&fd);
   
if (!fd.is_open())
        cout
<< "File not found: " << nomFichier;
   
bool success = google::protobuf::TextFormat::Parse(&input1, &param);
   
for (int kSize = 0; kSize < param.input_size(); kSize++)
   
{
       
*param2.add_input() = param.input(kSize);
   
}
   
for (int j = 0; j < param.input_dim_size(); j++)
   
{
        param2
.add_input_dim(param.input_dim(j));
   
}
    param2
.set_name(param.name());
   
for (int nlayers = 0; nlayers < param.layer_size(); nlayers++)
   
{
        caffe
::LayerParameter *couche = new caffe::LayerParameter(param.layer(nlayers));
       
if (couche->name() == layerName)
       
{
            couche
->mutable_convolution_param()->set_num_output(numOutput);
       
}
       
*param2.add_layer() = *couche;
   
}
   
string s;
   
return param2;
}




Reply all
Reply to author
Forward
0 new messages