-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodel.cpp
More file actions
136 lines (104 loc) · 3.81 KB
/
Copy pathmodel.cpp
File metadata and controls
136 lines (104 loc) · 3.81 KB
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
/*****************************************************************************
*
* demo program - part of CLIPP (command line interfaces for modern C++)
*
* released under MIT license
*
* (c) 2017-2018 André Müller; foss@andremueller-online.de
*
*****************************************************************************/
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <clipp.h>
//-------------------------------------------------------------------
enum class mode {
none, train, validate, classify
};
struct settings {
mode selected = mode::none;
std::string imageFile;
std::string labelFile;
std::string modelFile = "out.mdl";
std::size_t batchSize = 128;
std::size_t threads = 0;
std::size_t inputLimit = 0;
std::vector<std::string> inputFiles;
};
//-------------------------------------------------------------------
settings configuration(int argc, char* argv[])
{
using namespace clipp;
settings s;
std::vector<std::string> unrecognized;
auto isfilename = clipp::match::prefix_not("-");
auto inputOptions = (
required("-i", "-I", "--img") & !value(isfilename, "image file", s.imageFile),
required("-l", "-L", "--lbl") & !value(isfilename, "label file", s.labelFile)
);
auto trainMode = (
command("train", "t", "T").set(s.selected,mode::train)
.if_conflicted([]{std::cerr << "conflicting modes\n"; }),
inputOptions,
(option("-n") & integer("limit", s.inputLimit))
% "limit number of input images",
(option("-o", "-O", "--out") & !value("model file", s.modelFile))
% "write model to specific file; default: 'out.mdl'",
(option("-b", "--batch-size") & integer("batch size", s.batchSize)),
(option("-p") & integer("threads", s.threads))
% "number of threads to use; default: optimum for machine"
);
auto validationMode = (
command("validate", "v", "V").set(s.selected,mode::validate),
!value(isfilename, "model", s.modelFile),
inputOptions
);
auto classificationMode = (
command("classify", "c", "C").set(s.selected,mode::classify),
!value(isfilename, "model", s.modelFile),
!values(isfilename, "images", s.inputFiles)
);
auto cli = (
trainMode | validationMode | classificationMode,
any_other(unrecognized)
);
auto res = parse(argc, argv, cli);
debug::print(std::cout, res);
if(!res || !unrecognized.empty()) {
std::string msg = "Wrong command line arguments!\n";
if(s.selected == mode::none) {
msg += "Please select a mode!\n";
}
else {
for(const auto& m : res.missing()) {
if(!m.param()->flags().empty()) {
msg += "Missing option: " + m.param()->flags().front() + '\n';
}
else if(!m.param()->label().empty()) {
msg += "Missing value: " + m.param()->label() + '\n';
}
}
for(const auto& arg : unrecognized) {
msg += "Argument not recognized: " + arg + '\n';
}
}
auto fmt = doc_formatting{}.first_column(8).doc_column(16);
//.max_flags_per_param_in_usage(3).surround_alternative_flags("(", ")");
msg += "\nUsage:\n" + usage_lines(cli, argv[0], fmt).str() + '\n';
msg += "\nOptions:\n" + documentation(cli, fmt).str() + '\n';
throw std::invalid_argument{msg};
}
return s;
}
//-------------------------------------------------------------------
int main(int argc, char* argv[])
{
try {
auto conf = configuration(argc, argv);
std::cout << "SUCCESS\n";
}
catch(std::exception& e) {
std::cerr << "ERROR: " << e.what() << '\n';
}
}