'UNET- resize image to size (256,256,1); could not broadcast input array from shape (256,256) into shape (256,256,1)

  1. I'm trying to resize these images which are gray scaled images(img) and mask images (mask) to input all the images into a Neural network model.

  2. I'm facing this error could not broadcast input array from shape (256,256) into shape (256,256,1)

  3. This training path(train_paths) has 15 images and their masks

    for epoch in range(nepochs):
        shuffle(train_paths) #shuffle the training paths to avoid overfitting
        out_imgs= np.zeros((15,)+(256,256)+(1,)) #define the input images for the model
        out_masks= np.zeros((15,)+(256,256)+(1,)) #define the input masks for the model
        for i, img_mask_path in enumerate(train_paths):
            img, mask = img_mask_path[i][0],img_mask_path[i][1]
            #out_imgs = np.zeros((256,256))  
            img = cv2.resize(img, (256,256))
            mask= cv2.resize(mask, (256,256))
            #img.ravel()
            out_imgs.reshape((15,256,256,1)).ravel()
            out_imgs[i,...]= img #create single array of images
            out_masks[i,...]= mask #create single array of masks
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source