File size: 10,390 Bytes
eaefa93 ee90412 eaefa93 |
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
'''
THis gile is to contain the DDPM implementation modularized for loading, prediciton and training.
'''
from torch import nn
import math
import torch
from utils import forward_diffusion_sample, sample_timestep, sample_plot_image
import torch.nn.functional as F
from attn_utils import SelfAttention, CBAM, Block_CBAM
class Block(nn.Module):
def __init__(self, in_ch, out_ch, time_emb_dim, up=False):
super().__init__()
self.time_mlp = nn.Linear(time_emb_dim, out_ch)
if up:
## up channel - gobig big big bigg from smol smol smol with 3x3 kernel
self.conv1 = nn.Conv2d(2*in_ch, out_ch, 3, padding=1)
self.transform = nn.ConvTranspose2d(out_ch, out_ch, 4, 2, 1)
else:
self.conv1 = nn.Conv2d(in_ch, out_ch, 3, padding=1)
self.transform = nn.Conv2d(out_ch, out_ch, 4,2,1)
self.conv2 = nn.Conv2d(out_ch, out_ch, 3, padding=1)
self.relu = nn.ReLU()
self.batch_norm1 = nn.BatchNorm2d(out_ch)
self.batch_norm2 = nn.BatchNorm2d(out_ch)
def forward(self, x, t, ):
h = self.batch_norm1(self.relu(self.conv1(x)))
time_emb = self.relu(self.time_mlp(t))
time_emb = time_emb[(..., ) + (None, ) * 2]
h = h + time_emb
h = self.batch_norm2(self.relu(self.conv2(h)))
return self.transform(h)
class PositionEmbeddings(nn.Module):
def __init__(self,dim):
super().__init__()
self.dim = dim
def forward(self, time):
device = time.device
half_dim = self.dim // 2
embeddings = math.log(10000) / (half_dim - 1)
embeddings = torch.exp(torch.arange(half_dim, device=device) * -embeddings)
embeddings = time[:, None] * embeddings[None, :]
embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1)
return embeddings
class SimpleUnet(nn.Module):
def __init__(self):
super().__init__()
image_channels = 3
down_channels = (64, 128, 256, 512, 1024)
up_channels = (1024, 512, 256, 128, 64)
self.device = "cuda" if torch.cuda.is_available() else "cpu"
out_dim = 3
time_emb_dim = 32
## timestep stored as positional encoding in terms of sine
self.time_mlp = nn.Sequential(
PositionEmbeddings(time_emb_dim),
nn.Linear(time_emb_dim, time_emb_dim),
nn.ReLU()
)
self.conv0 = nn.Conv2d(image_channels, down_channels[0], 3, padding=1)
self.down_blocks = nn.ModuleList([
Block(down_channels[i], down_channels[i+1], time_emb_dim)
for i in range(len(down_channels)-1)
])
self.up_blocks = nn.ModuleList([
Block(up_channels[i], up_channels[i+1], time_emb_dim, up=True)
for i in range(len(up_channels)-1)
])
## readout layer
self.output = nn.Conv2d(up_channels[-1], out_dim, 1)
def forward(self, x, timestep):
t = self.time_mlp(timestep)
x = self.conv0(x)
residual_inputs = []
for down in self.down_blocks:
x = down(x, t)
residual_inputs.append(x)
for up in self.up_blocks:
residual_x = residual_inputs.pop()
x = torch.cat((x, residual_x), dim=1)
x = up(x, t)
return self.output(x)
@torch.no_grad()
def sample(self, noise):
"""
Generate an image by denoising a given noise tensor using the reverse diffusion process.
Args:
noise (torch.Tensor): Initial noise tensor (e.g., sampled from a Gaussian distribution).
Returns:
torch.Tensor: Denoised image.
"""
img = noise # Start with the provided noise tensor
T = self.num_timesteps # Total timesteps for diffusion
stepsize = 1 # You can adjust if needed
# Iterate through the timesteps in reverse order
for i in range(0, T)[::-1]:
t = torch.full((noise.size(0),), i, device=noise.device, dtype=torch.long) # Current timestep
img = sample_timestep(self, img, t) # Perform one reverse diffusion step
img = torch.clamp(img, -1.0, 1.0) # Clamp the image to ensure values stay in [-1, 1]
return img
def get_loss(self, x_0, t):
x_noisy, noise = forward_diffusion_sample(x_0, t, self.device)
noise_pred = self(x_noisy, t)
return F.l1_loss(noise, noise_pred)
def train(self, dataloader, BATCH_SIZE=64,T=300, EPOCHS=50, verbose=True):
from torch.optim import Adam
device = "cuda" if torch.cuda.is_available() else "cpu"
self.to(device)
optimizer = Adam(self.parameters(), lr=0.001)
epochs = EPOCHS
for epoch in range(epochs):
for step, batch in enumerate(dataloader):
optimizer.zero_grad()
t = torch.randint(0, T, (BATCH_SIZE,), device=device).long()
loss = self.get_loss(self, batch[0], t)
loss.backward()
optimizer.step()
if verbose:
if epoch % 5 == 0 and step % 150 == 0:
print(f"Epoch {epoch} | step {step:03d} Loss: {loss.item()} ")
sample_plot_image(self)
def test():
## TODO: add the testing loop here
pass
################################################################################################
####################### ATTENTION LAYERS ADDEDD TO THE MODEL ###################################
################################################################################################
class SimpleUnetWSelfAttn(nn.Module):
def __init__(self):
super().__init__()
image_channels = 3
down_channels = (64, 128, 256, 512, 1024)
up_channels = (1024, 512, 256, 128, 64)
out_dim = 3
time_emb_dim = 32
## timestep stored as positional encoding in terms of sine
self.time_mlp = nn.Sequential(
PositionEmbeddings(time_emb_dim),
nn.Linear(time_emb_dim, time_emb_dim),
nn.ReLU()
)
self.num_timesteps = 300
self.conv0 = nn.Conv2d(image_channels, down_channels[0], 3, padding=1)
self.down_blocks = nn.ModuleList([
Block(down_channels[i], down_channels[i+1], time_emb_dim)
for i in range(len(down_channels)-1)
])
self.up_blocks = nn.ModuleList([
Block(up_channels[i], up_channels[i+1], time_emb_dim, up=True)
for i in range(len(up_channels)-1)
])
self.self_attention = SelfAttention(down_channels[-1])
## readout layer
self.output = nn.Conv2d(up_channels[-1], out_dim, 1)
# def settimestep()
def forward(self, x, timestep):
self.num_timesteps = timestep
t = self.time_mlp(timestep)
x = self.conv0(x)
residual_inputs = []
for down in self.down_blocks:
x = down(x, t)
residual_inputs.append(x)
x = self.self_attention(x)
for up in self.up_blocks:
residual_x = residual_inputs.pop()
x = torch.cat((x, residual_x), dim=1)
x = up(x, t)
return self.output(x)
@torch.no_grad()
def sample(self, noise):
"""
Generate an image by denoising a given noise tensor using the reverse diffusion process.
Args:
noise (torch.Tensor): Initial noise tensor (e.g., sampled from a Gaussian distribution).
Returns:
torch.Tensor: Denoised image.
"""
img = noise # Start with the provided noise tensor
T = self.num_timesteps # Total timesteps for diffusion
stepsize = 1 # You can adjust if needed
print(noise.device)
# Iterate through the timesteps in reverse order
for i in range(T - 1, -1, -1):
t = torch.full((noise.size(0),), i, device=noise.device, dtype=torch.long) # Current timestep
img = sample_timestep(self, img, t) # Perform one reverse diffusion step
img = torch.clamp(img, -1.0, 1.0) # Clamp the image to ensure values stay in [-1, 1]
return img
################################################################################################
#################### Convolutional Block Attention Module ADDED TO THE MODEL ###################
################################################################################################
class SimpleUnetWCBAM(nn.Module):
def __init__(self):
super().__init__()
image_channels = 3
down_channels = (64, 128, 256, 512, 1024)
up_channels = (1024, 512, 256, 128, 64)
out_dim = 3
time_emb_dim = 32
## timestep stored as positional encoding in terms of sine
self.time_mlp = nn.Sequential(
PositionEmbeddings(time_emb_dim),
nn.Linear(time_emb_dim, time_emb_dim),
nn.ReLU()
)
self.num_timesteps = 300
self.conv0 = nn.Conv2d(image_channels, down_channels[0], 3, padding=1)
self.down_blocks = nn.ModuleList([
Block_CBAM(down_channels[i], down_channels[i+1], time_emb_dim)
for i in range(len(down_channels)-1)
])
self.up_blocks = nn.ModuleList([
Block_CBAM(up_channels[i], up_channels[i+1], time_emb_dim, up=True)
for i in range(len(up_channels)-1)
])
self.self_attention = SelfAttention(down_channels[-1])
## readout layer
self.output = nn.Conv2d(up_channels[-1], out_dim, 1)
# def settimestep()
def forward(self, x, timestep):
self.num_timesteps = timestep
t = self.time_mlp(timestep)
x = self.conv0(x)
residual_inputs = []
for down in self.down_blocks:
x = down(x, t)
residual_inputs.append(x)
x = self.self_attention(x)
for up in self.up_blocks:
residual_x = residual_inputs.pop()
x = torch.cat((x, residual_x), dim=1)
x = up(x, t)
return self.output(x)
@torch.no_grad()
def sample(self, noise):
"""
Generate an image by denoising a given noise tensor using the reverse diffusion process.
Args:
noise (torch.Tensor): Initial noise tensor (e.g., sampled from a Gaussian distribution).
Returns:
torch.Tensor: Denoised image.
"""
img = noise # Start with the provided noise tensor
T = self.num_timesteps # Total timesteps for diffusion
stepsize = 1 # You can adjust if needed
print(noise.device)
# Iterate through the timesteps in reverse order
for i in range(T - 1, -1, -1):
t = torch.full((noise.size(0),), i, device=noise.device, dtype=torch.long) # Current timestep
img = sample_timestep(self, img, t) # Perform one reverse diffusion step
img = torch.clamp(img, -1.0, 1.0) # Clamp the image to ensure values stay in [-1, 1]
return img
|