-
Notifications
You must be signed in to change notification settings - Fork 32
/
train2.py
152 lines (113 loc) · 4.77 KB
/
train2.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
140
141
142
143
144
145
146
147
148
149
150
151
import torch
import os
import torch.nn as nn
from forward_process import *
from dataset import *
from torch.optim import Adam
from dataset import *
from backbone import *
from noise import *
from visualize import show_tensor_image
from torch.utils.tensorboard import SummaryWriter
from test import *
def build_optimizer(model, config):
lr = config.model.learning_rate
weight_decay = config.model.weight_decay
return Adam(
model.parameters(), lr=lr, weight_decay=weight_decay
)
def get_loss(model, constant_dict, x_0, t, config):
x_noisy, noise = forward_diffusion_sample(x_0, t , constant_dict, config)
noise_pred = model(x_noisy, t)
#loss = F.l1_loss(noise, noise_pred)
loss = F.mse_loss(noise, noise_pred)
return loss
@torch.no_grad()
def sample_timestep(config, model, constant_dict, x, t):
"""
Calls the model to predict the noise in the image and returns
the denoised image.
Applies noise to this image, if we are not in the last step yet.
"""
betas_t = get_index_from_list(constant_dict['betas'], t, x.shape)
sqrt_one_minus_alphas_cumprod_t = get_index_from_list(
constant_dict['sqrt_one_minus_alphas_cumprod'], t, x.shape
)
sqrt_recip_alphas_t = get_index_from_list(constant_dict['sqrt_recip_alphas'], t, x.shape)
# Call model (current image - noise prediction)
model_mean = sqrt_recip_alphas_t * (
x - betas_t * model(x, t) / sqrt_one_minus_alphas_cumprod_t
)
posterior_variance_t = get_index_from_list(constant_dict['posterior_variance'], t, x.shape)
if t == 0:
return model_mean
else:
noise = get_noise(x, t, config)
return model_mean + torch.sqrt(posterior_variance_t) * noise
@torch.no_grad()
def sample_plot_image(model, trainloader, constant_dict, epoch, category, config):
image = next(iter(trainloader))[0]
# Sample noise
trajectoy_steps = torch.Tensor([config.model.test_trajectoy_steps]).type(torch.int64)
image = forward_diffusion_sample(image, trajectoy_steps, constant_dict, config)[0]
num_images = 5
trajectory_steps = config.model.trajectory_steps
stepsize = int(trajectory_steps/num_images)
plt.figure(figsize=(15,15))
plt.axis('off')
image_to_show =show_tensor_image(image)
plt.subplot(1, num_images+1, int(trajectory_steps/stepsize)+1)
plt.imshow(image_to_show)
plt.title(trajectory_steps)
for i in range(0,trajectory_steps-1)[::-1]:
t = torch.full((1,), i, device=config.model.device, dtype=torch.long)
image = sample_timestep(config, model, constant_dict, image, t)
if i % stepsize == 0:
plt.subplot(1, num_images+1, int(i/stepsize)+1)
image_to_show =show_tensor_image(image.detach().cpu())
plt.imshow(image_to_show)
plt.title(i)
plt.subplots_adjust(wspace=0.4)
plt.savefig('results/{}backward_process_after_{}_epochs.png'.format(category, epoch))
# plt.show()
def trainer(model, constant_dict, config, category):
with open('readme.txt', 'a') as f:
f.write(f"\n {category} : ")
optimizer = build_optimizer(model, config)
train_dataset = MVTecDataset(
root= config.data.data_dir,
category=category,
input_size= config.data.image_size,
is_train=True,
)
trainloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=config.data.batch_size,
shuffle=True,
num_workers=config.model.num_workers,
drop_last=True,
)
writer = SummaryWriter('runs/DDAD')
for epoch in range(config.model.epochs):
for step, batch in enumerate(trainloader):
t = torch.randint(0, config.model.trajectory_steps, (batch[0].shape[0],), device=config.model.device).long()
optimizer.zero_grad()
loss = get_loss(model, constant_dict, batch[0], t, config)
writer.add_scalar('loss', loss, epoch)
loss.backward()
optimizer.step()
if epoch % 100 == 0 and step == 0:
print(f"Epoch {epoch} | Loss: {loss.item()}")
with open('readme.txt', 'a') as f:
f.write(f"\n Epoch {epoch} | Loss: {loss.item()}")
validate(model, constant_dict, config, category)
if epoch %30 == 0 and epoch > 0 and step ==0:
sample_plot_image(model, trainloader, constant_dict, epoch, category, config)
if config.model.save_model:
model_save_dir = os.path.join(os.getcwd(), config.model.checkpoint_dir)
if not os.path.exists(model_save_dir):
os.mkdir(model_save_dir)
torch.save(model.state_dict(), os.path.join(config.model.checkpoint_dir, category), #config.model.checkpoint_name
)
writer.flush()
writer.close()