repos
/ ai-art master

ai-art

mirror archived upstream

Art generation with VQGAN + CLIP in Docker, with a simple web UI for anyone with a GPU. A simplified and expanded take on Kevin Costa's work.

aiai-artclipdockerdocker-composegpuhandcodedimagenetpythonpytorchtorchtorchvisionvqganvqgan-clip

1.4 KB · 45 lines · Python Raw History
 1# https://github.com/pratogab/batch-transforms
 2
 3import torch
 4
 5
 6class Normalize:
 7    """Applies the :class:`~torchvision.transforms.Normalize` transform to a batch of images.
 8
 9    .. note::
10        This transform acts out of place by default, i.e., it does not mutate the input tensor.
11
12    Args:
13        mean (sequence):
14            Sequence of means for each channel.
15        std (sequence):
16            Sequence of standard deviations for each channel.
17        inplace(bool,optional):
18            Bool to make this operation in-place.
19        dtype (torch.dtype,optional):
20            The data type of tensors to which the transform will be applied.
21        device (torch.device,optional):
22            The device of tensors to which the transform will be applied.
23    """
24
25    def __init__(self, mean, std, inplace=False, dtype=torch.float, device="cpu"):
26        self.mean = torch.as_tensor(mean, dtype=dtype, device=device)[
27            None, :, None, None
28        ]
29        self.std = torch.as_tensor(std, dtype=dtype, device=device)[None, :, None, None]
30        self.inplace = inplace
31
32    def __call__(self, tensor):
33        """
34        Args:
35            tensor (Tensor): Tensor of size (N, C, H, W) to be normalized.
36
37        Returns:
38            Tensor: Normalized Tensor.
39        """
40        if not self.inplace:
41            tensor = tensor.clone()
42
43        tensor.sub_(self.mean).div_(self.std)
44        return tensor