Create a Diffusers-compatible Dataset for Stable Diffusion Fine-tuning
Community Article
Published
July 19, 2024
To appeal to the youth, I will be speaking in you kid's lingo. π
Yo, whatβs up squad! π Today, weβre diving into how to make a diffusers dataset (yeah, I know, itβs kinda meh, but hang tight!). π€
Step 1. Load your groovy Img2Img pipeline and send it to GPU (whatever that means) πππ
from transformers import pipeline
pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device="cuda") # I'm using blip, as that's what most SD models expect
Step 2: Time for a Break (You #earnedIt! ποΈπ)
Chill for a sec and letβs regroup. Grab a snack or something. ππ
Step 3. Epic to have you back, now let's set up your Super Cool Image Folder πβ¨
import os
folder = "your_image_folder" # file structure is your_image_folder/train/*.png
image_folder = os.path.join(folder, "train")
Step 4. That was totally exhausting, all those characters I had to copy and paste without giving credit, let's take another break π₯΅π₯΅π₯΅π₯΅π₯
Rest those fingers, βcause weβre about to get back to it. π€―π₯
Step- I forgot.. 4 I think? Load That Folder #LikeABoss ππΆοΈ
from datasets import load_dataset
dataset = load_dataset('imagefolder', data_dir=image_folder, split='train')
Step 4 (yeah that sounds right). Craft Your Formatting Function with Style π§ββοΈπ¨
def generate_captions(examples):
captions = []
for image in examples['image']:
result = pipe(image)
caption = result[0]['generated_text']
captions.append(caption)
return {"text": captions}
Step 4. Map the dataset (data again? that's so lame)
dataset = dataset.map(generate_captions, batched=True)
Step 4. Bring back the train split πππͺ
from datasets import DatasetDict
dataset_dict = DatasetDict({
'train': dataset
})
dataset_dict # test to see if the new columns are there in train, if you were paying attention they should be
Step 4. Push It to the Hub (Is it a cool club now? π€·ββοΈ) π₯π
dataset_dict.push_to_hub("your_output_repo")
Step 4. π You Did It! π (finally)
Now go watch Vine or whatever. Wait- that's not right.. you kids are using that new short-form-video platform.. Tik Tak?