Path: blob/main/transformers_doc/en/pytorch/image_to_image.ipynb
4542 views
Image-to-Image Task Guide
Image-to-Image task is the task where an application receives an image and outputs another image. This has various subtasks, including image enhancement (super resolution, low light enhancement, deraining and so on), image inpainting, and more.
This guide will show you how to:
Use an image-to-image pipeline for super resolution task,
Run image-to-image models for same task without a pipeline.
Note that as of the time this guide is released, image-to-image
pipeline only supports super resolution task.
Let's begin by installing the necessary libraries.
We can now initialize the pipeline with a Swin2SR model. We can then infer with the pipeline by calling it with an image. As of now, only Swin2SR models are supported in this pipeline.
Now, let's load an image.

We can now do inference with the pipeline. We will get an upscaled version of the cat image.
If you wish to do inference yourself with no pipeline, you can use the Swin2SRForImageSuperResolution
and Swin2SRImageProcessor
classes of transformers. We will use the same model checkpoint for this. Let's initialize the model and the processor.
pipeline
abstracts away the preprocessing and postprocessing steps that we have to do ourselves, so let's preprocess the image. We will pass the image to the processor and then move the pixel values to GPU.
We can now infer the image by passing pixel values to the model.
Output is an object of type ImageSuperResolutionOutput
that looks like below 👇
We need to get the reconstruction
and post-process it for visualization. Let's see how it looks like.
We need to squeeze the output and get rid of axis 0, clip the values, then convert it to be numpy float. Then we will arrange axes to have the shape [1072, 880], and finally, bring the output back to range [0, 255].
