File size: 13,052 Bytes
d5e5728
 
 
 
 
 
 
aa082d3
 
d5e5728
aa082d3
d5e5728
 
 
 
aa082d3
 
 
 
d5e5728
 
aa082d3
a4cf41d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa082d3
a4cf41d
 
 
 
 
 
 
 
 
 
aa082d3
 
 
a4cf41d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa082d3
 
 
a4cf41d
 
 
 
 
 
 
 
 
d5e5728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa082d3
 
 
 
 
d5e5728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa082d3
d5e5728
 
 
 
aa082d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5e5728
 
aa082d3
 
 
d5e5728
 
 
 
aa082d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5e5728
 
aa082d3
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import torch
import torch.nn as nn
import gradio as gr
from PIL import Image
import numpy as np
import math
import os
from threading import Event
import traceback

# Constants
IMG_SIZE = 128
TIMESTEPS = 300
NUM_CLASSES = 2

# Global Cancellation Flag
cancel_event = Event()

# Device Configuration
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# --- Model Definitions (from second file) ---
class SinusoidalPositionEmbeddings(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.dim = dim
        self.register_buffer('embeddings', self._precompute_embeddings(dim))

    def _precompute_embeddings(self, dim):
        half_dim = dim // 2
        emb = math.log(10000) / (half_dim - 1)
        emb = torch.exp(torch.arange(half_dim) * -emb)
        return emb

    def forward(self, time):
        device = time.device
        embeddings = self.embeddings.to(device)
        embeddings = time[:, None] * embeddings[None, :]
        output = torch.cat([embeddings.sin(), embeddings.cos()], dim=-1)
        return output

class UNet(nn.Module):
    def __init__(self, in_channels=3, out_channels=3, num_classes=2, time_dim=256):
        super().__init__()
        self.num_classes = num_classes
        self.label_embedding = nn.Embedding(num_classes, time_dim)

        self.time_mlp = nn.Sequential(
            SinusoidalPositionEmbeddings(time_dim),
            nn.Linear(time_dim, time_dim),
            nn.ReLU(),
            nn.Linear(time_dim, time_dim)
        )

        self.inc = self.double_conv(in_channels, 64)
        self.down1 = self.down(64 + time_dim * 2, 128)
        self.down2 = self.down(128 + time_dim * 2, 256)
        self.down3 = self.down(256 + time_dim * 2, 512)

        self.bottleneck = self.double_conv(512 + time_dim * 2, 1024)

        self.up1 = nn.ConvTranspose2d(1024, 256, kernel_size=2, stride=2)
        self.upconv1 = self.double_conv(256 + 256 + time_dim * 2, 256)

        self.up2 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2)
        self.upconv2 = self.double_conv(128 + 128 + time_dim * 2, 128)

        self.up3 = nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)
        self.upconv3 = self.double_conv(64 + 64 + time_dim * 2, 64)

        self.outc = nn.Conv2d(64, out_channels, kernel_size=1)

    def double_conv(self, in_channels, out_channels):
        return nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
            nn.ReLU(inplace=True)
        )

    def down(self, in_channels, out_channels):
        return nn.Sequential(
            nn.MaxPool2d(2),
            self.double_conv(in_channels, out_channels)
        )

    def forward(self, x, labels, time):
        label_indices = torch.argmax(labels, dim=1)
        label_emb = self.label_embedding(label_indices)
        t_emb = self.time_mlp(time)

        combined_emb = torch.cat([t_emb, label_emb], dim=1)
        combined_emb = combined_emb.unsqueeze(-1).unsqueeze(-1)

        x1 = self.inc(x)
        x1_cat = torch.cat([x1, combined_emb.repeat(1, 1, x1.shape[-2], x1.shape[-1])], dim=1)

        x2 = self.down1(x1_cat)
        x2_cat = torch.cat([x2, combined_emb.repeat(1, 1, x2.shape[-2], x2.shape[-1])], dim=1)

        x3 = self.down2(x2_cat)
        x3_cat = torch.cat([x3, combined_emb.repeat(1, 1, x3.shape[-2], x3.shape[-1])], dim=1)

        x4 = self.down3(x3_cat)
        x4_cat = torch.cat([x4, combined_emb.repeat(1, 1, x4.shape[-2], x4.shape[-1])], dim=1)

        x5 = self.bottleneck(x4_cat)

        x = self.up1(x5)
        x = torch.cat([x, x3], dim=1)
        x = torch.cat([x, combined_emb.repeat(1, 1, x.shape[-2], x.shape[-1])], dim=1)
        x = self.upconv1(x)

        x = self.up2(x)
        x = torch.cat([x, x2], dim=1)
        x = torch.cat([x, combined_emb.repeat(1, 1, x.shape[-2], x.shape[-1])], dim=1)
        x = self.upconv2(x)

        x = self.up3(x)
        x = torch.cat([x, x1], dim=1)
        x = torch.cat([x, combined_emb.repeat(1, 1, x.shape[-2], x.shape[-1])], dim=1)
        x = self.upconv3(x)

        output = self.outc(x)
        return output

