-
Notifications
You must be signed in to change notification settings - Fork 10
/
shiro-rest.c
269 lines (247 loc) · 7.24 KB
/
shiro-rest.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
SHIRO
===
Copyright (c) 2017-2018 Kanru Hua. All rights reserved.
This file is part of SHIRO.
SHIRO 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 3 of the License, or
(at your option) any later version.
SHIRO 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 SHIRO. If not, see <http://www.gnu.org/licenses/>.
*/
#include <getopt.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "external/cJSON/cJSON.h"
#include "external/liblrhsmm/common.h"
#include "external/liblrhsmm/estimate.h"
#include "external/liblrhsmm/serial.h"
#include <omp.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#include "cli-common.h"
static void print_usage() {
fprintf(stderr,
"shiro-rest\n"
" -m model-file\n"
" -s segmentation-file\n"
" -n num-iteration\n"
" -g (treat model as HMM)\n"
" -p state-level-pruning (HSMM)\n"
" -P state-level-pruning (HMM)\n"
" -d extra-duration-search-space\n"
" -t termination-threshold\n"
" -l export-likelihood-file\n"
" -i (isolated training)\n"
" -D (DAEM training)\n"
" -T (enable multi-threading)\n"
" -M (display-mean-frame-likelihood)\n"
" -h (print usage)\n");
exit(1);
}
int opt_niter = 1;
int opt_geodur = 0;
int opt_daem = 0;
int opt_mthread = 0;
int opt_meanlikelihood = 0;
int opt_embdtrain = 1;
FILE* fp_likelihood = NULL;
FP_TYPE reestimate(lrh_model_stat* hstat, lrh_model* hsmm, lrh_observ* o,
cJSON* j_states) {
FP_TYPE lh = 0;
if(! opt_embdtrain) {
lrh_dataset* d = load_isolated_data_from_json(j_states, o);
int nsample = d -> observset -> nsample;
for(int e = 0; e < nsample; e ++) {
lrh_seg* es = d -> segset -> samples[e];
lrh_observ* eo = d -> observset -> samples[e];
for(int i = 0; i < es -> nseg; i ++)
if(es -> time[i] > eo -> nt)
es -> time[i] = eo -> nt;
lrh_seg_buildjumps(es);
FP_TYPE e_lh = 0;
if(opt_geodur)
e_lh = lrh_estimate_geometric(hstat, hsmm, eo, es);
else
e_lh = lrh_estimate(hstat, hsmm, eo, es);
if(opt_meanlikelihood)
e_lh /= eo -> nt;
if(fp_likelihood != NULL)
fprintf(fp_likelihood, "%f%s", e_lh, e == nsample - 1 ? "\n" : ",");
lh += e_lh / nsample;
}
delete_dataset(d);
} else {
lrh_seg* s = load_seg_from_json(j_states, hsmm -> nstream);
for(int i = 0; i < s -> nseg; i ++)
if(s -> time[i] > o -> nt)
s -> time[i] = o -> nt;
lrh_seg_buildjumps(s);
if(opt_geodur)
lh = lrh_estimate_geometric(hstat, hsmm, o, s);
else
lh = lrh_estimate(hstat, hsmm, o, s);
if(opt_meanlikelihood)
lh /= o -> nt;
if(fp_likelihood != NULL)
fprintf(fp_likelihood, "%f\n", lh);
lrh_delete_seg(s);
}
return lh;
}
extern char* optarg;
int main(int argc, char** argv) {
# ifdef _WIN32
_setmode(_fileno(stdout), _O_BINARY);
# endif
int c;
cJSON* j_segm = NULL;
lrh_model* hsmm = NULL;
FP_TYPE opt_threshold = 1.0;
while((c = getopt(argc, argv, "m:s:n:gp:P:d:t:l:iDTMh")) != -1) {
char* jsonstr = NULL;
switch(c) {
case 'm':
hsmm = load_model(optarg);
if(hsmm == NULL) {
fprintf(stderr, "Error: failed to load model from %s\n", optarg);
return 1;
}
break;
case 's':
jsonstr = readall(optarg);
if(jsonstr == NULL) {
fprintf(stderr, "Error: cannot open %s.\n", optarg);
return 1;
}
j_segm = cJSON_Parse(jsonstr);
if(j_segm == NULL) {
fprintf(stderr, "Error: failed to parse %s.\n", optarg);
return 1;
}
free(jsonstr);
break;
case 'n':
opt_niter = atoi(optarg);
break;
case 'g':
opt_geodur = 1;
break;
case 'p':
lrh_inference_stprune = atoi(optarg);
break;
case 'P':
lrh_inference_stprune_full_slope = atof(optarg);
break;
case 'd':
lrh_inference_duration_extra = atoi(optarg);
break;
case 't':
opt_threshold = atof(optarg);
break;
case 'i':
opt_embdtrain = 0;
break;
case 'l':
fp_likelihood = fopen(optarg, "w");
if(fp_likelihood == NULL) {
fprintf(stderr, "Error: cannot create %s\n", optarg);
exit(1);
}
break;
case 'D':
opt_daem = 1;
break;
case 'T':
opt_mthread = 1;
break;
case 'M':
opt_meanlikelihood = 1;
break;
case 'h':
print_usage();
break;
default:
abort();
}
}
if(j_segm == NULL) {
fprintf(stderr, "Error: segmentation file is not specified.\n");
return 1;
}
if(hsmm == NULL) {
fprintf(stderr, "Error: model file is not specified.\n");
return 1;
}
if(fp_likelihood != NULL && opt_mthread == 1) {
fprintf(stderr, "Warning: multi-threading disabled due to -l option.\n");
opt_mthread = 0;
}
# ifdef _OPENMP
if(opt_mthread == 0)
omp_set_num_threads(1);
# endif
# ifndef _OPENMP
if(opt_mthread == 1)
fprintf(stderr, "Warning: OpenMP is not supported by this build.\n");
# endif
cJSON* j_file_list = cJSON_GetObjectItem(j_segm, "file_list");
checkvar(file_list);
int nfile = cJSON_GetArraySize(j_file_list);
FP_TYPE prev_lh = 0;
for(int iter = 0; iter < opt_niter; iter ++) {
if(opt_daem) {
lrh_daem_temperature = sqrt((FP_TYPE)(iter + 1) / opt_niter);
fprintf(stderr, "Running iteration %d/%d, temperature = %.2f...\n",
iter, opt_niter, lrh_daem_temperature);
} else {
fprintf(stderr, "Running iteration %d/%d...\n", iter, opt_niter);
}
FP_TYPE total_lh = 0;
lrh_model_stat* hstat = lrh_model_stat_from_model(hsmm);
lrh_model_precompute(hsmm);
# pragma omp parallel for
for(int f = 0; f < nfile; f ++) {
cJSON* j_file_list_f = cJSON_GetArrayItem(j_file_list, f);
cJSON* j_filename = cJSON_GetObjectItem(j_file_list_f, "filename");
checkvar(filename);
cJSON* j_states = cJSON_GetObjectItem(j_file_list_f, "states");
checkvar(states);
lrh_observ* o = load_observ_from_float(j_filename -> valuestring, hsmm);
FP_TYPE e = reestimate(hstat, hsmm, o, j_states);
if(e < -1e8) {
fprintf(stderr, "Inference failed on file %d (%s).\n", f,
j_filename -> valuestring);
}
total_lh += e;
lrh_delete_observ(o);
}
if(opt_geodur)
lrh_model_update(hsmm, hstat, 1);
else
lrh_model_update(hsmm, hstat, 0);
lrh_delete_model_stat(hstat);
FP_TYPE mean_lh = total_lh / nfile / lrh_daem_temperature;
fprintf(stderr, "Average log likelihood = %f.\n", mean_lh);
if(iter > 0 && opt_threshold > 0 && mean_lh < prev_lh + opt_threshold) {
fprintf(stderr, "Training converged.\n");
break;
}
prev_lh = mean_lh;
}
cmp_ctx_t cmpobj;
cmp_init(& cmpobj, stdout, file_reader, file_writer);
lrh_write_model(& cmpobj, hsmm);
cJSON_Delete(j_segm);
lrh_delete_model(hsmm);
if(fp_likelihood != NULL) fclose(fp_likelihood);
return 0;
}