-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_gan.py
194 lines (160 loc) · 7.01 KB
/
train_gan.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
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
import os
print("Current directory:", os.getcwd())
print("Contents:", os.listdir())
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image
import os
from pathlib import Path
import time
class ArtDataset(Dataset):
def __init__(self, folder_path, transform=None):
self.folder_path = folder_path
self.transform = transform
self.image_files = [f for f in Path(folder_path).glob("*.jpg")]
print(f"Loading dataset: Found {len(self.image_files)} images")
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')
if self.transform:
image = self.transform(image)
return image
except Exception as e:
print(f"Error loading image {img_path}: {str(e)}")
return torch.zeros((3, 64, 64))
class Generator(nn.Module):
def __init__(self, latent_dim):
super(Generator, self).__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(latent_dim, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
nn.Tanh()
)
def forward(self, x):
return self.main(x)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(3, 64, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(256, 512, 4, 2, 1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(512, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)
def forward(self, x):
return self.main(x)
def save_checkpoint(generator, discriminator, epoch, path="checkpoints"):
os.makedirs(path, exist_ok=True)
torch.save({
'generator_state_dict': generator.state_dict(),
'discriminator_state_dict': discriminator.state_dict(),
'epoch': epoch
}, f"{path}/checkpoint_epoch_{epoch}.pt")
print(f"Saved checkpoint for epoch {epoch}")
def train_art_gan(folder_path, num_epochs=100, batch_size=32, latent_dim=100, lr=0.0002, save_interval=10):
print("\n🎨 Starting the AI training process...")
transform = transforms.Compose([
transforms.Resize(64),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
dataset = ArtDataset(folder_path, transform=transform)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=2)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
generator = Generator(latent_dim).to(device)
discriminator = Discriminator().to(device)
criterion = nn.BCELoss()
g_optimizer = torch.optim.Adam(generator.parameters(), lr=lr, betas=(0.5, 0.999))
d_optimizer = torch.optim.Adam(discriminator.parameters(), lr=lr, betas=(0.5, 0.999))
print(f"\n🎯 Starting training for {num_epochs} epochs...")
start_time = time.time()
try:
for epoch in range(num_epochs):
for i, real_images in enumerate(dataloader):
batch_size = real_images.size(0)
real_images = real_images.to(device)
d_optimizer.zero_grad()
label_real = torch.ones(batch_size, 1, 1, 1).to(device)
label_fake = torch.zeros(batch_size, 1, 1, 1).to(device)
output_real = discriminator(real_images)
d_loss_real = criterion(output_real, label_real)
noise = torch.randn(batch_size, latent_dim, 1, 1).to(device)
fake_images = generator(noise)
output_fake = discriminator(fake_images.detach())
d_loss_fake = criterion(output_fake, label_fake)
d_loss = d_loss_real + d_loss_fake
d_loss.backward()
d_optimizer.step()
g_optimizer.zero_grad()
output_fake = discriminator(fake_images)
g_loss = criterion(output_fake, label_real)
g_loss.backward()
g_optimizer.step()
if i % 10 == 0:
elapsed = time.time() - start_time
print(f'⏱️ {elapsed:.1f}s | Epoch [{epoch+1}/{num_epochs}] Batch [{i}/{len(dataloader)}] '
f'Loss_D: {d_loss.item():.4f} Loss_G: {g_loss.item():.4f}')
if (epoch + 1) % save_interval == 0:
save_checkpoint(generator, discriminator, epoch + 1)
except KeyboardInterrupt:
print("\n⚠️ Training interrupted! Saving final checkpoint...")
save_checkpoint(generator, discriminator, epoch + 1, path="checkpoints/interrupted")
return generator
print("\n✨ Training complete!")
return generator
def generate_art(generator, output_dir="generated_artwork", latent_dim=100, num_images=5):
print(f"\n🎨 Generating {num_images} new artworks...")
os.makedirs(output_dir, exist_ok=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
generator.eval()
with torch.no_grad():
noise = torch.randn(num_images, latent_dim, 1, 1).to(device)
generated_images = generator(noise)
generated_images = (generated_images + 1) / 2
transform = transforms.ToPILImage()
for i, img_tensor in enumerate(generated_images):
image = transform(img_tensor)
save_path = os.path.join(output_dir, f"generated_artwork_{i+1}.png")
image.save(save_path)
print(f"Saved artwork {i+1} to: {save_path}")
if __name__ == "__main__":
print("🎨 Art Generation AI - Starting Training")
folder_path = "part1" # Updated to use part1 folder
try:
generator = train_art_gan(
folder_path,
num_epochs=100,
batch_size=32
)
output_dir = "generated_artwork"
generate_art(generator, output_dir, num_images=10)
print(f"\n✅ Process complete! Check the '{output_dir}' folder for your generated artwork!")
except Exception as e:
print(f"\n❌ An error occurred: {str(e)}")