File size: 1,831 Bytes
14a2100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""Diffusion.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1bcJlVBYDIxhySq0b6YHyKsumLgIomNqf

#Diffusion

Setup
"""

!nvidia-smi

!pip install diffusers==0.11.1
!pip install transformers scipy ftfy accelerate

"""pipeline"""

import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")

# move pipeline to GPU
pipe = pipe.to("cuda")

# Let's Generate image
prompt = "cute panda eating pizza on bamboo tree "
image = pipe(prompt).images[0]

image.save(f"Happy_panda.png")

image

import torch

generator = torch.Generator("cuda").manual_seed(2048)

image = pipe(prompt, generator=generator).images[0]

image

# increase inference steps
import torch

generator = torch.Generator("cuda").manual_seed(2048)

image = pipe(prompt, num_inference_steps=70, generator=generator).images[0]

image

from PIL import Image

def image_grid(imgs, rows, cols):
    assert len(imgs) == rows*cols

    w, h = imgs[0].size
    grid = Image.new('RGB', size=(cols*w, rows*h))
    grid_w, grid_h = grid.size

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i%cols*w, i//cols*h))
    return grid

num_images = 3
prompt = ["cute panda eating pizza on bamboo tree "] * num_images

images = pipe(prompt).images

grid = image_grid(images, rows=1, cols=3)
grid

num_cols = 3
num_rows = 4

prompt = ["cute panda eating pizza on bamboo tree "] * num_cols

all_images = []
for i in range(num_rows):
  images = pipe(prompt).images
  all_images.extend(images)

grid = image_grid(all_images, rows=num_rows, cols=num_cols)
grid

# Generating rectangle image
prompt = "cute panda eating pizza on bamboo tree "

image = pipe(prompt, height=512, width=752).images[0]
image