Skip to content

ChannelDropout augmentation (augmentations.dropout.channel_dropout)

class ChannelDropout (channel_drop_range=(1, 1), fill_value=0, always_apply=None, p=0.5) [view source on GitHub] ΒΆ

Randomly Drop Channels in the input Image.

Parameters:

Name Type Description
channel_drop_range int, int

range from which we choose the number of channels to drop.

fill_value int, float

pixel value for the dropped channel.

p float

probability of applying the transform. Default: 0.5.

Targets

image

Image types: uint8, uint16, unit32, float32

Interactive Tool Available!

Explore this transform visually and adjust parameters interactively using this tool:

Open Tool

Source code in albumentations/augmentations/dropout/channel_dropout.py
Python
class ChannelDropout(ImageOnlyTransform):
    """Randomly Drop Channels in the input Image.

    Args:
        channel_drop_range (int, int): range from which we choose the number of channels to drop.
        fill_value (int, float): pixel value for the dropped channel.
        p (float): probability of applying the transform. Default: 0.5.

    Targets:
        image

    Image types:
        uint8, uint16, unit32, float32

    """

    class InitSchema(BaseTransformInitSchema):
        channel_drop_range: OnePlusIntRangeType = (1, 1)
        fill_value: Annotated[ColorType, Field(description="Pixel value for the dropped channel.")]

    def __init__(
        self,
        channel_drop_range: tuple[int, int] = (1, 1),
        fill_value: float = 0,
        always_apply: bool | None = None,
        p: float = 0.5,
    ):
        super().__init__(p=p, always_apply=always_apply)

        self.channel_drop_range = channel_drop_range
        self.fill_value = fill_value

    def apply(self, img: np.ndarray, channels_to_drop: tuple[int, ...], **params: Any) -> np.ndarray:
        return channel_dropout(img, channels_to_drop, self.fill_value)

    def get_params_dependent_on_data(self, params: Mapping[str, Any], data: Mapping[str, Any]) -> dict[str, Any]:
        image = data["image"] if "image" in data else data["images"][0]
        num_channels = get_num_channels(image)

        if num_channels == 1:
            msg = "Images has one channel. ChannelDropout is not defined."
            raise NotImplementedError(msg)

        if self.channel_drop_range[1] >= num_channels:
            msg = "Can not drop all channels in ChannelDropout."
            raise ValueError(msg)

        num_drop_channels = random.randint(self.channel_drop_range[0], self.channel_drop_range[1])

        channels_to_drop = random.sample(range(num_channels), k=num_drop_channels)

        return {"channels_to_drop": channels_to_drop}

    def get_transform_init_args_names(self) -> tuple[str, ...]:
        return "channel_drop_range", "fill_value"

apply (self, img, channels_to_drop, **params)

Apply transform on image.

Source code in albumentations/augmentations/dropout/channel_dropout.py
Python
def apply(self, img: np.ndarray, channels_to_drop: tuple[int, ...], **params: Any) -> np.ndarray:
    return channel_dropout(img, channels_to_drop, self.fill_value)

get_params_dependent_on_data (self, params, data)

Returns parameters dependent on input.

Source code in albumentations/augmentations/dropout/channel_dropout.py
Python
def get_params_dependent_on_data(self, params: Mapping[str, Any], data: Mapping[str, Any]) -> dict[str, Any]:
    image = data["image"] if "image" in data else data["images"][0]
    num_channels = get_num_channels(image)

    if num_channels == 1:
        msg = "Images has one channel. ChannelDropout is not defined."
        raise NotImplementedError(msg)

    if self.channel_drop_range[1] >= num_channels:
        msg = "Can not drop all channels in ChannelDropout."
        raise ValueError(msg)

    num_drop_channels = random.randint(self.channel_drop_range[0], self.channel_drop_range[1])

    channels_to_drop = random.sample(range(num_channels), k=num_drop_channels)

    return {"channels_to_drop": channels_to_drop}

get_transform_init_args_names (self)

Returns names of arguments that are used in init method of the transform.

Source code in albumentations/augmentations/dropout/channel_dropout.py
Python
def get_transform_init_args_names(self) -> tuple[str, ...]:
    return "channel_drop_range", "fill_value"