Skip to content

Re. Height and Width of image, mask or masks should be equal. You can disable shapes check by setting a parameter is_check_shapes=False of Compose class #162

Open
@andysingal

Description

@andysingal

Hi,

import random
import cv2
from matplotlib import pyplot as plt
import matplotlib.patches as patches
import numpy as np
import albumentations as A


def visualize(image):
    plt.figure(figsize=(10, 10))
    plt.axis("off")
    plt.imshow(image)
    plt.show()


def plot_examples(images, bboxes=None):
    fig = plt.figure(figsize=(15, 15))
    columns = 4
    rows = 5

    for i in range(1, len(images)):
        if bboxes is not None:
            img = visualize_bbox(images[i - 1], bboxes[i - 1], class_name="Elon")
        else:
            img = images[i - 1]
        fig.add_subplot(rows, columns, i)
        plt.imshow(img)
    plt.show()


# From https://albumentations.ai/docs/examples/example_bboxes/
def visualize_bbox(img, bbox, class_name, color=(255, 0, 0), thickness=5):
    """Visualizes a single bounding box on the image"""
    x_min, y_min, x_max, y_max = map(int, bbox)
    cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, thickness)
    return img
import cv2
import albumentations as A
import numpy as np
# from utils import plot_examples
from PIL import Image

image = Image.open("/content/Machine-Learning-Collection/ML/Pytorch/Basics/albumentations_tutorial/images/elon.jpeg")
mask = Image.open("/content/Machine-Learning-Collection/ML/Pytorch/Basics/albumentations_tutorial/images/mask.jpeg")
mask2 = Image.open("/content/Machine-Learning-Collection/ML/Pytorch/Basics/albumentations_tutorial/images/second_mask.jpeg")

transform = A.Compose(
    [
        A.Resize(width=1920, height=1080),
        A.RandomCrop(width=1280, height=720),
        A.Rotate(limit=40, p=0.9, border_mode=cv2.BORDER_CONSTANT),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.1),
        A.RGBShift(r_shift_limit=25, g_shift_limit=25, b_shift_limit=25, p=0.9),
        A.OneOf([
            A.Blur(blur_limit=3, p=0.5),
            A.ColorJitter(p=0.5),
        ], p=1.0),
    ]
)

images_list = [image]
image = np.array(image)
mask = np.array(mask) # np.asarray(mask), np.array(mask)
mask2 = np.array(mask2)
for i in range(4):
    augmentations = transform(image=image, masks=[mask, mask2])
    augmented_img = augmentations["image"]
    augmented_masks = augmentations["masks"]
    images_list.append(augmented_img)
    images_list.append(augmented_masks[0])
    images_list.append(augmented_masks[1])
plot_examples(images_list)

which gives error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-6-a1bcbb09e6b0>](https://localhost:8080/#) in <cell line: 30>()
     29 mask2 = np.array(mask2)
     30 for i in range(4):
---> 31     augmentations = transform(image=image, masks=[mask, mask2])
     32     augmented_img = augmentations["image"]
     33     augmented_masks = augmentations["masks"]

1 frames
[/usr/local/lib/python3.10/dist-packages/albumentations/core/composition.py](https://localhost:8080/#) in _check_args(self, **kwargs)
    284 
    285         if self.is_check_shapes and shapes and shapes.count(shapes[0]) != len(shapes):
--> 286             raise ValueError(
    287                 "Height and Width of image, mask or masks should be equal. You can disable shapes check "
    288                 "by setting a parameter is_check_shapes=False of Compose class (do it only if you are sure "

ValueError: Height and Width of image, mask or masks should be equal. You can disable shapes check by setting a parameter is_check_shapes=False of Compose class (do it only if you are sure about your data consistency).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions