-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinference.py
More file actions
86 lines (69 loc) · 2.87 KB
/
inference.py
File metadata and controls
86 lines (69 loc) · 2.87 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
import torch
import torch.nn as nn
from diffusers import StableDiffusionControlNetPipeline, DDIMScheduler
from huggingface_hub import hf_hub_download
from utils import process_hint, create_sample_hint
# =============================================================================
# Configuration
# =============================================================================
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Choose model: "MVRL/VectorSynth" (CLIP) or "MVRL/VectorSynth-COSA" (COSA)
MODEL_REPO = "MVRL/VectorSynth"
RENDER_ENCODER_FILE = "render_encoder/clip-render_encoder.pth"
# For COSA variant:
# MODEL_REPO = "MVRL/VectorSynth-COSA"
# RENDER_ENCODER_FILE = "render_encoder/cosa-render_encoder.pth"
# =============================================================================
# Load Models
# =============================================================================
class RenderEncoder(nn.Module):
def __init__(self, in_channels=768, out_channels=3):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1),
nn.Sigmoid()
)
def forward(self, x):
return self.model(x)
print(f"Loading from {MODEL_REPO}...")
# Load pipeline
pipe = StableDiffusionControlNetPipeline.from_pretrained(
MODEL_REPO, torch_dtype=torch.float16
)
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(device)
# Load RenderEncoder
render_path = hf_hub_download(MODEL_REPO, RENDER_ENCODER_FILE)
ckpt = torch.load(render_path, map_location=device, weights_only=False)
render_encoder = RenderEncoder(
in_channels=ckpt['config']['in_channels'],
out_channels=ckpt['config']['out_channels']
)
render_encoder.load_state_dict(ckpt['state_dict'])
render_encoder = render_encoder.to(device).eval()
# =============================================================================
# Generate
# =============================================================================
# For creating hint tensors: see data/dataset.md
# For COSA version: see cosa/README.md
# Option 1: Load pre-saved hint
hint = torch.load("path/to/hint.pt", map_location=device, weights_only=False)
# Option 2: Create hint from data (see data/dataset.md for generating these files)
# hint = create_sample_hint(
# point_id="123",
# pixel_tensor_path="path/to/pixel_tensors/",
# embedding_tensor_path="path/to/embeddings.pt", # CLIP or COSA embeddings
# taglist_vocab_path="path/to/taglist_vocab.pt",
# device=device
# )
control = process_hint(hint, render_encoder, device)
output = pipe(
prompt="An aerial image of a city neighborhood",
image=control,
num_inference_steps=40,
guidance_scale=7.5,
controlnet_conditioning_scale=1.0,
generator=torch.manual_seed(42)
)
output.images[0].save("generated_satellite.png")
print("Saved: generated_satellite.png")