Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

PYTHON: How to resolve inhomogenous shape?

$
0
0

I am attempt to save data in a *.npy file, using np.save.But encounter this error: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (200, 2) + inhomogeneous part.

Concerned codelines are:

dataset = []#within some loop:   img = cv2.imread(image_file,1)   img = cv2.resize(img,(224,224))   img = img.astype(np.float32)   locations = [value1, value2, value3, value4] #(1x4) vector of integer values   dataset.append(np.array(img), np.array(locations), predicted_class, filename) #predicted_class is an integer and filename is a string\`

Basically, the code iterates through a collections of images and performs some operations on such images that outputs the (1x4) vector. The image and its operated-output are then added to the dataset.

I inspected the dataset, it has the correct number of processed images (len(dataset)) and it has the correct structure:

[ array([[[#,#,#],...,[#,#,#]]], dtype=float32), array([#,#,#,#]), #, 'filename.jpg']

But when I attempt to save it

np.save('dataset.npy',dataset)

I encounter the error:

ValueError                                Traceback (most recent call last)<ipython-input-3-1b033c5c2e47> in <cell line: 4>()      2 print(dataset[0])      3 os.chdir('/content/drive/My Drive/Misc/')----> 4 np.save('dataset.npy', dataset)/usr/local/lib/python3.10/dist-packages/numpy/lib/npyio.py in save(file, arr, allow_pickle, fix_imports)    543     544     with file_ctx as fid:--> 545         arr = np.asanyarray(arr)    546         format.write_array(fid, arr, allow_pickle=allow_pickle,    547                            pickle_kwargs=dict(fix_imports=fix_imports))ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (200, 2) + inhomogeneous part.

Any suggestion for how this might be resolved?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>