sriram anush
sriram anush

Reputation: 115

How to convert image tensor in to numpy array in tensorflow?

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?

Upvotes: 1

Views: 4401

Answers (1)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11218

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:]))

Upvotes: 3

Related Questions