ติดตั้งและใช้งาน Google Colab Stable Diffusion 2024

3 min read 5 hours ago
Published on Nov 24, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to set up and use Stable Diffusion on Google Colab. This is particularly useful for those with lower-spec computers or without a dedicated graphics card, as Google Colab provides the necessary resources. The service incurs a small fee, but it allows you to utilize powerful computing capabilities without needing local hardware upgrades.

Step 1: Set Up Your Google Drive

Before using Google Colab, you need to ensure you have sufficient storage space on your Google Drive.

  • Check Storage: Ensure you have at least 10 GB of free space.
  • Create a Google Account: If you don’t already have one, create a Google account to access Google Drive.

Step 2: Access Google Colab

To start using Stable Diffusion, you need to access Google Colab.

  • Visit Google Colab: Go to Google Colab.
  • Sign In: Use your Google account credentials to log in.

Step 3: Create a New Notebook

Once you are in Google Colab, you will need to create a new notebook.

  • New Notebook: Click on File in the top left corner and select New Notebook.
  • Rename Notebook: Click on the default notebook name (usually "Untitled0") and rename it to something relevant, like "Stable Diffusion Setup".

Step 4: Install Required Libraries

Stable Diffusion requires specific libraries to function properly. You will need to run some code to install these.

  • Run the following code in a code cell:
!pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
!pip install diffusers transformers
  • Execute the Cell: Click the play button to run the code and install the necessary libraries.

Step 5: Load Stable Diffusion Model

You will need to load the Stable Diffusion model into your notebook.

  • Insert Another Code Cell: Click on the + Code button.
  • Run the following code:
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
  • Execute the Cell: Wait for the model to load. This may take some time depending on your internet speed.

Step 6: Generate Images

Now you can start generating images using the Stable Diffusion model.

  • Insert Another Code Cell: Click on + Code.
  • Run the following code to generate an image:
prompt = "A fantasy landscape"
image = pipe(prompt).images[0]
image.save("fantasy_landscape.png")
  • Change the Prompt: Modify the string in the prompt variable to generate different images.
  • Execute the Cell: This will save the generated image to your Google Drive.

Step 7: Download Your Images

To access the generated images, you’ll need to download them from Google Drive.

  • Locate the File: Open Google Drive and find the saved image.
  • Download: Right-click on the image file and select Download to save it to your local device.

Conclusion

You have successfully set up and used Stable Diffusion on Google Colab! This setup allows you to generate high-quality images without needing a high-performance computer. For further exploration, try different prompts and settings in the code to create unique artworks. Additionally, consider exploring more advanced features and models available through the Diffusers library. Happy generating!