DreamBooth is an advanced technique designed for fine-tuning text-to-image diffusion models to generate personalized images of specific subjects. By leveraging a few reference images (around 5 or so), DreamBooth integrates unique visual features of the subject into the model's output domain.
This is achieved by binding a unique identifier "<..IDENTIFIER..>", such as <leaf microstructure> in this work, to the subject. An optional class-specific prior preservation loss can be used to maintain high fidelity and contextual diversity. The result is a model capable of synthesizing novel, photorealistic images of the subject in various scenes, poses, and lighting conditions, guided by text prompts. In this project, DreamBooth has been applied to render images with specific biological patterns, making it ideal for applications in materials science and engineering where accurate representation of biological material microstructures is crucial.
For example, an original prompt might be: "a vase with intricate patterns, high quality." With the fine-tuned model, using the unique identifier, the prompt becomes: "a vase that resembles a <leaf microstructure>, high quality." This allows the model to generate images that specifically incorporate the desired biological pattern.
Model description
These are LoRA adaption weights for the SDXL-base-1.0 model (stabilityai/stable-diffusion-xl-base-1.0).
Trigger keywords
The following images were used during fine-tuning using the keyword <leaf microstructure>:
image/png
You should use <leaf microstructure> to trigger this feature during image generation.
1prompt ="a vase that resembles a <leaf microstructure>, high quality"23num_samples =44num_rows =45guidance_scale =1567all_images =[]89for _ inrange(num_rows):10# Define how many steps and what % of steps to be run on each experts (80/20)11 n_steps =2512 high_noise_frac =0.81314# run both experts15 image = base(16 prompt=prompt,17 num_inference_steps=n_steps, guidance_scale=guidance_scale,18 denoising_end=high_noise_frac,num_images_per_prompt=num_samples,19 output_type="latent",20).images
21 image = refiner(22 prompt=prompt,23 num_inference_steps=n_steps, guidance_scale=guidance_scale,24 denoising_start=high_noise_frac,num_images_per_prompt=num_samples,25 image=image,26).images
2728 all_images.extend(image)2930grid = image_grid(all_images, num_rows, num_samples,31 save_individual_files=True,32)33grid
The code will automatically download the training script.
The training script can handle custom prompts associated with each image, which are generated using BLIP.
For instance, for the images used here, they are:
raw
1{"file_name": "0.jpeg", "prompt": "<leaf microstructure>, a close up of a green plant with a lot of small holes"}
2{"file_name": "1.jpeg", "prompt": "<leaf microstructure>, a close up of a leaf with a small insect on it"}
3{"file_name": "2.jpeg", "prompt": "<leaf microstructure>, a close up of a plant with a lot of green leaves"}
4{"file_name": "3.jpeg", "prompt": "<leaf microstructure>, a close up of a leaf with a yellow substance in it"}
5{"file_name": "87.jpg", "prompt": "<leaf microstructure>, a close up of a green plant with a yellow light"}
6{"file_name": "88.jpg", "prompt": "<leaf microstructure>, a close up of a green plant with a white center"}
7{"file_name": "90.jpg", "prompt": "<leaf microstructure>, arafed leaf with a white line on the center"}
8{"file_name": "91.jpg", "prompt": "<leaf microstructure>, arafed image of a green leaf with a white spot"}
9{"file_name": "92.jpg", "prompt": "<leaf microstructure>, a close up of a leaf with a yellow light shining through it"}
10{"file_name": "94.jpg", "prompt": "<leaf microstructure>, arafed image of a green plant with a yellow cross"}
Training then proceeds as:
python
1HF_username ='lamm-mit'23pretrained_model_name_or_path="stabilityai/stable-diffusion-xl-base-1.0"4pretrained_vae_model_name_or_path="madebyollin/sdxl-vae-fp16-fix"56instance_prompt ="<leaf microstructure>"7instance_data_dir ="./leaf_concept_dir_SDXL/"89val_prompt ="a vase that resembles a <leaf microstructure>, high quality"10val_epochs =1001112instance_output_dir="leaf_LoRA_SDXL_V10"#for checkpointing
This produces a JSON file in the instance_data_dir directory:
raw
1{"file_name": "0.jpeg", "prompt": "<leaf microstructure>, a close up of a green plant with a lot of small holes"}
2{"file_name": "1.jpeg", "prompt": "<leaf microstructure>, a close up of a leaf with a small insect on it"}
3{"file_name": "2.jpeg", "prompt": "<leaf microstructure>, a close up of a plant with a lot of green leaves"}
4{"file_name": "3.jpeg", "prompt": "<leaf microstructure>, a close up of a leaf with a yellow substance in it"}
5{"file_name": "87.jpg", "prompt": "<leaf microstructure>, a close up of a green plant with a yellow light"}
6{"file_name": "88.jpg", "prompt": "<leaf microstructure>, a close up of a green plant with a white center"}
7{"file_name": "90.jpg", "prompt": "<leaf microstructure>, arafed leaf with a white line on the center"}
8{"file_name": "91.jpg", "prompt": "<leaf microstructure>, arafed image of a green leaf with a white spot"}
9{"file_name": "92.jpg", "prompt": "<leaf microstructure>, a close up of a leaf with a yellow light shining through it"}
10{"file_name": "94.jpg", "prompt": "<leaf microstructure>, arafed image of a green plant with a yellow cross"}
Set --with_prior_preservation flag to include prior preservation. In this case you must specify --class_data_dir (directory with class images) and --class_prompt (class prompt). You should also set --num_class_images to specify how many class preservation images you want to use. Either place them in the directory (specified via --class_data_dir) or the code with auto-generate them based off the base model. You can also provide a few yourself and let the code generate the remaining ones.
An example is provided below, commented out. The code that will run here will NOT use prior preservation.
Some other useful parameters that can be set include:
--rank: LoRA adapter rank (LoRA alpha will be set identical to rank)
--use_dora: Set if you want to use DORA
Type python train_dreambooth_lora_sdxl.py to get a full list of parameters
python
1instance_data_dir ='local_instance_data_dir'2class_prompt ='a prompt that describes the images in the directory local_instance_data_dir'3num_class_images =10#how many images you want in this class45!\accelerate launch train_dreambooth_lora_sdxl.py \
6--pretrained_model_name_or_path="{pretrained_model_name_or_path}" \
7--pretrained_vae_model_name_or_path="{pretrained_vae_model_name_or_path}"\
8--dataset_name="{instance_data_dir}" \
9--class_prompt="{class_prompt}" \
10--num_class_images={num_class_images} \
11--with_prior_preservation \
12--class_data_dir="{class_data_dir}" \
13--output_dir="{instance_output_dir}" \
14--caption_column="prompt"\
15--mixed_precision="fp16" \
16--instance_prompt="{instance_prompt}" \
17--validation_prompt="{val_prompt}" \
18--validation_epochs={val_epochs} \
19--resolution=1024 \
20--train_batch_size=1 \
21--gradient_accumulation_steps=4 \
22--gradient_checkpointing \
23--learning_rate=1e-4 \
24--snr_gamma=5.0 \
25--lr_scheduler="constant" \
26--lr_warmup_steps=0 \
27--mixed_precision="fp16" \
28--use_8bit_adam \
29--max_train_steps=500 \
30--checkpointing_steps=500 \
31--seed="0"