1import torch
2from huggingface_hub import hf_hub_download
3from pytorch_pretrained_biggan import BigGAN, truncated_noise_sample
4from torchvision.utils import make_grid
5import matplotlib.pyplot as plt
6
7model = BigGAN.from_pretrained("biggan-deep-256")
8
9checkpoint_path = hf_hub_download(
10 repo_id="egpivo/biggan-mnist-finetuned",
11 filename="biggan_mnist_embedding.pth"
12)
13state_dict = torch.load(checkpoint_path, map_location="cpu")
14
15mnist_embedding = torch.nn.Embedding(10, 1000)
16mnist_embedding.load_state_dict({"weight": state_dict["weight"]})
17
18device = "cuda" if torch.cuda.is_available() else "cpu"
19model.to(device)
20model.eval()
21mnist_embedding.to(device)
22
23batch_size = 16
24truncation = 0.7
25noise = torch.randn(batch_size, 128, device=device)
26
27labels = torch.randint(0, 10, (batch_size,), device=device, dtype=torch.long)
28print("Generated labels:", labels.cpu().numpy())
29
30class_embedding = mnist_embedding(labels) # Use separate embedding layer
31
32with torch.no_grad():
33 generated_images = model(noise, class_embedding, truncation)
34
35generated_images = (generated_images + 1) / 2
36grid = make_grid(generated_images, nrow=4)
37plt.imshow(grid.permute(1, 2, 0).cpu())
38plt.axis("off")
39plt.show()
Fine-tuned using the MNIST dataset. Special thanks to the creators of BigGAN and the MNIST dataset.