File size: 10,210 Bytes
ee6eca1 |
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 |
import gradio as gr
import PIL.Image
from pathlib import Path
import pandas as pd
from diffusers.pipelines import StableDiffusionPipeline
import torch
import argparse
import os
import warnings
from safetensors.torch import load_file
import yaml
warnings.filterwarnings("ignore")
OUTPUT_DIR = "OUTPUT"
cuda_device = 1
device = f"cuda:{cuda_device}" if torch.cuda.is_available() else "cpu"
TITLE = "Demo for Generating Chest X-rays using Diferent Parameter-Efficient Fine-Tuned Stable Diffusion Pipelines"
INFO_ABOUT_TEXT_PROMPT = "INFO_ABOUT_TEXT_PROMPT"
INFO_ABOUT_GUIDANCE_SCALE = "INFO_ABOUT_GUIDANCE_SCALE"
INFO_ABOUT_INFERENCE_STEPS = "INFO_ABOUT_INFERENCE_STEPS"
EXAMPLE_TEXT_PROMPTS = [
"No acute cardiopulmonary abnormality.",
"Normal chest radiograph.",
"No acute intrathoracic process.",
"Mild pulmonary edema.",
"No focal consolidation concerning for pneumonia",
"No radiographic evidence for acute cardiopulmonary process",
]
def load_adapted_unet(unet_pretraining_type, exp_path, pipe):
"""
Loads the adapted U-Net for the selected PEFT Type
Parameters:
unet_pretraining_type (str): The type of PEFT to use for generating the X-ray
exp_path (str): The path to the best trained model for the selected PEFT Type
pipe (StableDiffusionPipeline): The Stable Diffusion Pipeline to use for generating the X-ray
Returns:
None
"""
sd_folder_path = "runwayml/stable-diffusion-v1-5"
if unet_pretraining_type == "freeze":
pass
elif unet_pretraining_type == "svdiff":
print("SV-DIFF UNET")
pipe.unet = load_unet_for_svdiff(
sd_folder_path,
spectral_shifts_ckpt=os.path.join(
os.path.join(exp_path, "unet"), "spectral_shifts.safetensors"
),
subfolder="unet",
)
for module in pipe.unet.modules():
if hasattr(module, "perform_svd"):
module.perform_svd()
elif unet_pretraining_type == "lorav2":
exp_path = os.path.join(exp_path, "pytorch_lora_weights.safetensors")
pipe.unet.load_attn_procs(exp_path)
else:
exp_path = unet_pretraining_type + "_" + "diffusion_pytorch_model.safetensors"
state_dict = load_file(exp_path)
print(pipe.unet.load_state_dict(state_dict, strict=False))
def loadSDModel(unet_pretraining_type, exp_path, cuda_device):
"""
Loads the Stable Diffusion Model for the selected PEFT Type
Parameters:
unet_pretraining_type (str): The type of PEFT to use for generating the X-ray
exp_path (str): The path to the best trained model for the selected PEFT Type
cuda_device (str): The CUDA device to use for generating the X-ray
Returns:
pipe (StableDiffusionPipeline): The Stable Diffusion Pipeline to use for generating the X-ray
"""
sd_folder_path = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(sd_folder_path, revision="fp16")
load_adapted_unet(unet_pretraining_type, exp_path, pipe)
pipe.safety_checker = None
return pipe
def load_all_pipelines():
"""
Loads all the Stable Diffusion Pipelines for each PEFT Type for efficient caching (Design Choice 2)
Parameters:
None
Returns:
sd_pipeline_full (StableDiffusionPipeline): The Stable Diffusion Pipeline for Full Fine-Tuning
sd_pipeline_norm (StableDiffusionPipeline): The Stable Diffusion Pipeline for Norm Fine-Tuning
sd_pipeline_bias (StableDiffusionPipeline): The Stable Diffusion Pipeline for Bias Fine-Tuning
sd_pipeline_attention (StableDiffusionPipeline): The Stable Diffusion Pipeline for Attention Fine-Tuning
sd_pipeline_NBA (StableDiffusionPipeline): The Stable Diffusion Pipeline for NBA Fine-Tuning
sd_pipeline_difffit (StableDiffusionPipeline): The Stable Diffusion Pipeline for Difffit Fine-Tuning
"""
# Dictionary containing the path to the best trained models for each PEFT type
MODEL_PATH_DICT = {
"full": "full_diffusion_pytorch_model.safetensors",
"norm": "norm_diffusion_pytorch_model.safetensors",
"bias": "bias_diffusion_pytorch_model.safetensors",
"attention": "attention_diffusion_pytorch_model.safetensors",
"norm_bias_attention": "norm_bias_attention_diffusion_pytorch_model.safetensors",
"difffit": "difffit_diffusion_pytorch_model.safetensors",
}
device = "0"
cuda_device = f"cuda:{device}" if torch.cuda.is_available() else "cpu"
# Full FT
unet_pretraining_type = "full"
print("Loading Pipeline for Full Fine-Tuning")
sd_pipeline_full = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
# Norm
unet_pretraining_type = "norm"
print("Loading Pipeline for Norm Fine-Tuning")
sd_pipeline_norm = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
# bias
unet_pretraining_type = "bias"
print("Loading Pipeline for Bias Fine-Tuning")
sd_pipeline_bias = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
# attention
unet_pretraining_type = "attention"
print("Loading Pipeline for Attention Fine-Tuning")
sd_pipeline_attention = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
# NBA
unet_pretraining_type = "norm_bias_attention"
print("Loading Pipeline for NBA Fine-Tuning")
sd_pipeline_NBA = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
# difffit
unet_pretraining_type = "difffit"
print("Loading Pipeline for Difffit Fine-Tuning")
sd_pipeline_difffit = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
return (
sd_pipeline_full,
sd_pipeline_norm,
sd_pipeline_bias,
sd_pipeline_attention,
sd_pipeline_NBA,
sd_pipeline_difffit,
)
# LOAD ALL PIPELINES FIRST AND CACHE THEM
# (
# sd_pipeline_full,
# sd_pipeline_norm,
# sd_pipeline_bias,
# sd_pipeline_attention,
# sd_pipeline_NBA,
# sd_pipeline_difffit,
# ) = load_all_pipelines()
# PIPELINE_DICT = {
# "full": sd_pipeline_full,
# "norm": sd_pipeline_norm,
# "bias": sd_pipeline_bias,
# "attention": sd_pipeline_attention,
# "norm_bias_attention": sd_pipeline_NBA,
# "difffit": sd_pipeline_difffit,
# }
def predict(
unet_pretraining_type,
input_text,
guidance_scale=4,
num_inference_steps=75,
device="0",
OUTPUT_DIR="OUTPUT",
PIPELINE_DICT=PIPELINE_DICT,
):
NUM_TUNABLE_PARAMS = {
"full": 86,
"attention": 26.7,
"bias": 0.343,
"norm": 0.2,
"norm_bias_attention": 26.7,
"lorav2": 0.8,
"svdiff": 0.222,
"difffit": 0.581,
}
cuda_device = f"cuda:{device}" if torch.cuda.is_available() else "cpu"
#sd_pipeline = PIPELINE_DICT[unet_pretraining_type]
print("Loading Pipeline for {} Fine-Tuning".format(unet_pretraining_type))
sd_pipeline_norm = loadSDModel(
unet_pretraining_type=unet_pretraining_type,
exp_path=MODEL_PATH_DICT[unet_pretraining_type],
cuda_device=cuda_device,
)
sd_pipeline.to(cuda_device)
result_image = sd_pipeline(
prompt=input_text,
height=224,
width=224,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
)
result_pil_image = result_image["images"][0]
# Create a Bar Plot displaying the number of tunable parameters for the selected PEFT Type
# Create a Pandas DataFrame
df = pd.DataFrame(
{
"PEFT Type": list(NUM_TUNABLE_PARAMS.keys()),
"Number of Tunable Parameters": list(NUM_TUNABLE_PARAMS.values()),
}
)
df = df[df["PEFT Type"].isin(["full", unet_pretraining_type])].reset_index(
drop=True
)
bar_plot = gr.BarPlot(
value=df,
x="PEFT Type",
y="Number of Tunable Parameters",
label="PEFT Type",
title="Number of Tunable Parameters",
vertical=False,
)
return result_pil_image, bar_plot
# Create a Gradio interface
"""
Input Parameters:
1. PEFT Type: (Dropdown) The type of PEFT to use for generating the X-ray
2. Input Text: (Textbox) The text prompt to use for generating the X-ray
3. Guidance Scale: (Slider) The guidance scale to use for generating the X-ray
4. Num Inference Steps: (Slider) The number of inference steps to use for generating the X-ray
Output Parameters:
1. Generated X-ray Image: (Image) The generated X-ray image
2. Number of Tunable Parameters: (Bar Plot) The number of tunable parameters for the selected PEFT Type
"""
iface = gr.Interface(
fn=predict,
inputs=[
gr.Dropdown(
["full", "difffit", "svdiff", "norm", "bias", "attention"],
label="PEFT Type",
),
gr.Dropdown(
EXAMPLE_TEXT_PROMPTS, info=INFO_ABOUT_TEXT_PROMPT, label="Input Text"
),
gr.Slider(
minimum=1,
maximum=10,
value=4,
step=1,
info=INFO_ABOUT_GUIDANCE_SCALE,
label="Guidance Scale",
),
gr.Slider(
minimum=1,
maximum=100,
value=75,
step=1,
info=INFO_ABOUT_INFERENCE_STEPS,
label="Num Inference Steps",
),
],
outputs=[gr.Image(type="pil"), gr.BarPlot()],
live=True,
analytics_enabled=False,
title=TITLE,
)
# Launch the Gradio interface
iface.launch(share=True)
|