[go: up one dir, main page]

File: train_model.cc

package info (click to toggle)
imms 3.1.0~svn301-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,320 kB
  • sloc: cpp: 7,810; ansic: 1,923; xml: 252; sh: 189; makefile: 142
file content (220 lines) | stat: -rw-r--r-- 6,850 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <torch/MatDataSet.h>
#include <torch/TwoClassFormat.h>
#include <torch/ClassMeasurer.h>
#include <torch/MSEMeasurer.h>
#include <torch/QCTrainer.h>
#include <torch/CmdLine.h>
#include <torch/Random.h>
#include <torch/MeanVarNorm.h>
#include <torch/SVMClassification.h>
#include <torch/KFold.h>
#include <torch/DiskXFile.h>
#include <torch/ClassFormatDataSet.h>

#include <assert.h>
#include <iostream>

#include <model/model.h>
#include <immscore/immsutil.h>

const string AppName = "train_model";

using namespace Torch;
using namespace std;

int main(int argc, char **argv)
{
  char *file;
  real c_cst, stdv = 12;
  real accuracy, cache_size;
  int iter_shrink, k_fold;
  char *model_file;

  Allocator *allocator = new Allocator;

  //=================== The command-line ==========================

  // Construct the command line
  CmdLine cmd;

  // Train mode
  cmd.addText("\nArguments:");
  cmd.addSCmdArg("file", &file, "the train file");
  cmd.addSCmdArg("model", &model_file, "the model file");

  cmd.addText("\nModel Options:");
  cmd.addRCmdOption("-c", &c_cst, 100., "trade off cst between error/margin");
  cmd.addRCmdOption("-std", &stdv, 12,
          "the std parameter in the gaussian kernel", true);

  cmd.addText("\nLearning Options:");
  cmd.addRCmdOption("-e", &accuracy, 0.01, "end accuracy");
  cmd.addRCmdOption("-m", &cache_size, 50., "cache size in Mo");
  cmd.addICmdOption("-h", &iter_shrink,
          100, "minimal number of iterations before shrinking");

  cmd.addText("\nMisc Options:");

  // KFold mode (one difference with previous mode: no model is available)
  cmd.addMasterSwitch("--kfold");
  cmd.addText("\nArguments:");
  cmd.addSCmdArg("file", &file, "the train file");
  cmd.addICmdArg("k", &k_fold, "number of folds");

  cmd.addText("\nModel Options:");
  cmd.addRCmdOption("-c", &c_cst, 100.,
          "trade off cst between error/margin");
  cmd.addRCmdOption("-std", &stdv, 12,
          "the std parameter in the gaussian kernel", true);

  cmd.addText("\nLearning Options:");
  cmd.addRCmdOption("-e", &accuracy, 0.01, "end accuracy");
  cmd.addRCmdOption("-m", &cache_size, 50., "cache size in Mo");
  cmd.addICmdOption("-h", &iter_shrink, 100,
          "minimal number of iterations before shrinking");

  // Test mode
  cmd.addMasterSwitch("--test");
  cmd.addText("\nArguments:");
  cmd.addSCmdArg("model", &model_file, "the model file");
  cmd.addSCmdArg("file", &file, "the test file");

  // Test mode
  cmd.addMasterSwitch("--validate");
  cmd.addText("\nArguments:");
  cmd.addSCmdArg("model", &model_file, "the model file");
  cmd.addSCmdArg("file", &file, "the test file");

  // Read the command line
  int mode = cmd.read(argc, argv);

  DiskXFile *model = NULL;
  if(mode >= 2)
    model = new(allocator) DiskXFile(model_file, "r");

  if(mode < 2)
      Random::seed();
  cmd.setWorkingDirectory(".");

  //=================== Create the SVM... =========================
  Kernel *kernel = new(allocator) GaussianKernel(1./(stdv*stdv));
  SVM *svm = new(allocator) SVMClassification(kernel);

  if(mode < 2)
  {
    svm->setROption("C", c_cst);
    svm->setROption("cache size", cache_size);
  }
  //=================== DataSets & Measurers... ===================

  // Create the training dataset
  MatDataSet *mat_data =
      new(allocator) MatDataSet(file, -1, 1, false, -1, false);
  MatDataSet *orig_mat_data =
      new(allocator) MatDataSet(file, -1, 1, false, -1, false);
  Sequence *class_labels = new(allocator) Sequence(2, 1);
  class_labels->frames[0][0] = -1;
  class_labels->frames[0][1] = 1;

  MeanVarNorm *mv_norm = new(allocator) MeanVarNorm(mat_data);
  if(mode >= 2)
      mv_norm->loadXFile(model);
  mat_data->preProcess(mv_norm);

  DataSet *data = new(allocator) ClassFormatDataSet(mat_data, class_labels);
  DataSet *orig_data =
      new(allocator) ClassFormatDataSet(orig_mat_data, class_labels);

  // The list of measurers...
  MeasurerList measurers;
  TwoClassFormat *class_format = new(allocator) TwoClassFormat(data);
  ClassMeasurer *class_meas = new(allocator) ClassMeasurer(
          svm->outputs, data, class_format, cmd.getXFile("the_class_err"));

  if(mode > 0)
      measurers.addNode(class_meas);

  // Reload the model in test mode
  if(mode >= 2)
    svm->loadXFile(model);
  
  //=================== The Trainer ===============================

  QCTrainer trainer(svm);
  if(mode == 0)
  {
    trainer.setROption("end accuracy", accuracy);
    trainer.setIOption("iter shrink", iter_shrink);
  }

  //=================== Let's go... ===============================

  // Train
  if(mode == 0)
  {
    trainer.train(data, NULL);
    message("%d SV with %d at bounds", svm->n_support_vectors,
            svm->n_support_vectors_bound);
    DiskXFile model_(model_file, "w");
    mv_norm->saveXFile(&model_);
    svm->saveXFile(&model_);
  }

  // KFold
  if(mode == 1)
  {
    KFold k(&trainer, k_fold);
    k.crossValidate(data, NULL, &measurers);
  }

  // Test
  if(mode >= 2) {
    trainer.test(&measurers);

    SVMSimilarityModel model;

    float correct = 0, wrong = 0;
    for (int t = 0; t < data->n_examples; t++) {
        data->setExample(t);
        orig_data->setExample(t);

        svm->forward(data->inputs);
        int true_class = class_format->getClass(data->targets->frames[0]);
        int obs_class = class_format->getClass(svm->outputs->frames[0]);
        (true_class == obs_class ? correct : wrong) += 1;
        float score = svm->outputs->frames[0][0] / 3;
        assert(obs_class > 0 || score < 0);
        if (mode == 3) {
            float model_score = model.evaluate(orig_data->inputs->frames[0]);
            if (fabs(score - model_score) > 0.01)
                cout << "Er: " << score << " vs. " << model_score << endl;
        }
    }

    cout << "TOTAL      : " << correct + wrong << endl;
    cout << "CORRECT    : " << correct << endl;
    cout << "WRONG      : " << wrong << endl;
    cout << "ERROR      : " << wrong / (correct + wrong) << endl;
  }

  delete allocator;
  return 0;
}