Home > database >  could not broadcast input array from shape (512,512,100) into shape (512,512)
could not broadcast input array from shape (512,512,100) into shape (512,512)

Time:11-15

I am trying to use the following code for data generator to work with brain vessel defects segmentation. I have generated npy files for nifiti files. each npy files different dimensions [512,512,140] ,[560,560,141]. I am using the following code:

def load_img(img_dir, img_list):
    images=[]
    for i, image_name in enumerate(img_list):    
        if (image_name.split('.')[1] == 'npy'):
            
            image = np.load(img_dir image_name)
                      
            images.append(image)
    images = np.array(images)
    
    return(images)




def imageLoader(img_dir, img_list, mask_dir, mask_list, batch_size):

    L = len(img_list)

    #keras needs the generator infinite, so we will use while true  
    while True:

        batch_start = 0
        batch_end = batch_size

        while batch_start < L:
            limit = min(batch_end, L)
                       
            X = load_img(img_dir, img_list[batch_start:limit])
            Y = load_img(mask_dir, mask_list[batch_start:limit])

            yield (X,Y) #a tuple with two numpy arrays with batch_size samples     

            batch_start  = batch_size   
            batch_end  = batch_size

############################################

#Test the generator

from matplotlib import pyplot as plt
import random

train_img_dir = "/content/drive/MyDrive/input_data_512/train/images/"
train_mask_dir = "/content/drive/MyDrive/input_data_512/train/masks/"
train_img_list=os.listdir(train_img_dir)
train_mask_list = os.listdir(train_mask_dir)

batch_size = 2

train_img_datagen = imageLoader(train_img_dir, train_img_list, 
                                train_mask_dir, train_mask_list, batch_size)

The error:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested 
 sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated.    
 If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  del sys.path[0]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ac15ea7a12c9> in <module>()
     57 
     58 #Verify generator.... In python 3 next() is renamed as __next__()
---> 59 img, msk = train_img_datagen.__next__()
     60 
     61 

1 frames
<ipython-input-6-ac15ea7a12c9> in load_img(img_dir, img_list)
     11 
     12             images.append(image)
---> 13     images = np.array(images)
     14 
     15     return(images)

ValueError: could not broadcast input array from shape (512,512,100) into shape (512,512)

CodePudding user response:

Your problem is with the line images = np.array(images)

The input is a list of differently shaped images. You try to convert it into a single higher-dimensional array. This cannot work. So what do you want to achieve?

From the looks of it, your input has shape (512, 512, 100) and (512, 512). What output shape do you want? Where should the pixels go? What you told numpy to do is create shape (2, 512, 512) but that obviously doesn't work.

What you could do is create (512, 512, 101). If that is desired, replace the offending line with images = np.dstack(images)

CodePudding user response:

that worked thanks. However when I try to plot them I am getting this error: too many indices for array: array is 2-dimensional, but 3 were indexed

img, msk = train_img_datagen.__next__()
img_num = random.randint(0,img.shape[0]-1)
test_img=img[img_num]
test_mask=msk[img_num]
test_mask=np.argmax(test_mask, axis=2)


n_slice=random.randint(0, test_mask.shape[0])
plt.figure(figsize=(12, 8))

plt.subplot(221)
plt.imshow(test_img[:,:,n_slice], cmap='gray')
plt.title('Image flair')
plt.subplot(222)
plt.imshow(test_mask[:,:,n_slice],cmap='gray')
plt.title('Mask')
plt.show()

if I change the axis to 3 I get: axis 3 is out of bounds for array of dimension 3

  • Related