ValueError: operands could not be broadcast together with shapes (624,605) (624,605,43)

what is causing this error ?

add_elastic_transform(image, alpha, sigma, pad_size, seed)
     29 
     30     x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
---> 31     indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1))
     32     return cropping(map_coordinates(image, indices, order=1).reshape(shape), 512, pad_size, pad_size), seed
    

is it because of RGB channels ?

It seems numpy throws the error, if one dimension is missing.
Try to add the missing dim2 to x or y.

Thank you , i added the Z dim too but my images is RGB and thier labels is gray scales, when I apply add_elastic_transform it gives me error, should I change the images to gray scale or should I add else in add_elastic_transform for just x,y dim if so how can I add it .

I’m not sure what your code is exactly doing, but I think the addition is throwing this error:

a = np.random.randn(2, 3)
b = np.random.randn(2, 3, 4)

try:
    a + b
except ValueError as e:
    print(e)

a[:, :, None] + b  # works

If you would like to broadcast x or y before the addition, just use the last line of code in my snippet.

my code is like this

def add_elastic_transform(image, alpha, sigma, pad_size=30, seed=None):
    """
    Args:
        image : numpy array of image
        alpha : α is a scaling factor
        sigma :  σ is an elasticity coefficient
        random_state = random integer
        Return :
        image : elastically transformed numpy array of image
    """
    image_size = int(image.shape[0])
    image = np.pad(image, pad_size, mode="symmetric")
    if seed is None:
        seed = randint(1, 100)
        random_state = np.random.RandomState(seed)
    else:
        random_state = np.random.RandomState(seed)
    shape = image.shape
    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))

    indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))


    return cropping(map_coordinates(image, indices, order=1).reshape(shape), 512, pad_size, pad_size), seed

``
it does the add_elastic_transform which is a preprocessing step, but the code gave the error 'IndexError: tuple index out of range' because the images are RGB and the labels are gray scale

Your code works for me, if I remove cropping since it is undefined and I’m not sure where to import it from:

image = np.random.randn(24, 24, 3)
add_elastic_transform(image, 0.9, 0.9)
bs = 2
num_epochs = 100
learning_rate = 1e-3
mom  = 0.9
class SEMDataTrain(data.Dataset):

    def __init__(self, image_path, mask_path, in_size=512, out_size=512 ,transforms = None):
        """
        Args:
            image_path (str): the path where the image is located
            mask_path (str): the path where the mask is located
            option (str): decide which dataset to import
        """
        # all file names
        self.mask_path = mask_path
        self.image_path = image_path
        self.mask_arr = os.listdir(mask_path)
        self.image_arr = os.listdir(image_path)
        self.in_size, self.out_size = in_size, out_size
        # Calculate len
        self.data_len = len(self.mask_arr)
    
    def transform(self, img_as_img, msk_as_img):
        # Resize
        resize = transforms.Resize(size=(256, 256))
        msk_as_img = resize(img_as_img)
        msk_as_img = resize(msk_as_img)
         
    
    def __getitem__(self,idx):
        """Get specific data corresponding to the index
        Args:
            index (int): index of the data
        Returns:
            Tensor: specific data on index which is converted to Tensor
        """
        """
        # GET IMAGE
        """
        single_image_name = self.image_arr[idx]
        img_as_img = Image.open(os.path.join(self.image_path,single_image_name))
        # img_as_img.show()
        img_as_np = np.asarray(img_as_img)
        mg_as_img = Image.fromarray(img_as_np)
        img_as_img = img_as_img.convert('L')
        plt.imshow(img_as_img)
        plt.show()
        
        # Augmentation
        # flip {0: vertical, 1: horizontal, 2: both, 3: none}
        flip_num = randint(0, 3)
        img_as_np = flip(img_as_np, flip_num)

        # Noise Determine {0: Gaussian_noise, 1: uniform_noise
        if randint(0, 1):
            # Gaussian_noise
            gaus_sd, gaus_mean = randint(0, 20), 0
            img_as_np = add_gaussian_noise(img_as_np, gaus_mean, gaus_sd)
        else:
            # uniform_noise
            l_bound, u_bound = randint(-20, 0), randint(0, 20)
            img_as_np = add_uniform_noise(img_as_np, l_bound, u_bound)

        # Brightness
        pix_add = randint(-20, 20)
        img_as_np = change_brightness(img_as_np, pix_add)

        # Elastic distort {0: distort, 1:no distort}
        sigma = randint(6, 12)
        # sigma = 4, alpha = 34
        img_as_np, seed = add_elastic_transform(img_as_np, alpha=34, sigma=sigma, pad_size=20)

        # Crop the image
        img_height, img_width = img_as_np.shape[0], img_as_np.shape[1]
        pad_size = int((self.in_size - self.out_size)/2)
        img_as_np = np.pad(img_as_np, pad_size, mode="symmetric")
        y_loc, x_loc = randint(0, img_height-self.out_size), randint(0, img_width-self.out_size)
        img_as_np = cropping(img_as_np, crop_size=self.in_size, dim1=y_loc, dim2=x_loc)
        '''
        # Sanity Check for image
        img1 = Image.fromarray(img_as_np)
        img1.show()
        '''
        # Normalize the image
        img_as_np = normalization2(img_as_np, max=1, min=0)
        img_as_np = np.expand_dims(img_as_np, axis=0)  # add additional dimension
        img_as_tensor = torch.from_numpy(img_as_np).float()  # Convert numpy array to tensor
        
        

        """
        # GET MASK
        """
        single_mask_name = self.mask_arr[idx]
        msk_as_img = Image.open(os.path.join(self.mask_path,single_mask_name))
        # msk_as_img.show()
        msk_as_np = np.asarray(msk_as_img)

        # flip the mask with respect to image
        msk_as_np = flip(msk_as_np, flip_num)

        # elastic_transform of mask with respect to image

        # sigma = 4, alpha = 34, seed = from image transformation
        msk_as_np, _ = add_elastic_transform(
            msk_as_np, alpha=34, sigma=sigma, seed=seed, pad_size=20)
        msk_as_np = approximate_image(msk_as_np)
        

        # Crop the mask
        msk_as_np = cropping(msk_as_np, crop_size=self.out_size, dim1=y_loc, dim2=x_loc)
        '''
         Sanity Check for mask
        img2 = Image.fromarray(msk_as_np)
        img2.show()
        '''

        # Normalize mask to only 0 and 1
        msk_as_np = msk_as_np/255
        # msk_as_np = np.expand_dims(msk_as_np, axis=0)  # add additional dimension
        msk_as_tensor = torch.from_numpy(msk_as_np).long()  # Convert numpy array to tensor
        

        return (img_as_tensor, msk_as_tensor)
    
         
    def __len__(self):
        """
        Returns:
            length (int): length of the data
            
        """
        return self.data_len

yes it works if with RGB images but sicnce the labels are gray scale it is not work

The code also works with:

image = np.random.randn(24, 24, 1)
add_elastic_transform(image, 0.9, 0.9)

Could it be that the channel dimension is missing for your grayscale images?

1 Like