Views
No views yet
noise + stage_label → synthetic_brain_MRI1# Install dependencies
2pip install torch torchvision huggingface_hub pillow
3
4# Download model_architecture.py and inference.py from this repo
5# Then run:
6python inference.pygenerated_stage_0.png, generated_stage_1.png, etc.1import torch
2from huggingface_hub import hf_hub_download
3from PIL import Image
4
5# Download model architecture first
6# (Get model_architecture.py from this repo)
7from model_architecture import Generator
8
9# Download generator checkpoint from HuggingFace
10model_path = hf_hub_download(
11 repo_id="Arga23/dementia-cgan-mri",
12 filename="cDCGAN_generator.pth"
13)
14
15# Load checkpoint
16device = "cuda" if torch.cuda.is_available() else "cpu"
17checkpoint = torch.load(model_path, map_location=device)
18
19# Initialize Generator
20G = Generator(
21 z_dim=checkpoint['z_dim'],
22 num_classes=checkpoint['num_classes'],
23 img_channels=1
24).to(device)
25
26G.load_state_dict(checkpoint['model'])
27G.eval()
28
29# Generate image for specific dementia stage
30def generate_sample(stage):
31 with torch.no_grad():
32 z = torch.randn(1, checkpoint['z_dim'], 1, 1).to(device)
33 label = torch.tensor([stage]).to(device)
34 img = G(z, label)
35 img = (img.squeeze().cpu() + 1) / 2
36 img = torch.clamp(img, 0, 1)
37 return Image.fromarray((img.numpy() * 255).astype('uint8'), mode='L')
38
39# Example: Generate Stage 2 (Mild Dementia)
40img = generate_sample(stage=2)
41img.save('generated_mild_dementia.png')cDCGAN_generator.pth: Generator weights (inference-ready)model_architecture.py: PyTorch Generator architectureinference.py: Ready-to-use inference script (downloads from HuggingFace)torch>=2.0.0
torchvision
huggingface_hub
pillow