Hanah

Hanah

TinyDiffusion

Reference:Datawhale tinydiffusion

Kaggle Implementation:#

Data Preparation#

  • git clone to download tinydiffusion locally
  • Download the cifar-10-python dataset to the datasets/ folder, maintaining the directory structure as datasets/cifar-10-batches-py.
  • Package and upload to Kaggle dataset, named cifar-10-python.

Run#

  • Create a new notebook, add the dataset cifar-10-python as input.
  • Run the default code
  • Configure the environment
!pip install -r /kaggle/input/tinydiffusion/TinyDiffusion/ddpm/requirements.txt
  • Copy the dataset to the output path
import shutil
import os

# Define the input and output directories for the dataset
input_dir = '/kaggle/input/tinydiffusion'
output_dir = '/kaggle/output/tinydiffusion'

# Check if the output directory exists, if not, create it
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# Traverse all files and folders in the input directory
for root, dirs, files in os.walk(input_dir):
    # Build the corresponding output directory
    relative_path = os.path.relpath(root, input_dir)
    output_sub_dir = os.path.join(output_dir, relative_path)
    
    # If the output subdirectory does not exist, create it
    if not os.path.exists(output_sub_dir):
        os.makedirs(output_sub_dir)
    
    # Copy files to the output directory
    for file in files:
        input_file_path = os.path.join(root, file)
        output_file_path = os.path.join(output_sub_dir, file)
        shutil.copy2(input_file_path, output_file_path)

print("Dataset copy completed!")
  • Create a path to store images generated every 10 epochs
import os

# Define the target directory for saving images
save_dir = '/kaggle/output/tinydiffusion/TinyDiffusion/samples'

# Check if the directory exists, if not, create it
if not os.path.exists(save_dir):
    os.makedirs(save_dir)
    print(f"Successfully created directory: {save_dir}")
else:
    print(f"Directory {save_dir} already exists.")
  • Enter the output path
cd /kaggle/output/tinydiffusion/TinyDiffusion
  • Process the data
!python /kaggle/input/tinydiffusion/TinyDiffusion/ddpm/dataloader.py
  • Print the structure
!python /kaggle/input/tinydiffusion/TinyDiffusion/ddpm/unet.py
  • Add noise to the images
!python /kaggle/input/tinydiffusion/TinyDiffusion/ddpm/diffusion.py
  • Train, here only training for 100 times, if you want to modify, you can change it in the command line
!python /kaggle/output/tinydiffusion/TinyDiffusion/ddpm/train.py --epochs 100 --batch_size 128 --lr 1e-4 --img_size 32
  • View the images
import os
from PIL import Image
import matplotlib.pyplot as plt

# Define the image directory
save_dir = '/kaggle/output/tinydiffusion/TinyDiffusion/samples'

# Check if the directory exists
if not os.path.exists(save_dir):
    print(f"Directory {save_dir} does not exist.")
else:
    # Get all files in the directory
    all_files = os.listdir(save_dir)
    
    # Filter out image files (assuming image file extensions are .png, .jpg, .jpeg)
    image_files = [f for f in all_files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
    
    if not image_files:
        print(f"There are no image files in the directory {save_dir}.")
    else:
        print(f"Found the following image files:")
        for idx, image_file in enumerate(image_files):
            print(f"{idx + 1}. {image_file}")
            # Display the image (optional)
            image_path = os.path.join(save_dir, image_file)
            img = Image.open(image_path)
            plt.imshow(img)
            plt.title(image_file)
            plt.axis('off')
            plt.show()

Results#

image
image
image
image
image
image
image
image
image
image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.