-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet.py
139 lines (118 loc) · 4.27 KB
/
unet.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# References:
# https://github.com/openai/guided-diffusion/blob/22e0df8183507e13a7813f8d38d51b072ca1e67c/guided_diffusion/unet.py
import torch
from torch import nn
from torch.nn import functional as F
import math
from classifier import Swish, TimeEmbedding, ResBlock, Downsample
class Upsample(nn.Module):
def __init__(self, channels):
super().__init__()
self.layers = nn.Sequential(
nn.Upsample(scale_factor=2, mode="nearest"),
nn.Conv2d(channels, channels, 3, 1, 1),
)
def forward(self, x):
return self.layers(x)
class UNet(nn.Module):
def __init__(
self,
n_classes,
channels=128,
channel_mults=[1, 2, 2, 2],
attns=[False, True, False, False],
n_res_blocks=2,
):
super().__init__()
assert all([i < len(channel_mults) for i in attns]), "attns index out of bound"
time_channels = channels * 4
self.time_embed = nn.Sequential(
TimeEmbedding(max_len=1000, time_channels=time_channels),
nn.Linear(channels, time_channels),
Swish(),
nn.Linear(time_channels, time_channels),
)
self.label_emb = nn.Embedding(n_classes, time_channels)
self.init_conv = nn.Conv2d(3, channels, 3, 1, 1)
self.down_blocks = nn.ModuleList()
cxs = [channels]
cur_channels = channels
for i, mult in enumerate(channel_mults):
out_channels = channels * mult
for _ in range(n_res_blocks):
self.down_blocks.append(
ResBlock(
in_channels=cur_channels,
out_channels=out_channels,
time_channels=time_channels,
attn=attns[i]
)
)
cur_channels = out_channels
cxs.append(cur_channels)
if i != len(channel_mults) - 1:
self.down_blocks.append(Downsample(cur_channels))
cxs.append(cur_channels)
self.mid_blocks = nn.ModuleList([
ResBlock(
in_channels=cur_channels,
out_channels=cur_channels,
time_channels=time_channels,
attn=True,
),
ResBlock(
in_channels=cur_channels,
out_channels=cur_channels,
time_channels=time_channels,
attn=False,
),
])
self.up_blocks = nn.ModuleList()
for i, mult in reversed(list(enumerate(channel_mults))):
out_channels = channels * mult
for _ in range(n_res_blocks + 1):
self.up_blocks.append(
ResBlock(
in_channels=cxs.pop() + cur_channels,
out_channels=out_channels,
time_channels=time_channels,
attn=attns[i],
)
)
cur_channels = out_channels
if i != 0:
self.up_blocks.append(Upsample(cur_channels))
assert len(cxs) == 0
self.fin_block = nn.Sequential(
nn.GroupNorm(32, cur_channels),
Swish(),
nn.Conv2d(cur_channels, 3, 3, 1, 1)
)
def forward(self, noisy_image, diffusion_step, label):
x = self.init_conv(noisy_image)
t = self.time_embed(diffusion_step)
y = self.label_emb(label)
xs = [x]
for layer in self.down_blocks:
if isinstance(layer, Downsample):
x = layer(x)
else:
x = layer(x, t + y)
xs.append(x)
for layer in self.mid_blocks:
x = layer(x, t + y)
for layer in self.up_blocks:
if isinstance(layer, Upsample):
x = layer(x)
else:
x = torch.cat([x, xs.pop()], dim=1)
x = layer(x, t + y)
assert len(xs) == 0
return self.fin_block(x)
if __name__ == "__main__":
model = UNet(n_classes=10)
noisy_image = torch.randn(4, 3, 32, 32)
diffusion_step = torch.randint(0, 1000, size=(4,))
label = torch.randint(0, 10, size=(4,))
out = model(noisy_image, diffusion_step, label)
out.shape