📄 testmlp.cc
字号:
const char *help = "\progname: testMLP.cc\n\code2html: This program tests a MLP.\n\version: Torch3 vision2.0, 2003-2005\n\(c) Sebastien Marcel (marcel@idiap.ch)\n";/** Torch*/#include "DiskXFile.h"// command-lines#include "FileListCmdOption.h"#include "CmdLine.h"/** Torch3vision*/// datasets#include "FileBinDataSet.h"#include "DiskBinDataSet.h"// custommachines#include "MyMLP.h"#include "MyMeanVarNorm.h"// image processing#include "ipHistoEqual.h"#include "ipSmoothGaussian3.h"using namespace Torch;int main(int argc, char **argv){ // int n_inputs; char *model_file; // char *output_basename; int n_histo_bins; int width_pattern; int height_pattern; bool image_normalize; // bool verbose; bool use_disk; // real threshold; Allocator *allocator = new Allocator; DiskXFile::setLittleEndianMode(); //=================== The command-line ========================== FileListCmdOption filelist_class("file name", "the list files or one data file of positive patterns"); filelist_class.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("model", &model_file, "the model file"); cmd.addCmdOption(&filelist_class); cmd.addICmdArg("n_inputs", &n_inputs, "input dimension of the data"); cmd.addText("\nOptions:"); cmd.addSCmdOption("-o", &output_basename, "test", "output basename"); cmd.addICmdOption("-nbins", &n_histo_bins, 100, "number of patterns to output"); cmd.addBCmdOption("-verbose", &verbose, false, "verbose"); cmd.addBCmdOption("-usedisk", &use_disk, false, "read data from disk"); cmd.addRCmdOption("-threshold", &threshold, 0.0, "threshold"); cmd.addText("\nImage Options:"); cmd.addICmdOption("-width", &width_pattern, 19, "the width of the pattern"); cmd.addICmdOption("-height", &height_pattern, 19, "the height of the pattern"); cmd.addBCmdOption("-imagenorm", &image_normalize, false, "considers the input pattern as an image and performs a photometric normalization"); // Read the command line cmd.read(argc, argv); // ipHistoEqual *enhancing = NULL; ipCore *smoothing = NULL; if(image_normalize) { print("Perform photometric normalization ...\n"); if(width_pattern * height_pattern != n_inputs) error("incorrect image size."); print("The input pattern is an %dx%d image.\n", width_pattern, height_pattern); enhancing = new(allocator) ipHistoEqual(width_pattern, height_pattern, "float"); smoothing = new(allocator) ipSmoothGaussian3(width_pattern, height_pattern, "gray", 0.25); } // print("n_filenames = %d\n", filelist_class.n_files); for(int i = 0 ; i < filelist_class.n_files ; i++) print(" filename[%d] = %s\n", i, filelist_class.file_names[i]); // MyMLP mlp; // Create the training dataset (normalize inputs) MyMeanVarNorm *mv_norm = NULL; mv_norm = new(allocator) MyMeanVarNorm(n_inputs, 1); mlp.load(model_file, mv_norm); mlp.info(); DataSet *bindata; if(use_disk) { DiskBinDataSet *bindata_ = NULL; bindata_ = new(allocator) DiskBinDataSet(filelist_class.file_names, filelist_class.n_files, n_inputs, -1); bindata = bindata_; bindata_->info(false); } else { FileBinDataSet *bindata_ = NULL; bindata_ = new(allocator) FileBinDataSet(filelist_class.file_names, filelist_class.n_files, n_inputs); bindata = bindata_; bindata_->info(false); } real min_ = 1000.0; real max_ = -1000.0; // real *outputs = (real *) allocator->alloc(bindata->n_examples * sizeof(real)); // char output_filename[250]; sprintf(output_filename, "%s.output", output_basename); DiskXFile *pf_output = new(allocator) DiskXFile(output_filename, "w"); int n_over = 0; for(int i=0; i< bindata->n_examples; i++) { if(verbose) print("[%d]:\n", i); bindata->setExample(i); if(verbose) print(" Input = [%2.3f %2.3f %2.3f ...]\n", bindata->inputs->frames[0][0], bindata->inputs->frames[0][1], bindata->inputs->frames[0][2]); if(image_normalize) { enhancing->process(bindata->inputs); smoothing->process(enhancing->seq_out); mv_norm->preProcessInputs(smoothing->seq_out); mlp.forward(smoothing->seq_out); } else { mv_norm->preProcessInputs(bindata->inputs); mlp.forward(bindata->inputs); } outputs[i] = mlp.outputs->frames[0][0]; if(outputs[i] >= threshold) n_over++; if(verbose) print(" -> %g\n", outputs[i]); pf_output->printf("%g\n", outputs[i]); if(i == 0) { min_ = outputs[i]; max_ = outputs[i]; } else { if(outputs[i] < min_) min_ = outputs[i]; if(outputs[i] > max_) max_ = outputs[i]; } } print("min = %g\n", min_); print("max = %g\n", max_); real rate = (real) n_over / (real) bindata->n_examples; print("# patterns >= %g = %g % (%d / %d)\n", threshold, rate*100.0, n_over, bindata->n_examples); // int *histo = (int *) allocator->alloc(n_histo_bins * sizeof(int)); for(int i = 0 ; i < n_histo_bins ; i++) histo[i] = 0; // real n_1 = n_histo_bins - 1; for(int i = 0 ; i < bindata->n_examples ; i++) { int index = FixI(n_1 * (outputs[i] - min_) / (max_ - min_)); histo[index]++; } // real histo_max_ = 0.0; for(int i = 0 ; i < n_histo_bins ; i++) if(histo[i] > histo_max_) histo_max_ = histo[i]; // char histo_filename[250]; sprintf(histo_filename, "%s.histo", output_basename); DiskXFile *pf_histo = new(allocator) DiskXFile(histo_filename, "w"); for(int i = 0 ; i < n_histo_bins ; i++) if(histo[i] != 0) { real output_ = (real) i * (max_ - min_) / n_1 + min_; real histo_ = (real) histo[i] / histo_max_; pf_histo->printf("%g %g\n", output_, histo_); } // delete allocator; return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -