Transitioning from StyleGAN to Diffusion Models: A Medical Image Generation Study on APTOS Retinal…
In my earlier work, I established a foundational understanding of generative modeling by studying StyleGAN2 and its adversarial training…
Transitioning from StyleGAN to Diffusion Models: A Medical Image Generation Study on APTOS Retinal Dataset
In my earlier work, I established a foundational understanding of generative modeling by studying StyleGAN2 and its adversarial training framework for realistic image synthesis. Building on this, I extended the application of StyleGAN2 to a medical imaging context, specifically targeting the APTOS 2019 retinal fundus dataset. The primary objective was to synthesize retinal images representative of severe diabetic retinopathy, a class characterized by both clinical significance and limited data availability.
However, experiments with StyleGAN2 revealed clear and consistent limitations in its ability to capture fine-grained pathological detail under low-data conditions. These shortcomings ultimately motivated a transition toward diffusion-based generative models, which offered more stable training dynamics and greater suitability for structured medical image synthesis.
Index
- Dataset: APTOS Retinal Fundus Images
- Initial Experiments with StyleGAN2 on APTOS Dataset
- Transition and Motivation to Diffusion Models
- Stable Diffusion Pipeline Setup
- Prompt Engineering for Retinal Image Synthesis
- Future Direction: Soft Prompting
1. Dataset: APTOS Retinal Fundus Images
The dataset used in this work is the APTOS 2019 Blindness Detection dataset, comprising retinal fundus images acquired using clinical ophthalmology imaging systems. Retinal fundus images capture the interior surface of the posterior eye, encompassing the retina, optic disc, macula, and blood vessels, and are obtained using a specialized fundus camera. The dataset provides severity labels for diabetic retinopathy on a five-point scale ranging from 0 (no disease) to 4 (proliferative diabetic retinopathy), where higher grades correspond to more advanced retinal damage and an increased risk of vision loss.

In this work, I focused exclusively on Grade 3 (severe diabetic retinopathy) images, as this class contains clinically significant pathological features including hard exudates, intraretinal hemorrhages, microaneurysms, and irregular illumination patterns characteristic of advanced disease. Additionally, severe diabetic retinopathy represents a relatively underrepresented class within the dataset, making it a particularly suitable candidate for synthetic data generation and augmentation experiments.

2. Initial Experiments with StyleGAN2 on APTOS Dataset
In my initial experiments, I trained StyleGAN2 with Adaptive Discriminator Augmentation (StyleGAN2-ADA) from scratch using the Rosinality PyTorch implementation on the severe-class subset of the APTOS 2019 dataset, comprising 193 retinal fundus images. The model was initialized with random weights and trained directly on this data to learn the underlying image distribution, with ADA employed specifically to mitigate overfitting under this low-data regime.
Training was conducted in two stages: an initial phase at 256×256 resolution for approximately 10K iterations, followed by an upscaled phase at 512×512 resolution for approximately 35K iterations. The resolution increase was motivated by the expectation that higher spatial detail would improve the model’s ability to capture fine-grained retinal structures. However, the transition did not yield any meaningful improvement in the learning of pathological or anatomical detail.
Throughout training, the model was able to reproduce only coarse-level visual features, such as the general shape of the eye, an approximate optic disc region, and rough vessel-like patterns. It consistently failed to generate anatomically coherent retinal vasculature or any clinically meaningful pathological features. The training process itself remained slow and unstable, characterized by weak convergence and negligible qualitative improvement even after extended iterations.
These results indicate that despite ADA’s regularization benefits, a dataset of 193 images remains fundamentally insufficient for robust GAN training from scratch. StyleGAN2-ADA was unable to meet the requirements of this project, which demand reliable and fine-grained synthesis of severe diabetic retinopathy pathology, and was therefore not pursued further.

At 34.8 iteration
3. Transition and Motivation to Diffusion Models
Following the limitations identified with StyleGAN2-ADA, I transitioned to diffusion-based generative models as a more principled approach for retinal image synthesis. My primary motivation was to adopt a generative framework that offers stable training dynamics, strong performance in low-data regimes, and fine-grained controllability over pathological features.
Unlike GAN-based approaches, which are prone to training instabilities such as mode collapse and discriminator imbalance, diffusion models learn through a progressive noise corruption and reconstruction process. During training, images are iteratively corrupted with Gaussian noise, and the model learns to reverse this process step by step. This formulation leads to significantly more stable optimization compared to adversarial training.
A further practical advantage is that diffusion models do not need to be trained from scratch. Large-scale pretrained models, trained on broad and diverse image datasets, are openly available and carry strong prior knowledge of general visual structure. This prior can be efficiently adapted to specialized domains through lightweight fine-tuning methods such as LoRA, making diffusion models well-suited for low-data settings such as medical imaging, where large annotated datasets are rarely available.
Finally, I was particularly motivated by the native text-conditioning capability of Stable Diffusion, which allows image generation to be guided by natural language descriptions. This provided a direct and intuitive mechanism for specifying clinically relevant pathological features such as hemorrhages, hard exudates, and microaneurysms — attributes that are difficult to control explicitly in GAN-based frameworks.

