?? distbindata.cc
字號:
const char *help = "\progname: distbindata.cc\n\code2html: This program computes a distance between 2 bindata files.\n\version: Torch3 vision2.0, 2004-2006\n\(c) Sebastien Marcel (marcel@idiap.ch)\n";// core#include "string_utils.h"// datasets#include "FileBinDataSet.h"// machines#include "ConnectedMachine.h"#include "Linear.h"#include "Tanh.h"#include "LogSoftMax.h"// normalisation#include "MyMeanVarNorm.h"// metrics#include "Pearson.h"#include "Canberra.h"#include "NormalizeCorrelation.h"#include "StandardCorrelation.h"#include "StandardCovariance.h"#include "ChiSquare.h"#include "TangentDistance.h"#include "Mahanalobis.h"// eigen#include "PCAMachine.h"// misc#include "CmdLine.h"#include "FileListCmdOption.h"using namespace Torch;real mMlpOneHot(int n_inputs, real *x, real *y, ConnectedMachine *mlp, MyMeanVarNorm *mv_norm, Sequence *seq, bool diff, bool delta){ if(diff) for(int i = 0 ; i < n_inputs ; i++) seq->frames[0][i] = x[i] - y[i]; else if(delta) { int j = 0; for(int i = 0 ; i < n_inputs ; i++, j++) seq->frames[0][j] = x[i] - y[i]; for(int i = 0 ; i < n_inputs ; i++, j++) seq->frames[0][j] = y[i]; } else { int j = 0; for(int i = 0 ; i < n_inputs ; i++, j++) seq->frames[0][j] = x[i]; for(int i = 0 ; i < n_inputs ; i++, j++) seq->frames[0][j] = y[i]; } mv_norm->preProcessInputs(seq); mlp->forward(seq); return mlp->outputs->frames[0][0] - mlp->outputs->frames[0][1];}int main(int argc, char **argv){ char *template_file; char *model_file; char *score_filename; char *norm_model_filename; bool use_mean_template; bool verbose; bool one_score_per_file; // bool mahalanobis; bool canberra; bool pearson; bool nc; bool stdcor; bool stdcov; bool chisquare; bool td; bool mlpmetric1hot; // bool diff; bool delta; int dim; // int width; int height; // Allocator *allocator = new Allocator; DiskXFile::setLittleEndianMode(); //=================== The command-line ========================== FileListCmdOption filelist("file name", "the list files or one data file"); filelist.isArgument(true); // Construct the command line CmdLine cmd; cmd.setBOption("write log", false); // Put the help line at the beginning cmd.info(help); // Train mode cmd.addText("\nArguments:"); cmd.addSCmdArg("template", &template_file, "the template file to compare with"); cmd.addCmdOption(&filelist); cmd.addText("\nOptions:"); cmd.addBCmdOption("-verbose", &verbose, false, "verbose", true); cmd.addBCmdOption("-use_mean", &use_mean_template, false, "use the mean model", true); cmd.addBCmdOption("-one_score_per_file", &one_score_per_file, false, "computes one score per input file", true); cmd.addSCmdOption("-score", &score_filename, "", "score filename"); cmd.addSCmdOption("-norm", &norm_model_filename, "", "norm model filename"); cmd.addText("\nFeatures:"); cmd.addBCmdOption("-diff", &diff, false, "diff input features", true); cmd.addBCmdOption("-delta", &delta, false, "delta input features", true); cmd.addICmdOption("-dim", &dim, -1, "dimension to use", true); cmd.addText("\nMetrics:"); cmd.addBCmdOption("-mahalanobis", &mahalanobis, false, "Mahalanobis metric", true); cmd.addBCmdOption("-canberra", &canberra, false, "Canberra metric", true); cmd.addBCmdOption("-pearson", &pearson, false, "one minus Pearson correlation", true); cmd.addBCmdOption("-nc", &nc, false, "Normalized correlation", true); cmd.addBCmdOption("-stdcor", &stdcor, false, "Standard Correlation", true); cmd.addBCmdOption("-stdcov", &stdcov, false, "Standard Covariance", true); cmd.addBCmdOption("-chisquare", &chisquare, false, "Chi Square", true); cmd.addBCmdOption("-td", &td, false, "tangent distance", true); cmd.addICmdOption("-width", &width, -1, "width of the image for tangent distance", true); cmd.addICmdOption("-height", &height, -1, "height of the image for tangent distance", true); cmd.addBCmdOption("-mlpmetric1hot", &mlpmetric1hot, false, "mlpmetric1hot distance"); cmd.addSCmdOption("-model", &model_file, "", "model filename"); // Read the command line cmd.read(argc, argv); // if(verbose) { if(mahalanobis) print("Using Mahalanobis-cosine metric with PCA model %s\n", model_file); else if(td) print("Using Tangent distance on %dx%d images\n", width, height); else if(canberra) print("Using Canberra metric\n"); else if(pearson) print("Using one minus Pearson correlation\n"); else if(nc) print("Using Normalized correlation\n"); else if(stdcor) print("Using Standard Correlation\n"); else if(stdcov) print("Using Standard Covariance\n"); else if(chisquare) print("Using Chi Square\n"); else if(mlpmetric1hot) print("Using One hot MLP metric with model %s\n", model_file); else print("No metric chosen, setting to Euclidean by default\n"); print(" + n_filenames = %d\n", filelist.n_files); for(int i = 0 ; i < filelist.n_files ; i++) print(" filename[%d] = %s\n", i, filelist.file_names[i]); } // load the template int n_inputs_template; int n_patterns_model; DiskXFile model(template_file, "r"); model.read(&n_patterns_model, sizeof(int), 1); model.read(&n_inputs_template, sizeof(int), 1); if(verbose) { print(" Number of inputs = %d\n", n_inputs_template); print(" Number of reference patterns = %d\n", n_patterns_model); } real **ref_model = new real*[n_patterns_model]; real *mean_model = new real [n_inputs_template]; for(int j=0; j< n_inputs_template; j++) mean_model[j] = 0.0; for(int p = 0 ; p < n_patterns_model ; p++) { ref_model[p] = new real [n_inputs_template]; model.read(ref_model[p], sizeof(real), n_inputs_template); for(int j=0; j< n_inputs_template; j++) { mean_model[j] += ref_model[p][j]; } } for(int j=0; j< n_inputs_template; j++) mean_model[j] /= (real) n_patterns_model; real *inputs = new real [n_inputs_template]; // load the normalization DiskXFile *normfile = NULL; real mu = 0.0; real sigma = 1.0; if(strcmp(norm_model_filename, "") != 0) { normfile = new(allocator) DiskXFile(norm_model_filename, "r"); normfile->read(&mu, sizeof(real), 1); normfile->read(&sigma, sizeof(real), 1); print("Norm model (%s): mu=%g \t sigma = %g\n", norm_model_filename, mu, sigma); } // DiskXFile *scorefile = NULL; if(strcmp(score_filename, "") != 0) scorefile = new(allocator) DiskXFile(score_filename, "w"); // create the metric Metric *metric = NULL; ConnectedMachine *mlp = NULL; MyMeanVarNorm *mv_norm = NULL; Sequence *seq; PCAMachine *pca_machine = NULL; int dim_ = dim; if((dim_ == -1) || (dim_ > n_inputs_template)) dim_ = n_inputs_template; if(canberra) metric = new mCanberra(dim_); else if(pearson) metric = new mPearson(dim_); else if(nc) metric = new mNC(dim_); else if(stdcor) metric = new mStdCorrelation(dim_); else if(stdcov) metric = new mStdCovariance(dim_); else if(chisquare) metric = new mChiSquare(dim_); else if(td) { if(width != -1 && height != -1 && width * height == n_inputs_template) metric = new mTangentDistance(width, height); else error("width(%d) or height (%d) incorrect for Tangent Distance", width, height); } else if(mahalanobis) { if(strcmp(model_file, "")) { pca_machine = new PCAMachine(n_inputs_template); DiskXFile *file = NULL; file = new DiskXFile(model_file, "r"); pca_machine->loadXFile(file); delete file; pca_machine->setIOption("verbose_level", 1); pca_machine->setROption("variance", -1.0); pca_machine->init(); if(dim > 0) pca_machine->n_outputs = dim; metric = new mMahanalobisCosine(n_inputs_template, pca_machine); } else error("No PCA model available for Mahalanobis"); } else if(mlpmetric1hot) { if(strcmp(model_file, "")) { int n_inputs_; int n_hu; int n_outputs; print("Loading One hot MLP metric\n"); DiskXFile mlpmodel(model_file, "r"); mlpmodel.taggedRead(&n_inputs_, sizeof(int), 1, "N_INPUTS"); mlpmodel.taggedRead(&n_hu, sizeof(int), 1, "N_HU"); mlpmodel.taggedRead(&n_outputs, sizeof(int), 1, "N_OUTPUTS"); print(" Number of inputs = %d\n", n_inputs_); print(" Number of hidden units = %d\n", n_hu); print(" Number of outputs = %d\n", n_outputs); if(diff) { print("Using diff features.\n"); if(n_inputs_ != n_inputs_template) error("Number of inputs incorrect."); } else { if(delta) print("Using delta features.\n"); if(n_inputs_ != 2*n_inputs_template) error("Number of inputs incorrect."); } if(n_outputs != 2) error("Number of outputs incorrect."); // mlp = new(allocator) ConnectedMachine; Linear *c1 = new(allocator) Linear(n_inputs_, n_hu); Tanh *c2 = new(allocator) Tanh(n_hu); Linear *c3 = new(allocator) Linear(n_hu, n_outputs); GradientMachine *c4 = new(allocator) LogSoftMax(n_outputs); mlp->addFCL(c1); mlp->addFCL(c2); mlp->addFCL(c3); mlp->addFCL(c4); mlp->build(); // mv_norm = new(allocator) MyMeanVarNorm(n_inputs_, 1); // mv_norm->loadXFile(&mlpmodel); mlp->loadXFile(&mlpmodel); seq = new(allocator) Sequence(1, n_inputs_); } else error("No model available"); } else metric = new mEuclidean(dim_); for(int i = 0 ; i < filelist.n_files ; i++) { if(verbose) print(" + filename[%d] = %s\n", i, filelist.file_names[i]); char *temp = strBaseName(filelist.file_names[i]); char *file_name = strRemoveSuffix(temp); if(scorefile != NULL) if(one_score_per_file) scorefile->printf("%s ", file_name); int n_inputs; int n_patterns; // Test the file DiskXFile *file = new DiskXFile(filelist.file_names[i], "r"); file->read(&n_patterns, sizeof(int), 1); file->read(&n_inputs, sizeof(int), 1); if(verbose) { print("Reading bindata file (%s)\n", filelist.file_names[i]); print(" n_inputs = %d\n", n_inputs); print(" n_patterns = %d\n", n_patterns); } if(n_inputs != n_inputs_template) error("Incorrect number of inputs (%d <> %d) !", n_inputs, n_inputs_template); real min_ = +1000.0; real max_ = -1000.0; real sum_ = 0.0; real avg; for(int j=0; j< n_patterns; j++) { if(!one_score_per_file) scorefile->printf("%s_%03d ", file_name, j); file->read(inputs, sizeof(real), n_inputs); real d = 0.0; if(use_mean_template) { if(mlpmetric1hot) d = -mMlpOneHot(n_inputs, inputs, mean_model, mlp, mv_norm, seq, diff, delta); else d = metric->measure(inputs, mean_model); } else { for(int p = 0 ; p < n_patterns_model ; p++) { //if(strcmp(mlpmetric1hot, "")) d = -mMlpOneHot(n_inputs, inputs, ref_model[p], mlp, mv_norm, seq, diff, delta); //else d += mEuclidean(n_inputs, inputs, ref_model[p]); if(mlpmetric1hot) d += -mMlpOneHot(n_inputs, inputs, ref_model[p], mlp, mv_norm, seq, diff, delta); else d += metric->measure(inputs, ref_model[p]); } d /= (real) n_patterns_model; } if(!one_score_per_file) { real z = -d; if(strcmp(norm_model_filename, "") != 0) { z -= mu; z /= sigma; } scorefile->printf("%g\n", z); } sum_ += d; // if(d < min_) min_ = d; if(d > max_) max_ = d; } avg = sum_/(real)n_patterns; // if(verbose) { print("Outputs:\n"); print(" min = %g\n", min_); print(" max = %g\n", max_); print(" sum = %g\n", sum_); print(" avg = %g\n", avg); } if(scorefile != NULL) { if(one_score_per_file) { real z = -avg; if(strcmp(norm_model_filename, "") != 0) { z -= mu; z /= sigma; } scorefile->printf("%g ", z); } } // delete file; // if(scorefile != NULL) if(one_score_per_file) scorefile->printf("\n"); } // for(int p = 0 ; p < n_patterns_model ; p++) delete [] ref_model[p]; delete [] ref_model; delete [] inputs; delete metric; // delete allocator; return(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -