Project - Introduction to Neural Style Transfer using Deep Learning & TensorFlow 2 (Art Generation Project)

16 / 18

Converting Tensor to Image

  • Let us define a function tensor_to_image to convert the input tensor to an image format.

  • We do that as follows:

    • Make the pixel values from [0 , 1] to [0, 255].

    • Convert the pixels from float type to int type.

    • Get the first item(the image with 3 channels) if the tensor shape is greater than 3. In our exercise, the input tensor will be 4, where the first dimension is always 1. It is so because some of the functions we are using will be expecting the input tensors to be of size 4, for processing purposes.

    • Use PIL.Image.fromarray(tensor) to convert the tensor to image.

INSTRUCTIONS
  • Use the below code:

    def tensor_to_image(tensor):
        tensor = tensor*255
        tensor = np.array(tensor, dtype=np.uint8)
        if np.ndim(tensor)>3:
            assert tensor.shape[0] == 1
            tensor = tensor[0]
        return PIL.Image.fromarray(tensor)
    
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...