Downloads · 30 days
6
9% of all-time downloads
benetraco/latent_finetuning_scanners
latent_finetuning_scanners is a text-to-image model from benetraco. Use it when you need an image from a text prompt. It is set up for diffusers. The card lists the license as mit.
: Brain MRI Synthesis with Stable Diffusion (Fine-Tuned with Dataset Prompts) Fine-tuned version of Stable Diffusion v1-4 for brain MRI synthesis. It uses latent diffusion and dataset-specific prompts to generate real…
Downloads · 30 days
6
9% of all-time downloads
All-time downloads
69
Public
Parameters
860M
4.3 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors4.3 GB · 100%
From the Hugging Face model README
#: Brain MRI Synthesis with Stable Diffusion (Fine-Tuned with Dataset Prompts) Fine-tuned version of Stable Diffusion v1-4 for brain MRI synthesis. It uses latent diffusion and dataset-specific prompts to generate realistic 256x256 FLAIR brain scans, with control over the dataset style.
This model is a fine-tuned version of Stable Diffusion v1-4 for prompt-conditioned brain MRI image synthesis, trained on 2D FLAIR slices from the SHIFTS, VH, and WMH2017 datasets. It uses latent diffusion to generate realistic 256×256 scans from latent representations of resolution 32×32 and includes special prompt tokens that allow control over the visual style.
Each training image was paired with a specific dataset prompt:
These prompts were added as new tokens in the tokenizer and trained jointly with the model, enabling conditional generation aligned with dataset distribution.
The text encoder, U-Net, and special prompt embeddings were trained jointly. Images were encoded into 32×32 latent space using a VAE and trained using latent diffusion.
from diffusers import StableDiffusionPipeline
import torch
from torchvision.utils import save_image
pipe = StableDiffusionPipeline.from_pretrained("benetraco/latent_finetuning", torch_dtype=torch.float32).to("cuda")
pipe.scheduler.set_timesteps(999)
def get_embeddings(prompt):
tokens = pipe.tokenizer(prompt, return_tensors="pt", padding="max_length", max_length=77).to("cuda")
return pipe.text_encoder(**tokens).last_hidden_state
def sample(prompt, guidance_scale=2.0, seed=42):
torch.manual_seed(seed)
latent = torch.randn(1, 4, 32, 32).to("cuda") * pipe.scheduler.init_noise_sigma
text_emb = get_embeddings(prompt)
uncond_emb = get_embeddings("")
for t in pipe.scheduler.timesteps:
latent_in = pipe.scheduler.scale_model_input(latent, t)
with torch.no_grad():
noise_uncond = pipe.unet(latent_in, t, encoder_hidden_states=uncond_emb).sample
noise_text = pipe.unet(latent_in, t, encoder_hidden_states=text_emb).sample
noise = noise_uncond + guidance_scale * (noise_text - noise_uncond)
latent = pipe.scheduler.step(noise, t, latent).prev_sample
latent /= pipe.vae.config.scaling_factor
with torch.no_grad():
decoded = pipe.vae.decode(latent).sample
image = (decoded + 1.0) / 2.0
image = image.clamp(0, 1)
save_image(image, f"{prompt.replace(' ', '_')}_g{guidance_scale}.png")
sample("SHIFTS FLAIR MRI", guidance_scale=5.0)