NativeImageLoader extends BaseImageLoader { @Override public INDArray asMatrix(File f) throws IOException { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { return asMatrix(bis); } } NativeImageLoader();  NativeImageLoader(int height, int width);  NativeImageLoader(int height, int width, int channels);  NativeImageLoader(int height, int width, int channels, boolean centerCropIfNeeded);  NativeImageLoader(int height, int width, int channels, ImageTransform imageTransform);  NativeImageLoader(int height, int width, int channels, MultiPageMode mode); protected  NativeImageLoader(NativeImageLoader other); @Override String[] getAllowedFormats(); @Override INDArray asRowVector(File f); @Override INDArray asRowVector(InputStream is); INDArray asRowVector(Object image); INDArray asRowVector(Frame image); INDArray asRowVector(Mat image); @Override INDArray asMatrix(File f); @Override INDArray asMatrix(InputStream is); @Override Image asImageMatrix(File f); @Override Image asImageMatrix(InputStream is); INDArray asMatrix(Object image); void asMatrixView(InputStream is, INDArray view); void asMatrixView(File f, INDArray view); void asMatrixView(Mat image, INDArray view); INDArray asMatrix(Frame image); INDArray asMatrix(Mat image); ImageWritable asWritable(File f); INDArray asMatrix(ImageWritable writable); Frame asFrame(INDArray array); Frame asFrame(INDArray array, int dataType); Mat asMat(INDArray array); Mat asMat(INDArray array, int dataType); static final String[] ALLOWED_FORMATS; }
@Test public void testBufferRealloc() throws Exception { Field f = NativeImageLoader.class.getDeclaredField("buffer"); Field m = NativeImageLoader.class.getDeclaredField("bufferMat"); f.setAccessible(true); m.setAccessible(true); File f1 = new ClassPathResource("voc/2007/JPEGImages/000005.jpg").getFile(); File f2 = new ClassPathResource("voc/2007/JPEGImages/000007.jpg").getFile(); byte[] buffer = new byte[20*1024*1024]; Mat bufferMat = new Mat(buffer); NativeImageLoader loader = new NativeImageLoader(28, 28, 1); f.set(loader, buffer); m.set(loader, bufferMat); INDArray img1LargeBuffer = loader.asMatrix(f1); INDArray img2LargeBuffer = loader.asMatrix(f2); INDArray img1LargeBuffer2 = loader.asMatrix(f1); INDArray img1LargeBuffer3 = loader.asMatrix(f1); assertEquals(img1LargeBuffer2, img1LargeBuffer3); INDArray img2LargeBuffer2 = loader.asMatrix(f1); INDArray img2LargeBuffer3 = loader.asMatrix(f1); assertEquals(img2LargeBuffer2, img2LargeBuffer3); f.set(loader, null); INDArray img1NoBuffer1 = loader.asMatrix(f1); INDArray img1NoBuffer2 = loader.asMatrix(f1); assertEquals(img1LargeBuffer, img1NoBuffer1); assertEquals(img1LargeBuffer, img1NoBuffer2); f.set(loader, null); INDArray img2NoBuffer1 = loader.asMatrix(f2); INDArray img2NoBuffer2 = loader.asMatrix(f2); assertEquals(img2LargeBuffer, img2NoBuffer1); assertEquals(img2LargeBuffer, img2NoBuffer2); buffer = new byte[10]; bufferMat = new Mat(buffer); f.set(loader, buffer); m.set(loader, bufferMat); INDArray img1SmallBuffer1 = loader.asMatrix(f1); INDArray img1SmallBuffer2 = loader.asMatrix(f1); assertEquals(img1LargeBuffer, img1SmallBuffer1); assertEquals(img1LargeBuffer, img1SmallBuffer2); f.set(loader, buffer); m.set(loader, bufferMat); INDArray img2SmallBuffer1 = loader.asMatrix(f2); INDArray img2SmallBuffer2 = loader.asMatrix(f2); assertEquals(img2LargeBuffer, img2SmallBuffer1); assertEquals(img2LargeBuffer, img2SmallBuffer2); try(InputStream is = new FileInputStream(f1)){ byte[] temp = IOUtils.toByteArray(is); buffer = new byte[temp.length]; bufferMat = new Mat(buffer); } f.set(loader, buffer); m.set(loader, bufferMat); INDArray img1ExactBuffer = loader.asMatrix(f1); assertEquals(img1LargeBuffer, img1ExactBuffer); }
@Test public void testAsMatrix() throws Exception { BufferedImage img1 = makeRandomBufferedImage(0, 0, 3); Mat img2 = makeRandomImage(0, 0, 4); int w1 = 33, h1 = 77, ch1 = 1; NativeImageLoader loader1 = new NativeImageLoader(h1, w1, ch1); INDArray array1 = loader1.asMatrix(img1); assertEquals(4, array1.rank()); assertEquals(1, array1.size(0)); assertEquals(1, array1.size(1)); assertEquals(h1, array1.size(2)); assertEquals(w1, array1.size(3)); INDArray array2 = loader1.asMatrix(img2); assertEquals(4, array2.rank()); assertEquals(1, array2.size(0)); assertEquals(1, array2.size(1)); assertEquals(h1, array2.size(2)); assertEquals(w1, array2.size(3)); int w2 = 111, h2 = 66, ch2 = 3; NativeImageLoader loader2 = new NativeImageLoader(h2, w2, ch2); INDArray array3 = loader2.asMatrix(img1); assertEquals(4, array3.rank()); assertEquals(1, array3.size(0)); assertEquals(3, array3.size(1)); assertEquals(h2, array3.size(2)); assertEquals(w2, array3.size(3)); INDArray array4 = loader2.asMatrix(img2); assertEquals(4, array4.rank()); assertEquals(1, array4.size(0)); assertEquals(3, array4.size(1)); assertEquals(h2, array4.size(2)); assertEquals(w2, array4.size(3)); int w3 = 123, h3 = 77, ch3 = 3; NativeImageLoader loader3 = new NativeImageLoader(h3, w3, ch3); File f3 = new ClassPathResource("/testimages/class0/2.jpg").getFile(); ImageWritable iw3 = loader3.asWritable(f3); INDArray array5 = loader3.asMatrix(iw3); assertEquals(4, array5.rank()); assertEquals(1, array5.size(0)); assertEquals(3, array5.size(1)); assertEquals(h3, array5.size(2)); assertEquals(w3, array5.size(3)); Mat mat = loader3.asMat(array5); assertEquals(w3, mat.cols()); assertEquals(h3, mat.rows()); assertEquals(ch3, mat.channels()); assertTrue(mat.type() == CV_32FC(ch3) || mat.type() == CV_64FC(ch3)); Frame frame = loader3.asFrame(array5, Frame.DEPTH_UBYTE); assertEquals(w3, frame.imageWidth); assertEquals(h3, frame.imageHeight); assertEquals(ch3, frame.imageChannels); assertEquals(Frame.DEPTH_UBYTE, frame.imageDepth); Java2DNativeImageLoader loader4 = new Java2DNativeImageLoader(); BufferedImage img12 = loader4.asBufferedImage(array1); assertEquals(array1, loader4.asMatrix(img12)); }