Repository: deepjavalibrary/djl
Pull request #1814 (2022-07-20 03:36:04)

[api] Allows convert float32 NDArray to Image.
Change-Id: I5b40ff44af3bd11c80f56d20f2046cf2ed52ee79

## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here


codecov-commenter: # [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
> Merging [#1814](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (3003140) into [master](https://codecov.io/gh/deepjavalibrary/djl/commit/bb5073f7ffc33f1883621fc1783dbb01e70b418f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (bb5073f) will **decrease** coverage by `2.41%`.
> The diff coverage is `65.09%`.

```diff
@@             Coverage Diff              @@
##             master    #1814      +/-   ##
============================================
- Coverage     72.08%   69.67%   -2.42%     
- Complexity     5126     5506     +380     
============================================
  Files           473      524      +51     
  Lines         21970    24339    +2369     
  Branches       2351     2652     +301     
============================================
+ Hits          15838    16957    +1119     
- Misses         4925     6073    +1148     
- Partials       1207     1309     +102     
```


| [Impacted Files](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | Coverage Δ | |
|---|---|---|
| [api/src/main/java/ai/djl/modality/cv/Image.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L0ltYWdlLmphdmE=) | `69.23% <ø> (-4.11%)` | :arrow_down: |
| [...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L011bHRpQm94UHJpb3IuamF2YQ==) | `76.00% <ø> (ø)` | |
| [...rc/main/java/ai/djl/modality/cv/output/Joints.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9Kb2ludHMuamF2YQ==) | `71.42% <ø> (ø)` | |
| [.../main/java/ai/djl/modality/cv/output/Landmark.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9MYW5kbWFyay5qYXZh) | `100.00% <ø> (ø)` | |
| [...main/java/ai/djl/modality/cv/output/Rectangle.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9SZWN0YW5nbGUuamF2YQ==) | `72.41% <0.00%> (ø)` | |
| [...i/djl/modality/cv/translator/BigGANTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvci5qYXZh) | `21.42% <0.00%> (-5.24%)` | :arrow_down: |
| [...odality/cv/translator/BigGANTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvckZhY3RvcnkuamF2YQ==) | `33.33% <0.00%> (+8.33%)` | :arrow_up: |
| [...nslator/InstanceSegmentationTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvSW5zdGFuY2VTZWdtZW50YXRpb25UcmFuc2xhdG9yRmFjdG9yeS5qYXZh) | `14.28% <0.00%> (-3.90%)` | :arrow_down: |
| [.../cv/translator/SemanticSegmentationTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvU2VtYW50aWNTZWdtZW50YXRpb25UcmFuc2xhdG9yLmphdmE=) | `0.00% <0.00%> (ø)` | |
| [.../cv/translator/StyleTransferTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvU3R5bGVUcmFuc2ZlclRyYW5zbGF0b3JGYWN0b3J5LmphdmE=) | `40.00% <ø> (ø)` | |
| ... and [416 more](https://codecov.io/gh/deepjavalibrary/djl/pull/1814/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Last update [ac56f58...3003140](https://codecov.io/gh/deepjavalibrary/djl/pull/1814?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).


Patch:
diff --git a/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java b/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java
index 9d78fa787..573fef16a 100644
--- a/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java
+++ b/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java
@@ -35,6 +35,7 @@ import ai.djl.modality.cv.output.DetectedObjects;
 import ai.djl.modality.cv.output.Joints;
 import ai.djl.modality.cv.output.Mask;
 import ai.djl.modality.cv.output.Rectangle;
+import ai.djl.modality.cv.util.NDImageUtils;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDManager;
 import ai.djl.ndarray.types.DataType;
@@ -88,24 +89,18 @@ public class BitmapImageFactory extends ImageFactory {
         if (shape.dimension() != 3) {
             throw new IllegalArgumentException("Shape should only have three dimension follow CHW");
         }
-        if (array.getDataType() != DataType.UINT8 && array.getDataType() != DataType.INT8) {
-            throw new IllegalArgumentException("Datatype should be INT8");
-        }
         if (shape.get(0) == 1) {
             throw new UnsupportedOperationException("Grayscale image is not supported");
-        } else if (shape.get(0) != 3) {
-            if (shape.get(2) == 3) {
-                array = array.transpose(2, 0, 1);
-                shape = array.getShape();
-            } else {
-                throw new IllegalArgumentException("First or last dimension should be number of channel with value 1 or 3");
-            }
+        }
+        if (NDImageUtils.isCHW(shape)) {
+            array = array.transpose(2, 0, 1);
+            shape = array.getShape();
         }
         int height = (int) shape.get(1);
         int width = (int) shape.get(2);
         int imageArea = width * height;
         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
-        int[] raw = array.toUint8Array();
+        int[] raw = array.toType(DataType.UINT8, false).toUint8Array();
         IntStream.range(0, imageArea).parallel().forEach(ele -> {
             int x = ele % width;
             int y = ele / width;
diff --git a/api/src/main/java/ai/djl/modality/cv/BufferedImageFactory.java b/api/src/main/java/ai/djl/modality/cv/BufferedImageFactory.java
index a39e575c3..9a59979c8 100644
--- a/api/src/main/java/ai/djl/modality/cv/BufferedImageFactory.java
+++ b/api/src/main/java/ai/djl/modality/cv/BufferedImageFactory.java
@@ -90,8 +90,6 @@ public class BufferedImageFactory extends ImageFactory {
             throw new UnsupportedOperationException("Batch is not supported");
         } else if (shape.get(0) == 1 || shape.get(2) == 1) {
             throw new UnsupportedOperationException("Grayscale image is not supported");
-        } else if (array.getDataType() != DataType.UINT8 && array.getDataType() != DataType.INT8) {
-            throw new IllegalArgumentException("Datatype should be INT8 or UINT8");
         }
         int height = (int) shape.get(0);
         int width = (int) shape.get(1);
@@ -101,7 +99,7 @@ public class BufferedImageFactory extends ImageFactory {
         }
         int imageArea = width * height;
         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-        int[] raw = array.toUint8Array();
+        int[] raw = array.toType(DataType.UINT8, false).toUint8Array();
         int finalWidth = width;
         IntStream.range(0, imageArea)
                 .forEach(
diff --git a/api/src/main/java/ai/djl/modality/cv/translator/BigGANTranslator.java b/api/src/main/java/ai/djl/modality/cv/translator/BigGANTranslator.java
index 58de72190..39159f7fc 100644
--- a/api/src/main/java/ai/djl/modality/cv/translator/BigGANTranslator.java
+++ b/api/src/main/java/ai/djl/modality/cv/translator/BigGANTranslator.java
@@ -17,7 +17,6 @@ import ai.djl.modality.cv.ImageFactory;
 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.Shape;
 import ai.djl.translate.NoBatchifyTranslator;
 import ai.djl.translate.TranslatorContext;
@@ -41,7 +40,7 @@ public final class BigGANTranslator implements NoBatchifyTranslator<int[], Image
     /** {@inheritDoc} */
     @Override
     public Image[] processOutput(TranslatorContext ctx, NDList list) {
-        NDArray output = list.get(0).addi(1).muli(128).clip(0, 255).toType(DataType.UINT8, false);
+        NDArray output = list.get(0).addi(1).muli(128).clip(0, 255);
 
         int sampleSize = (int) output.getShape().get(0);
         Image[] images = new Image[sampleSize];
diff --git a/api/src/main/java/ai/djl/modality/cv/translator/SemanticSegmentationTranslator.java b/api/src/main/java/ai/djl/modality/cv/translator/SemanticSegmentationTranslator.java
index 0cda70c01..65d7ce624 100644
--- a/api/src/main/java/ai/djl/modality/cv/translator/SemanticSegmentationTranslator.java
+++ b/api/src/main/java/ai/djl/modality/cv/translator/SemanticSegmentationTranslator.java
@@ -113,9 +113,8 @@ public class SemanticSegmentationTranslator extends BaseImageTranslator<Image> {
             NDArray fullImage =
                     manager.create(bb, new Shape(height, width, CHANNEL), DataType.UINT8);
             NDArray resized = NDImageUtils.resize(fullImage, originW, originH);
-            NDArray ret = resized.toType(DataType.UINT8, false);
 
-            return ImageFactory.getInstance().fromNDArray(ret);
+            return ImageFactory.getInstance().fromNDArray(resized);
         }
     }
 
diff --git a/api/src/main/java/ai/djl/modality/cv/translator/StyleTransferTranslator.java b/api/src/main/java/ai/djl/modality/cv/translator/StyleTransferTranslator.java
index 7ad058af1..8c1edd3e2 100644
--- a/api/src/main/java/ai/djl/modality/cv/translator/StyleTransferTranslator.java
+++ b/api/src/main/java/ai/djl/modality/cv/translator/StyleTransferTranslator.java
@@ -34,7 +34,7 @@ public class StyleTransferTranslator implements NoBatchifyTranslator<Image, Imag
     /** {@inheritDoc} */
     @Override
     public Image processOutput(TranslatorContext ctx, NDList list) {
-        NDArray output = list.get(0).addi(1).muli(128).toType(DataType.UINT8, false);
+        NDArray output = list.get(0).addi(1).muli(128);
         return ImageFactory.getInstance().fromNDArray(output.squeeze());
     }
 
diff --git a/engines/paddlepaddle/paddlepaddle-model-zoo/src/main/java/ai/djl/paddlepaddle/zoo/cv/objectdetection/PpWordDetectionTranslator.java b/engines/paddlepaddle/paddlepaddle-model-zoo/src/main/java/ai/djl/paddlepaddle/zoo/cv/objectdetection/PpWordDetectionTranslator.java
index 6cda53433..1761ec946 100644
--- a/engines/paddlepaddle/paddlepaddle-model-zoo/src/main/java/ai/djl/paddlepaddle/zoo/cv/objectdetection/PpWordDetectionTranslator.java
+++ b/engines/paddlepaddle/paddlepaddle-model-zoo/src/main/java/ai/djl/paddlepaddle/zoo/cv/objectdetection/PpWordDetectionTranslator.java
@@ -58,7 +58,7 @@ public class PpWordDetectionTranslator implements NoBatchifyTranslator<Image, De
         List<BoundingBox> boxes;
         // faster mechanism
         if ("ai.djl.opencv.OpenCVImageFactory".equals(factory.getClass().getName())) {
-            result = result.squeeze(0).toType(DataType.UINT8, true);
+            result = result.squeeze(0);
             Image image = factory.fromNDArray(result);
             boxes =
                     image.findBoundingBoxes().parallelStream()
diff --git a/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolution.java b/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolution.java
index 81e75b8ce..80e04ff19 100644
--- a/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolution.java
+++ b/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolution.java
@@ -95,7 +95,6 @@ public final class SuperResolution {
             }
 
             return stitches.stream()
-                    .map(array -> array.toType(DataType.UINT8, false))
                     .map(array -> ImageFactory.getInstance().fromNDArray(array))
                     .collect(Collectors.toList());
         }
diff --git a/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolutionTranslator.java b/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolutionTranslator.java
index 2083f1f86..f84d7daac 100644
--- a/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolutionTranslator.java
+++ b/examples/src/main/java/ai/djl/examples/inference/sr/SuperResolutionTranslator.java
@@ -33,7 +33,7 @@ public class SuperResolutionTranslator implements Translator<Image, Image> {
     /** {@inheritDoc} */
     @Override
     public Image processOutput(TranslatorContext ctx, NDList list) {
-        NDArray output = list.get(0).clip(0, 255).toType(DataType.UINT8, false);
+        NDArray output = list.get(0).clip(0, 255);
         return ImageFactory.getInstance().fromNDArray(output.squeeze());
     }
 }
diff --git a/extensions/opencv/src/main/java/ai/djl/opencv/OpenCVImageFactory.java b/extensions/opencv/src/main/java/ai/djl/opencv/OpenCVImageFactory.java
index 0eff732ed..2b5baa3e9 100644
--- a/extensions/opencv/src/main/java/ai/djl/opencv/OpenCVImageFactory.java
+++ b/extensions/opencv/src/main/java/ai/djl/opencv/OpenCVImageFactory.java
@@ -77,9 +77,8 @@ public class OpenCVImageFactory extends ImageFactory {
         Shape shape = array.getShape();
         if (shape.dimension() == 4) {
             throw new UnsupportedOperationException("Batch is not supported");
-        } else if (array.getDataType() != DataType.UINT8 && array.getDataType() != DataType.INT8) {
-            throw new IllegalArgumentException("Datatype should be INT8 or UINT8");
         }
+        array = array.toType(DataType.UINT8, false);
         boolean grayScale = shape.get(0) == 1 || shape.get(2) == 1;
         if (grayScale) {
             // expected CHW


Test patch:
diff --git a/android/core/src/androidTest/java/ai/djl/android/core/BitmapWrapperTest.java b/android/core/src/androidTest/java/ai/djl/android/core/BitmapWrapperTest.java
index db992d2fa..20d42e038 100644
--- a/android/core/src/androidTest/java/ai/djl/android/core/BitmapWrapperTest.java
+++ b/android/core/src/androidTest/java/ai/djl/android/core/BitmapWrapperTest.java
@@ -85,7 +85,7 @@ public class BitmapWrapperTest {
         try (NDManager manager = NDManager.newBaseManager()) {
             NDArray array = manager.arange(0.0f, 12.0f).reshape(3, 2, 2);
             ImageFactory factory = ImageFactory.getInstance();
-            Image image = factory.fromNDArray(array.toType(DataType.INT8, true));
+            Image image = factory.fromNDArray(array);
             NDArray converted = image.toNDArray(manager)
                     .transpose(2, 0, 1)
                     .toType(DataType.FLOAT32, true);
diff --git a/engines/pytorch/pytorch-model-zoo/src/test/resources/mlrepo/model/cv/semantic_segmentation/ai/djl/pytorch/deeplabv3/metadata.json b/engines/pytorch/pytorch-model-zoo/src/test/resources/mlrepo/model/cv/semantic_segmentation/ai/djl/pytorch/deeplabv3/metadata.json
new file mode 100644
index 000000000..31ddf1b81
--- /dev/null
+++ b/engines/pytorch/pytorch-model-zoo/src/test/resources/mlrepo/model/cv/semantic_segmentation/ai/djl/pytorch/deeplabv3/metadata.json
@@ -0,0 +1,41 @@
+{
+  "metadataVersion": "0.1",
+  "resourceType": "model",
+  "application": "cv/semantic_segmentation",
+  "groupId": "ai.djl.pytorch",
+  "artifactId": "deeplabv3",
+  "name": "Semantic segmentation",
+  "description": "Semantic segmentation",
+  "website": "http://www.djl.ai/pytorch/model-zoo",
+  "licenses": {
+    "license": {
+      "name": "Berkeley Software Distribution (BSD) Zero Clause License",
+      "url": "https://opensource.org/licenses/0BSD"
+    }
+  },
+  "artifacts": [
+    {
+      "version": "0.0.1",
+      "snapshot": false,
+      "name": "deeplabv3",
+      "properties": {
+        "backbone": "resnet50",
+        "flavor": "v1b",
+        "dataset": "coco"
+      },
+      "arguments": {
+        "toTensor": true,
+        "normalize": true,
+        "translatorFactory": "ai.djl.modality.cv.translator.SemanticSegmentationTranslatorFactory"
+      },
+      "files": {
+        "model": {
+          "uri": "0.0.1/deeplabv3.zip",
+          "sha1Hash": "06b8471f40d41d5f4fce7736cdd24081b9fa2e8e",
+          "name": "",
+          "size": 155844147
+        }
+      }
+    }
+  ]
+}
diff --git a/extensions/opencv/src/test/java/ai/djl/opencv/OpenCVImageFactoryTest.java b/extensions/opencv/src/test/java/ai/djl/opencv/OpenCVImageFactoryTest.java
index 7809f58e2..917fa088e 100644
--- a/extensions/opencv/src/test/java/ai/djl/opencv/OpenCVImageFactoryTest.java
+++ b/extensions/opencv/src/test/java/ai/djl/opencv/OpenCVImageFactoryTest.java
@@ -23,7 +23,6 @@ import ai.djl.modality.cv.output.Mask;
 import ai.djl.modality.cv.output.Rectangle;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDManager;
-import ai.djl.ndarray.types.DataType;
 import ai.djl.testing.TestRequirements;
 
 import org.testng.Assert;
@@ -144,7 +143,7 @@ public class OpenCVImageFactoryTest {
                         {1, 1, 0, 0, 0},
                         {1, 0, 0, 0, 1}
                     };
-            NDArray array = manager.create(arr).muli(255).toType(DataType.INT8, true).expandDims(0);
+            NDArray array = manager.create(arr).muli(255).expandDims(0);
             OpenCVImage image = (OpenCVImage) factory.fromNDArray(array);
             List<BoundingBox> rectangles = image.findBoundingBoxes();
             List<Rectangle> expected =
diff --git a/integration/src/main/java/ai/djl/integration/tests/modality/cv/BufferedImageFactoryTest.java b/integration/src/main/java/ai/djl/integration/tests/modality/cv/BufferedImageFactoryTest.java
index 7dfabebc4..a01db157f 100644
--- a/integration/src/main/java/ai/djl/integration/tests/modality/cv/BufferedImageFactoryTest.java
+++ b/integration/src/main/java/ai/djl/integration/tests/modality/cv/BufferedImageFactoryTest.java
@@ -43,7 +43,7 @@ public class BufferedImageFactoryTest {
         try (NDManager manager = NDManager.newBaseManager()) {
             NDArray array = manager.arange(0.0f, 12.0f).reshape(3, 2, 2);
             ImageFactory factory = ImageFactory.getInstance();
-            Image image = factory.fromNDArray(array.toType(DataType.INT8, true));
+            Image image = factory.fromNDArray(array);
             NDArray converted =
                     image.toNDArray(manager).transpose(2, 0, 1).toType(DataType.FLOAT32, true);
             Assertions.assertAlmostEquals(array, converted);
diff --git a/integration/src/main/java/ai/djl/integration/tests/modality/cv/NDImageUtilsTest.java b/integration/src/main/java/ai/djl/integration/tests/modality/cv/NDImageUtilsTest.java
index 4f737db35..844d56dfe 100644
--- a/integration/src/main/java/ai/djl/integration/tests/modality/cv/NDImageUtilsTest.java
+++ b/integration/src/main/java/ai/djl/integration/tests/modality/cv/NDImageUtilsTest.java
@@ -17,6 +17,7 @@ import ai.djl.modality.cv.util.NDImageUtils;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDManager;
 import ai.djl.ndarray.index.NDIndex;
+import ai.djl.ndarray.types.DataType;
 import ai.djl.ndarray.types.Shape;
 import ai.djl.testing.Assertions;
 import ai.djl.testing.TestRequirements;
@@ -108,7 +109,7 @@ public class NDImageUtilsTest {
                     continue;
                 }
                 // test 3D H, W, C
-                NDArray image = manager.ones(new Shape(100, 50, 3));
+                NDArray image = manager.ones(new Shape(100, 50, 3), DataType.UINT8);
                 NDArray result = NDImageUtils.resize(image, 50, 50);
                 NDArray expected = manager.ones(new Shape(50, 50, 3));
                 Assertions.assertAlmostEquals(result, expected);
@@ -122,6 +123,7 @@ public class NDImageUtilsTest {
                 expected = manager.ones(new Shape(5, 20, 20, 3));
                 Assertions.assertAlmostEquals(result, expected);
                 result = NDImageUtils.resize(batchImages, 25, 50, interpolation);
+                Assert.assertEquals(result.getDataType(), DataType.FLOAT32);
                 expected = manager.ones(new Shape(5, 50, 25, 3));
                 Assertions.assertAlmostEquals(result, expected);