[Resolved]Convert 5D numpy array with dtype('O) to a 5D Tensor

Hi all,

So I’ve tried this but the dtype of img_patches is still ‘O’ which can not do the conversion.

for i in xrange(len(img_patches)):
for j in xrange(len(img_patches[i])):
img_patches[i][j].astype(float)

Thanks so much in advance!

Simply doing img_patches.astype(float) does not work?

Thank you for the help!

Yeah I 've tried that but then I got this error:

Traceback (most recent call last):
File “<pyshell#4>”, line 1, in
img_patches.astype(float)
ValueError: setting an array element with a sequence.

So it seems that I have to loop over the items in the “img_patches” to do the conversion as it somehow supports 3D array conversion but not 4D or 5D.

But I want the whole 5D array to be a tensor so that they can be a batch of inputs for the network.

PS: DataLoader does not support my data format.

I can reproduce your error with this:

u = np.array([[1,2], [2, 3, 4]])
u.astype(float)

it looks you are creating an array of vectors with different sizes. So your objects (‘o’) you are trying to convert into float are probably python sequences.

1 Like

thank you so much for pointing that out! I’m a newbie for python and pytorch and my dataset is too large that I didn’t detect such errors.
But I’m still struggling with this because I couldn’t exclude those data with different size otherwise the results will probably be biased…

Never mind. Solved it! Thank you so much!!

1 Like

Hi ! I have the same problem… how did you solve it ?