Spaces:
Sleeping
Sleeping
updated architecture
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ from fastapi import FastAPI, Response, HTTPException, Query
|
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from huggingface_hub import hf_hub_download, login
|
| 12 |
-
from models import Generator
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
@@ -26,6 +25,44 @@ Z_DIM = 100
|
|
| 26 |
DEVICE = torch.device("cpu")
|
| 27 |
REPO_ID = "SaniaE/GeoGen"
|
| 28 |
FILENAME = "dcgans_model_checkpoint.pth"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
gen_model = None
|
| 30 |
|
| 31 |
@app.on_event("startup")
|
|
@@ -44,15 +81,14 @@ def load_model():
|
|
| 44 |
|
| 45 |
checkpoint = torch.load(model_path, map_location=DEVICE)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
print("Missing keys: ", missing)
|
| 53 |
|
| 54 |
gen_model.eval()
|
| 55 |
-
print("SUCCESS: Petrol Pump GAN
|
| 56 |
except Exception as e:
|
| 57 |
print(f"Error loading model: {e}")
|
| 58 |
|
|
@@ -94,7 +130,7 @@ def explore_latent(
|
|
| 94 |
x_shift: float = Query(0.0, ge=-5.0, le=5.0),
|
| 95 |
y_shift: float = Query(0.0, ge=-5.0, le=5.0)
|
| 96 |
):
|
| 97 |
-
"""Endpoint 2: Controlled generation
|
| 98 |
if gen_model is None:
|
| 99 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 100 |
|
|
@@ -106,6 +142,7 @@ def explore_latent(
|
|
| 106 |
|
| 107 |
noise = torch.randn(1, Z_DIM, device=DEVICE)
|
| 108 |
|
|
|
|
| 109 |
gen_x = torch.Generator(device=DEVICE).manual_seed(seed + 101)
|
| 110 |
dir_x = torch.randn(1, Z_DIM, generator=gen_x, device=DEVICE)
|
| 111 |
dir_x = dir_x / dir_x.norm()
|
|
@@ -114,7 +151,6 @@ def explore_latent(
|
|
| 114 |
dir_y = torch.randn(1, Z_DIM, generator=gen_y, device=DEVICE)
|
| 115 |
dir_y = dir_y / dir_y.norm()
|
| 116 |
|
| 117 |
-
# Apply controlled linear shifts along the unit directions
|
| 118 |
steered_noise = noise + (x_shift * dir_x) + (y_shift * dir_y)
|
| 119 |
|
| 120 |
fake_img = gen_model(steered_noise)
|
|
|
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from huggingface_hub import hf_hub_download, login
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 25 |
DEVICE = torch.device("cpu")
|
| 26 |
REPO_ID = "SaniaE/GeoGen"
|
| 27 |
FILENAME = "dcgans_model_checkpoint.pth"
|
| 28 |
+
|
| 29 |
+
class Generator(nn.Module):
|
| 30 |
+
def __init__(self, z_dim=100, input_channels=3, hidden_dim=64):
|
| 31 |
+
super(Generator, self).__init__()
|
| 32 |
+
self.z_dim = z_dim
|
| 33 |
+
|
| 34 |
+
self.gen = nn.Sequential(
|
| 35 |
+
nn.Sequential(
|
| 36 |
+
nn.ConvTranspose2d(z_dim, hidden_dim * 16, kernel_size=4, stride=1, padding=0, bias=False),
|
| 37 |
+
nn.BatchNorm2d(hidden_dim * 16),
|
| 38 |
+
nn.LeakyReLU(0.2, inplace=True)
|
| 39 |
+
),
|
| 40 |
+
self.generator_block(hidden_dim * 16, hidden_dim * 8),
|
| 41 |
+
self.generator_block(hidden_dim * 8, hidden_dim * 4),
|
| 42 |
+
self.generator_block(hidden_dim * 4, hidden_dim * 2),
|
| 43 |
+
self.generator_block(hidden_dim * 2, hidden_dim),
|
| 44 |
+
self.generator_block(hidden_dim, hidden_dim // 2),
|
| 45 |
+
self.generator_block(hidden_dim // 2, input_channels, final_layer=True)
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def generator_block(self, input_channels, output_channels, final_layer=False):
|
| 49 |
+
if not final_layer:
|
| 50 |
+
return nn.Sequential(
|
| 51 |
+
nn.Conv2d(input_channels, output_channels * 4, kernel_size=3, stride=1, padding=1, bias=False),
|
| 52 |
+
nn.PixelShuffle(2),
|
| 53 |
+
nn.BatchNorm2d(output_channels),
|
| 54 |
+
nn.LeakyReLU(0.2, inplace=True)
|
| 55 |
+
)
|
| 56 |
+
else:
|
| 57 |
+
return nn.Sequential(
|
| 58 |
+
nn.Conv2d(input_channels, output_channels * 4, kernel_size=3, stride=1, padding=1),
|
| 59 |
+
nn.PixelShuffle(2),
|
| 60 |
+
nn.Tanh()
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
def forward(self, noise):
|
| 64 |
+
return self.gen(noise.view(len(noise), self.z_dim, 1, 1))
|
| 65 |
+
|
| 66 |
gen_model = None
|
| 67 |
|
| 68 |
@app.on_event("startup")
|
|
|
|
| 81 |
|
| 82 |
checkpoint = torch.load(model_path, map_location=DEVICE)
|
| 83 |
|
| 84 |
+
# Initialize generator with matching architecture (hidden_dim=64 -> 1024 channels)
|
| 85 |
+
gen_model = Generator(z_dim=Z_DIM, hidden_dim=64).to(DEVICE)
|
| 86 |
+
|
| 87 |
+
# Load state dict strictly since shapes now match perfectly
|
| 88 |
+
gen_model.load_state_dict(checkpoint["gen_state_dict"], strict=True)
|
|
|
|
| 89 |
|
| 90 |
gen_model.eval()
|
| 91 |
+
print("SUCCESS: Petrol Pump GAN Generator loaded perfectly!")
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Error loading model: {e}")
|
| 94 |
|
|
|
|
| 130 |
x_shift: float = Query(0.0, ge=-5.0, le=5.0),
|
| 131 |
y_shift: float = Query(0.0, ge=-5.0, le=5.0)
|
| 132 |
):
|
| 133 |
+
"""Endpoint 2: Controlled generation using stable normalized steering vectors."""
|
| 134 |
if gen_model is None:
|
| 135 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 136 |
|
|
|
|
| 142 |
|
| 143 |
noise = torch.randn(1, Z_DIM, device=DEVICE)
|
| 144 |
|
| 145 |
+
# Create stable, orthogonal unit direction vectors based on the seed
|
| 146 |
gen_x = torch.Generator(device=DEVICE).manual_seed(seed + 101)
|
| 147 |
dir_x = torch.randn(1, Z_DIM, generator=gen_x, device=DEVICE)
|
| 148 |
dir_x = dir_x / dir_x.norm()
|
|
|
|
| 151 |
dir_y = torch.randn(1, Z_DIM, generator=gen_y, device=DEVICE)
|
| 152 |
dir_y = dir_y / dir_y.norm()
|
| 153 |
|
|
|
|
| 154 |
steered_noise = noise + (x_shift * dir_x) + (y_shift * dir_y)
|
| 155 |
|
| 156 |
fake_img = gen_model(steered_noise)
|