Training Models using Dreambooth (Google Colab) (2024)

Training Models using Dreambooth (Google Colab) (1)

Imagine you don't have enough GPU to train and fine-tune the model. Your quick solution is Dreambooth, officially managed by Google is a way to fine tuning your subject with a set of relevant data. We can train any dataset like any objects, human faces, animals etc. The more detailed information can be gathered from the research paper.

Here, we are going to fine tune the pre-trained stable diffusion model with new image data set. To do this, there are multiple ways like LoRA, Hyper networks, etc. are available which we have covered.

Now, we will see what we can do using Dreambooth in Google Colab. Well, you can also do in locally to perform this operation but make sure you have at least 8GB(recommended) of VRAM.

Training Steps:

Google Colab provides the provision to test your project on the free tier. If you observe any performance issues while training then you can also switch to their paid plan. But, for tutorial, we are using the free plan.

Training Models using Dreambooth (Google Colab) (2)

1. First, move to the Dreambooth notebook file which will open into the Google Colab provided below.

https://colab.research.google.com/github/TheLastBen/fast-stable-diffusion/blob/main/fast-DreamBooth.ipynb

2. Click on the "Connect" button to get connected to T4 GPU. It's compulsory to activate it to use the VRAM. After getting connected it will show a green check mark.

Training Models using Dreambooth (Google Colab) (3)

3. Next is to click on "Play" button to connect to your Google Drive. Yeah, this will use your Google Drive for temporary purpose to store the models and perquisites.

You can check the respective repository by analyzing the stars and forks it got. This signifies that how much popular GitHub repository. This determines that the community is using it aggressively and its trustworthy.

Training Models using Dreambooth (Google Colab) (4)

Now, click "Run anyway" and again select "Connect to google Drive".

4. Select the model you want to do training with. By default it has Stable Diffusion1.5, you can also alternatively use different models like Stable Diffusion 2.1, Stable Diffusion XL1.0 from Hugging Face platform.

Training Models using Dreambooth (Google Colab) (5)

To use other models you need to just copy and paste the Hugging Face Id of any specific model into the given box "Path_to_HuggingFace".

Training Models using Dreambooth (Google Colab) (6)

5. Other ways is that if you have the ".ckpt" files, then you can also upload them into your Google Drive and copy the its path by right clicking on it and paste into the "MODEL_PATH" section. After that just click on respective "Play" button to start the Colab session.

Training Models using Dreambooth (Google Colab) (7)

6. Now, you have to create or load your session. It means that if you are new to this Colab and doing the Dreambooth training for the first time then, you need to create a session by creating its name (You can name it anything that relate your workflow).

But if you have already created and saved the trained session (ckpt file), then simply add the path from google drive of it. Then, click the "Play" button.

Training Models using Dreambooth (Google Colab) (8)

7. Instance images section: Now you will see check box "Remove_existing_instance_images". This option is used to remove the existing images which you have previously used for training your model. Uncheck it if you are training for the first time.

8. IMAGES_FOLDER_OPTIONAL-If you don't want to upload images from your PC/Laptop then this as an alternative. Actually, this method is used for adding the path location of your images folder which you will use to do training with. Make sure you use the Google drive folder link only.

9. Smart_Crop_Images- This option is used to crop your uploaded images. It means that uploaded images should be in the same dimensions. You can use image cropping tools like MS Paint, Photoshop or any online tool for better output because we experienced sometimes the smart cropping option doesn't work up to mark. By default, the value is 512.

Crop_size-To crop the size mention the size in pixels like 512, 1024, or 768. However, we have selected stable Diffusion 1.5 as a pre-trained model. So, its mandatory to use 512 by 512 dimensions because the Stable diffusion1.5 has been trained on these dimensions.

If you are using Stable Diffusion XL(SDXL) then you should use 1024 by 1024 dimensions.

Training Models using Dreambooth (Google Colab) (9)

Another important thing is you also is to rename your image files into serial wise with same name as we have shown as example above. All the images should be in the same image format(which means you must not use one image in jpeg and another in PNG format).

If you are a windows user, then you need to enable the option "File name extensions" under Menu section otherwise you can't rename the files with its extensions.

10. Now we have earlier mentioned that if you want to upload your set of images from a PC/Laptop then simply use this "Choose files" option. During the uploading process, all the image files will be shown in real time.

