xoakkeyy / pipeline.py
miittnnss's picture
Update pipeline.py
0deb359
raw
history blame
2.03 kB
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'")