class DiffusionModel(nn.Module):
    def __init__(self, model, timesteps=500, time_dim=256):
        super().__init__()
        self.model = model
        self.timesteps = timesteps
        self.time_dim = time_dim

        self.betas = self.linear_schedule(timesteps)
        self.alphas = 1. - self.betas
        self.register_buffer('alpha_bars', torch.cumprod(self.alphas, dim=0).float())

    def linear_schedule(self, timesteps):
        scale = 1000 / timesteps
        beta_start = scale * 0.0001
        beta_end = scale * 0.02
        return torch.linspace(beta_start, beta_end, timesteps, dtype=torch.float64)

    def forward_diffusion(self, x_0, t, noise):
        x_0 = x_0.float()
        noise = noise.float()
        alpha_bar_t = self.alpha_bars[t].view(-1, 1, 1, 1)
        x_t = torch.sqrt(alpha_bar_t) * x_0 + torch.sqrt(1. - alpha_bar_t) * noise
        return x_t

    def forward(self, x_0, labels):
        t = torch.randint(0, self.timesteps, (x_0.shape[0],), device=x_0.device).long()
        noise = torch.randn_like(x_0)
        x_t = self.forward_diffusion(x_0, t, noise)
        predicted_noise = self.model(x_t, labels, t.float())
        return predicted_noise, noise, t

@torch.no_grad()
def sample(model, num_images, timesteps, img_size, num_classes, labels, device, progress_callback=None):
    x_t = torch.randn(num_images, 3, img_size, img_size).to(device)

    if labels.ndim == 1:
        labels_one_hot = torch.zeros(num_images, num_classes).to(device)
        labels_one_hot[torch.arange(num_images), labels] = 1
        labels = labels_one_hot
    else:
        labels = labels.to(device)

    for t in reversed(range(timesteps)):
        if cancel_event.is_set():
            return None
            
        t_tensor = torch.full((num_images,), t, device=device, dtype=torch.float)

        predicted_noise = model.model(x_t, labels, t_tensor)

        beta_t = model.betas[t].to(device)
        alpha_t = model.alphas[t].to(device)
        alpha_bar_t = model.alpha_bars[t].to(device)

        mean = (1 / torch.sqrt(alpha_t)) * (x_t - (beta_t / torch.sqrt(1 - alpha_bar_t)) * predicted_noise)
        variance = beta_t

        if t > 0:
            noise = torch.randn_like(x_t)
        else:
            noise = torch.zeros_like(x_t)

        x_t = mean + torch.sqrt(variance) * noise
        
        if progress_callback:
            progress_callback((timesteps - t) / timesteps)

    x_0 = torch.clamp(x_t, -1., 1.)

    mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(device)
    std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(device)
    x_0 = std * x_0 + mean
    x_0 = torch.clamp(x_0, 0., 1.)

    return x_0

def load_model(model_path, device):
    unet_model = UNet(num_classes=NUM_CLASSES).to(device)
    diffusion_model = DiffusionModel(unet_model, timesteps=TIMESTEPS).to(device)
    
    try:
        checkpoint = torch.load(model_path, map_location=device)
        if 'model_state_dict' in checkpoint:
            diffusion_model.model.load_state_dict(checkpoint['model_state_dict'])
        else:
            diffusion_model.model.load_state_dict(checkpoint)
        print(f"Successfully loaded model from {model_path}")
    except Exception as e:
        print(f"Error loading model: {e}")
        print("Using randomly initialized weights")
    
    diffusion_model.eval()
    return diffusion_model

def cancel_generation():
    cancel_event.set()
    return "Generation cancelled"

