Create pipeline.py
Browse files- pipeline.py +21 -0
pipeline.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from torchvision import transforms
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
class PretrainedPipeline():
|
7 |
+
def __init__(self):
|
8 |
+
self.device = torch.device("cpu")
|
9 |
+
self.generator = Generator() # Instantiate your GAN generator class
|
10 |
+
self.generator.load_state_dict(torch.load("pytorch_model.bin", map_location=self.device))
|
11 |
+
self.generator.eval()
|
12 |
+
|
13 |
+
def generate_image(self):
|
14 |
+
with torch.no_grad():
|
15 |
+
noise = torch.randn(1, 128, 1, 1).to(self.device) # Assuming input noise size is 100
|
16 |
+
generated_image_tensor = self.generator(noise)
|
17 |
+
generated_image = generated_image_tensor.cpu().detach().squeeze(0)
|
18 |
+
# Assuming the generator output is in the range [-1, 1]
|
19 |
+
generated_image = (generated_image + 1) / 2.0
|
20 |
+
pil_image = transforms.ToPILImage()(generated_image)
|
21 |
+
return pil_image
|