/
DeLFTModel.java
389 lines (342 loc) · 15.6 KB
/
DeLFTModel.java
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package org.grobid.core.jni;
import org.grobid.core.GrobidModel;
import org.grobid.core.engines.label.TaggingLabels;
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.utilities.GrobidProperties;
import org.grobid.core.utilities.IOUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
import java.io.*;
import java.lang.StringBuilder;
import java.util.*;
import java.util.regex.*;
import jep.Jep;
import jep.JepException;
import java.util.function.Consumer;
/**
*
* @author: Patrice
*/
public class DeLFTModel {
public static final Logger LOGGER = LoggerFactory.getLogger(DeLFTModel.class);
// Exploit JNI CPython interpreter to execute load and execute a DeLFT deep learning model
private String modelName;
public DeLFTModel(GrobidModel model, String architecture) {
this.modelName = model.getModelName().replace("-", "_");
try {
LOGGER.info("Loading DeLFT model for " + model.getModelName() + " with architecture " + architecture + "...");
JEPThreadPool.getInstance().run(new InitModel(this.modelName, GrobidProperties.getInstance().getModelPath(), architecture));
} catch(InterruptedException e) {
LOGGER.error("DeLFT model " + this.modelName + " initialization failed", e);
}
}
class InitModel implements Runnable {
private String modelName;
private File modelPath;
private String architecture;
public InitModel(String modelName, File modelPath, String architecture) {
this.modelName = modelName;
this.modelPath = modelPath;
this.architecture = architecture;
}
@Override
public void run() {
Jep jep = JEPThreadPool.getInstance().getJEPInstance();
try {
String fullModelName = this.modelName.replace("_", "-");
if (architecture != null && !architecture.equals("BidLSTM_CRF"))
fullModelName += "-" + this.architecture;
if (GrobidProperties.getInstance().useELMo() && modelName.toLowerCase().indexOf("bert") == -1)
fullModelName += "-with_ELMo";
jep.eval(this.modelName+" = Sequence('" + fullModelName + "')");
jep.eval(this.modelName+".load(dir_path='"+modelPath.getAbsolutePath()+"')");
} catch(JepException e) {
throw new GrobidException("DeLFT model initialization failed. ", e);
}
}
}
private class LabelTask implements Callable<String> {
private String data;
private String modelName;
public LabelTask(String modelName, String data) {
//System.out.println("label thread: " + Thread.currentThread().getId());
this.modelName = modelName;
this.data = data;
}
private void setJepStringValueWithFileFallback(
Jep jep, String name, String value
) throws JepException, IOException {
try {
jep.set(name, value);
} catch(JepException e) {
File tempFile = IOUtilities.newTempFile(name, ".data");
LOGGER.debug(
"Falling back to file {} due to exception: {}",
tempFile, e.toString()
);
IOUtilities.writeInFile(tempFile.getAbsolutePath(), value);
jep.eval("from pathlib import Path");
jep.eval(
name + " = Path('" + tempFile.getAbsolutePath() +
"').read_text(encoding='utf-8')"
);
tempFile.delete();
}
}
@Override
public String call() {
Jep jep = JEPThreadPool.getInstance().getJEPInstance();
StringBuilder labelledData = new StringBuilder();
try {
//System.out.println(this.data);
// load and tag
this.setJepStringValueWithFileFallback(jep, "input", this.data);
jep.eval("x_all, f_all = load_data_crf_string(input)");
Object objectResults = jep.getValue(this.modelName+".tag(x_all, None)");
// inject back the labels
List<List<List<String>>> results = (List<List<List<String>>>) objectResults;
BufferedReader bufReader = new BufferedReader(new StringReader(data));
String inputLine;
int i = 0; // sentence index
int j = 0; // word index in the sentence
List<List<String>> result = results.get(0);
while ((inputLine = bufReader.readLine()) != null) {
inputLine = inputLine.trim();
if ((inputLine.length() == 0) && (j != 0)) {
j = 0;
i++;
if (i == results.size())
break;
result = results.get(i);
continue;
}
if (inputLine.length() == 0)
continue;
labelledData.append(inputLine);
labelledData.append(" ");
if (j >= result.size()) {
labelledData.append(TaggingLabels.OTHER_LABEL);
} else {
List<String> pair = result.get(j);
// first is the token, second is the label (DeLFT format)
String token = pair.get(0);
String label = pair.get(1);
labelledData.append(DeLFTModel.delft2grobidLabel(label));
}
labelledData.append("\n");
j++;
}
// cleaning
jep.eval("del input");
jep.eval("del x_all");
jep.eval("del f_all");
//jep.eval("K.clear_session()");
} catch(JepException e) {
LOGGER.error("DeLFT model labelling via JEP failed", e);
} catch(IOException e) {
LOGGER.error("DeLFT model labelling failed", e);
}
//System.out.println(labelledData.toString());
return labelledData.toString();
}
}
public String label(String data) {
String result = null;
try {
result = JEPThreadPool.getInstance().call(new LabelTask(this.modelName, data));
} catch(InterruptedException e) {
LOGGER.error("DeLFT model " + this.modelName + " labelling interrupted", e);
} catch(ExecutionException e) {
LOGGER.error("DeLFT model " + this.modelName + " labelling failed", e);
}
return result;
}
/**
* Training via JNI CPython interpreter (JEP). It appears that after some epochs, the JEP thread
* usually hangs... Possibly issues with IO threads at the level of JEP (output not consumed because
* of \r and no end of line?).
*/
public static void trainJNI(String modelName, File trainingData, File outputModel, String architecture) {
try {
LOGGER.info("Train DeLFT model " + modelName + "...");
JEPThreadPool.getInstance().run(
new TrainTask(modelName, trainingData, GrobidProperties.getInstance().getModelPath(), architecture));
} catch(InterruptedException e) {
LOGGER.error("Train DeLFT model " + modelName + " task failed", e);
}
}
private static class TrainTask implements Runnable {
private String modelName;
private File trainPath;
private File modelPath;
private String architecture;
public TrainTask(String modelName, File trainPath, File modelPath, String architecture) {
//System.out.println("train thread: " + Thread.currentThread().getId());
this.modelName = modelName;
this.trainPath = trainPath;
this.modelPath = modelPath;
this.architecture = architecture;
}
@Override
public void run() {
Jep jep = JEPThreadPool.getInstance().getJEPInstance();
try {
// load data
jep.eval("x_all, y_all, f_all = load_data_and_labels_crf_file('" + this.trainPath.getAbsolutePath() + "')");
jep.eval("x_train, x_valid, y_train, y_valid = train_test_split(x_all, y_all, test_size=0.1)");
jep.eval("print(len(x_train), 'train sequences')");
jep.eval("print(len(x_valid), 'validation sequences')");
String useELMo = "False";
if (GrobidProperties.getInstance().useELMo() && modelName.toLowerCase().indexOf("bert") == -1) {
useELMo = "True";
}
// init model to be trained
if (architecture == null)
jep.eval("model = Sequence('"+this.modelName+
"', max_epoch=100, recurrent_dropout=0.50, embeddings_name='glove-840B', use_ELMo="+useELMo+")");
else
jep.eval("model = Sequence('"+this.modelName+
"', max_epoch=100, recurrent_dropout=0.50, embeddings_name='glove-840B', use_ELMo="+useELMo+
", model_type='"+architecture+"')");
// actual training
//start_time = time.time()
jep.eval("model.train(x_train, y_train, x_valid, y_valid)");
//runtime = round(time.time() - start_time, 3)
//print("training runtime: %s seconds " % (runtime))
// saving the model
System.out.println(this.modelPath.getAbsolutePath());
jep.eval("model.save('"+this.modelPath.getAbsolutePath()+"')");
// cleaning
jep.eval("del x_all");
jep.eval("del y_all");
jep.eval("del f_all");
jep.eval("del x_train");
jep.eval("del x_valid");
jep.eval("del y_train");
jep.eval("del y_valid");
jep.eval("del model");
} catch(JepException e) {
LOGGER.error("DeLFT model training via JEP failed", e);
}
}
}
/**
* Train with an external process rather than with JNI, this approach appears to be more stable for the
* training process (JNI approach hangs after a while) and does not raise any runtime/integration issues.
*/
public static void train(String modelName, File trainingData, File outputModel, String architecture) {
try {
LOGGER.info("Train DeLFT model " + modelName + "...");
List<String> command = Arrays.asList("python3",
"grobidTagger.py",
modelName,
"train",
"--input", trainingData.getAbsolutePath(),
"--output", GrobidProperties.getInstance().getModelPath().getAbsolutePath());
if (architecture != null) {
command.add("--architecture");
command.add(architecture);
}
if (GrobidProperties.getInstance().useELMo() && modelName.toLowerCase().indexOf("bert") == -1) {
command.add("--use-ELMo");
}
ProcessBuilder pb = new ProcessBuilder(command);
File delftPath = new File(GrobidProperties.getInstance().getDeLFTFilePath());
pb.directory(delftPath);
Process process = pb.start();
//pb.inheritIO();
CustomStreamGobbler customStreamGobbler =
new CustomStreamGobbler(process.getInputStream(), System.out);
Executors.newSingleThreadExecutor().submit(customStreamGobbler);
SimpleStreamGobbler streamGobbler = new SimpleStreamGobbler(process.getErrorStream(), System.err::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
//assert exitCode == 0;
} catch(IOException e) {
LOGGER.error("IO error when training DeLFT model " + modelName, e);
} catch(InterruptedException e) {
LOGGER.error("Train DeLFT model " + modelName + " task failed", e);
}
}
public synchronized void close() {
try {
LOGGER.info("Close DeLFT model " + this.modelName + "...");
JEPThreadPool.getInstance().run(new CloseModel(this.modelName));
} catch(InterruptedException e) {
LOGGER.error("Close DeLFT model " + this.modelName + " task failed", e);
}
}
private class CloseModel implements Runnable {
private String modelName;
public CloseModel(String modelName) {
this.modelName = modelName;
}
@Override
public void run() {
Jep jep = JEPThreadPool.getInstance().getJEPInstance();
try {
jep.eval("del "+this.modelName);
} catch(JepException e) {
LOGGER.error("Closing DeLFT model failed", e);
}
}
}
private static String delft2grobidLabel(String label) {
if (label.equals(TaggingLabels.IOB_OTHER_LABEL)) {
label = TaggingLabels.OTHER_LABEL;
} else if (label.startsWith(TaggingLabels.IOB_START_ENTITY_LABEL_PREFIX)) {
label = label.replace(TaggingLabels.IOB_START_ENTITY_LABEL_PREFIX, TaggingLabels.GROBID_START_ENTITY_LABEL_PREFIX);
} else if (label.startsWith(TaggingLabels.IOB_INSIDE_LABEL_PREFIX)) {
label = label.replace(TaggingLabels.IOB_INSIDE_LABEL_PREFIX, TaggingLabels.GROBID_INSIDE_ENTITY_LABEL_PREFIX);
}
return label;
}
private static class SimpleStreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public SimpleStreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines()
.forEach(consumer);
}
}
/**
* This is a custom gobbler that reproduces correctly the Keras training progress bar
* by injecting a \r for progress line updates.
*/
private static class CustomStreamGobbler implements Runnable {
public static final Logger LOGGER = LoggerFactory.getLogger(CustomStreamGobbler.class);
private final InputStream is;
private final PrintStream os;
private Pattern pattern = Pattern.compile("\\d/\\d+ \\[");
public CustomStreamGobbler(InputStream is, PrintStream os) {
this.is = is;
this.os = os;
}
@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(this.is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
os.print("\r" + line);
os.flush();
} else {
os.println(line);
}
}
}
catch (IOException e) {
LOGGER.warn("IO error between embedded python and java process", e);
}
}
}
}