Upload diffusion.py
Browse files- diffusion.py +93 -0
diffusion.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Diffusion.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1bcJlVBYDIxhySq0b6YHyKsumLgIomNqf
|
8 |
+
|
9 |
+
#Diffusion
|
10 |
+
|
11 |
+
Setup
|
12 |
+
"""
|
13 |
+
|
14 |
+
!nvidia-smi
|
15 |
+
|
16 |
+
!pip install diffusers==0.11.1
|
17 |
+
!pip install transformers scipy ftfy accelerate
|
18 |
+
|
19 |
+
"""pipeline"""
|
20 |
+
|
21 |
+
import torch
|
22 |
+
from diffusers import StableDiffusionPipeline
|
23 |
+
|
24 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
25 |
+
|
26 |
+
# move pipeline to GPU
|
27 |
+
pipe = pipe.to("cuda")
|
28 |
+
|
29 |
+
# Let's Generate image
|
30 |
+
prompt = "cute panda eating pizza on bamboo tree "
|
31 |
+
image = pipe(prompt).images[0]
|
32 |
+
|
33 |
+
image.save(f"Happy_panda.png")
|
34 |
+
|
35 |
+
image
|
36 |
+
|
37 |
+
import torch
|
38 |
+
|
39 |
+
generator = torch.Generator("cuda").manual_seed(2048)
|
40 |
+
|
41 |
+
image = pipe(prompt, generator=generator).images[0]
|
42 |
+
|
43 |
+
image
|
44 |
+
|
45 |
+
# increase inference steps
|
46 |
+
import torch
|
47 |
+
|
48 |
+
generator = torch.Generator("cuda").manual_seed(2048)
|
49 |
+
|
50 |
+
image = pipe(prompt, num_inference_steps=70, generator=generator).images[0]
|
51 |
+
|
52 |
+
image
|
53 |
+
|
54 |
+
from PIL import Image
|
55 |
+
|
56 |
+
def image_grid(imgs, rows, cols):
|
57 |
+
assert len(imgs) == rows*cols
|
58 |
+
|
59 |
+
w, h = imgs[0].size
|
60 |
+
grid = Image.new('RGB', size=(cols*w, rows*h))
|
61 |
+
grid_w, grid_h = grid.size
|
62 |
+
|
63 |
+
for i, img in enumerate(imgs):
|
64 |
+
grid.paste(img, box=(i%cols*w, i//cols*h))
|
65 |
+
return grid
|
66 |
+
|
67 |
+
num_images = 3
|
68 |
+
prompt = ["cute panda eating pizza on bamboo tree "] * num_images
|
69 |
+
|
70 |
+
images = pipe(prompt).images
|
71 |
+
|
72 |
+
grid = image_grid(images, rows=1, cols=3)
|
73 |
+
grid
|
74 |
+
|
75 |
+
num_cols = 3
|
76 |
+
num_rows = 4
|
77 |
+
|
78 |
+
prompt = ["cute panda eating pizza on bamboo tree "] * num_cols
|
79 |
+
|
80 |
+
all_images = []
|
81 |
+
for i in range(num_rows):
|
82 |
+
images = pipe(prompt).images
|
83 |
+
all_images.extend(images)
|
84 |
+
|
85 |
+
grid = image_grid(all_images, rows=num_rows, cols=num_cols)
|
86 |
+
grid
|
87 |
+
|
88 |
+
# Generating rectangle image
|
89 |
+
prompt = "cute panda eating pizza on bamboo tree "
|
90 |
+
|
91 |
+
image = pipe(prompt, height=512, width=752).images[0]
|
92 |
+
image
|
93 |
+
|