forked from qupath/qupath-extension-djl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDjlTools.java
More file actions
725 lines (651 loc) · 24.5 KB
/
DjlTools.java
File metadata and controls
725 lines (651 loc) · 24.5 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*-
* Copyright 2022-2025 QuPath developers, University of Edinburgh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package qupath.ext.djl;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ai.djl.Device;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.PointerScope;
import org.bytedeco.javacpp.indexer.BooleanIndexer;
import org.bytedeco.javacpp.indexer.ByteIndexer;
import org.bytedeco.javacpp.indexer.DoubleIndexer;
import org.bytedeco.javacpp.indexer.FloatIndexer;
import org.bytedeco.javacpp.indexer.HalfIndexer;
import org.bytedeco.javacpp.indexer.IntIndexer;
import org.bytedeco.javacpp.indexer.LongIndexer;
import org.bytedeco.javacpp.indexer.UByteIndexer;
import org.bytedeco.javacpp.indexer.UShortIndexer;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_dnn;
import org.bytedeco.opencv.opencv_core.Mat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ai.djl.MalformedModelException;
import ai.djl.Model;
import ai.djl.engine.Engine;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.LayoutType;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorContext;
import ai.djl.util.Pair;
import ai.djl.util.PairList;
import qupath.opencv.dnn.DnnModel;
import qupath.opencv.dnn.DnnShape;
import qupath.opencv.tools.OpenCVTools;
/**
* Tools to help work with Deep Java Library within QuPath.
*
* @author Pete Bankhead
*/
public class DjlTools {
private static final Logger logger = LoggerFactory.getLogger(DjlTools.class);
/**
* PyTorch engine name
*/
public static String ENGINE_PYTORCH = "PyTorch";
/**
* TensorFlow engine name
*/
public static String ENGINE_TENSORFLOW = "TensorFlow";
/**
* MXNet engine name
*/
public static String ENGINE_MXNET = "MXNet";
/**
* TensorFlow Lite engine name
*/
public static String ENGINE_TFLITE = "TFLite";
/**
* ONNX Runtime engine name
*/
public static String ENGINE_ONNX_RUNTIME = "OnnxRuntime";
/**
* XGBoost engine name
*/
public static String ENGINE_XGBOOST = "XGBoost";
/**
* LightGBM engine name
*/
public static String ENGINE_LIGHTGBM = "LightGBM";
/**
* Neo DLR engine name
*/
public static String ENGINE_DLR = "DLR";
/**
* TensorRT engine name
*/
public static String ENGINE_TENSORRT = "TensorRT";
/**
* PaddlePaddle engine name
*/
public static String ENGINE_PADDLEPADDLE = "PaddlePaddle";
/**
* Maintain a set of all engines that are known to have been loaded.
* This enables us to check for available (and downloaded) engines without
* having to try to instantiate a new one.
*/
public static Set<String> loadedEngines = new HashSet<>();
/**
* Default devices for each engine.
* This can be used to override the default used by DJL.
*/
private static Map<String, Device> defaultDevices = new HashMap<>();
static Set<String> ALL_ENGINES = Set.of(
ENGINE_DLR, ENGINE_LIGHTGBM, ENGINE_MXNET, ENGINE_ONNX_RUNTIME, ENGINE_PADDLEPADDLE,
ENGINE_PYTORCH, ENGINE_TENSORFLOW, ENGINE_TENSORRT, ENGINE_TFLITE, ENGINE_XGBOOST
);
/**
* Create a {@link DnnModel} wrapping a model from Deep Java Library.
* @param uri URI of the model file
* @param ndLayout a layout string, e.g. NCHW
* @param inputShape expected input shape, according to ndLayout
* @return
*/
public static DnnModel createDnnModel(URI uri, String ndLayout, int[] inputShape) {
DnnShape shape = null;
if (inputShape != null)
shape = DnnShape.of(Arrays.stream(inputShape).mapToLong(i -> i).toArray());
return createDnnModel(null, uri, ndLayout, Map.of(DnnModel.DEFAULT_INPUT_NAME, shape), null);
}
/**
* Create a {@link DnnModel} wrapping a model from Deep Java Library.
* @param engine name of the engine to use, or null to try to determine this from the URIs
* @param uri URI of the model file
* @param ndLayout a layout string, e.g. NCHW
* @param inputs input shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)
* @param outputs outputs shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)
* @return
*/
public static DnnModel createDnnModel(String engine, URI uri, String ndLayout, Map<String, DnnShape> inputs, Map<String, DnnShape> outputs) {
return createDnnModel(engine, Collections.singletonList(uri), ndLayout, inputs, outputs);
}
/**
* Create a {@link DnnModel} wrapping a model from Deep Java Library, based on reading from multiple URIs.
* @param engine name of the engine to use, or null to try to determine this from the URIs
* @param uris one or more URIs needed to load the model file
* @param ndLayout a layout string, e.g. NCHW
* @param inputs input shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)
* @param outputs outputs shapes, if known; if these are null, an attempt will be made to get them from DJL (but this does not always work)
* @return
*/
private static DnnModel createDnnModel(String engine, Collection<URI> uris, String ndLayout, Map<String, DnnShape> inputs, Map<String, DnnShape> outputs) {
return new DjlDnnModel(engine, uris, ndLayout, inputs, outputs, false); // Eagerly initialize (so we know if it doesn't work sooner)
}
/**
* Check if an Deep Java Library engine is potentially available.
* Note that this does not necessarily mean that it has been downloaded or is supported
* on this platform, but only that the necessary engine jar is on the classpath.
* @param name
* @return true if the engine jars are on the classpath, false otherwise
* @see #getEngine(String, boolean)
* @see #isEngineAvailable(String)
*/
public static boolean hasEngine(String name) {
return Engine.hasEngine(name);
}
/**
* Check if an engine is available and ready for use without any additional downloading.
* This first checks whether an engine with the given name has already been seen
* by this class; if so, the method returns true without attempting to instantiate
* the engine.
* Otherwise, the method returns true only if {@code hasEngine(name)} is false or if
* {@code getEngine(name, false)} is not null.
* @param name
* @return
* @see #hasEngine(String)
* @see #getEngine(String, boolean)
*/
public static boolean isEngineAvailable(String name) {
if (loadedEngines.contains(name))
return true;
if (!hasEngine(name))
return false;
logger.debug("Need to try to get engine to test availability");
return getEngine(name, false) != null;
}
private static final Object lock = new Object();
/**
* Get an {@link Engine} by name.
* <p>
* This is similar to {@link Engine#getEngine(String)} except provides control over
* whether the native libraries for the engine are downloaded if required.
* This avoids unexpectedly long blocking calls for an engine request if the native
* libraries need to be downloaded.
*
* @param name the name of the engine
* @param downloadIfNeeded if true, download the necessary native libraries if needed.
* If false, return null if the engine cannot be loaded.
* @return
* @throws IllegalArgumentException if the engine is not available, which means that {@link #hasEngine(String)} returns false
* @see #hasEngine(String)
* @see #isEngineAvailable(String)
*/
public static Engine getEngine(String name, boolean downloadIfNeeded) throws IllegalArgumentException {
if (!hasEngine(name)) {
throw new IllegalArgumentException("Requested engine " + name + " is not available!");
}
synchronized (lock) {
var offlineStatus = System.getProperty("ai.djl.offline");
try {
if (downloadIfNeeded)
System.setProperty("ai.djl.offline", "false");
else
System.setProperty("ai.djl.offline", "true");
var engine = Engine.getEngine(name);
if (engine != null)
loadedEngines.add(name);
return engine;
} catch (Exception e) {
if (downloadIfNeeded) {
logger.error("Unable to get engine " + name + ": " + e.getMessage(), e);
} else {
var msg = e.getLocalizedMessage();
if (msg == null)
logger.warn("Unable to get engine {}", name);
else
logger.warn("Unable to get engine {} ({})", name, e.getMessage());
}
return null;
} finally {
System.setProperty("ai.djl.offline", offlineStatus);
}
}
}
static DnnShape convertShape(Shape shape) {
return DnnShape.of(shape.getShape());
}
static ZooModel<NDList, NDList> loadModel(String engineName, URI... uris) throws ModelNotFoundException, MalformedModelException, IOException {
return loadModel(engineName, NDList.class, NDList.class, null, uris);
}
static <P, Q> ZooModel<P, Q> loadModel(String engineName, Class<P> inputClass, Class<Q> outputClass, Translator<P, Q> translator, URI... uris) throws ModelNotFoundException, MalformedModelException, IOException {
var sb = new StringBuilder();
boolean isFirst = true;
for (var uri : uris) {
if (isFirst)
isFirst = false;
else
sb.append(",");
// TODO: Handle unzipping zipped models to a temporary directory, if needed
var s = uri.toString();
if (s.toLowerCase().startsWith("jar:file:") || s.toLowerCase().endsWith(".zip")) {
logger.warn("Model URI is zipped - please unzip the model and recreate it");
}
sb.append(uri.toString());
}
return loadModel(engineName, inputClass, outputClass, translator, sb.toString());
}
private static <P, Q> ZooModel<P, Q> loadModel(String engineName, Class<P> inputClass, Class<Q> outputClass, Translator<P, Q> translator, String urls) throws ModelNotFoundException, MalformedModelException, IOException {
var builder = Criteria.builder()
.setTypes(inputClass, outputClass)
.optModelUrls(urls)
.optTranslator(translator)
.optProgress(new ProgressBar());
String selectedEngine = null;
if (engineName != null) {
if (Engine.getAllEngines().contains(engineName)) {
selectedEngine = engineName;
}
}
// Try to figure out the engine name
if (selectedEngine == null) {
var urlString = urls.toString().toLowerCase();
if (urlString.endsWith(".onnx") && Engine.hasEngine("OnnxRuntime"))
selectedEngine = "OnnxRuntime";
else if ((urlString.endsWith("pytorch") || urlString.endsWith(".pt")) && Engine.hasEngine("PyTorch"))
selectedEngine = "PyTorch";
else if (urlString.endsWith(".tflite") && Engine.hasEngine("TFLite"))
selectedEngine = "TFLite";
else if ((urlString.endsWith(".pb") || urlString.endsWith("tf_savedmodel.zip") || urlString.endsWith("tf_savedmodel")) && Engine.hasEngine("TensorFlow"))
selectedEngine = "TensorFlow";
}
if (selectedEngine != null) {
builder.optEngine(selectedEngine);
var device = defaultDevices.getOrDefault(selectedEngine, null);
if (device != null) {
builder.optDevice(device);
builder.optOption("mapLocation", "true");
}
}
var criteria = builder.build();
return ModelZoo.loadModel(criteria);
}
/**
* Set the default device for the specified engine.
* This will be used only whenever the model is build using this class, overriding
* DJL's default.
* <p>
* Note that the default device chosen automatically by DJL is usually fine,
* and so it is generally not necessary to set this.
* However it can be useful for exploring, or if DJL does not use the device you want.
* @param engineName
* @param device
*/
public static void setOverrideDevice(String engineName, Device device) {
if (device == null)
defaultDevices.remove(engineName);
else
defaultDevices.put(engineName, device);
}
/**
* Get the default device for the specified engine, which overrides DJL's default device for
* the specified engine.
* @param engineName
* @return the default device, or null if not set
*/
public static Device getOverrideDevice(String engineName) {
return defaultDevices.getOrDefault(engineName, null);
}
static Mat predict(Model model, Mat mat) throws TranslateException {
try (var predictor = model.newPredictor(new MatTranslator("CHW", "CHW"))) {
return predictor.batchPredict(Collections.singletonList(mat)).get(0);
}
}
/**
* Convert an Opencv {@link Mat} to a Deep Java Library {@link NDArray}.
* Note that this ass
* @param manager an {@link NDManager}, required to create the NDArray
* @param mat the mat to convert
* @param ndLayout a layout string for the NDArray, e.g. "CHW"; currently, HW must appear together (in that order)
* @return an NDArray containing the values in the Mat, with the specified layout
*/
public static NDArray matToNDArray(NDManager manager, Mat mat, String ndLayout) {
var dataType = getDataType(mat);
if (dataType == DataType.UNKNOWN)
throw new IllegalArgumentException("Unsupported data type for " + mat);
var shape = getShape(mat, ndLayout);
int indC = ndLayout.indexOf("C");
int indHW = ndLayout.indexOf("HW");
if (indHW < 0)
throw new IllegalArgumentException("Expected layout contains HW, but provided layout is " + ndLayout);
long nChannels = shape.get(indC);
// Copy all at once is using the same storage order as OpenCV
// TODO: Check what this order is!!!
NDArray array = null;
if (indC > indHW || shape.get(indC) == 1) {
var buffer = mat.createBuffer();
array = manager.create(buffer, shape, dataType);
} else if (("NCHW".equals(ndLayout) || "CHW".equals(ndLayout)) && (nChannels == 3L || nChannels == 4L)) {
// Channels-first - an OpenCV blob is defined to have the order NCHW, but an Image can only have 1, 3 or 4 channels
try (var blob = opencv_dnn.blobFromImage(mat)) {
array = manager.create(blob.createBuffer(), shape, dataType);
}
} else {
// Really awkward strategy to handle channels in an uncommon place (shouldn't actually occur?)
var shapeDims = shape.getShape().clone();
shapeDims[indC] = 1;
var shapeChannel = new Shape(shapeDims, shape.getLayout());
for (var mat2 : OpenCVTools.splitChannels(mat)) {
var buffer = mat2.createBuffer();
var arrayTemp = manager.create(buffer, shapeChannel, dataType);
if (array == null)
array = arrayTemp;
else {
var arrayTemp2 = array.concat(arrayTemp, indC);
array.close();
arrayTemp.close();
array = arrayTemp2;
}
}
}
return array;
}
/**
* Convert an {@link NDArray} into an OpenCV {@link Mat}, automatically squeezing singleton dimensions.
* The {@link Mat} should have no more than 3 dimensions (height, width and channels).
*
* @param array the NDArray to convert
* @param ndLayout a layout string, e.g. NCHW. If this is null, an attempt will be made to request the layout
* from the array - however this can often fail if the dimensions are unknown.
* @return a {@link Mat} with pixels corresponding to the {@link NDArray}.
*/
public static Mat ndArrayToMat(NDArray array, String ndLayout) {
return ndArrayToMat(array, ndLayout, true);
}
/**
* Convert an {@link NDArray} into an OpenCV {@link Mat}, optionally squeezing singleton dimensions.
* The {@link Mat} should have no more than 3 dimensions (height, width and channels).
*
* @param array the NDArray to convert
* @param ndLayout a layout string, e.g. NCHW. If this is null, an attempt will be made to request the layout
* from the array - however this can often fail if the dimensions are unknown.
* @param doSqueeze if true, squeeze singleton dimensions
* @return a {@link Mat} with pixels corresponding to the {@link NDArray}.
*/
// TODO: Check this for corner cases, and arrays with unexpected layouts
public static Mat ndArrayToMat(NDArray array, String ndLayout, boolean doSqueeze) {
var dataType = array.getDataType();
var shape = array.getShape();
if (ndLayout == null) {
if (shape.isLayoutKnown())
ndLayout = LayoutType.toString(shape.getLayout());
else
throw new IllegalArgumentException("Can't convert ndArray to Mat - layout is unknown");
}
// Get dimensions, trimming any leading/trailing ones
int nDim = shape.dimension();
int nLeading = doSqueeze ? shape.getLeadingOnes() : 0;
int nTrailing = doSqueeze ? shape.getTrailingOnes() : 0;
int[] dims = new int[nDim - nLeading - nTrailing];
for (int i = 0; i < dims.length; i++) {
dims[i] = (int)shape.get(i + nLeading);
}
if (doSqueeze) {
array = array.squeeze();
ndLayout = ndLayout.substring(nLeading, ndLayout.length()-nTrailing);
}
// If we have multiple channels, we might need to handle them separately
int indH = ndLayout.indexOf("H");
int indW = ndLayout.indexOf("W");
int indC = ndLayout.indexOf("C");
int height = indH >= 0 && indH < dims.length ? dims[indH] : 1;
int width = indW >= 0 && indW < dims.length ? dims[indW] : 1;
int nChannels = indC >= 0 && indC < dims.length ? dims[indC] : 1;
if (nChannels > 1 && indC >= 0 && indC < indH) {
Mat mat = new Mat();
try (var scope = new PointerScope()) {
List<Mat> channels = new ArrayList<>();
try (var list = array.split(nChannels, indC)) {
for (var ndChannel : list) {
channels.add(ndArrayToMat(ndChannel, ndLayout, false));
ndChannel.close();
}
}
OpenCVTools.mergeChannels(channels, mat);
}
return mat;
}
Mat mat;
var cvDepth = getMatDepth(dataType);
if (dims.length <= 3 && width * height * nChannels == array.size()) {
mat = new Mat(height, width, opencv_core.CV_MAKETYPE(cvDepth, nChannels));
} else {
mat = new Mat(dims, cvDepth);
}
try (var indexer = mat.createIndexer()) {
if (indexer instanceof ByteIndexer) {
((ByteIndexer) indexer).put(0L, array.toByteArray());
} else if (indexer instanceof UByteIndexer) {
((UByteIndexer) indexer).put(0L, getInts(array));
} else if (indexer instanceof UShortIndexer) {
((UShortIndexer) indexer).put(0L, getInts(array));
} else if (indexer instanceof IntIndexer) {
((IntIndexer) indexer).put(0L, getInts(array));
} else if (indexer instanceof FloatIndexer) {
((FloatIndexer) indexer).put(0L, getFloats(array));
} else if (indexer instanceof HalfIndexer) {
((HalfIndexer) indexer).put(0L,getFloats(array));
} else if (indexer instanceof DoubleIndexer) {
((DoubleIndexer) indexer).put(0L, getDoubles(array));
} else if (indexer instanceof LongIndexer) {
((LongIndexer) indexer).put(0L, getLongs(array));
} else if (indexer instanceof BooleanIndexer) {
((BooleanIndexer) indexer).put(0L, getBooleans(array));
} else
throw new IllegalArgumentException("Unable to convert array " + array + " to Mat");
}
return mat;
}
/**
* Extract array values as longs, converting if necessary.
* @param array
* @return
*/
public static long[] getLongs(NDArray array) {
if (array.getDataType() == DataType.INT64) {
try {
return array.toLongArray();
} catch (Exception e) {
logger.error("Exception requesting longs from NDArray");
}
}
return array.toType(DataType.INT64, true).toLongArray();
}
/**
* Extract array values as booleans, converting if necessary.
* @param array
* @return
*/
private static boolean[] getBooleans(NDArray array) {
if (array.getDataType() == DataType.BOOLEAN) {
try {
return array.toBooleanArray();
} catch (Exception e) {
logger.error("Exception requesting ints from NDArray");
}
}
return array.toType(DataType.BOOLEAN, true).toBooleanArray();
}
/**
* Extract array values as ints, converting if necessary.
* @param array
* @return
*/
private static int[] getInts(NDArray array) {
if (array.getDataType() == DataType.INT32) {
try {
return array.toIntArray();
} catch (Exception e) {
logger.error("Exception requesting ints from NDArray");
}
} else if (array.getDataType() == DataType.UINT8) {
try {
return array.toUint8Array();
} catch (Exception e) {
logger.error("Exception requesting ints from NDArray");
}
}
return array.toType(DataType.INT32, true).toIntArray();
}
/**
* Extract array values as doubles, converting if necessary.
* @param array
* @return
*/
private static double[] getDoubles(NDArray array) {
if (array.getDataType() == DataType.FLOAT64) {
try {
return array.toDoubleArray();
} catch (Exception e) {
logger.error("Exception requesting doubles from NDArray");
}
} else if (array.getDataType() == DataType.INT64) {
try {
return Arrays.stream(array.toLongArray()).mapToDouble(i -> i).toArray();
} catch (Exception e) {
logger.error("Exception requesting doubles from NDArray (from longs)");
}
}
// If the device is MPS, we can't convert to float64 - so make sure we're on the CPU
return array.toDevice(Device.cpu(), false).toType(DataType.FLOAT64, true).toDoubleArray();
}
/**
* Extract array values as floats, converting if necessary.
* @param array
* @return
*/
private static float[] getFloats(NDArray array) {
if (array.getDataType() == DataType.FLOAT32 || array.getDataType() == DataType.FLOAT16) {
try {
return array.toFloatArray();
} catch (Exception e) {
logger.error("Exception requesting floats from NDArray", e);
}
}
return array.toType(DataType.FLOAT32, true).toFloatArray();
}
static class MatTranslator implements Translator<Mat, Mat> {
private String inputLayoutNd, outputLayoutNd;
MatTranslator(String inputLayoutNd, String outputLayoutNd) {
this.inputLayoutNd = inputLayoutNd;
this.outputLayoutNd = outputLayoutNd;
}
/**
* Convert Mat to NDArray and add to an NDList.
* Note that not all OpenCV types are supported.
* Specifically, 16-bit types should be avoided.
*/
@Override
public NDList processInput(TranslatorContext ctx, Mat input) throws Exception {
var ndarray = matToNDArray(ctx.getNDManager(), input, inputLayoutNd);
return new NDList(ndarray);
}
@Override
public Mat processOutput(TranslatorContext ctx, NDList list) throws Exception {
var array = list.get(0);
return ndArrayToMat(array, outputLayoutNd);
}
}
static Shape getShape(Mat mat, String ndLayout) {
List<Pair<Long, LayoutType>> pairs = new ArrayList<>();
for (var layout : LayoutType.fromValue(ndLayout)) {
switch (layout) {
case CHANNEL:
pairs.add(new Pair<>((long)mat.arrayChannels(), layout));
break;
case HEIGHT:
pairs.add(new Pair<>((long)mat.arrayHeight(), layout));
break;
case WIDTH:
pairs.add(new Pair<>((long)mat.arrayWidth(), layout));
break;
case BATCH:
case DEPTH:
case TIME:
case UNKNOWN:
default:
pairs.add(new Pair<>(1L, layout));
}
}
var shape = new Shape(new PairList<>(pairs));
return shape;
}
static DataType getDataType(Mat mat) {
switch (mat.depth()) {
case opencv_core.CV_8U: return DataType.UINT8;
case opencv_core.CV_8S: return DataType.INT8;
case opencv_core.CV_32S: return DataType.INT32;
case opencv_core.CV_32F: return DataType.FLOAT32;
case opencv_core.CV_64F: return DataType.FLOAT64;
case opencv_core.CV_16F: return DataType.FLOAT16; // TODO: Check this! 16-bit float support in OpenCV is limited
case opencv_core.CV_16U: // Not supported
case opencv_core.CV_16S:
default: return DataType.UNKNOWN;
}
}
static int getMatDepth(DataType dt) {
switch (dt) {
case BOOLEAN:
return opencv_core.CV_8U;
case FLOAT16:
return opencv_core.CV_16F;
case FLOAT32:
return opencv_core.CV_32F;
case FLOAT64:
return opencv_core.CV_64F;
case INT32:
return opencv_core.CV_32S;
case INT64:
return opencv_core.CV_64F;
case INT8:
return opencv_core.CV_8S;
case UINT8:
return opencv_core.CV_8U;
case STRING:
case UNKNOWN:
default:
throw new UnsupportedOperationException("Cannot convert data type " + dt + " to Mat");
}
}
}