Repository navigation
Expand file tree
/
Copy pathtraindiff.py
More file actions
232 lines (193 loc) · 8.33 KB
/
Copy pathtraindiff.py
File metadata and controls
232 lines (193 loc) · 8.33 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
"""
Train a diffusion model on a custom image directory using GPU acceleration.
"""
import argparse
import os
import time
from PIL import Image
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
import torchvision.transforms as transforms
from torchvision.transforms.functional import to_tensor
from guided_diffusion.script_util import (
model_and_diffusion_defaults,
create_model_and_diffusion,
add_dict_to_argparser,
args_to_dict,
)
class CustomImageDataset(Dataset):
"""Dataset for training on custom image directories."""
def __init__(self, image_dir, image_size=256, random_crop=False, random_flip=False):
self.image_dir = image_dir
self.image_size = image_size
self.random_crop = random_crop
self.random_flip = random_flip
# Find all image files
self.image_files = []
for root, _, files in os.walk(image_dir):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.webp')):
self.image_files.append(os.path.join(root, file))
# Create transformations
if random_crop:
self.transform = transforms.Compose([
transforms.RandomResizedCrop(image_size, scale=(0.8, 1.0)),
transforms.RandomHorizontalFlip() if random_flip else transforms.Lambda(lambda x: x),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
else:
self.transform = transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.RandomHorizontalFlip() if random_flip else transforms.Lambda(lambda x: x),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
def __len__(self):
return len(self.image_files)
def __getitem__(self, idx):
img_path = self.image_files[idx]
try:
image = Image.open(img_path).convert('RGB')
tensor = self.transform(image)
return tensor, {} # The second element is for optional conditioning
except Exception as e:
print(f"Error loading image {img_path}: {e}")
# Return a random tensor as fallback
return torch.randn(3, self.image_size, self.image_size), {}
def main():
args = create_argparser().parse_args()
# Ensure data directory exists
if not os.path.exists(args.data_dir):
raise ValueError(f"Data directory not found: {args.data_dir}")
# Create checkpoint directory
os.makedirs(args.checkpoint_dir, exist_ok=True)
# Create model and diffusion process
print("Creating model and diffusion...")
model, diffusion = create_model_and_diffusion(
**args_to_dict(args, model_and_diffusion_defaults().keys())
)
# Load pretrained model if specified
if args.pretrained_model_path:
print(f"Loading pretrained model from {args.pretrained_model_path}...")
try:
model.load_state_dict(torch.load(args.pretrained_model_path, map_location="cpu"))
print("Pretrained model loaded successfully.")
except Exception as e:
print(f"Error loading pretrained model: {e}")
print("Starting training from scratch.")
# Move to GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
model.to(device)
# Use multiple GPUs if available
if torch.cuda.device_count() > 1 and args.use_multi_gpu:
print(f"Using {torch.cuda.device_count()} GPUs!")
model = torch.nn.DataParallel(model)
# Use mixed precision for faster training if requested
if args.use_fp16 and device.type == 'cuda':
scaler = torch.cuda.amp.GradScaler()
use_amp = True
print("Using mixed precision training")
else:
use_amp = False
if args.use_fp16 and device.type != 'cuda':
print("FP16 requested but not using CUDA. Falling back to FP32.")
# Create optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
# Create dataset and loader
print(f"Loading dataset from {args.data_dir}...")
dataset = CustomImageDataset(
args.data_dir,
image_size=args.image_size,
random_crop=args.random_crop,
random_flip=args.random_flip
)
loader = DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True if device.type == 'cuda' else False,
drop_last=True
)
print(f"Dataset contains {len(dataset)} images")
# Training loop
print("Starting training...")
for epoch in range(args.epochs):
model.train()
epoch_loss = 0.0
start_time = time.time()
num_batches = 0
for batch, cond in loader:
batch = batch.to(device)
# Zero gradients
optimizer.zero_grad()
# Get random timesteps
t = torch.randint(
0, diffusion.num_timesteps, (batch.shape[0],), device=device
).long()
if use_amp:
# Use automatic mixed precision
with torch.cuda.amp.autocast():
# Calculate loss
loss = diffusion.training_losses(model, batch, t)["loss"].mean()
# Backward pass with scaling
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
# Calculate loss
loss = diffusion.training_losses(model, batch, t)["loss"].mean()
# Backward pass and optimize
loss.backward()
optimizer.step()
epoch_loss += loss.item()
num_batches += 1
# Print progress
if num_batches % args.log_interval == 0:
print(f"Epoch {epoch+1}/{args.epochs}, Batch {num_batches}/{len(loader)}, Loss: {loss.item():.6f}")
# Epoch summary
epoch_time = time.time() - start_time
print(f"Epoch {epoch+1} complete. Avg Loss: {epoch_loss/num_batches:.6f}, Time: {epoch_time:.2f}s")
# Save checkpoint
if (epoch + 1) % args.save_interval == 0 or epoch == args.epochs - 1:
checkpoint_path = os.path.join(args.checkpoint_dir, f"model_epoch_{epoch+1}.pt")
# If using DataParallel, save the underlying model
if isinstance(model, torch.nn.DataParallel):
torch.save(model.module.state_dict(), checkpoint_path)
else:
torch.save(model.state_dict(), checkpoint_path)
print(f"Checkpoint saved to {checkpoint_path}")
print("Training complete!")
# Save final model
final_model_path = os.path.join(args.checkpoint_dir, "model_final.pt")
if isinstance(model, torch.nn.DataParallel):
torch.save(model.module.state_dict(), final_model_path)
else:
torch.save(model.state_dict(), final_model_path)
print(f"Final model saved to {final_model_path}")
def create_argparser():
defaults = dict(
data_dir="", # Input directory with training images
checkpoint_dir="checkpoints", # Directory to save checkpoints
pretrained_model_path="", # Optional path to pretrained model for fine-tuning
epochs=100,
batch_size=4,
lr=1e-4,
log_interval=10,
save_interval=10,
random_crop=True,
random_flip=True,
num_workers=4,
use_multi_gpu=True, # Whether to use multiple GPUs if available
)
defaults.update(model_and_diffusion_defaults())
parser = argparse.ArgumentParser()
add_dict_to_argparser(parser, defaults)
return parser
if __name__ == "__main__":
main()