def forward(
        self, x: "torch.Tensor", y: "torch.Tensor" | list[dict[str, "torch.Tensor"]] | None = None
    ) -> tuple["torch.Tensor", "torch.Tensor" | list[dict[str, "torch.Tensor"]] | None]:
        """
        Apply transformations to inputs `x` and labels `y`.

        :param x: Input samples.
        :param y: Label of the samples `x`.
        :return: Transformed samples and labels.
        """
        import torch

        x_preprocess_list = []
        y_preprocess_list_classification: list[torch.Tensor] = []
        y_preprocess_list_object_detection: list[list[dict[str, torch.Tensor]]] = []

        for i_image in range(x.shape[0]):
            for _ in range(self.nb_samples):
                x_i = x[[i_image]]
                y_i: torch.Tensor | list[dict[str, torch.Tensor]] | None
                if y is not None:
                    if isinstance(y, list):
                        y_i = [y[i_image]]
                    else:
                        y_i = y[[i_image]]
                else:
                    y_i = None
                x_preprocess, y_preprocess_i = self._transform(x_i, y_i)
                x_preprocess_list.append(torch.squeeze(x_preprocess, dim=0))

                if y is not None and y_preprocess_i is not None:
                    if isinstance(y_preprocess_i, torch.Tensor):
                        y_preprocess_list_classification.append(torch.squeeze(y_preprocess_i, dim=0))
                    else:
                        y_preprocess_list_object_detection.append(y_preprocess_i)

        x_preprocess = torch.stack(x_preprocess_list, dim=0)
        y_preprocess: "torch.Tensor" | list[dict[str, "torch.Tensor"]] | None
        if y is None:
            y_preprocess = y
        else:
            if isinstance(y, torch.Tensor):
                y_preprocess = torch.stack(y_preprocess_list_classification, dim=0)
            else:
                y_preprocess = [item for sublist in y_preprocess_list_object_detection for item in sublist]

        return x_preprocess, y_preprocess

        Apply transformations to inputs `x` and labels `y`.

        :param x: Input samples.
        :param y: Label of the samples `x`.
        :return: Transformed samples and labels.
        
art/preprocessing/expectation_over_transformation/pytorch.py