-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
1 addition
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d4b2afe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import torch
from torchvision.utils import save_image
import legacy
مسیر مدل پیشآموزش دیده
network_pkl = "models/ffhq.pkl"
بارگذاری مدل
print('Loading networks from "%s"...' % network_pkl)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open(network_pkl, 'rb') as f:
G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
تولید تصویر
z = torch.randn([1, G.z_dim]).to(device) # latent vector
label = torch.zeros([1, G.c_dim]).to(device) # class label
img = G(z, label, truncation_psi=0.5, noise_mode='const')
ذخیره تصویر
img = (img + 1) * (255 / 2)
img = img.clamp(0, 255).to(torch.uint8)
img = img.permute(0, 2, 3, 1) # NCHW -> NHWC
img = img[0].cpu().numpy()
ذخیره تصویر به عنوان فایل
save_image(torch.Tensor(img).permute(2, 0, 1), 'generated_image.png')
print("Image generated and saved as generated_image.png")
d4b2afe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
سلام