File size: 2,031 Bytes
2015204 0deb359 2015204 7e72a13 0deb359 2015204 7e72a13 2015204 7e72a13 2015204 7e72a13 2015204 7e72a13 2015204 7e72a13 2015204 7e72a13 |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import torch
from PIL import Image
from torchvision import transforms
import torch.nn as nn
class Generator(nn.Module):
def __init__(self, input_size, output_channels):
super(Generator, self).__init__()
# Define the architecture of the generator
self.model = nn.Sequential(
nn.Linear(input_size, 128), # Input layer
nn.LeakyReLU(0.2), # Activation function
nn.Linear(128, 256), # Hidden layer
nn.BatchNorm1d(256), # Batch normalization
nn.LeakyReLU(0.2), # Activation function
nn.Linear(256, 512), # Hidden layer
nn.BatchNorm1d(512), # Batch normalization
nn.LeakyReLU(0.2), # Activation function
nn.Linear(512, output_channels), # Output layer
nn.Tanh() # Tanh activation for output
)
def forward(self, x):
# Forward pass through the generator network
return self.model(x)
class PreTrainedPipeline():
def __init__(self, path=""):
"""
Initialize model
"""
self.model = Generator() # Initialize your Generator model here
def generate_random_image(self):
"""
Generate a random image using the GAN model.
Return:
A :obj:`PIL.Image` with the generated image.
"""
noise = torch.randn(1, 100, 1, 1)
with torch.no_grad():
output_image = self.model(noise)
# Scale generated image
output_image = (output_image + 1) / 2
# Convert to PIL Image
pil_image = transforms.ToPILImage()(output_image[0])
return pil_image
# Example usage
if __name__ == "__main__":
pipeline = PreTrainedPipeline()
generated_image = pipeline.generate_random_image()
generated_image.save('generated_image.jpg')
print("Generated image saved at 'generated_image.jpg'")
|