{ "cells": [ { "cell_type": "markdown", "id": "494d5ce4-5843-4d70-ae96-c1983e21b6e8", "metadata": {}, "source": [ "## Dreambooth v2 Paperspace Notebook From https://github.com/TheLastBen/fast-stable-diffusion, if you encounter any issues, feel free to discuss them. [Support](https://ko-fi.com/thelastben)" ] }, { "cell_type": "markdown", "id": "8afdca63-eff3-4a9d-b4d9-127c0f028033", "metadata": { "tags": [] }, "source": [ "# Dependencies" ] }, { "cell_type": "code", "execution_count": null, "id": "be74b2d5-da96-4bf4-ae82-4fe4b8abc04c", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Install the dependencies\n", "\n", "force_reinstall= False\n", "\n", "# Set to true only if you want to install the dependencies again.\n", "\n", "\n", "#--------------------\n", "with open('/dev/null', 'w') as devnull:import requests, os, time, importlib;open('/notebooks/mainpaperspacev2.py', 'wb').write(requests.get('https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Scripts/mainpaperspacev2.py').content);os.chdir('/notebooks');time.sleep(3);import mainpaperspacev2;importlib.reload(mainpaperspacev2);from mainpaperspacev2 import *;Deps(force_reinstall)" ] }, { "cell_type": "markdown", "id": "7a4ef4a2-6863-4603-9254-a1e2a547ee38", "metadata": { "tags": [] }, "source": [ "# Download the model" ] }, { "cell_type": "code", "execution_count": null, "id": "a1ba734e-515b-4761-8c88-ef7f165d7971", "metadata": { "tags": [] }, "outputs": [], "source": [ "Model_Version = \"768\"\n", "\n", "# Choices are : \"512\", \"768\"\n", "\n", "#-----------------------------------------------------------------------------------------------------------------------------------\n", "\n", "Custom_Model_Version = \"768\"\n", "\n", "# Choices are : \"512\", \"768\"\n", "\n", "Path_to_HuggingFace= \"\"\n", "\n", "# Load and finetune a model from Hugging Face, use the format \"profile/model\" like : runwayml/stable-diffusion-v1-5.\n", "\n", "Model_Path = \"\"\n", "\n", "# Load a CKPT model from the storage.\n", "\n", "Model_Link = \"\"\n", "\n", "# A CKPT direct link, huggingface CKPT link or a shared CKPT from gdrive.\n", "\n", "\n", "safetensors= False\n", "\n", "# Set to True if the model from the link/path is in safetensors format.\n", "\n", "\n", "#-------------\n", "MODEL_NAMEv2=dlv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version, Custom_Model_Version, safetensors)" ] }, { "cell_type": "markdown", "id": "4c6c4932-e614-4f5e-8d4a-4feca5ce54f5", "metadata": {}, "source": [ "# Create/Load a Session" ] }, { "cell_type": "code", "execution_count": null, "id": "b6595c37-8ad2-45ff-a055-fe58c6663d2f", "metadata": { "tags": [] }, "outputs": [], "source": [ "Session_Name = \"sd-session\"\n", "\n", "# Enter the session name, it if it exists, it will load it, otherwise it'll create an new session.\n", "\n", "Session_Link_optional = \"\"\n", "\n", "# Import a session from another gdrive, the shared gdrive link must point to the specific session's folder that contains the trained CKPT, remove any intermediary CKPT if any.\n", "\n", "Model_Version = \"768\"\n", "\n", "# Ignore this if you're not loading a previous session that contains a trained model, choices are : \"512\", \"768\"\n", "\n", "\n", "#-----------------\n", "[PT, WORKSPACE, Session_Name, INSTANCE_NAME, OUTPUT_DIR, SESSION_DIR, CONCEPT_DIR, INSTANCE_DIR, CAPTIONS_DIR, MDLPTH, MODEL_NAMEv2, resumev2]=sessv2(Session_Name, Session_Link_optional, Model_Version, MODEL_NAMEv2 if 'MODEL_NAMEv2' in locals() else \"\")" ] }, { "cell_type": "markdown", "id": "5698de61-08d3-4d90-83ef-f882ed956d01", "metadata": {}, "source": [ "# Instance Images" ] }, { "cell_type": "code", "execution_count": null, "id": "bc2f8f28-226e-45b8-8257-804bbb711f56", "metadata": { "tags": [] }, "outputs": [], "source": [ "Remove_existing_instance_images= True\n", "\n", "# Set to False to keep the existing instance images if any.\n", "\n", "\n", "IMAGES_FOLDER_OPTIONAL=\"\"\n", "\n", "# If you prefer to specify directly the folder of the pictures instead of uploading, this will add the pictures to the existing (if any) instance images. Leave EMPTY to upload.\n", "\n", "\n", "Smart_crop_images= True\n", "\n", "# Automatically crop your input images.\n", "\n", "\n", "Crop_size = 768\n", "\n", "# Choices: \"512\", \"576\", \"640\", \"704\", \"768\", \"832\", \"896\", \"960\", \"1024\"\n", "\n", "# Check out this example for naming : https://i.imgur.com/d2lD3rz.jpeg\n", "\n", "\n", "#-----------------\n", "uplder(Remove_existing_instance_images, Smart_crop_images, Crop_size, IMAGES_FOLDER_OPTIONAL, INSTANCE_DIR, CAPTIONS_DIR, False)" ] }, { "cell_type": "markdown", "id": "0e93924f-a6bf-45d5-aa77-915ad7385dcd", "metadata": {}, "source": [ "# Manual Captioning" ] }, { "cell_type": "code", "execution_count": null, "id": "c5dbcb29-b42f-4cfc-9be8-83355838d5a2", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Open a tool to manually caption the instance images.\n", "\n", "#-----------------\n", "caption(CAPTIONS_DIR, INSTANCE_DIR)" ] }, { "cell_type": "markdown", "id": "c90140c1-6c91-4cae-a222-e1a746957f95", "metadata": {}, "source": [ "# Concept Images" ] }, { "cell_type": "code", "execution_count": null, "id": "55c27688-8601-4943-b61d-fc48b9ded067", "metadata": {}, "outputs": [], "source": [ "Remove_existing_concept_images= True\n", "\n", "# Set to False to keep the existing concept images if any.\n", "\n", "\n", "IMAGES_FOLDER_OPTIONAL=\"\"\n", "\n", "# If you prefer to specify directly the folder of the pictures instead of uploading, this will add the pictures to the existing (if any) concept images. Leave EMPTY to upload.\n", "\n", "\n", "#-----------------\n", "uplder(Remove_existing_concept_images, True, 512, IMAGES_FOLDER_OPTIONAL, CONCEPT_DIR, CAPTIONS_DIR, True)" ] }, { "cell_type": "markdown", "id": "2a4aa42a-fd68-41ad-9ba7-da99f834e2c1", "metadata": {}, "source": [ "# Dreambooth" ] }, { "cell_type": "code", "execution_count": null, "id": "612d8335-b984-4f34-911d-5457ff98e507", "metadata": {}, "outputs": [], "source": [ "Resume_Training = False\n", "\n", "# If you're not satisfied with the result, Set to True, run again the cell and it will continue training the current model.\n", "\n", "\n", "UNet_Training_Steps= 1500\n", "\n", "UNet_Learning_Rate= \"2e-6\"\n", "\n", "# If you use 10 images, use 650 steps, if you're not satisfied with the result, resume training for another 200 steps with a lower learning rate (8e-6), and so on ...\n", "\n", "\n", "Text_Encoder_Training_Steps= 350\n", "\n", "Text_Encoder_Learning_Rate= \"1e-6\"\n", "\n", "# 350-600 steps is enough for a small dataset, keep this number small to avoid overfitting, set to 0 to disable, set it to 0 before resuming training if it is already trained.\n", "\n", "\n", "Text_Encoder_Concept_Training_Steps= 0\n", "\n", "# Suitable for training a style/concept as it acts as regularization, with a minimum of 300 steps, 1 step/image is enough to train the concept(s), set to 0 to disable, set both the settings above to 0 to fintune only the text_encoder on the concept, set it to 0 before resuming training if it is already trained.\n", "\n", "\n", "External_Captions= False\n", "\n", "# Get the captions from a text file for each instance image.\n", "\n", "\n", "Offset_Noise= False\n", "\n", "# Always use it for style training.\n", "\n", "\n", "Resolution = 768\n", "\n", "# Choices : \"512\", \"576\", \"640\", \"704\", \"768\", \"832\", \"896\", \"960\", \"1024\"\n", "# Higher resolution = Higher quality, make sure the instance images are cropped to this selected size (or larger).\n", "\n", "#---------------------------------------------------------------\n", "\n", "Save_Checkpoint_Every_n_Steps= False\n", "\n", "Save_Checkpoint_Every= 500\n", "\n", "# Minimum 200 steps between each save.\n", "\n", "\n", "Start_saving_from_the_step= 500\n", "\n", "# Start saving intermediary checkpoints from this step.\n", "\n", "\n", "#-----------------\n", "resumev2=dbtrainv2(Resume_Training, UNet_Training_Steps, UNet_Learning_Rate, Text_Encoder_Training_Steps, Text_Encoder_Concept_Training_Steps, Text_Encoder_Learning_Rate, Offset_Noise, Resolution, MODEL_NAMEv2, SESSION_DIR, INSTANCE_DIR, CONCEPT_DIR, CAPTIONS_DIR, External_Captions, INSTANCE_NAME, Session_Name, OUTPUT_DIR, PT, resumev2, Save_Checkpoint_Every_n_Steps, Start_saving_from_the_step, Save_Checkpoint_Every)" ] }, { "cell_type": "markdown", "id": "bf6f2232-60b3-41c5-bea6-b0dcc4aef937", "metadata": {}, "source": [ "# Test the Trained Model" ] }, { "cell_type": "code", "execution_count": null, "id": "1263a084-b142-4e63-a0aa-2706673a4355", "metadata": {}, "outputs": [], "source": [ "Previous_Session_Name=\"\"\n", "\n", "# Leave empty if you want to use the current trained model.\n", "\n", "\n", "Custom_Path = \"\"\n", "\n", "# Input the full path to a desired model.\n", "\n", "\n", "User = \"\" \n", "\n", "Password= \"\"\n", "\n", "# Add credentials to your Gradio interface (optional).\n", "\n", "\n", "Use_localtunnel = False\n", "\n", "# If you have trouble using Gradio server, use this one.\n", "\n", "\n", "#-----------------\n", "configf=test(Custom_Path, Previous_Session_Name, Session_Name, User, Password, Use_localtunnel) if 'Session_Name' in locals() else test(Custom_Path, Previous_Session_Name, \"\", User, Password, Use_localtunnel)\n", "!python /notebooks/sd/stable-diffusion-webui/webui.py $configf" ] }, { "cell_type": "markdown", "id": "53ccbcaf-3319-44f5-967b-ecbdfa9d0e78", "metadata": {}, "source": [ "# Upload The Trained Model to Hugging Face" ] }, { "cell_type": "code", "execution_count": null, "id": "2c9cb205-d828-4e51-9943-f337bd410ea8", "metadata": {}, "outputs": [], "source": [ "#Save it to your personal profile or collaborate to the public [library of concepts](https://huggingface.co/sd-dreambooth-library)\n", "\n", "Name_of_your_concept = \"\"\n", "\n", "# Leave empty if you want to name your concept the same as the current session.\n", "\n", "\n", "hf_token_write = \"\"\n", "\n", "# Create a write access token here : https://huggingface.co/settings/tokens, go to \"New token\" -> Role : Write, a regular read token won't work here.\n", "\n", "\n", "#---------------------------------\n", "hfv2(Name_of_your_concept, Save_concept_to, hf_token_write, INSTANCE_NAME, OUTPUT_DIR, Session_Name, MDLPTH)" ] }, { "cell_type": "markdown", "id": "881d80a3-4ebf-41bc-b68f-ac1cacb080f3", "metadata": {}, "source": [ "# Free up space" ] }, { "cell_type": "code", "execution_count": null, "id": "7403744d-cc45-419f-88ac-5475fa0f7f45", "metadata": {}, "outputs": [], "source": [ "# Display a list of sessions from which you can remove any session you don't need anymore\n", "\n", "#-------------------------\n", "clean()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }