-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDecodeNumpy.java
482 lines (455 loc) · 19.8 KB
/
DecodeNumpy.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
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
/*-
* #%L
* Use deep learning frameworks from Java in an agnostic and isolated way.
* %%
* Copyright (C) 2022 - 2023 Institut Pasteur and BioImage.IO developers.
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the BioImage.io nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package io.bioimage.modelrunner.numpy;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.bioimage.modelrunner.utils.IndexingUtils;
import net.imglib2.Cursor;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.cell.CellImgFactory;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.ByteType;
import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.type.numeric.integer.LongType;
import net.imglib2.type.numeric.integer.ShortType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.integer.UnsignedIntType;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
public class DecodeNumpy {
private static final int BUFFER_SIZE = 1024 * 1024;
private static final String NUMPY_EXTENSION = ".npy";
private static final byte[] MAGIC_PREFIX = {(byte) 0x93, 'N', 'U', 'M', 'P', 'Y'};
private static final Map<String, Integer> DATA_TYPES_MAP = new HashMap<>();
static
{
DATA_TYPES_MAP.put("byte", 1);
DATA_TYPES_MAP.put("int16", 2);
DATA_TYPES_MAP.put("int32", 4);
DATA_TYPES_MAP.put("int64", 8);
DATA_TYPES_MAP.put("boolean", 1);
DATA_TYPES_MAP.put("uint8", 4);
DATA_TYPES_MAP.put("uint16", 4);
DATA_TYPES_MAP.put("uint32", 8);
DATA_TYPES_MAP.put("float16", 4);
DATA_TYPES_MAP.put("float32", 4);
DATA_TYPES_MAP.put("float64", 8);
}
private static final Pattern HEADER_PATTERN =
Pattern.compile("\\{'descr': '(.+)', 'fortran_order': False, 'shape': \\((.*)\\),");
public static void main(String[] args) throws FileNotFoundException, IOException {
String npy = "C:\\Users\\angel\\OneDrive\\Documentos\\pasteur\\git\\deep-icy\\models\\HPA Bestfitting InceptionV3_13102022_173532\\test_input.npy";
RandomAccessibleInterval<?> aa = retrieveImgLib2FromNpy(npy);
}
public static < T extends RealType< T > & NativeType< T > >
RandomAccessibleInterval<T> retrieveImgLib2FromNpy(String path) throws FileNotFoundException, IOException{
File npyFile = new File(path);
if (!npyFile.isFile() || !path.endsWith(NUMPY_EXTENSION)) {
throw new IllegalArgumentException("Path provided does not correspond to a Numpy file: " + path);
}
try (InputStream targetStream = new FileInputStream(npyFile)) {
return decodeNumpy(targetStream);
}
}
private static < T extends RealType< T > & NativeType< T > >
RandomAccessibleInterval<T> decodeNumpy(InputStream is) throws IOException {
DataInputStream dis;
if (is instanceof DataInputStream) {
dis = (DataInputStream) is;
} else {
dis = new DataInputStream(is);
}
byte[] buf = new byte[MAGIC_PREFIX.length];
dis.readFully(buf);
if (!Arrays.equals(buf, MAGIC_PREFIX)) {
throw new IllegalArgumentException("Malformed or unsopported Numpy array");
}
byte major = dis.readByte();
byte minor = dis.readByte();
if (major < 1 || major > 3 || minor != 0) {
throw new IllegalArgumentException("Unknown numpy version: " + major + '.' + minor);
}
int len = major == 1 ? 2 : 4;
dis.readFully(buf, 0, len);
ByteBuffer bb = ByteBuffer.wrap(buf, 0, len);
bb.order(ByteOrder.LITTLE_ENDIAN);
if (major == 1) {
len = bb.getShort();
} else {
len = bb.getInt();
}
buf = new byte[len];
dis.readFully(buf);
String header = new String(buf, StandardCharsets.UTF_8).trim();
Matcher m = HEADER_PATTERN.matcher(header);
if (!m.find()) {
throw new IllegalArgumentException("Invalid numpy header: " + header);
}
String typeStr = m.group(1);
String shapeStr = m.group(2);
long[] shape = new long[0];
if (!shapeStr.isEmpty()) {
String[] tokens = shapeStr.split(", ?");
shape = Arrays.stream(tokens).mapToLong(Long::parseLong).toArray();
}
String dtype = getDataType(typeStr);
long numBytes = DATA_TYPES_MAP.get(dtype);
long count;
if (shape.length == 0)
count = 1;
else
count = Arrays.stream(shape).reduce(Math::multiplyExact).getAsLong();
//len = Math.toIntExact(shape.length * numBytes);
len = Math.toIntExact(count * numBytes);
ByteBuffer data = ByteBuffer.allocate(len);
char order = typeStr.charAt(0);
ByteOrder byteOrder = null;
if (order == '>') {
byteOrder = ByteOrder.BIG_ENDIAN;
} else if (order == '<') {
byteOrder = ByteOrder.LITTLE_ENDIAN;
} else {
new IllegalArgumentException("Not supported ByteOrder for the provided .npy array.");
}
data.order(byteOrder);
readData(dis, data, len);
return build(data, byteOrder, dtype, shape);
}
public static String getDataType(String npDtype) throws IllegalArgumentException {
if (npDtype.startsWith(">") || npDtype.startsWith("<"))
npDtype = npDtype.substring(1);
if (npDtype.equals("i1") || npDtype.equals("b") || npDtype.equals("c"))
return "byte";
else if (npDtype.equals("i2") || npDtype.equals("h"))
return "int16";
else if (npDtype.equals("i4") || npDtype.equals("i"))
return "int32";
else if (npDtype.equals("i8") || npDtype.equals("l")
|| npDtype.equals("q"))
return "int64";
else if (npDtype.equals("b1"))
return "boolean";
else if (npDtype.equals("u1") || npDtype.equals("B"))
return "uint8";
else if (npDtype.equals("u2") || npDtype.equals("H"))
return "uint16";
else if (npDtype.equals("u4") || npDtype.equals("I"))
return "uint32";
else if (npDtype.equals("f2") || npDtype.equals("e"))
return "float16";
else if (npDtype.equals("f") || npDtype.equals("f4"))
return "float32";
else if (npDtype.equals("f8") || npDtype.equals("d"))
return "float64";
else if (npDtype.equals("u8") || npDtype.equals("L")
|| npDtype.equals("Q"))
throw new IllegalArgumentException("Numpy dtype 'uint64' cannot "
+ " be supported in Java.");
else if (npDtype.equals("c8"))
throw new IllegalArgumentException("Numpy dtype 'complex64' is not "
+ "supported at the moment.");
else
throw new IllegalArgumentException("Numpy dtype '" + npDtype + "' is not "
+ "supported at the moment.");
}
private static void readData(DataInputStream dis, ByteBuffer data, int len) throws IOException {
if (len > 0) {
byte[] buf = new byte[BUFFER_SIZE];
while (len > BUFFER_SIZE) {
dis.readFully(buf);
data.put(buf);
len -= BUFFER_SIZE;
}
dis.readFully(buf, 0, len);
data.put(buf, 0, len);
data.rewind();
}
}
/**
* Creates a {@link Img} from a given {@link ByteBuffer} and an array with its dimensions order.
*
* @param buf
* The buffer data is read from.
* @param byteOrder
* Endianness of the buffer data.
* @param dtype
* NumPy dtype of the data.
* @param shape
* NumPy shape of the data.
* @return The Img built from the tensor.
* @throws IllegalArgumentException
* If the tensor type is not supported.
*/
@SuppressWarnings("unchecked")
public static <T extends Type<T>> Img<T> build(ByteBuffer buf, ByteOrder byteOrder, String dtype, long[] shape) throws IllegalArgumentException
{
// Create an Img of the same type of the tensor
byte[] data = new byte[buf.remaining()];
buf.get(data);
if (dtype.equals("byte")) {
return (Img<T>) buildByteFromByte(data, byteOrder, shape);
} else if (dtype.equals("ubyte")) {
return (Img<T>) buildUByteFromByte(data, byteOrder, shape);
} else if (dtype.equals("int16")) {
return (Img<T>) buildInt16FromByte(data, byteOrder, shape);
} else if (dtype.equals("uint16")) {
return (Img<T>) buildUInt16FromByte(data, byteOrder, shape);
} else if (dtype.equals("int32")) {
return (Img<T>) buildInt32FromByte(data, byteOrder, shape);
} else if (dtype.equals("uint32")) {
return (Img<T>) buildUInt32FromByte(data, byteOrder, shape);
} else if (dtype.equals("int64")) {
return (Img<T>) buildInt64FromByte(data, byteOrder, shape);
} else if (dtype.equals("float32")) {
return (Img<T>) buildFloat32FromByte(data, byteOrder, shape);
} else if (dtype.equals("float64")) {
return (Img<T>) buildFloat64FromByte(data, byteOrder, shape);
} else {
throw new IllegalArgumentException("Unsupported tensor type: " + dtype);
}
}
/** TODO check BigEndian LittleEndian
* Builds a {@link Img} from a unsigned byte-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The Img built from the tensor of type {@link DataType#UBYTE}.
*/
private static Img<ByteType> buildByteFromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< ByteType > factory = new CellImgFactory<>( new ByteType(), 5 );
final Img< ByteType > outputImg = (Img<ByteType>) factory.create(tensorShape);
Cursor<ByteType> tensorCursor= outputImg.cursor();
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
byte val = tensor[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/** TODO check BigEndian LittleEndian
* Builds a {@link Img} from a unsigned byte-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The Img built from the tensor of type {@link DataType#UBYTE}.
*/
private static Img<UnsignedByteType> buildUByteFromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< UnsignedByteType > factory = new CellImgFactory<>( new UnsignedByteType(), 5 );
final Img< UnsignedByteType > outputImg = (Img<UnsignedByteType>) factory.create(tensorShape);
Cursor<UnsignedByteType> tensorCursor= outputImg.cursor();
int[] flatArr = ByteArrayUtils.convertIntoUInt8(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
int val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned integer-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The sequence built from the tensor of type {@link DataType#INT}.
*/
private static Img<ShortType> buildInt16FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< ShortType > factory = new CellImgFactory<>( new ShortType(), 5 );
final Img< ShortType > outputImg = (Img<ShortType>) factory.create(tensorShape);
Cursor<ShortType> tensorCursor= outputImg.cursor();
short[] flatArr = ByteArrayUtils.convertIntoSignedShort16(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
short val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned integer-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The sequence built from the tensor of type {@link DataType#INT}.
*/
private static Img<UnsignedShortType> buildUInt16FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< UnsignedShortType > factory = new CellImgFactory<>( new UnsignedShortType(), 5 );
final Img< UnsignedShortType > outputImg = (Img<UnsignedShortType>) factory.create(tensorShape);
Cursor<UnsignedShortType> tensorCursor= outputImg.cursor();
int[] flatArr = ByteArrayUtils.convertIntoUnsignedIn16(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
int val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned integer-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The sequence built from the tensor of type {@link DataType#INT}.
*/
private static Img<IntType> buildInt32FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< IntType > factory = new CellImgFactory<>( new IntType(), 5 );
final Img< IntType > outputImg = (Img<IntType>) factory.create(tensorShape);
Cursor<IntType> tensorCursor= outputImg.cursor();
int[] flatArr = ByteArrayUtils.convertIntoSignedInt32(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
int val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned integer-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The sequence built from the tensor of type {@link DataType#INT}.
*/
private static Img<UnsignedIntType> buildUInt32FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< UnsignedIntType > factory = new CellImgFactory<>( new UnsignedIntType(), 5 );
final Img< UnsignedIntType > outputImg = (Img<UnsignedIntType>) factory.create(tensorShape);
Cursor<UnsignedIntType> tensorCursor= outputImg.cursor();
long[] flatArr = ByteArrayUtils.convertIntoUnsignedInt32(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
long val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned float-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The Img built from the tensor of type {@link DataType#FLOAT}.
*/
private static Img<FloatType> buildFloat32FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< FloatType > factory = new CellImgFactory<>( new FloatType(), 5 );
final Img< FloatType > outputImg = (Img<FloatType>) factory.create(tensorShape);
Cursor<FloatType> tensorCursor= outputImg.cursor();
float[] flatArr = ByteArrayUtils.convertIntoSignedFloat32(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
float val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned double-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The Img built from the tensor of type {@link DataType#DOUBLE}.
*/
private static Img<DoubleType> buildFloat64FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< DoubleType > factory = new CellImgFactory<>( new DoubleType(), 5 );
final Img< DoubleType > outputImg = (Img<DoubleType>) factory.create(tensorShape);
Cursor<DoubleType> tensorCursor= outputImg.cursor();
double[] flatArr = ByteArrayUtils.convertIntoSignedFloat64(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
double val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
/**
* Builds a {@link Img} from a unsigned double-typed {@code Tensor}.
*
* @param tensor
* The tensor data is read from.
* @return The Img built from the tensor of type {@link DataType#DOUBLE}.
*/
private static Img<LongType> buildInt64FromByte(byte[] tensor, ByteOrder byteOrder, long[] tensorShape)
{
final ImgFactory< LongType > factory = new CellImgFactory<>( new LongType(), 5 );
final Img< LongType > outputImg = (Img<LongType>) factory.create(tensorShape);
Cursor<LongType> tensorCursor= outputImg.cursor();
long[] flatArr = ByteArrayUtils.convertIntoSignedInt64(tensor, byteOrder);
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
long val = flatArr[flatPos];
tensorCursor.get().set(val);
}
return outputImg;
}
}