After uploading, a confirmation message will be shown "Done, proceed to the next cell".

Training Models using Dreambooth (Google Colab) (10)

11. Caption- The Caption section is used to caption your uploaded images with a description. Run the cell by pressing the "Play" button. after a few seconds you will see all the uploaded images.

Try to rename it as what is described in the image by selecting one by one and save it. Take in mind to clearly describe the image because the better you add the relevant information the better your model will generate the results.

Training Models using Dreambooth (Google Colab) (11)

12. Concept Images(Regularization)- Well, this is optional. There are some cases where you need to train you model in some specific type or art style like Picasso, Davincii or Anime. Then you need to upload the amount of 100-300 images of a similar specific style that you have wanted. For example you want to train a cat image then you have to use only the cat's images in different positions.

Similarly, like we explained earlier "IMAGES_FOLDER_OPTIONAL" is helps to use your Google drive folder path for Regularization images.

Training Models using Dreambooth (Google Colab) (12)

13. Training section- According to the developers of Dreambooth, Stable Diffusion easily over fits much easier. So, its to take care between its learning rate and the training steps. So, they instructed in their research paper is to use lower learning rate yielding in better results.

While training what we experienced is for human faces and objects (tabular form) the best combination is-


Object Human
Learning Rate 2e-6 1e-6 or 2e-6
Training steps 4501200 or 1500

Here, the "Resume Training" option is used to resume the training wherever you have left it.

Now, after training if you observe that the images generated are not up to mark, like blurry, noisy then you need to play with the learning rate and training steps values.

If we don't get satisfactory results, then we will try to change the learning rate from 1500 to ~2000. Again if this will not be perfect then we will try with learning rate 1500 with training steps of "1e-6".

Sometimes you just don't need to do anything.

14. Text_Encoder_Training_Steps- This option is used for mentioning the steps. Usually, 200-450 is used for smaller data sets. In our case it's a very small amount of images. So, feeding it to 200 is a good value.

Training Models using Dreambooth (Google Colab) (13)

15. Text_Encoder_Concept_Training_Steps- This is used when you are training an object in different positions. This is set to 0 if you are using human faces.

16. Text_Encoder_Learning_Rate- Its been instructed to keep it low to avoid over fitting (1e-6 is higher than 4e-7). This means its 1 timesthe power of -6 and 4 timesthe power of -7.

17. Offset Noise- You should use this option if you are training your model with something particular style. But, we have unchecked this because we are only training the human faces.

18. External Captions- This is for external text files with captioning. It can be left as it is.

19. Resolution- Make sure to use the lower value as compared with the resolution you have set in after cropping the images. For instance, if you have uploaded images with 1024 pixels then you should set it between 512-960.

Training Models using Dreambooth (Google Colab) (14)

20. Save_Checkpoint_Every_n_Steps-Because training takes almost 20-30 minutes, an option to save your checkpoints with every step has been provided.

Training Models using Dreambooth (Google Colab) (15)

21. Test the trained model- This section is used to testing the already trained model. This will open the Stable Diffusion WebUI for using your local tunnel. To use this, you need to check the "Use_localtunnel" option.

Training Models using Dreambooth (Google Colab) (16)

The alternate way is to use the Gradio app with your credentials. Now, you can run the cell by clicking on the play button.

Training Models using Dreambooth (Google Colab) (17)

After a few seconds, you will see a live URL. Click on it to open a WebUI.

22. Save your trained model to Hugging Face- After training your model, you can sign in to the Hugging Face account and save your models as your repository. Now, if you want to load your saved model then simply copy its Hugging Face ID and paste inPath_to_HuggingFaceand use it in your workflow.

Important Tips:

According to the research paper, if you are not getting the perfect results in image generation then you should try some tweaking with the parameters which we have mentioned below:-

  1. Try different sampling methods like Euler, DDIM or DPM++, etc.
  2. Be focused on detailing when putting your image prompt.
  3. Use negative prompts to add more perfection.
  4. Symbols can also be used to give more importance.
  5. Set the CFG scale to low from7-15for more creativity.
  6. Using Sampling Steps values of20-25will be better but the generation time will be comparatively longer.
Training Models using Dreambooth (Google Colab) (2024)

FAQs

Can I train models on Google Colab? ›

Colaboratory by Google (Google Colab in short) is a Jupyter notebook based runtime environment which allows you to run code entirely on the cloud. This is necessary because it means that you can train large scale ML and DL models even if you don't have access to a powerful machine or a high speed internet access.

