Spaces:
Runtime error
Runtime error
Upload train_model.py
Browse files- train_model.py +73 -0
train_model.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#train.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Import the new data loader and the existing model builder
|
| 8 |
+
from utils import load_div2k_data
|
| 9 |
+
from model import build_enhanced_model, psnr
|
| 10 |
+
|
| 11 |
+
# --- 1. Training Configuration ---
|
| 12 |
+
BATCH_SIZE = 16 # Smaller batch size for larger images to fit in GPU memory
|
| 13 |
+
EPOCHS = 30 # Fewer epochs, as each one takes longer. Increase for higher quality.
|
| 14 |
+
|
| 15 |
+
# --- 2. Load the Dataset ---
|
| 16 |
+
train_ds, valid_ds, ds_info = load_div2k_data(batch_size=BATCH_SIZE)
|
| 17 |
+
|
| 18 |
+
# Calculate steps per epoch
|
| 19 |
+
steps_per_epoch = ds_info.splits['train'].num_examples // BATCH_SIZE
|
| 20 |
+
validation_steps = ds_info.splits['validation'].num_examples // BATCH_SIZE
|
| 21 |
+
|
| 22 |
+
# --- 3. Build the Model for 128x128 Input ---
|
| 23 |
+
INPUT_SHAPE = (128, 128, 3)
|
| 24 |
+
model = build_enhanced_model(input_shape=INPUT_SHAPE)
|
| 25 |
+
model.summary()
|
| 26 |
+
|
| 27 |
+
# --- 4. Train the Model ---
|
| 28 |
+
print("\nStarting model training on 128x128 images...")
|
| 29 |
+
history = model.fit(
|
| 30 |
+
train_ds,
|
| 31 |
+
epochs=EPOCHS,
|
| 32 |
+
steps_per_epoch=steps_per_epoch,
|
| 33 |
+
validation_data=valid_ds,
|
| 34 |
+
validation_steps=validation_steps
|
| 35 |
+
)
|
| 36 |
+
print("Training finished.")
|
| 37 |
+
|
| 38 |
+
# --- 5. Save the New Model ---
|
| 39 |
+
if not os.path.exists('models'):
|
| 40 |
+
os.makedirs('models')
|
| 41 |
+
|
| 42 |
+
model_path = 'models/sr_128_model.h5'
|
| 43 |
+
model.save(model_path)
|
| 44 |
+
print(f"✅ Model for 128x128 saved to {model_path}")
|
| 45 |
+
|
| 46 |
+
# --- 6. Visualize a Test Result ---
|
| 47 |
+
print("\nVisualizing a sample prediction...")
|
| 48 |
+
# Get one batch from the validation dataset to visualize
|
| 49 |
+
for lr_batch, hr_batch in valid_ds.take(1):
|
| 50 |
+
# Take the first image from the batch
|
| 51 |
+
lr_image = lr_batch[0]
|
| 52 |
+
hr_image = hr_batch[0]
|
| 53 |
+
|
| 54 |
+
# Predict
|
| 55 |
+
pred_image = model.predict(tf.expand_dims(lr_image, axis=0))[0]
|
| 56 |
+
|
| 57 |
+
# Plot
|
| 58 |
+
plt.figure(figsize=(15, 6))
|
| 59 |
+
plt.subplot(1, 3, 1)
|
| 60 |
+
plt.imshow(lr_image)
|
| 61 |
+
plt.title('Low-Res Input (128x128 Upscaled)')
|
| 62 |
+
plt.axis('off')
|
| 63 |
+
|
| 64 |
+
plt.subplot(1, 3, 2)
|
| 65 |
+
plt.imshow(tf.clip_by_value(pred_image, 0, 1)) # Clip values to [0,1] for display
|
| 66 |
+
plt.title('AI Super-Resolved Output')
|
| 67 |
+
plt.axis('off')
|
| 68 |
+
|
| 69 |
+
plt.subplot(1, 3, 3)
|
| 70 |
+
plt.imshow(hr_image)
|
| 71 |
+
plt.title('Original High-Resolution')
|
| 72 |
+
plt.axis('off')
|
| 73 |
+
plt.show()
|