51 #define PROG_NAME "piped-model-evaluator"
53 #define DEFAULT_MAX_EXAMPLES -1
54 #define DEFAULT_MAX_CANDIDATES -1
55 #define DEFAULT_REPORTING_INTERVAL 1000
56 #define DEFAULT_USE_WEIGHTED_LOSS true
61 #define XSTR(arg) STR(arg)
66 using namespace reranker;
70 PROG_NAME " -d|--devtest <devtest input file>+\n",
71 "\t[--dev-config <devtest feature extractor config file>]\n",
72 "\t[--model-files <file with model filenames>\n",
73 "\t[-u] [--no-base64]\n",
74 "\t[--max-examples <max num examples>]\n",
75 "\t[--max-candidates <max num candidates>]\n",
76 "\t[-r <reporting interval>] [ --use-weighted-loss[=][true|false] ]\n",
78 "\t<devtest input file> is the name of a stream of serialized\n",
79 "\t\tCandidateSet instances, or \"-\" for input from standard input\n",
80 "\t\t(required unless training in mapper mode)\n",
81 "\t--model-files specifies the name of a file from which to read model\n",
82 "\t\tmodel filenames (use this option for debugging; defaults to stdin)\n",
83 "\t-u specifies that the input files are uncompressed\n",
84 "\t--no-base64 specifies not to use base64 encoding/decoding\n",
85 "\t--max-examples specifies the maximum number of examples to read from\n",
87 "\t--max-candidates specifies the maximum number of candidates to read\n",
89 "\t-r specifies the interval at which the CandidateSetReader reports how\n",
90 "\t\tmany candidate sets it has read (defaults to "
92 "\t--use-weighted-loss specifies whether to weight losses on devtest\n",
93 "\t\texamples by the number of tokens in the reference, where, e.g.,\n",
94 "\t\tweighted loss is appropriate for computing WER, but not BLEU\n",
101 int usage_msg_len =
sizeof(
usage_msg)/
sizeof(
const char *);
102 for (
int i = 0; i < usage_msg_len; ++i) {
110 cerr <<
PROG_NAME <<
": error: " << err_msg << endl;
121 bool using_model_filenames_file =
false;
122 string model_filenames_file;
123 vector<string> devtest_files;
124 string devtest_feature_extractor_config_file;
125 bool compressed =
true;
126 bool use_base64 =
true;
128 string use_weighted_loss_arg_prefix =
"--use-weighted-loss";
129 size_t use_weighted_loss_arg_prefix_len =
130 use_weighted_loss_arg_prefix.length();
136 for (
int i = 1; i < argc; ++i) {
137 string arg = argv[i];
138 if (arg ==
"-d" || arg ==
"-devtest" || arg ==
"--devtest") {
139 string err_msg = string(
"no input files specified with ") + arg;
145 for ( ; i < argc; ++i) {
146 if (argv[i][0] ==
'-') {
150 devtest_files.push_back(argv[i]);
152 }
else if (arg ==
"-dev-config" || arg ==
"--dev-config") {
154 string(
"no feature extractor config file specified with ") + arg;
158 devtest_feature_extractor_config_file = argv[++i];
159 }
else if (arg ==
"-model-files" || arg ==
"--model-files") {
160 string err_msg = string(
"no model filenames file specified with ") + arg;
164 model_filenames_file = argv[++i];
165 using_model_filenames_file =
true;
166 }
else if (arg ==
"-u") {
168 }
else if (arg ==
"--no-base64") {
170 }
else if (arg ==
"-max-examples" || arg ==
"--max-examples") {
171 string err_msg = string(
"no arg specified with ") + arg;
175 max_examples = atoi(argv[++i]);
176 }
else if (arg ==
"-max-candidates" || arg ==
"--max-candidates") {
177 string err_msg = string(
"no arg specified with ") + arg;
181 max_candidates = atoi(argv[++i]);
182 }
else if (arg ==
"-r") {
183 string err_msg = string(
"no arg specified with ") + arg;
187 reporting_interval = atoi(argv[++i]);
188 }
else if (arg.substr(0, use_weighted_loss_arg_prefix_len) ==
189 use_weighted_loss_arg_prefix) {
190 string use_weighted_loss_str;
191 if (arg.length() > use_weighted_loss_arg_prefix_len &&
192 arg[use_weighted_loss_arg_prefix_len] ==
'=') {
193 use_weighted_loss_str =
194 arg.substr(use_weighted_loss_arg_prefix_len + 1);
197 string(
"no \"true\" or \"false\" arg specified with ") + arg;
201 use_weighted_loss_str = argv[++i];
203 if (use_weighted_loss_str !=
"true" &&
204 use_weighted_loss_str !=
"false") {
205 cerr <<
PROG_NAME <<
": error: must specify \"true\" or \"false\""
206 <<
" with --use-weighted-loss" << endl;
210 if (use_weighted_loss_str !=
"true") {
211 use_weighted_loss =
false;
213 }
else if (arg.size() > 0 && arg[0] ==
'-') {
214 cerr <<
PROG_NAME <<
": error: unrecognized option: " << arg << endl;
220 if (devtest_files.size() == 0) {
221 cerr <<
PROG_NAME <<
": error: must specify devtest input files when "
222 <<
"not in mapper mode" << endl;
227 shared_ptr<ExecutiveFeatureExtractor> devtest_efe;
228 if (devtest_feature_extractor_config_file !=
"") {
229 devtest_efe = ExecutiveFeatureExtractor::InitFromSpec(
230 devtest_feature_extractor_config_file);
235 bool reset_counters =
true;
237 cerr <<
"Reading devtest examples." << endl;
239 vector<shared_ptr<CandidateSet> > devtest_examples;
240 for (vector<string>::const_iterator file_it = devtest_files.begin();
241 file_it != devtest_files.end();
243 csr.
Read(*file_it, compressed, use_base64, reset_counters,
247 for (vector<shared_ptr<CandidateSet> >::iterator it =
248 devtest_examples.begin();
249 it != devtest_examples.end();
251 devtest_efe->Extract(*(*it));
254 cerr <<
"Done reading devtest examples." << endl;
256 if (devtest_examples.size() == 0) {
257 cerr <<
"Could not read any devtest examples. Exiting." << endl;
262 CandidateSetVectorIt;
264 istream *model_filenames_stream =
265 using_model_filenames_file ?
266 new ifstream(model_filenames_file.c_str()) : &cin;
269 while (getline(*model_filenames_stream, model_file)) {
270 cerr <<
"Evaluating model \"" << model_file <<
"\"." << endl;
271 shared_ptr<Model> model =
272 model_reader.Read(model_file, compressed, use_base64);
273 model->set_use_weighted_loss(use_weighted_loss);
274 CandidateSetVectorIt devtest_examples_it(devtest_examples);
276 cout << model->Evaluate(devtest_examples_it) << endl;
280 devtest_examples_it.Reset();
281 while (devtest_examples_it.HasNext()) {
282 CandidateSet &candidate_set = devtest_examples_it.Next();
283 bool dont_force =
false;
287 if (using_model_filenames_file) {
288 delete model_filenames_stream;
#define DEFAULT_REPORTING_INTERVAL
Provides an interface and some implementations for iterating over CandidateSet instances.
Provides the ModelReader class, which can create Model instances from a file.
#define DEFAULT_MAX_EXAMPLES
void set_verbosity(int verbosity)
Sets the verbosity of this reader (mostly for debugging purposes).
void DecompileFeatures(Symbols *symbols, bool clear_symbolic_features=false, bool clear_features=true, bool force=false)
Decompiles any non-symbolic features in the candidates in this candidate set.
Class for reading streams of training or test instances, where each training or test instance is a re...
A class to hold a set of candidates, either for training or test.
void Read(const string &filename, bool compressed, bool use_base64, bool reset_counters, vector< shared_ptr< CandidateSet > > &examples)
Reads a stream of CandidateSet instances from the specified file or from standard input...
int main(int argc, char **argv)
An implementation of the CandidateSetIterator interface that is backed by an arbitrary C++ collection...
#define DEFAULT_MAX_CANDIDATES
#define XSTR(arg)
Expands the string value of the specified argument using the STR macro.
#define DEFAULT_USE_WEIGHTED_LOSS
A class for reading streams of training or test instances, where each training or test instance is a ...
bool check_for_required_arg(int argc, int i, string err_msg)
Knows how to create Model instances that have been serialized to a file.
Reranker model interface.