tummalapenta commited on
Commit
5959f2c
1 Parent(s): a7b7cd8

Upload 2 files

Browse files
p2_Stable_Diffusion_ProductSnapAI_Training_and_Inference_Martin_Valen.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
p2_stable_diffusion_productsnapai_training_and_inference_martin_valen.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """p2_Stable_Diffusion_ProductSnapAI_Training_and_Inference-Martin_Valen.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1ZVSof0szrYoCO_lPNzP9ctTP_sZrUp1G
8
+
9
+ ##Part 2: Fine-tuning Stable Diffusion
10
+
11
+ To train stable diffusion on our images, we can use a technique called fine-tuning, which involves taking a pre-trained stable diffusion model and training it further on a new dataset. In this way, we can leverage the knowledge already learned by the pre-trained model and adapt it to the new dataset, allowing us to achieve better performance with less training time. In this context, we will be fine-tuning a stable diffusion model on our own images, which will enable us to generate new images that are similar in style and content to our original dataset. Once we have fine-tuned the model, we will be able to use it for inference, generating new images on demand.
12
+ """
13
+
14
+ !cd /content/
15
+ !git clone https://github.com/huggingface/diffusers.git
16
+ !pip install ./diffusers
17
+ !pip install -U -r /content/diffusers/examples/text_to_image/requirements.txt
18
+
19
+ """Let's configure HuggingFace Accelerate, a platform for allowing us to automatically configure our system to be able to run our training script. You can learn more about Accelerate [here](https://huggingface.co/docs/accelerate/index)."""
20
+
21
+ !nvidia-smi
22
+
23
+ !accelerate config default --mixed_precision fp16
24
+
25
+ """Time to configure our environment variables. For this, we want to tell the script our model name, dataset name, and where we would like it to output the model. We will be automatically pushing the model directly to HuggingFace Hub which will require us to login to our HuggingFace account using the token provided through your HuggingFace account settings."""
26
+
27
+ import os
28
+
29
+ os.environ['MODEL_NAME'] = f'CompVis/stable-diffusion-v1-2'
30
+ os.environ['DATASET_NAME'] = f'Ali-fb/martin_valen_dataset'
31
+ os.environ['OUTPUT_DIR'] = f'sd_martin_valen-model-v1-2_400_demo'
32
+
33
+ from huggingface_hub import notebook_login
34
+ notebook_login()
35
+
36
+ """Run our training script using HuggingFace accelerate. We'll be inputing our dataset and model with 400 steps (selected) and 134 epochs (default). The model will then be automatically pushed to the hub."""
37
+
38
+ !accelerate launch diffusers/examples/text_to_image/train_text_to_image.py \
39
+ --pretrained_model_name_or_path=$MODEL_NAME \
40
+ --dataset_name=$DATASET_NAME \
41
+ --use_ema \
42
+ --resolution=512 --center_crop --random_flip \
43
+ --train_batch_size=1 \
44
+ --gradient_accumulation_steps=4 \
45
+ --gradient_checkpointing \
46
+ --mixed_precision="fp16" \
47
+ --max_train_steps=400 \
48
+ --learning_rate=1e-05 \
49
+ --max_grad_norm=1 \
50
+ --push_to_hub \
51
+ --checkpointing_steps=100000 \
52
+ --lr_scheduler="constant" \
53
+ --lr_warmup_steps=0 \
54
+ --output_dir=$OUTPUT_DIR
55
+
56
+ # Stable Diffusion V1
57
+ from diffusers import StableDiffusionPipeline
58
+ import torch
59
+ from PIL import Image
60
+
61
+ model_path = "./sd_martin_valen-model-v1-2_400_demo"
62
+ pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
63
+ pipe.to("cuda")
64
+
65
+ # Run inference using ChatGPT prompts to acquire 4 image panels
66
+ image1 = pipe(prompt="black hoodie with a front half zipper by martin valen").images[0]
67
+ image1.save("ProductSnapAI_panel_1.png")
68
+
69
+ image2 = pipe(prompt="white hoodie with a blue design by martin valen").images[0]
70
+ image2.save("ProductSnapAI_panel_2.png")
71
+
72
+ image3 = pipe(prompt="stripped hoodie by martin valen").images[0]
73
+ image3.save("ProductSnapAI_panel_3.png")
74
+
75
+ image4 = pipe(prompt="camouflage hoodie by martin valen").images[0]
76
+ image4.save("ProductSnapAI_panel_4.png")
77
+
78
+ # Image grid helper function from HuggingFace
79
+ def image_grid(imgs, rows, cols):
80
+ assert len(imgs) == rows*cols
81
+
82
+ w, h = imgs[0].size
83
+ grid = Image.new('RGB', size=(cols*w, rows*h))
84
+ grid_w, grid_h = grid.size
85
+
86
+ for i, img in enumerate(imgs):
87
+ grid.paste(img, box=(i%cols*w, i//cols*h))
88
+ return grid
89
+
90
+ all_images = [image1, image2, image3, image4]
91
+ grid = image_grid(all_images, rows=1, cols=4)
92
+ grid