-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
184 lines (152 loc) · 5.61 KB
/
train.py
File metadata and controls
184 lines (152 loc) · 5.61 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
# Import required libraries
from torchvision import transforms, utils as vutils
from diffusers import UNet2DModel, DDPMScheduler
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
from PIL import UnidentifiedImageError
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm
import torch
import os
# Define a safe image folder class to handle errors
class SafeImageFolder(ImageFolder):
# Override __getitem__ method
def __getitem__(self, index):
try:
# Attempt to get image and label
return super().__getitem__(index)
except UnidentifiedImageError:
# If corrupted, try another random image recursively
new_index = (index + 1) % len(self)
return self.__getitem__(new_index)
# Define training function
def train_diffusion():
# Set hyperparameters
image_size = 192
batch_size = 24
num_epochs = 300
learning_rate = 1e-4
save_every = 1
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Define transforms
transform = transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
transforms.ToTensor(),
transforms.Normalize([0.5] * 3, [0.5] * 3)
])
# Load dataset with safe loader
dataset = SafeImageFolder("images", transform=transform)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=4)
# Initialize model
model = UNet2DModel(
sample_size=image_size,
in_channels=3,
out_channels=3,
layers_per_block=2,
dropout=0.1,
block_out_channels=(64, 128, 256, 512),
down_block_types=("DownBlock2D", "DownBlock2D", "DownBlock2D", "DownBlock2D"),
up_block_types=("UpBlock2D", "UpBlock2D", "UpBlock2D", "UpBlock2D"),
).to(device)
# Print model architecture
print(model)
# Initialize noise scheduler
noise_scheduler = DDPMScheduler(num_train_timesteps=1000)
# Initialize optimizer
optimizer = optim.AdamW(model.parameters(), lr=learning_rate)
# Initialize cosine annealing learning rate scheduler
scheduler = optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=num_epochs,
eta_min=1e-6
)
# Initialize gradient scaler
scaler = torch.amp.GradScaler('cuda')
# Training loop
for epoch in range(num_epochs):
progress_bar = tqdm(dataloader, desc=f"Epoch {epoch + 1}/{num_epochs}")
for batch in progress_bar:
# Get real images
images, _ = batch
images = images.to(device)
# Sample noise
noise = torch.randn_like(images)
# Sample random timesteps
timesteps = torch.randint(
0,
noise_scheduler.config.num_train_timesteps,
(images.shape[0],),
device=device
).long()
# Add noise to images
noisy_images = noise_scheduler.add_noise(images, noise, timesteps)
# Enable mixed precision context
with torch.amp.autocast('cuda'):
# Predict noise
noise_pred = model(noisy_images, timesteps).sample
# Compute loss
loss = F.mse_loss(noise_pred, noise)
# Backpropagate with scaled loss
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
# Update progress bar
progress_bar.set_postfix({
"loss": f"{loss.item():.4f}",
"lr": f"{scheduler.get_last_lr()[0]:.6f}"
})
# Step scheduler
scheduler.step()
# Save progress sample
if (epoch + 1) % save_every == 0 or epoch == 0:
save_sample(model, noise_scheduler, epoch + 1, device)
# Save final model
model.save_pretrained("unet_final")
noise_scheduler.save_pretrained("scheduler_final")
# Define sampling function
def save_sample(model, scheduler, epoch, device, num_images=8):
# Generate noise
images = torch.randn((num_images, 3, 192, 192), device=device)
# Set model to evaluation mode
model.eval()
# Denoise step-by-step
with torch.no_grad():
for t in reversed(range(scheduler.config.num_train_timesteps)):
# Create a tensor of timesteps
timesteps = torch.full((num_images,), t, device=device, dtype=torch.long)
# Predict noise
with torch.amp.autocast('cuda'):
noise_pred = model(images, timesteps).sample
# Step through the scheduler one image at a time
new_images = []
for i in range(num_images):
step_output = scheduler.step(
noise_pred[i].unsqueeze(0),
timesteps[i].unsqueeze(0),
images[i].unsqueeze(0)
)
new_images.append(step_output.prev_sample)
# Stack the new images
images = torch.cat(new_images, dim=0)
# Save final denoised images
vutils.save_image(
images,
f"progress/sample_epoch_{epoch:03d}.png",
nrow=4,
normalize=True,
value_range=(-1, 1)
)
# Define main function
def main():
# Create progress directory
os.makedirs("progress", exist_ok=True)
# Start training
train_diffusion()
# Run script
if __name__ == "__main__":
main()