1

I am training a CNN to generate images. The type of all the images are tensors. I want them to be converted into numpy arrays then I can process them using opencv.

I know about the .numpy() method, it converts my tensor into an numpy array but the shape is still tensor. I can't get it to work in cv2.

Here is my code:

p=model_(x) 
s=p.numpy()
print(s.shape)
cv2.imwrite("hello.jpg",s)

(1, 183, 275, 3), this is the shape of the array generated using .numpy(), how can I change its shape to retain output image?

1 Answer 1

3

You need to get rid of the first dim (batch), just use slicing with reshape.

s=p.numpy()
print(s.shape)
cv2.imwrite("hello.jpg",s.reshape(s.shape[1:]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.