how to save, load and test a trained machine

166 views
Skip to first unread message

Tianxu Jia

unread,
Oct 18, 2012, 11:08:14 AM10/18/12
to ebl...@googlegroups.com

Dear All

Now I am using C++ for training Lenet5 save it and load it again and testing it. I don’t know whether I am doing it right.

My code for training and saving it

 

#include "stdafx.h"

#include "libeblearn.h"

#include "libeblearngui.h"

#include "idxIO.h"

#include "libidxgui.h"

 

#ifdef _DEBUG

 

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\eblearntools.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\eblearn.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\idx.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\idxgui.lib");

#pragma comment(lib,"C:\\QtSDK\\Desktop\\Qt\\4.8.1\\msvc2010\\lib\\QtGuid4.lib");

#pragma comment(lib,"C:\\QtSDK\\Desktop\\Qt\\4.8.1\\msvc2010\\lib\\QtCored4.lib");

#else

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\eblearntools.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\eblearn.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\idx.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\idxgui.lib");

#endif

 

MAIN_QTHREAD(int, argc, char**, argv)

{       

       typedef double Tnet;

       typedef ebl::ubyte Tdata;

       typedef ebl::ubyte Tlab;

       bool display = true;

       uint ninternals = 1;

 

       ebl::init_drand(time(NULL)); // initialize random seed

 

       std::string mnist_dir = "D:\\work\\EBLearn\\eblearn-1.0\\data";

 

       //! load MNIST datasets

       ebl::mnist_datasource<Tnet, ebl::ubyte,ebl::ubyte>   

              train_ds(mnist_dir.c_str(), true, 2000);

 

       train_ds.set_balanced(true);

       train_ds.set_shuffle_passes(true);

      

       train_ds.set_weigh_samples(false, false, false, 0.01);

       train_ds.set_epoch_show(100); // show progress every 100 samples

       train_ds.ignore_correct(true);

 

       // create 1-of-n targets with target 1.0 for shown class, -1.0 for the rest

       ebl::intg temp = 1+idx_max(train_ds.labels);

       ebl::idx<Tnet> targets =

              ebl::create_target_matrix<Tnet>(1+idx_max(train_ds.labels), 1.0);

       uint nclasses = targets.dim(0);

 

        // create the network weights, network and trainer

       ebl::parameter<Tnet> theparam(60000); // create trainable parameter 

       ebl::lenet5<Tnet> net(theparam, 32, 32, 5, 5, 2, 2, 5, 5, 2, 2, 120, nclasses, true, false, true, false);

       std::cout << net.describe() << std::endl;

 

       ebl::l2_energy<Tnet> energy;

       ebl::class_answer<Tnet,Tdata,Tlab> answer(nclasses);

       ebl::trainable_module<Tnet,Tdata,Tlab> trainable(energy, net, NULL, NULL, &answer);

       ebl::supervised_trainer<Tnet, ebl::ubyte, ebl::ubyte> thetrainer(trainable, theparam);

 

       ebl::supervised_trainer_gui<Tnet,Tdata,Tlab> stgui;

 

       // a classifier-meter measures classification errors

       ebl::classifier_meter trainmeter;

 

       // initialize the network weights

       ebl::forget_param_linear fgp(1, 0.5);

       trainable.forget(fgp);

 

       // gradient parameters

       ebl::gd_param gdp(/* double eta*/ 0.0001,

              /* double ln */     0.0,

              /* double l1 */     0.0,

              /* double l2 */     0.0,

              /* intg dtime */    0,

              /* double iner */0.0,

              /* double anneal_value */ 0.001,

              /* intg anneal_period */ 2000,

              /* double g_t*/     0.0);

       std::cout << "gdp = "<<gdp << std::endl;

       ebl::infer_param infp;

       ebl::fixed_init_drand(); // fixed randomization

      

       thetrainer.compute_diaghessian(train_ds, 100, 0.02);

         

       for (int i = 0; i < 5; ++i) {

              thetrainer.train(train_ds, trainmeter, gdp, 1, infp);                     

              thetrainer.compute_diaghessian(train_ds, 100, 0.02);         

       }

 

       const std::string  filename = "D:\\work\\EBLearn\\eblearn-1.0\\lenet5.mat";

 

       theparam.save_x(filename.c_str());

 

       std::cout<<"OK";

 

       return 0;

}

 

 

Here is my code of loading the lenet5 machine

#include "stdafx.h"

#include "libeblearn.h"

#include "libeblearngui.h"

 

#include "libidxgui.h"

 

#ifdef _DEBUG

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\eblearntools.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\eblearn.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\idx.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\debug\\idxgui.lib");

#pragma comment(lib,"C:\\QtSDK\\Desktop\\Qt\\4.8.1\\msvc2010\\lib\\QtGuid4.lib");

#pragma comment(lib,"C:\\QtSDK\\Desktop\\Qt\\4.8.1\\msvc2010\\lib\\QtCored4.lib");

#else

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\eblearntools.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\eblearn.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\idx.lib");

#pragma comment(lib,"D:\\work\\EBLearn\\eblearn-1.0\\bin\\Release\\idxgui.lib");

#endif

 

MAIN_QTHREAD(int, argc, char**, argv)

{       

       typedef double Tnet;

       typedef ebl::ubyte Tdata;

       typedef ebl::ubyte Tlab;

       bool display = true;

       uint ninternals = 1;

 

       ebl::init_drand(time(NULL)); // initialize random seed

      

       std::string mnist_dir = "D:\\work\\EBLearn\\eblearn-1.0\\data";

 

       //! load MNIST datasets: trsize for training set and tesize for testing set

       ebl::mnist_datasource<Tnet, ebl::ubyte,ebl::ubyte>   

              test_ds(mnist_dir.c_str(), false, 1000);

 

       // create 1-of-n targets with target 1.0 for shown class, -1.0 for the rest

       ebl::idx<Tnet> targets =

              ebl::create_target_matrix<Tnet>(1+idx_max(test_ds.labels), 1.0);

       uint nclasses = targets.dim(0);

 

        // create the network weights, network and trainer

       ebl::idxdim dims(test_ds.sample_dims()); // get order and dimensions of sample

       ebl::parameter<Tnet> theparam;// create trainable parameter

       std::string file = "D:\\work\\EBLearn\\eblearn-1.0\\lenet5.mat";

       theparam.load_x(file.c_str());

       ebl::lenet5<Tnet> net(theparam, 32, 32, 5, 5, 2, 2, 5, 5, 2, 2, 120, nclasses, true, false, true, false);

 

       return 0;

}

 

Here are my problems:

1.  Is my way of saving the trained machine(lenet5) correct? What is the right way of doing it if it is wrong?

 

2.  Is my way of using the trained machine saved in file correct? Otherwise, how to do it?

 

3.  I don’t know the API of running(testing) the machine(lenet5) with input a frame of image. Can you please tell me how to do?

 

4.  My code always has run time exception at the line

 

         theparam.load_x(file.c_str());

        

I don’t know why

 

I am struggling with these problems. Can you so kindly give me a help? It will be great help if you can show me with several pieces of codes

 

 

You any suggestion will be greatly appreciated

 

 

Best regards

 

Sunny

 

 

 

 

 

Reply all
Reply to author
Forward
0 new messages