-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·65 lines (58 loc) · 1.74 KB
/
train.py
File metadata and controls
executable file
·65 lines (58 loc) · 1.74 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
import torch
from torch.utils.data import DataLoader
from torch import nn, optim
import torch.nn.functional as F
from tqdm import tqdm
from dit import DiT
import os
from dataset import MNIST
from config import T
from diffusion import forward_add_noise
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = torch.device("mps" if torch.mps.is_available() else "cpu")
print('Using device:', device)
dataset = MNIST()
model = DiT(imgSize=28,
patchSize=4,
channels=1,
embSize=64,
labelNum=10,
ditNum=3,
headNum=4)
model.to(device)
# load model
try:
model.load_state_dict(torch.load('model.pth'))
except:
pass
# setup training
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.L1Loss()
<<<<<<< HEAD
EPOCH = 500
=======
EPOCH = 1000
>>>>>>> c243c90d14375dfc56d8836243aa4e7526d50675
batch_size = 300
progress = tqdm(total=EPOCH)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
model.train()
iter_counter = 0
for epoch in range(EPOCH):
for img, label in dataloader:
# convert image pixel range from [0, 1] to [-1, 1]
x = img * 2 - 1
t = torch.randint(0, T, (img.size(0),))
y = label
x, noise = forward_add_noise(x, t)
pred_noise = model(x.to(device), t.to(device), y.to(device))
loss = criterion(noise.to(device), pred_noise)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if iter_counter % 1000 == 0:
print(f">> Epoch: {epoch}, Iteration {iter_counter}: loss = {loss}")
torch.save(model.state_dict(), f".model.pth")
os.replace(".model.pth", "model.pth")
iter_counter += 1
progress.update(1)