def generate_single_image(label_str):
    label_map = {'Pneumonia': 0, 'Pneumothorax': 1}
    try:
        label_index = label_map[label_str]
    except KeyError:
        raise gr.Error(f"Invalid label '{label_str}'. Please select either 'Pneumonia' or 'Pneumothorax'.")

    labels = torch.zeros(1, NUM_CLASSES, device=device)
    labels[0, label_index] = 1

    with torch.no_grad():
        generated_image = sample(
            model=loaded_model,
            num_images=1,
            timesteps=TIMESTEPS,
            img_size=IMG_SIZE,
            num_classes=NUM_CLASSES,
            labels=labels,
            device=device
        )

    img_np = generated_image.squeeze(0).cpu().permute(1, 2, 0).numpy()
    img_np = np.clip(img_np, 0, 1)
    img_pil = Image.fromarray((img_np * 255).astype(np.uint8))
    
    return img_pil

def generate_batch_images(label_str, num_images, progress=gr.Progress()):
    global loaded_model
    cancel_event.clear()
    
    if num_images < 1 or num_images > 10:
        raise gr.Error("Number of images must be between 1 and 10")
    
    label_map = {'Pneumonia': 0, 'Pneumothorax': 1}
    if label_str not in label_map:
        raise gr.Error("Invalid condition selected")

    labels = torch.zeros(num_images, NUM_CLASSES, device=device)
    labels[:, label_map[label_str]] = 1

    try:
        def progress_callback(progress_val):
            progress(progress_val, desc="Generating...")
            if cancel_event.is_set():
                raise gr.Error("Generation was cancelled by user")

        with torch.no_grad():
            images = sample(
                model=loaded_model,
                num_images=num_images,
                timesteps=TIMESTEPS,
                img_size=IMG_SIZE,
                num_classes=NUM_CLASSES,
                labels=labels,
                device=device,
                progress_callback=progress_callback
            )
        
        if images is None:
            return None
            
        processed_images = []
        for img in images:
            img_np = img.cpu().permute(1, 2, 0).numpy()
            img_np = np.clip(img_np, 0, 1)
            pil_img = Image.fromarray((img_np * 255).astype(np.uint8))
            processed_images.append(pil_img)
        
        return processed_images

    except torch.cuda.OutOfMemoryError:
        torch.cuda.empty_cache()
        raise gr.Error("Out of GPU memory - try generating fewer images")
    except Exception as e:
        traceback.print_exc()
        if str(e) != "Generation was cancelled by user":
            raise gr.Error(f"Generation failed: {str(e)}")
        return None
    finally:
        torch.cuda.empty_cache()

# Load model
MODEL_DIR = "models"
MODEL_NAME = "diffusion_unet_xray.pth"
model_path = os.path.join(MODEL_DIR, MODEL_NAME)
print("Loading model...")
loaded_model = load_model(model_path, device)
print("Model loaded successfully!")

# --- Gradio UI (from first file with modifications) ---
with gr.Blocks(theme=gr.themes.Soft(
    primary_hue="violet",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont("Poppins")],
    text_size="md"
)) as demo:
    gr.Markdown("""
    <center>
    <h1>Synthetic X-ray Generator</h1>
    <p><em>Generate synthetic chest X-rays conditioned on pathology</em></p>
    </center>
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            condition = gr.Dropdown(
                ["Pneumonia", "Pneumothorax"],
                label="Select Condition",
                value="Pneumonia",
                interactive=True
            )
            num_images = gr.Slider(
                1, 10, value=1, step=1,
                label="Number of Images",
                interactive=True
            )
            
            with gr.Row():
                submit_btn = gr.Button("Generate", variant="primary")
                cancel_btn = gr.Button("Cancel", variant="stop")
            
            gr.Markdown("""
            <div style="text-align: center; margin-top: 10px;">
                <small>Note: Generation may take several seconds per image</small>
            </div>
            """)
        
        with gr.Column(scale=2):
            with gr.Tab("Single Image"):
                single_image = gr.Image(
                    type="pil",
                    label="Generated X-ray",
                    height=400
                )
            with gr.Tab("Batch Images"):
                gallery = gr.Gallery(
                    label="Generated X-rays",
                    columns=3,
                    height="auto",
                    object_fit="contain"
                )
    
    # Single image generation
    condition.change(
        fn=generate_single_image,
        inputs=condition,
        outputs=single_image
    )
    
    # Batch image generation
    submit_btn.click(
        fn=generate_batch_images,
        inputs=[condition, num_images],
        outputs=gallery
    )
    
    cancel_btn.click(
        fn=cancel_generation,
        outputs=None
    )

    demo.css = """
    .gradio-container {
        background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
    }
    .gallery-container {
        background-color: white !important;
    }
    """

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)