diffusion model output (1800 steps)
4. Stable Diffusion Pipeline Setup
Following the transition from GAN-based methods, I implemented a Stable Diffusion 1.5 pipeline for retinal image synthesis. I adopted the pretrained model available on Hugging Face (runwayml/stable-diffusion-v1-5) as the base, which is built on a latent diffusion framework comprising a variational autoencoder (VAE), a U-Net denoising network, and a CLIP-based text encoder. Rather than training from scratch, I adapted this general-purpose model to the retinal domain using Low-Rank Adaptation (LoRA), which enabled efficient fine-tuning of the U-Net layers while keeping the base model weights frozen — making training feasible on a dataset of limited size.
Training Setup
For training, I constructed natural language captions for each image in the severe-class subset, describing the retinal condition and associated pathological features in a structured format. Representative examples include:
- “retinal fundus photograph, severe diabetic retinopathy, multiple hemorrhages, hard exudates around macula, abnormal retinal illumination”
- “fundus image showing severe diabetic retinopathy with microaneurysms and blot hemorrhages near optic disc”
- “severely affected retinal fundus with dense exudates and vascular abnormalities”
Each image-caption pair served as a conditioning input during fine-tuning, allowing the model to associate pathological descriptors with corresponding visual features in the retinal domain.
Inference and Generation Control
During inference, I loaded the pretrained Stable Diffusion pipeline and injected the trained LoRA weights to specialize generation toward severe retinal fundus imagery. Text prompts describing severe diabetic retinopathy and its associated pathological findings were used as the primary conditioning signal. Negative prompts were additionally employed to suppress non-medical artifacts such as cartoon-like textures, oversaturation, and anatomically implausible structures.
To systematically assess their influence on output quality, I experimented with varying guidance scales, inference step counts, and LoRA weight strengths, analyzing their individual effects on pathological realism and structural consistency in the generated images.
This setup established a flexible and controllable framework for severe retinal image synthesis, leveraging the strong visual priors of a pretrained diffusion model and adapting them efficiently to a highly specific medical imaging sub-domain through lightweight fine-tuning.

5. Prompt Engineering for Retinal Image Synthesis
Limitations of CLIP-Based Text Conditioning
In Stable Diffusion, text prompts are encoded using the CLIP text encoder, which enforces a fixed token limit of 77 tokens. Prompts exceeding this limit are truncated at the boundary, with all subsequent tokens permanently discarded. The retained tokens are projected into a single fixed-dimensional embedding vector that serves as the conditioning signal for the diffusion model. While effective for general-purpose image synthesis, this pipeline introduced fundamental constraints when fine-grained, attribute-level control over pathological features was required.
Structured Prompt Design and Truncation
To address this, I designed a parameterized hashmap that encoded clinically distinct pathological attributes — including hemorrhage density, exudate distribution, microaneurysm severity, and vascular changes — as discrete, independently weighted prompt components. However, attributes positioned beyond the 77-token boundary were silently discarded regardless of their clinical significance, making it impossible to reliably represent all relevant pathological descriptors within a single prompt.
Semantic Compression Within CLIP
Even when my full prompt remained within the token limit, CLIP’s architecture did not preserve the internal relational structure of the input. Rather than maintaining independent representations for each attribute, the encoder compressed all tokens into a unified semantic vector, effectively collapsing structured multi-attribute descriptions into a single global concept — most commonly a generalized notion of “severe diabetic retinopathy.” This compression erased the explicit distinctions between individual pathological parameters that my hashmap was designed to preserve.
Loss of Controllability
This semantic compression had direct consequences for output controllability. Increasing prompt complexity, adjusting attribute weightings, or repeating key pathological descriptors did not produce consistent or proportional changes in the generated images. Attributes such as hemorrhage intensity, exudate clustering, and microaneurysm density could not be modulated independently. Instead, the model consistently produced averaged disease representations, conflating multiple pathological cues into a single severity-level interpretation. This behavior fundamentally undermined the utility of my structured prompt system and rendered fine-grained, clinically meaningful control over retinal pathology unreliable.
These limitations highlighted the need to move beyond hard prompting toward more expressive and learnable conditioning strategies.
6. Future Direction: Soft Prompting
In future work, I plan to explore soft prompting techniques, specifically Textual Inversion, as a promising direction to address the conditioning limitations identified in this study.
Rather than relying on discrete natural language tokens, Textual Inversion introduces a single learnable token whose continuous embedding is optimized directly within the text conditioning space of a pretrained diffusion model. During training, the base model weights remain frozen while the soft token embedding is updated through the standard diffusion loss, using real training images as supervision. Through this process, complex pathological characteristics — such as hemorrhages, exudates, and vascular abnormalities — are implicitly encoded into this single continuous representation, without requiring explicit textual decomposition.
This approach would make both the 77-token CLIP truncation limit and the semantic compression problem significantly less restrictive. Since the learned embedding directly captures the full visual concept of severe diabetic retinopathy from real image data, structured pathological control no longer depends on carefully engineered natural language prompts. This is expected to yield more stable, consistent, and clinically meaningful synthesis of severe retinal pathology compared to the hard prompting strategies explored in this work.
메타데이터
- post_id
- 4e2bf9dd106f
- slug
- transitioning-from-stylegan-to-diffusion-models-a-medical-image-generation-study-on-aptos-retinal-4e2bf9dd106f
- url
- https://medium.com/@ujjjwalx/transitioning-from-stylegan-to-diffusion-models-a-medical-image-generation-study-on-aptos-retinal-4e2bf9dd106f
- canonical_url
- https://medium.com/@ujjjwalx/transitioning-from-stylegan-to-diffusion-models-a-medical-image-generation-study-on-aptos-retinal-4e2bf9dd106f
- author_url
- https://medium.com/@ujjjwalx
- status
- ok
- fetched_at
- 2026-07-10 10:20:21