-
Notifications
You must be signed in to change notification settings - Fork 5
/
training_model.py
44 lines (33 loc) · 1.33 KB
/
training_model.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
from forward_noising import forward_diffusion_sample
from unet import SimpleUnet
from dataloader import load_transformed_dataset
import torch.nn.functional as F
import torch
from torch.optim import Adam
import logging
logging.basicConfig(level=logging.INFO)
def get_loss(model, x_0, t, device):
x_noisy, noise = forward_diffusion_sample(x_0, t, device)
noise_pred = model(x_noisy, t)
# return F.l1_loss(noise, noise_pred)
return F.mse_loss(noise, noise_pred)
if __name__ == "__main__":
model = SimpleUnet()
T = 300
BATCH_SIZE = 128
epochs = 100
dataloader = load_transformed_dataset(batch_size=BATCH_SIZE)
device = "cuda" if torch.cuda.is_available() else "cpu"
logging.info(f"Using device: {device}")
model.to(device)
optimizer = Adam(model.parameters(), lr=0.001)
for epoch in range(epochs):
for batch_idx, (batch, _) in enumerate(dataloader):
optimizer.zero_grad()
t = torch.randint(0, T, (BATCH_SIZE,), device=device).long()
loss = get_loss(model, batch, t, device=device)
loss.backward()
optimizer.step()
if batch_idx % 50 == 0:
logging.info(f"Epoch {epoch} | Batch index {batch_idx:03d} Loss: {loss.item()}")
torch.save(model.state_dict(), "./trained_models/ddpm_mse_epochs_100.pth")