Spaces:
Sleeping
Sleeping
File size: 1,705 Bytes
e02f101 | 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 | # ==============================================
# model.py | Residual Super-Resolution Model
# ==============================================
%%writefile model.py
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, Add, Activation
def psnr(y_true, y_pred):
"""
Computes the Peak Signal-to-Noise Ratio (PSNR) metric.
"""
return tf.image.psnr(y_true, y_pred, max_val=1.0)
def build_enhanced_model(input_shape=(32, 32, 3)):
"""
Builds an enhanced residual model for image super-resolution.
"""
# --- Input Layer ---
inputs = Input(shape=input_shape)
# --- Feature Extraction Layers ---
# Using smaller 3x3 kernels improves efficiency and generalization
x = Conv2D(64, (3, 3), padding='same', activation='relu')(inputs)
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) # Extra depth for richer features
# --- Reconstruction Layer ---
x = Conv2D(3, (3, 3), padding='same')(x) # No activation here (linear output)
# --- Residual Connection ---
# The model learns to predict the missing details (residuals)
outputs = Add()([inputs, x])
outputs = Activation('sigmoid')(outputs) # Keeps pixel values in [0, 1]
# --- Build and Compile the Model ---
model = Model(inputs=inputs, outputs=outputs)
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss='mean_squared_error',
metrics=[psnr]
)
return model
|