deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.07k stars 648 forks source link

How to see a preprocessed image (from translator)? #1432

Closed rebelate closed 2 years ago

rebelate commented 2 years ago

I want to take a peek on how an image looks like after being preprocessed from a translator eg:

val translator: Translator<Image, Classifications> = ImageClassificationTranslator.builder()
        .addTransform(Crop(933, 439, 1593, 843))
        .addTransform(CenterCrop(124, 124))
        .addTransform(ToTensor())
        .addTransform(Normalize(floatArrayOf(0.485f, 0.456f, 0.406f), floatArrayOf(0.229f, 0.224f, 0.225f)))
        .optApplySoftmax(true)
        .build()

https://stackoverflow.com/questions/53623472/how-do-i-display-a-single-image-in-pytorch/55196345

frankfliu commented 2 years ago

@rebelate

DJL uses Image class to represent a image object, it wraps BufferedImage or Bitmap (for Android). Using DJL's built-in pre-processing pipeline, you can convert the image into a NDArray object. You can convert NDArray back to Image if want to display the processed image:

        Path imageFile = Paths.get("my_image.jpg");
        ImageFactory factory = ImageFactory.getInstance();
        Image img = factory.fromFile(imageFile);

        try (NDManager manager = NDManager.newBaseManager()) {
            NDArray array = img.toNDArray(manager);
            Pipeline pipeline = new Pipeline();
            pipeline.add(new Crop(1, 1, 300, 300))
                    .add(new CenterCrop(224, 224))
                    .add(new ToTensor())
                    .add(new Normalize(new float[]{0.485f, 0.456f, 0.406f), new float[]{
                            0.229f, 0.224f, 0.225f
                    });
            NDArray output = pipeline.transform(new NDList(array)).head();
            Image result = factory.fromNDArray(output);

            // You can save your processed image into a file:
            result.save(Files.newOutputStream(Paths.get("output.png")), "png");

            // You can extract Java BufferedImage and display it Jupyter notebook or using AWT
            BufferedImage bufferedImage = (BufferedImage)result.getWrappedImage();
        }