-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
173 lines (135 loc) · 4.89 KB
/
Copy pathapi.py
File metadata and controls
173 lines (135 loc) · 4.89 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import io
from pathlib import Path
from typing import Optional, Tuple
import numpy as np
import torch
import tifffile
from fastapi import FastAPI, File, UploadFile, HTTPException, Query
from fastapi.responses import StreamingResponse
from PIL import Image
from src.models.generator import Generator
from src.inference.blending import SeamlessGenerator, create_blend_weights
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title="Road Generation API",
description="Generate seamless satellite images from segmentation masks using Pix2Pix",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://localhost:5173"], # Add your frontend URLs
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global model instance (loaded at startup)
generator: Optional[SeamlessGenerator] = None
def load_model(checkpoint_path: str, device: str) -> SeamlessGenerator:
"""Load the model and create a SeamlessGenerator instance."""
checkpoint = torch.load(checkpoint_path, map_location=device)
# Get config from checkpoint or use defaults
config = checkpoint.get('config', {})
in_channels = config.get('input_channels', 2)
out_channels = config.get('output_channels', 3)
features = config.get('generator_features', 64)
# Initialize model
model = Generator(
in_channels=in_channels,
out_channels=out_channels,
features=features
).to(device)
# Load weights
model.load_state_dict(checkpoint['generator_state_dict'])
# NOTE: Do NOT call model.eval() here for Pix2Pix!
# The SeamlessGenerator will properly configure dropout and BatchNorm modes
# Create seamless generator with randomness for empty patches
seamless_gen = SeamlessGenerator(
model=model,
patch_size=256,
overlap=64,
blend_mode="cosine",
device=device,
noise_strategy="gaussian", # Add randomness to empty patches
noise_kwargs={"noise_scale": 10.0, "empty_only": False},
empty_threshold=0.01,
enable_dropout=True # Keep dropout enabled for Pix2Pix
)
return seamless_gen
@app.on_event("startup")
async def startup_event():
"""Load the model on startup."""
global generator
# Auto-detect device
if torch.backends.mps.is_available():
device = "mps"
elif torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
print(f"Using device: {device}")
checkpoint_path = "checkpoints/best_model.pth"
print(f"Loading model from {checkpoint_path}...")
generator = load_model(checkpoint_path, device)
print("Model loaded successfully!")
def preprocess_image(
image_bytes: bytes,
filename: str,
c1_pixel_value: Tuple[int, int, int] = (255, 0, 0),
c2_pixel_value: Tuple[int, int, int] = (0, 255, 0),
) -> np.ndarray:
""" Convert uploaded image to the expected format (H, W, 2) for the model. """
# Read image
if filename.lower().endswith(('.tif', '.tiff')):
image = tifffile.imread(io.BytesIO(image_bytes))
else:
image = np.array(Image.open(io.BytesIO(image_bytes)))
if len(image.shape) != 3:
raise ValueError(f"Expected 3D image (H, W, C), got shape: {image.shape}")
# Handle both RGB (3 channels) and RGBA (4 channels)
if image.shape[2] == 4:
image = image[:, :, :3]
mask = np.zeros((image.shape[0], image.shape[1], 2))
mask[:, :, 0] = (image == c1_pixel_value).all(axis=2) * 255
mask[:, :, 1] = (image == c2_pixel_value).all(axis=2) * 255
return mask
@app.post("/generate",
summary="Generate satellite image",
description="Upload a segmentation mask and get a generated satellite image")
async def generate_image(
file: UploadFile = File(...),
):
global generator
try:
image_bytes = await file.read()
mask = preprocess_image(image_bytes, file.filename)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Failed to process input image: {str(e)}"
)
try:
result = generator.generate(mask)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Generation failed: {str(e)}"
)
output_image = Image.fromarray(result)
img_byte_arr = io.BytesIO()
output_image.save(img_byte_arr, format='PNG')
img_byte_arr.seek(0)
return StreamingResponse(
img_byte_arr,
media_type="image/png",
headers={"Content-Disposition": "attachment; filename=generated.png"}
)
@app.get("/health")
async def health_check():
"""Check if the API is running and if the model is loaded."""
return {
"status": "healthy",
"model_loaded": generator is not None
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)