Is Google Colab enough for machine learning? ›

Colab allows anyone to write and run arbitrary Python code from the browser, making it especially suitable for machine learning, data analysis, and education. Technically, Colab is a hosted Jupyter notebook service that provides free access to computing resources, including GPUs, while requiring no setup to use.

How long does stable diffusion Dreambooth take to train? ›

Training with Dreambooth only requires a limited amount of training data. For example: 20 images is enough to create an embedding, and the data doesn't have to be high-quality. It will still work fine with poorly lit or incomplete images. The training time is typically around an hour on an RTX3090 with 24G GPU RAM.

How long can I train a model on Google Colab? ›

Not forgetting about the maximum lifetime of a Colab instance of 12 hours. This sort of makes sense, that they want to fully utilize their GPU and TPUs and ensure that they are always in use and not idling. But it does not help you and me if we want to train our models overnight and over many days.

Which is better, Google Colab or Jupyter Notebook? ›

- Use Jupyter Notebooks if you prefer local development or if your organization has its own JupyterHub setup. - Choose Google Colab if you require free access to GPU/TPU resources, seamless collaboration, or if you prefer a cloud-based environment integrated with Google Drive.

What is the disadvantage of Google Colab? ›

Disadvantages: Limited runtime, dependency on internet connection. Advantages: Provides computational resources for running CNN training, avoids software configuration. Disadvantages: Potential challenges and risks in relying on Colab as an educational platform.

Why not use Google Colab? ›

No Live-Editing: Writing a code and sharing the same with your partner or a team allows you to collaborate. However, the option for live editing is completely missing in Google Colab, which restricts two people to write, or edit codes at the same time. Hence, it further leads to a lot of back and forth re-sharing.

How many hours can colab run? ›

In the version of Colab that is free of charge notebooks can run for at most 12 hours, depending on availability and your usage patterns. Colab Pro and Pay As You Go offer you increased compute availability based on your compute unit balance.

Can you train your own Stable Diffusion model? ›

How to train Stable Diffusion model at home? You have the flexibility to train your Stable Diffusion model using a range of tools and platforms, including Jupyter Notebooks, or TensorFlow. These platforms allow you to conduct experiments, handle model management tasks, and generate images seamlessly.

What is the difference between DreamBooth and LoRA? ›

Dreambooth produces larger models, Textual Inversion results in very small and easily shareable embeddings, and LoRA offers a faster training time with smaller, portable layers.

How many images do you need for Stable Diffusion training? ›

Training the Stable Diffusion Model. Now the pictures: You'll want at least 20 pictures or so for your AI model to analyze in order to avoid creating a bunch of generic person art or nightmare fuel. So bust out your phone and take some selfies!

Is Google Colab good for training? ›

Google Colab is particularly popular with machine learning researchers and practitioners. It provides access to free GPUs and TPUs, making it much easier to train machine learning models that require a lot of computing power.

Can I run Colab overnight? ›

Colab Pro+ supports continuous code execution for up to 24 hours if you have sufficient compute units. Idle timeouts only apply if code execution terminates. You can fully relax any runtime limits and idle timeouts by purchasing a dedicated VM at GCP Marketplace.

What are the limits of Google Colab? ›

Vertex AI request quotas
Vertex AI quotaColab Enterprise interactionLimit
Resource management requestsRuntime and runtime template requests600/minute
Job or long-running operation requests per minuteRuntime and runtime template requests60/minute

How to run model on GPU in Colab? ›

Setting up the Runtime: In Google Colab, go to the "Runtime" menu and select "Change runtime type." A dialog box will appear where you can choose the runtime type and hardware accelerator. Select "GPU" as the hardware accelerator and click "Save." This step ensures that your Colab notebook is configured to use the GPU.

How do I import a model into Colab? ›

Technique 1: Uploading the Module to Colab

You can do this by clicking on the “Files” button on the left-hand side of the Colab interface, and then clicking on the “Upload” button. Once you have uploaded the module file, you can import it in your Colab notebook using a standard Python import statement.

How to deploy a model from Colab? ›

Deploy-ML-models-using-google-colab

Create a new folder in the clients drive for the project. . Create an appropriate predict function for taking input from client and using the saved model make predictions and give output.

Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6036

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.