File size: 832 Bytes
8552e9f
 
 
 
 
 
 
d3e3435
8552e9f
 
 
 
 
5a96139
8552e9f
 
 
 
 
 
 
 
 
 
 
a098cb6
8552e9f
d3e3435
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Utils Standards

# Numerical Management
import numpy as np
# Pythorch
import torch
# A hugging face library
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN

"""Let's now set up the model functions"""

def load_model(model_name = "ceyda/butterfly_cropped_uniq1K_512", model_version=None):
    # GAN set-up
    gan = LightweightGAN.from_pretrained(model_name, version=model_version)
    # GAN Inference Evaluation 
    gan.eval()
    return gan

# Let's set-up a second function for generation form

def generate(gan, batch_size=1):
    with torch.no_grad():
        # Cleaning process to properly fit it to the model
        ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
        #
        ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
    return ims