Enrollments Open for **Online Instructor-Led Training** for Big Data Engineering and Machine Learning | Use Coupon **END15** for Extra 15% Off

- Home
- Assessment

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.

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)`

XP

Checking Please wait.

Success

Error

No hints are availble for this assesment

Answer is not availble for this assesment

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

## Loading comments...