forked from suinleelab/derm_audit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffusion.py
More file actions
355 lines (295 loc) · 11.3 KB
/
Copy pathdiffusion.py
File metadata and controls
355 lines (295 loc) · 11.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from typing import Dict, Tuple
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import transforms
from datasets import ISICDataset # Replace with your dataset
import torch.optim as optim
import csv
import os
class ResidualConvBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int, is_res: bool = False) -> None:
super().__init__()
self.same_channels = (in_channels == out_channels)
self.is_res = is_res
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, 1, 1),
nn.BatchNorm2d(out_channels),
nn.GELU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(out_channels, out_channels, 3, 1, 1),
nn.BatchNorm2d(out_channels),
nn.GELU(),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.is_res:
x1 = self.conv1(x)
x2 = self.conv2(x1)
if self.same_channels:
out = x + x2
else:
out = x1 + x2
return out / 1.414
else:
x1 = self.conv1(x)
x2 = self.conv2(x1)
return x2
class UnetDown(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.model = nn.Sequential(
ResidualConvBlock(in_channels, out_channels),
nn.MaxPool2d(2)
)
def forward(self, x):
return self.model(x)
class UnetUp(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.model = nn.Sequential(
nn.ConvTranspose2d(in_channels, out_channels, 2, 2),
ResidualConvBlock(out_channels, out_channels),
ResidualConvBlock(out_channels, out_channels),
)
def forward(self, x, skip):
x = torch.cat((x, skip), 1)
x = self.model(x)
return x
class EmbedFC(nn.Module):
def __init__(self, input_dim, emb_dim):
super().__init__()
self.input_dim = input_dim
self.model = nn.Sequential(
nn.Linear(input_dim, emb_dim),
nn.GELU(),
nn.Linear(emb_dim, emb_dim),
)
def forward(self, x):
x = x.view(-1, self.input_dim)
return self.model(x)
def convert_to_one_hot(labels, num_classes=2):
"""Convert binary labels to one-hot encoding"""
labels = labels.long()
one_hot = torch.zeros(labels.size(0), num_classes, device=labels.device)
one_hot.scatter_(1, labels.unsqueeze(1), 1)
return one_hot
class ContextUnet(nn.Module):
def __init__(self, in_channels, n_feat=128, n_classes=2):
super().__init__()
self.in_channels = in_channels
self.n_feat = n_feat
self.n_classes = n_classes
self.init_conv = ResidualConvBlock(in_channels, n_feat, is_res=True)
self.down1 = UnetDown(n_feat, n_feat)
self.down2 = UnetDown(n_feat, 2 * n_feat)
# Using AdaptiveAvgPool2d to get to 8x8
self.to_vec = nn.Sequential(
nn.AdaptiveAvgPool2d((8,8)),
nn.GELU()
)
self.timeembed1 = EmbedFC(1, 2*n_feat)
self.timeembed2 = EmbedFC(1, 1*n_feat)
self.contextembed1 = EmbedFC(n_classes, 2*n_feat)
self.contextembed2 = EmbedFC(n_classes, 1*n_feat)
# From 8x8 back to 64x64
# (8 - 1)*8 + 8 = 56 + 8 =64
self.up0 = nn.Sequential(
nn.ConvTranspose2d(2 * n_feat, 2 * n_feat, kernel_size=8, stride=8),
nn.GroupNorm(8, 2 * n_feat),
nn.ReLU(),
)
self.up1 = UnetUp(4 * n_feat, n_feat)
self.up2 = UnetUp(2 * n_feat, n_feat)
self.out = nn.Sequential(
nn.Conv2d(2 * n_feat, n_feat, 3, 1, 1),
nn.GroupNorm(8, n_feat),
nn.ReLU(),
nn.Conv2d(n_feat, self.in_channels, 3, 1, 1),
)
def forward(self, x, c, t, context_mask):
x = self.init_conv(x)
down1 = self.down1(x) # 128x128
down2 = self.down2(down1) # 64x64
hiddenvec = self.to_vec(down2) # 8x8
c_one_hot = convert_to_one_hot(c, self.n_classes)
context_mask = context_mask.view(-1, 1)
context_mask = context_mask.repeat(1, self.n_classes)
context_mask = (-1*(1-context_mask))
c_one_hot = c_one_hot * context_mask
cemb1 = self.contextembed1(c_one_hot).view(-1, self.n_feat * 2, 1, 1)
temb1 = self.timeembed1(t).view(-1, self.n_feat * 2, 1, 1)
cemb2 = self.contextembed2(c_one_hot).view(-1, self.n_feat, 1, 1)
temb2 = self.timeembed2(t).view(-1, self.n_feat, 1, 1)
up1 = self.up0(hiddenvec) # 64x64
up2 = self.up1(cemb1*up1 + temb1, down2) # from 64x64 to 128x128
up3 = self.up2(cemb2*up2 + temb2, down1) # from 128x128 to 256x256
out = self.out(torch.cat((up3, x), 1)) # final 256x256
return out
def ddpm_schedules(beta1, beta2, T):
beta_t = (beta2 - beta1) * torch.arange(0, T + 1, dtype=torch.float32) / T + beta1
sqrt_beta_t = torch.sqrt(beta_t)
alpha_t = 1 - beta_t
log_alpha_t = torch.log(alpha_t)
alphabar_t = torch.cumsum(log_alpha_t, dim=0).exp()
sqrtab = torch.sqrt(alphabar_t)
oneover_sqrta = 1 / torch.sqrt(alpha_t)
sqrtmab = torch.sqrt(1 - alphabar_t)
mab_over_sqrtmab_inv = (1 - alpha_t) / sqrtmab
return {
"alpha_t": alpha_t,
"oneover_sqrta": oneover_sqrta,
"sqrt_beta_t": sqrt_beta_t,
"alphabar_t": alphabar_t,
"sqrtab": sqrtab,
"sqrtmab": sqrtmab,
"mab_over_sqrtmab": mab_over_sqrtmab_inv,
}
class DDPM(nn.Module):
def __init__(self, nn_model, betas, n_T, device, drop_prob=0.1):
super().__init__()
self.nn_model = nn_model.to(device)
# Register DDPM schedule parameters
for k, v in ddpm_schedules(betas[0], betas[1], n_T).items():
self.register_buffer(k, v)
self.n_T = n_T
self.device = device
self.drop_prob = drop_prob
self.loss_mse = nn.MSELoss()
def forward(self, x, c):
_ts = torch.randint(1, self.n_T + 1, (x.shape[0],)).to(self.device)
noise = torch.randn_like(x)
# Create noisy image
x_t = (
self.sqrtab[_ts, None, None, None] * x
+ self.sqrtmab[_ts, None, None, None] * noise
)
c = (c > 0.5).long()
context_mask = torch.bernoulli(torch.zeros_like(c.float()) + self.drop_prob).to(self.device)
return self.loss_mse(noise, self.nn_model(x_t, c, _ts / self.n_T, context_mask))
def sample(self, n_sample, size, device, guide_w=0.0, condition=None, x_init=None, denoise_strength=0.7):
# Sampling logic (unchanged)
if x_init is not None:
start_t = int(self.n_T * denoise_strength)
noise = torch.randn_like(x_init)
x_i = self.sqrtab[start_t] * x_init + self.sqrtmab[start_t] * noise
start_step = start_t
else:
x_i = torch.randn(n_sample, *size).to(device)
start_step = self.n_T
if condition is not None:
c_i = condition
else:
c_i = torch.arange(0, 2).to(device)
c_i = c_i.repeat(int(n_sample/c_i.shape[0]))
context_mask = torch.zeros_like(c_i).to(device)
x_i_store = []
for i in range(start_step, 0, -1):
print(f'sampling timestep {i}', end='\r')
t_is = torch.tensor([i / self.n_T], device=device).reshape(1, 1, 1, 1)
t_is = t_is.repeat(n_sample, 1, 1, 1)
x_i_double = x_i.repeat(2, 1, 1, 1)
t_is_double = t_is.repeat(2, 1, 1, 1)
c_i_double = c_i.repeat(2)
context_mask_double = context_mask.repeat(2)
context_mask_double[n_sample:] = 1.
z = torch.randn(n_sample, *size).to(device) if i > 1 else 0
eps = self.nn_model(x_i_double, c_i_double, t_is_double, context_mask_double)
eps1 = eps[:n_sample]
eps2 = eps[n_sample:]
eps = (1 + guide_w) * eps1 - guide_w * eps2
x_i = (
self.oneover_sqrta[i] * (x_i - eps * self.mab_over_sqrtmab[i])
+ self.sqrt_beta_t[i] * z
)
if i % 20 == 0 or i == self.n_T or i < 8:
x_i_store.append(x_i.detach().cpu().numpy())
return x_i, x_i_store
def train_isic():
# Training parameters
n_epoch = 30
batch_size = 8
n_T = 400
device = "cuda:0" if torch.cuda.is_available() else "cpu"
n_classes = 2
n_feat = 128
image_size = 256 # Use a clean power-of-two image size
in_channels = 3
lrate = 1e-4
save_interval = 5
save_path = "diffusion_checkpoint.pth"
ddpm = DDPM(
nn_model=ContextUnet(in_channels=in_channels, n_feat=n_feat, n_classes=n_classes),
betas=(1e-4, 0.02),
n_T=n_T,
device=device,
drop_prob=0.1
)
ddpm.to(device)
tf = transforms.Compose([
transforms.Resize((image_size, image_size)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
dataset = ISICDataset(transform=tf)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
num_workers=2,
pin_memory=True,
persistent_workers=True
)
optim = torch.optim.Adam(ddpm.parameters(), lr=lrate)
csv_file = "training_loss.csv"
file_exists = os.path.isfile(csv_file)
for ep in range(n_epoch):
print(f'epoch {ep}')
ddpm.train()
# Linear LR decay
optim.param_groups[0]['lr'] = lrate * (1 - ep/n_epoch)
pbar = tqdm(dataloader)
loss_ema = None
for x, c in pbar:
optim.zero_grad()
x = x.to(device)
c = c.to(device)
try:
loss = ddpm(x, c)
loss.backward()
if loss_ema is None:
loss_ema = loss.item()
else:
loss_ema = 0.95 * loss_ema + 0.05 * loss.item()
pbar.set_description(f"loss: {loss_ema:.4f}")
optim.step()
except Exception as e:
print(f"Error in batch: {str(e)}")
continue
# Save model checkpoint
torch.save({
'model': ddpm.state_dict(),
'optimizer': optim.state_dict(),
'epoch': ep
}, save_path)
print(f"Saved checkpoint for epoch {ep}")
# Write loss to CSV
with open(csv_file, 'a', newline='') as f:
writer = csv.writer(f)
if not file_exists and ep == 0:
# Write header if file did not exist before
writer.writerow(["epoch", "loss_ema"])
writer.writerow([ep, loss_ema])
# Backup checkpoint every save_interval epochs
if ep % save_interval == 0:
backup_path = f"{save_path}.{ep}"
torch.save({
'model': ddpm.state_dict(),
'optimizer': optim.state_dict(),
'epoch': ep
}, backup_path)
print(f"Saved backup checkpoint to {backup_path}")
if __name__ == "__main__":
train_isic()