Spaces:
Sleeping
Sleeping
model - Copy.py
Browse files- model - Copy.py +0 -48
model - Copy.py
DELETED
|
@@ -1,48 +0,0 @@
|
|
| 1 |
-
# ==============================================
|
| 2 |
-
# model.py | Residual Super-Resolution Model
|
| 3 |
-
# ==============================================
|
| 4 |
-
|
| 5 |
-
%%writefile model.py
|
| 6 |
-
import tensorflow as tf
|
| 7 |
-
from tensorflow.keras.models import Model
|
| 8 |
-
from tensorflow.keras.layers import Input, Conv2D, Add, Activation
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def psnr(y_true, y_pred):
|
| 12 |
-
"""
|
| 13 |
-
Computes the Peak Signal-to-Noise Ratio (PSNR) metric.
|
| 14 |
-
"""
|
| 15 |
-
return tf.image.psnr(y_true, y_pred, max_val=1.0)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def build_enhanced_model(input_shape=(32, 32, 3)):
|
| 19 |
-
"""
|
| 20 |
-
Builds an enhanced residual model for image super-resolution.
|
| 21 |
-
"""
|
| 22 |
-
# --- Input Layer ---
|
| 23 |
-
inputs = Input(shape=input_shape)
|
| 24 |
-
|
| 25 |
-
# --- Feature Extraction Layers ---
|
| 26 |
-
# Using smaller 3x3 kernels improves efficiency and generalization
|
| 27 |
-
x = Conv2D(64, (3, 3), padding='same', activation='relu')(inputs)
|
| 28 |
-
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)
|
| 29 |
-
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)
|
| 30 |
-
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x) # Extra depth for richer features
|
| 31 |
-
|
| 32 |
-
# --- Reconstruction Layer ---
|
| 33 |
-
x = Conv2D(3, (3, 3), padding='same')(x) # No activation here (linear output)
|
| 34 |
-
|
| 35 |
-
# --- Residual Connection ---
|
| 36 |
-
# The model learns to predict the missing details (residuals)
|
| 37 |
-
outputs = Add()([inputs, x])
|
| 38 |
-
outputs = Activation('sigmoid')(outputs) # Keeps pixel values in [0, 1]
|
| 39 |
-
|
| 40 |
-
# --- Build and Compile the Model ---
|
| 41 |
-
model = Model(inputs=inputs, outputs=outputs)
|
| 42 |
-
model.compile(
|
| 43 |
-
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
|
| 44 |
-
loss='mean_squared_error',
|
| 45 |
-
metrics=[psnr]
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|