import torch import torch.nn as nn from torchvision import transforms class Generator(nn.Module): def __init__(self, nz=128, ngf=64, nc=3): super(Generator, self).__init__() self.main = nn.Sequential( nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False), nn.BatchNorm2d(ngf * 8), nn.LeakyReLU(0.2, inplace=True), nn.Dropout(0.2), nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 4), nn.LeakyReLU(0.2, inplace=True), nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 2), nn.LeakyReLU(0.2, inplace=True), nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf), nn.LeakyReLU(0.2, inplace=True), nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False), nn.Tanh() ) def forward(self, input): output = self.main(input) return output class PreTrainedPipeline(): def __init__(self, path=""): self.device = "cuda" if torch.cuda.is_available() else "cpu" self.model = Generator().to(self.device) self.model.load_state_dict(torch.load("pytorch_model.bin", map_location=self.device)) def __call__(self, inputs: str): """ Args: inputs (:obj:`str`): a string containing some text Return: A :obj:`PIL.Image` with the raw image representation as PIL. """ noise = torch.randn(1, 128, 1, 1, device=self.device) with torch.no_grad(): output = self.model(noise).cpu() img = output[0] img = (img + 1) / 2 img = transforms.ToPILImage()(img) return img