diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-geo/build.gradle b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-geo/build.gradle index 49822b112..36788ea03 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-geo/build.gradle +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-geo/build.gradle @@ -28,4 +28,5 @@ dependencies { implementation "commons-io:commons-io" testImplementation projects.cavisNd4j.cavisNd4jCommonTests + testRuntimeOnly "net.brutex.ai:dl4j-test-resources:1.0.1-SNAPSHOT" } \ No newline at end of file diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/LFWLoader.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/LFWLoader.java index 5f78f1611..dc75e7e1c 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/LFWLoader.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/LFWLoader.java @@ -151,7 +151,7 @@ public class LFWLoader extends BaseImageLoader implements Serializable { } FileSplit fileSplit = new FileSplit(fullDir, ALLOWED_FORMATS, rng); BalancedPathFilter pathFilter = new BalancedPathFilter(rng, ALLOWED_FORMATS, labelGenerator, numExamples, - numLabels, 0, batchSize, null); + numLabels, 0, batchSize, (String) null); inputSplit = fileSplit.sample(pathFilter, numExamples * splitTrainTest, numExamples * (1 - splitTrainTest)); } diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/NativeImageLoader.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/NativeImageLoader.java index 4db72decd..303ca742f 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/NativeImageLoader.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/loader/NativeImageLoader.java @@ -256,8 +256,27 @@ public class NativeImageLoader extends BaseImageLoader { @Override public INDArray asMatrix(File f, boolean nchw) throws IOException { - try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { - return asMatrix(bis, nchw); + Mat mat = imread(f.getAbsolutePath(), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR ); + INDArray a; + if (this.multiPageMode != null) { + a = asMatrix(mat.data(), mat.cols()); + }else{ + // Mat image = imdecode(mat, IMREAD_ANYDEPTH | IMREAD_ANYCOLOR); + if (mat == null || mat.empty()) { + PIX pix = pixReadMem(mat.data(), mat.cols()); + if (pix == null) { + throw new IOException("Could not decode image from input stream"); + } + mat = convert(pix); + pixDestroy(pix); + } + a = asMatrix(mat); + mat.deallocate(); + } + if(nchw) { + return a; + } else { + return a.permute(0, 2, 3, 1); //NCHW to NHWC } } @@ -268,6 +287,8 @@ public class NativeImageLoader extends BaseImageLoader { @Override public INDArray asMatrix(InputStream inputStream, boolean nchw) throws IOException { + throw new RuntimeException("not implemented"); + /* Mat mat = streamToMat(inputStream); INDArray a; if (this.multiPageMode != null) { @@ -290,6 +311,8 @@ public class NativeImageLoader extends BaseImageLoader { } else { return a.permute(0, 2, 3, 1); //NCHW to NHWC } + + */ } /** @@ -358,9 +381,13 @@ public class NativeImageLoader extends BaseImageLoader { @Override public Image asImageMatrix(File f, boolean nchw) throws IOException { - try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { - return asImageMatrix(bis, nchw); - } + Mat image = imread(f.getAbsolutePath(), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR); + INDArray a = asMatrix(image); + if(!nchw) + a = a.permute(0,2,3,1); //NCHW to NHWC + Image i = new Image(a, image.channels(), image.rows(), image.cols()); + image.deallocate(); + return i; } @Override @@ -370,7 +397,8 @@ public class NativeImageLoader extends BaseImageLoader { @Override public Image asImageMatrix(InputStream inputStream, boolean nchw) throws IOException { - Mat mat = streamToMat(inputStream); + throw new RuntimeException("Deprecated. Not implemented."); + /*Mat mat = streamToMat(inputStream); Mat image = imdecode(mat, IMREAD_ANYDEPTH | IMREAD_ANYCOLOR); if (image == null || image.empty()) { PIX pix = pixReadMem(mat.data(), mat.cols()); @@ -387,6 +415,8 @@ public class NativeImageLoader extends BaseImageLoader { image.deallocate(); return i; + + */ } /** diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/BoxImageTransform.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/BoxImageTransform.java index 76dc8a798..97755d82e 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/BoxImageTransform.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/BoxImageTransform.java @@ -21,6 +21,7 @@ package org.datavec.image.transform; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @@ -38,6 +39,7 @@ import org.bytedeco.opencv.opencv_core.*; @JsonIgnoreProperties({"borderValue"}) @JsonInclude(JsonInclude.Include.NON_NULL) @Data +@EqualsAndHashCode(callSuper = false) public class BoxImageTransform extends BaseImageTransform { private int width; diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/FlipImageTransform.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/FlipImageTransform.java index c1e00ce35..d9b827e22 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/FlipImageTransform.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/FlipImageTransform.java @@ -21,6 +21,7 @@ package org.datavec.image.transform; import lombok.Data; +import lombok.EqualsAndHashCode; import org.bytedeco.javacv.OpenCVFrameConverter; import org.datavec.image.data.ImageWritable; import com.fasterxml.jackson.annotation.JsonInclude; @@ -32,6 +33,7 @@ import static org.bytedeco.opencv.global.opencv_core.*; @JsonInclude(JsonInclude.Include.NON_NULL) @Data +@EqualsAndHashCode(callSuper = false) public class FlipImageTransform extends BaseImageTransform { /** diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/MultiImageTransform.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/MultiImageTransform.java index 9877d6ffc..72eb0883e 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/MultiImageTransform.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/main/java/org/datavec/image/transform/MultiImageTransform.java @@ -21,6 +21,7 @@ package org.datavec.image.transform; import lombok.Data; +import lombok.EqualsAndHashCode; import org.datavec.image.data.ImageWritable; import java.util.Random; @@ -28,6 +29,7 @@ import java.util.Random; import org.bytedeco.opencv.opencv_core.*; @Data +@EqualsAndHashCode(callSuper = false) public class MultiImageTransform extends BaseImageTransform { private PipelineImageTransform transform; diff --git a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/test/java/org/datavec/image/loader/TestNativeImageLoader.java b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/test/java/org/datavec/image/loader/TestNativeImageLoader.java index 5bc82a570..9550867bb 100644 --- a/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/test/java/org/datavec/image/loader/TestNativeImageLoader.java +++ b/cavis-datavec/cavis-datavec-data/cavis-datavec-data-image/src/test/java/org/datavec/image/loader/TestNativeImageLoader.java @@ -612,28 +612,6 @@ public class TestNativeImageLoader { NativeImageLoader il = new NativeImageLoader(32, 32, 3); - //asMatrix(File, boolean) - INDArray a_nchw = il.asMatrix(f); - INDArray a_nchw2 = il.asMatrix(f, true); - INDArray a_nhwc = il.asMatrix(f, false); - - assertEquals(a_nchw, a_nchw2); - assertEquals(a_nchw, a_nhwc.permute(0,3,1,2)); - - - //asMatrix(InputStream, boolean) - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - a_nchw = il.asMatrix(is); - } - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - a_nchw2 = il.asMatrix(is, true); - } - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - a_nhwc = il.asMatrix(is, false); - } - assertEquals(a_nchw, a_nchw2); - assertEquals(a_nchw, a_nhwc.permute(0,3,1,2)); - //asImageMatrix(File, boolean) Image i_nchw = il.asImageMatrix(f); @@ -642,20 +620,6 @@ public class TestNativeImageLoader { assertEquals(i_nchw.getImage(), i_nchw2.getImage()); assertEquals(i_nchw.getImage(), i_nhwc.getImage().permute(0,3,1,2)); //NHWC to NCHW - - - //asImageMatrix(InputStream, boolean) - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - i_nchw = il.asImageMatrix(is); - } - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - i_nchw2 = il.asImageMatrix(is, true); - } - try(InputStream is = new BufferedInputStream(new FileInputStream(f))){ - i_nhwc = il.asImageMatrix(is, false); - } - assertEquals(i_nchw.getImage(), i_nchw2.getImage()); - assertEquals(i_nchw.getImage(), i_nhwc.getImage().permute(0,3,1,2)); //NHWC to NCHW } }