forked from thatbrguy/Dehaze-GAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
371 lines (314 loc) · 13.8 KB
/
training.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import tensorflow as tf
import numpy as np
from pathlib import Path
from typing import Tuple, Dict, Generator
from tensorflow.keras.utils import Progbar
import datetime
from typing import Tuple, List, Dict
import re
class FogDataset:
"""
Dataset handler for fog removal GAN training
Expects directory structure:
dataset/
train/
input/
image1.png
image2.png
target/
image1.png
image2.png
val/
input/
...
target/
...
test/
input/
...
target/
...
"""
def __init__(self,
root_dir: str,
batch_size: int = 1,
image_size: Tuple[int, int] = (256, 256),
buffer_size: int = 1000):
self.root_dir = Path(root_dir)
self.batch_size = batch_size
self.image_size = image_size
self.buffer_size = buffer_size
def _find_matching_pairs(self, input_dir: Path, target_dir: Path) -> List[Tuple[Path, Path]]:
"""Find matching pairs between input and target directories"""
input_files = sorted(list(input_dir.glob('*.[pj][np][g]'))) # matches .png and .jpg
target_files = sorted(list(target_dir.glob('*.[pj][np][g]')))
print(f"\nFound {len(input_files)} input files and {len(target_files)} target files")
# Create a mapping of base names to full paths for target files
target_map = {}
for target_path in target_files:
# Handle different naming patterns
base_name = target_path.stem
# Remove any '-target' suffix
base_name = base_name.replace('-target', '')
base_name = base_name.replace('-targets', '')
# Store in map
target_map[base_name] = target_path
# Match input files with their targets
pairs = []
unmatched = []
for input_path in input_files:
base_name = input_path.stem
# Remove any '-input' suffix
base_name = base_name.replace('-input', '')
base_name = base_name.replace('-inputs', '')
if base_name in target_map:
pairs.append((input_path, target_map[base_name]))
else:
unmatched.append(input_path)
print(f"Successfully paired {len(pairs)} images")
if unmatched:
print(f"Warning: {len(unmatched)} input images could not be matched:")
for path in unmatched[:5]: # Show first 5 unmatched
print(f" {path.name}")
if len(unmatched) > 5:
print(f" ... and {len(unmatched) - 5} more")
return pairs
def _load_and_preprocess(self, input_path: str, target_path: str) -> Tuple[tf.Tensor, tf.Tensor]:
"""Load and preprocess image pairs"""
def load_image(path: str) -> tf.Tensor:
img = tf.io.read_file(path)
# Handle both PNG and JPEG
if tf.strings.regex_full_match(path, ".*\.jpg"):
img = tf.image.decode_jpeg(img, channels=3)
else:
img = tf.image.decode_png(img, channels=3)
img = tf.cast(img, tf.float32)
img = tf.image.resize(img, self.image_size, method='bicubic')
return (img / 127.5) - 1 # Normalize to [-1, 1]
input_img = load_image(input_path)
target_img = load_image(target_path)
return input_img, target_img
def get_dataset(self, mode: str = 'train') -> tf.data.Dataset:
"""Create tensorflow dataset for specified mode"""
mode_dir = self.root_dir / mode
input_dir = mode_dir / 'input'
target_dir = mode_dir / 'target'
print(f"\nCreating dataset for mode: {mode}")
print(f"Input directory: {input_dir}")
print(f"Target directory: {target_dir}")
# Verify directories exist
if not input_dir.exists() or not target_dir.exists():
raise ValueError(f"Missing directory for {mode} mode")
# Find matching pairs
pairs = self._find_matching_pairs(input_dir, target_dir)
if not pairs:
raise ValueError(f"No matching image pairs found for {mode} mode")
# Create dataset from pairs
input_paths = [str(p[0]) for p in pairs]
target_paths = [str(p[1]) for p in pairs]
dataset = tf.data.Dataset.from_tensor_slices((input_paths, target_paths))
# Map loading and preprocessing function
dataset = dataset.map(
lambda x, y: tf.py_function(
self._load_and_preprocess,
[x, y],
[tf.float32, tf.float32]
),
num_parallel_calls=tf.data.AUTOTUNE
)
# Set shapes explicitly
dataset = dataset.map(
lambda x, y: (
tf.ensure_shape(x, [*self.image_size, 3]),
tf.ensure_shape(y, [*self.image_size, 3])
)
)
if mode == 'train':
dataset = dataset.shuffle(min(len(pairs), self.buffer_size))
dataset = dataset.batch(self.batch_size)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
# Print dataset info
print(f"\nDataset created for {mode}:")
print(f"Number of image pairs: {len(pairs)}")
print(f"Batch size: {self.batch_size}")
print(f"Steps per epoch: {len(pairs) // self.batch_size}")
return dataset
def verify_dataset(self, mode: str = 'train'):
"""Verify dataset by showing first few samples"""
dataset = self.get_dataset(mode)
print(f"\nVerifying {mode} dataset:")
for i, (input_batch, target_batch) in enumerate(dataset.take(1)):
print(f"\nBatch {i + 1}:")
print(f"Input shape: {input_batch.shape}")
print(f"Target shape: {target_batch.shape}")
print(f"Input range: [{tf.reduce_min(input_batch)}, {tf.reduce_max(input_batch)}]")
print(f"Target range: [{tf.reduce_min(target_batch)}, {tf.reduce_max(target_batch)}]")
class GANTrainer:
"""Handler for GAN training process"""
def __init__(self,
model: tf.keras.Model,
dataset: FogDataset,
args: Dict):
self.model = model
self.dataset = dataset
self.args = args
# Training parameters
self.epochs = args.epochs
self.val_frequency = args.val_frequency
# Setup paths
self.model_dir = Path(args.model_name)
self.checkpoint_dir = self.model_dir / 'checkpoints'
self.sample_dir = self.model_dir / 'samples'
self.log_dir = self.model_dir / 'logs'
# Create directories
self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
self.sample_dir.mkdir(parents=True, exist_ok=True)
self.log_dir.mkdir(parents=True, exist_ok=True)
# Setup checkpointing
self.checkpoint = tf.train.Checkpoint(
generator_optimizer=model.generator_optimizer,
discriminator_optimizer=model.discriminator_optimizer,
generator=model.generator,
discriminator=model.discriminator
)
self.checkpoint_manager = tf.train.CheckpointManager(
self.checkpoint,
str(self.checkpoint_dir),
max_to_keep=3
)
# Setup metrics
self.train_metrics = {
'gen_total_loss': tf.keras.metrics.Mean(),
'gen_gan_loss': tf.keras.metrics.Mean(),
'gen_l1_loss': tf.keras.metrics.Mean(),
'gen_vgg_loss': tf.keras.metrics.Mean(),
'disc_loss': tf.keras.metrics.Mean()
}
self.val_metrics = {
'val_psnr': tf.keras.metrics.Mean(),
'val_ssim': tf.keras.metrics.Mean()
}
# Setup summary writers
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
self.train_summary_writer = tf.summary.create_file_writer(
str(self.log_dir / f'train_{current_time}')
)
self.val_summary_writer = tf.summary.create_file_writer(
str(self.log_dir / f'val_{current_time}')
)
def restore_checkpoint(self):
"""Restore from latest checkpoint if available"""
if self.checkpoint_manager.latest_checkpoint:
self.checkpoint.restore(self.checkpoint_manager.latest_checkpoint)
print(f"Restored from checkpoint: {self.checkpoint_manager.latest_checkpoint}")
return True
return False
def _reset_metrics(self):
"""Reset all metrics at epoch start"""
for metric in self.train_metrics.values():
metric.reset_state()
for metric in self.val_metrics.values():
metric.reset_state()
def _update_metrics(self, results: Dict[str, tf.Tensor]):
"""Update metrics with latest batch results"""
for name, value in results.items():
self.train_metrics[name].update_state(value)
def validate(self, val_dataset: tf.data.Dataset) -> Dict[str, float]:
"""Run validation loop"""
for batch in val_dataset:
input_image, target_image = batch
generated_image = self.model.generator(input_image, training=False)
# Calculate PSNR and SSIM
psnr = tf.image.psnr(
(target_image + 1) * 127.5,
(generated_image + 1) * 127.5,
max_val=255
)
ssim = tf.image.ssim(
(target_image + 1) * 127.5,
(generated_image + 1) * 127.5,
max_val=255
)
self.val_metrics['val_psnr'].update_state(psnr)
self.val_metrics['val_ssim'].update_state(ssim)
return {name: metric.result() for name, metric in self.val_metrics.items()}
def train(self):
"""Main training loop"""
# Setup datasets
train_dataset = self.dataset.get_dataset('train')
val_dataset = self.dataset.get_dataset('val')
# Restore checkpoint if available
if self.args.restore:
self.restore_checkpoint()
best_psnr = 0
steps_per_epoch = len(train_dataset)
for epoch in range(self.epochs):
print(f"\nEpoch {epoch + 1}/{self.epochs}")
self._reset_metrics()
# Progress bar
progress_bar = Progbar(steps_per_epoch, stateful_metrics=[
'gen_total_loss', 'disc_loss', 'gen_gan_loss', 'gen_l1_loss', 'gen_vgg_loss'
])
# Training loop
for step, batch in enumerate(train_dataset):
results = self.model.train_step(batch)
self._update_metrics(results)
# Update progress bar
progress_bar.update(
step + 1,
[(name, metric.result()) for name, metric in self.train_metrics.items()]
)
# Log training metrics
with self.train_summary_writer.as_default():
for name, metric in self.train_metrics.items():
tf.summary.scalar(name, metric.result(), step=self.model.optimizer.iterations)
# Validation
if (epoch + 1) % self.val_frequency == 0:
val_results = self.validate(val_dataset)
print("\nValidation Results:")
for name, value in val_results.items():
print(f"{name}: {value:.4f}")
# Log validation metrics
with self.val_summary_writer.as_default():
for name, value in val_results.items():
tf.summary.scalar(name, value, step=epoch)
# Save best model
if val_results['val_psnr'] > best_psnr:
best_psnr = val_results['val_psnr']
self.checkpoint_manager.save()
print(f"Saved new best model with PSNR: {best_psnr:.4f}")
class GANInference:
"""Handler for GAN inference"""
def __init__(self, model: tf.keras.Model, checkpoint_dir: str):
self.model = model
self.checkpoint = tf.train.Checkpoint(
generator=model.generator
)
self.checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)).expect_partial()
def process_image(self, image_path: str, output_path: str):
"""Process a single image"""
# Load and preprocess image
img = tf.io.read_file(image_path)
img = tf.image.decode_png(img, channels=3)
img = tf.cast(img, tf.float32)
img = tf.image.resize(img, (256, 256))
img = (img / 127.5) - 1
img = tf.expand_dims(img, 0)
# Generate output
output = self.model.generator(img, training=False)
output = (output[0] + 1) * 127.5
output = tf.clip_by_value(output, 0, 255)
output = tf.cast(output, tf.uint8)
# Save output
tf.io.write_file(output_path, tf.image.encode_png(output))
def process_directory(self, input_dir: str, output_dir: str):
"""Process all images in a directory"""
input_dir = Path(input_dir)
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
input_images = sorted(input_dir.glob('*-input.png'))
for img_path in input_images:
output_path = output_dir / img_path.name.replace('-input.png', '-output.png')
self.process_image(str(img_path), str(output_path))
print(f"Processed {img_path.name}")