-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path不可行torch.py
230 lines (192 loc) · 8.57 KB
/
不可行torch.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
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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from PIL import Image
import os
import time
# 数据集路径
dataset_path = 'C:/Users/admin/Desktop/AI风景model升级版/dataset'
BUFFER_SIZE = 400
BATCH_SIZE = 16
IMG_WIDTH = 256
IMG_HEIGHT = 256
LAMBDA = 100
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 定义数据集
class ImageDataset(Dataset):
def __init__(self, blurry_paths, clear_paths, transform=None):
self.blurry_paths = blurry_paths
self.clear_paths = clear_paths
self.transform = transform
def __len__(self):
return len(self.blurry_paths)
def __getitem__(self, idx):
blurry_image = Image.open(self.blurry_paths[idx]).convert('RGB')
clear_image = Image.open(self.clear_paths[idx]).convert('RGB')
if self.transform:
blurry_image = self.transform(blurry_image)
clear_image = self.transform(clear_image)
return blurry_image, clear_image
def get_dataset_paths(dataset_path, phase):
blurry_images = sorted(os.listdir(os.path.join(dataset_path, phase, 'blurry')))
clear_images = sorted(os.listdir(os.path.join(dataset_path, phase, 'clear')))
blurry_paths = [os.path.join(dataset_path, phase, 'blurry', img) for img in blurry_images]
clear_paths = [os.path.join(dataset_path, phase, 'clear', img) for img in clear_images]
return blurry_paths, clear_paths
# 数据变换
transform = transforms.Compose([
transforms.Resize((IMG_HEIGHT, IMG_WIDTH)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
blurry_train_paths, clear_train_paths = get_dataset_paths(dataset_path, 'train')
blurry_test_paths, clear_test_paths = get_dataset_paths(dataset_path, 'test')
train_dataset = ImageDataset(blurry_train_paths, clear_train_paths, transform=transform)
test_dataset = ImageDataset(blurry_test_paths, clear_test_paths, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, pin_memory=True)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, pin_memory=True)
# 构建Pix2Pix模型
class Downsample(nn.Module):
def __init__(self, in_channels, out_channels, size, apply_batchnorm=True):
super(Downsample, self).__init__()
layers = [
nn.Conv2d(in_channels, out_channels, kernel_size=size, stride=2, padding=1, bias=False)
]
if apply_batchnorm:
layers.append(nn.BatchNorm2d(out_channels))
layers.append(nn.LeakyReLU(inplace=False))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class Upsample(nn.Module):
def __init__(self, in_channels, out_channels, size, apply_dropout=False):
super(Upsample, self).__init__()
layers = [
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=size, stride=2, padding=1, bias=False),
nn.BatchNorm2d(out_channels)
]
if apply_dropout:
layers.append(nn.Dropout(0.5))
layers.append(nn.ReLU(inplace=False))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
Downsample(6, 64, 4, apply_batchnorm=False),
Downsample(64, 128, 4),
Downsample(128, 256, 4),
nn.ZeroPad2d(1),
nn.Conv2d(256, 512, kernel_size=4, stride=1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(inplace=False),
nn.ZeroPad2d(1),
nn.Conv2d(512, 1, kernel_size=4, stride=1)
)
def forward(self, x):
return self.model(x)
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.down_stack = nn.ModuleList([
Downsample(3, 64, 4, apply_batchnorm=False),
Downsample(64, 128, 4),
Downsample(128, 256, 4),
Downsample(256, 512, 4),
Downsample(512, 512, 4),
Downsample(512, 512, 4),
Downsample(512, 512, 4),
Downsample(512, 512, 4)
])
self.up_stack = nn.ModuleList([
Upsample(512, 512, 4, apply_dropout=True),
Upsample(1024, 512, 4, apply_dropout=True),
Upsample(1024, 512, 4, apply_dropout=True),
Upsample(1024, 512, 4),
Upsample(1024, 256, 4),
Upsample(512, 128, 4),
Upsample(256, 64, 4)
])
self.last = nn.Sequential(
nn.ConvTranspose2d(128, 3, kernel_size=4, stride=2, padding=1),
nn.Tanh()
)
def forward(self, x):
skips = []
for down in self.down_stack:
x = down(x)
skips.append(x)
skips = reversed(skips[:-1])
for up, skip in zip(self.up_stack, skips):
x = up(x)
x = torch.cat([x, skip], dim=1)
x = self.last(x)
return x
# 初始化模型
generator = Generator().to(DEVICE)
discriminator = Discriminator().to(DEVICE)
# 定义损失函数
criterion = nn.BCEWithLogitsLoss()
# 定义优化器
generator_optimizer = optim.Adam(generator.parameters(), lr=2e-4, betas=(0.5, 0.999))
discriminator_optimizer = optim.Adam(discriminator.parameters(), lr=2e-4, betas=(0.5, 0.999))
# 定义训练步骤
def generator_loss(disc_generated_output, gen_output, target):
gan_loss = criterion(disc_generated_output, torch.ones_like(disc_generated_output, device=DEVICE))
l1_loss = torch.mean(torch.abs(target - gen_output))
total_gen_loss = gan_loss + (LAMBDA * l1_loss)
return total_gen_loss
def discriminator_loss(disc_real_output, disc_generated_output):
real_loss = criterion(disc_real_output, torch.ones_like(disc_real_output, device=DEVICE))
generated_loss = criterion(disc_generated_output, torch.zeros_like(disc_generated_output, device=DEVICE))
total_disc_loss = real_loss + generated_loss
return total_disc_loss
# 训练和测试步骤
def train_step(input_image, target):
generator_optimizer.zero_grad()
discriminator_optimizer.zero_grad()
# 生成图像
gen_output = generator(input_image)
# 判别器输出
disc_real_output = discriminator(torch.cat([input_image, target], dim=1))
disc_generated_output = discriminator(torch.cat([input_image, gen_output], dim=1))
# 计算生成器损失并反向传播
gen_loss = generator_loss(disc_generated_output, gen_output, target)
gen_loss.backward() # 生成器反向传播
generator_optimizer.step()
# 计算判别器损失并反向传播
disc_loss = discriminator_loss(disc_real_output, disc_generated_output)
disc_loss.backward() # 判别器反向传播
discriminator_optimizer.step()
return gen_loss.item(), disc_loss.item()
def compute_metrics(loader):
total_gen_loss = 0.0
total_disc_loss = 0.0
num_batches = 0
for input_image, target in loader:
input_image = input_image.to(DEVICE)
target = target.to(DEVICE)
gen_loss, disc_loss = train_step(input_image, target)
total_gen_loss += gen_loss
total_disc_loss += disc_loss
num_batches += 1
return total_gen_loss / num_batches, total_disc_loss / num_batches
def fit(train_loader, epochs, test_loader):
torch.autograd.set_detect_anomaly(True) # 启用异常检测
for epoch in range(epochs):
start = time.time()
print(f"Starting Epoch {epoch + 1}/{epochs}")
train_gen_loss, train_disc_loss = compute_metrics(train_loader)
test_gen_loss, test_disc_loss = compute_metrics(test_loader)
print(f"Epoch {epoch + 1} Training Gen Loss: {train_gen_loss:.4f}, Disc Loss: {train_disc_loss:.4f}")
print(f"Epoch {epoch + 1} Test Gen Loss: {test_gen_loss:.4f}, Disc Loss: {test_disc_loss:.4f}")
print(f"Completed Epoch {epoch + 1} in {time.time() - start:.2f} seconds")
torch.save(generator.state_dict(), 'C:/Users/admin/Desktop/AI风景model升级版/model-gen/generator2.pth')
torch.save(discriminator.state_dict(), 'C:/Users/admin/Desktop/AI风景model升级版/model-dis/discriminator2.pth')
if __name__ == '__main__':
# 开始训练
fit(train_loader, epochs=1, test_loader=test_loader)