Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from
|
|
|
|
| 2 |
import torch
|
| 3 |
from io import BytesIO
|
| 4 |
from torchvision.utils import save_image
|
|
@@ -12,10 +13,14 @@ LATENT_FEATURES = 512
|
|
| 12 |
RESOLUTION = 128
|
| 13 |
|
| 14 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
-
def load_model_pt(path='model_128.pt'):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model.eval()
|
| 20 |
return model
|
| 21 |
|
|
@@ -29,6 +34,16 @@ def generate_image_stylegan(generator, steps=5, alpha=1.0):
|
|
| 29 |
save_image(image, buffer, format='PNG')
|
| 30 |
buffer.seek(0)
|
| 31 |
return buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def load_model_pkl(path='styleganv2.pkl'):
|
| 34 |
with open(path, 'rb') as f:
|
|
@@ -58,7 +73,7 @@ def generate_image_from_pkl(generator, seed=0, trunc=1):
|
|
| 58 |
def generate_image_from_onnx(path='model_128.onnx', model=None):
|
| 59 |
if model is None:
|
| 60 |
return ValueError("Model not provided.")
|
| 61 |
-
if model == 'progan':
|
| 62 |
z = np.random.randn(1, 512, 1, 1).astype(np.float32)
|
| 63 |
else:
|
| 64 |
z = np.random.randn(1, 512).astype(np.float32)
|
|
@@ -68,6 +83,9 @@ def generate_image_from_onnx(path='model_128.onnx', model=None):
|
|
| 68 |
image = inference_session.run(None, {input_name: z})[0]
|
| 69 |
|
| 70 |
image = image.squeeze(0)
|
|
|
|
|
|
|
|
|
|
| 71 |
image = (image * 0.5 + 0.5) * 255
|
| 72 |
image = image.astype(np.uint8)
|
| 73 |
image = np.transpose(image, (1, 2, 0))
|
|
@@ -77,4 +95,4 @@ def generate_image_from_onnx(path='model_128.onnx', model=None):
|
|
| 77 |
image.save(buffer, format='PNG')
|
| 78 |
buffer.seek(0)
|
| 79 |
|
| 80 |
-
return buffer
|
|
|
|
| 1 |
+
from stylegan_model import StyleGAN
|
| 2 |
+
from vanillagan_model import VanillaGAN
|
| 3 |
import torch
|
| 4 |
from io import BytesIO
|
| 5 |
from torchvision.utils import save_image
|
|
|
|
| 13 |
RESOLUTION = 128
|
| 14 |
|
| 15 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 16 |
+
def load_model_pt(path='model_128.pt',model_type='stylegan'):
|
| 17 |
+
if model_type == "stylegan":
|
| 18 |
+
model = StyleGAN(LATENT_FEATURES, RESOLUTION).to(DEVICE)
|
| 19 |
+
last_checkpoint = torch.load(path, map_location=DEVICE)
|
| 20 |
+
model.load_state_dict(last_checkpoint['generator'], strict=False)
|
| 21 |
+
elif model_type == "vanillagan":
|
| 22 |
+
model = VanillaGAN(RESOLUTION, LATENT_FEATURES).to(DEVICE)
|
| 23 |
+
model.load_state_dict(torch.load(path, map_location=DEVICE))
|
| 24 |
model.eval()
|
| 25 |
return model
|
| 26 |
|
|
|
|
| 34 |
save_image(image, buffer, format='PNG')
|
| 35 |
buffer.seek(0)
|
| 36 |
return buffer
|
| 37 |
+
|
| 38 |
+
def generate_image_vanillagan(generator):
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
image = generator(torch.randn(1, LATENT_FEATURES, device=DEVICE)).view(1, 3, RESOLUTION, RESOLUTION)
|
| 41 |
+
image = (image * 0.5 + 0.5).clamp(0, 1)
|
| 42 |
+
|
| 43 |
+
buffer = BytesIO()
|
| 44 |
+
save_image(image, buffer, format='PNG')
|
| 45 |
+
buffer.seek(0)
|
| 46 |
+
return buffer
|
| 47 |
|
| 48 |
def load_model_pkl(path='styleganv2.pkl'):
|
| 49 |
with open(path, 'rb') as f:
|
|
|
|
| 73 |
def generate_image_from_onnx(path='model_128.onnx', model=None):
|
| 74 |
if model is None:
|
| 75 |
return ValueError("Model not provided.")
|
| 76 |
+
if model == 'progan' or model== 'dcgan':
|
| 77 |
z = np.random.randn(1, 512, 1, 1).astype(np.float32)
|
| 78 |
else:
|
| 79 |
z = np.random.randn(1, 512).astype(np.float32)
|
|
|
|
| 83 |
image = inference_session.run(None, {input_name: z})[0]
|
| 84 |
|
| 85 |
image = image.squeeze(0)
|
| 86 |
+
|
| 87 |
+
if model == "vanillagan":
|
| 88 |
+
image = image.reshape(3, 128, 128)
|
| 89 |
image = (image * 0.5 + 0.5) * 255
|
| 90 |
image = image.astype(np.uint8)
|
| 91 |
image = np.transpose(image, (1, 2, 0))
|
|
|
|
| 95 |
image.save(buffer, format='PNG')
|
| 96 |
buffer.seek(0)
|
| 97 |
|
| 98 |
+
return buffer
|