keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
64 stars 31 forks source link

Added ReflectionPadding Layer In Response To Issue Number: #766 #768

Open thesahibnanda opened 8 months ago

thesahibnanda commented 8 months ago

Addressed Issue #766 (Feature Request)

Changes Made

Files Modified And Added

tf-keras ___tf-keras
____layers
____convolutional
___base_reflection_padding.py (New File Added)
___reflection_padding1d.py (New File Added)
___reflection_padding2d.py (New File Added)
___reflection_padding3d.py (New File Added)
___reflection_padding_test.py (New File Added)
___BUILD (Modified File)

Usage

  1. ReflectionPadding1D:
    
    import tensorflow as tf
    from tensorflow.keras.layers import ReflectionPadding1D

Example usage

model = tf.keras.Sequential([ ReflectionPadding1D(padding=1, input_shape=(10, 1)), tf.keras.layers.Conv1D(32, 3), tf.keras.layers.MaxPooling1D(2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(10, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ])

Compile the model

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

Train the model

model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_val, y_val))


2. **ReflectionPadding2D:**
```python
import tensorflow as tf
from tensorflow.keras.layers import ReflectionPadding2D

# Example usage
model = tf.keras.Sequential([
    ReflectionPadding2D(padding=(1, 1), input_shape=(28, 28, 1)),
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_val, y_val))
  1. ReflectionPadding3D:
    
    import tensorflow as tf
    from tensorflow.keras.layers import ReflectionPadding3D

Example usage

model = tf.keras.Sequential([ ReflectionPadding3D(padding=((1, 1, 1), input_shape=(32, 32, 32, 3)), tf.keras.layers.Conv3D(64, (3, 3, 3), activation='relu'), tf.keras.layers.MaxPooling3D((2, 2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ])

Compile the model

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Train the model

model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_val, y_val))



## **Benefits**
1. **Preservation of Spatial Information:** Reflection padding preserves spatial information along the borders of the input data by reflecting the values from the interior to the exterior. This helps in retaining the original spatial dimensions of the input.

2. **Reduction of Boundary Artifacts:** By reflecting values from inside to outside, reflection padding helps in reducing boundary artifacts that may occur during convolution operations, such as checkerboard artifacts or edge effects.

3. **Improved Performance:** Reflection padding can lead to improved performance in tasks where preserving spatial information is crucial, such as image processing or semantic segmentation. It ensures that the features extracted by convolutional layers near the borders are not biased by boundary effects.

4. **Symmetry:** Reflection padding maintains symmetry around the borders of the input data, which can be beneficial in tasks where symmetry is important, such as object detection or recognition.

5. **Compatibility with Convolution Operations:** Reflection padding seamlessly integrates with standard convolutional operations, requiring minimal modification to existing convolutional neural network architectures.

6. **Flexibility in Padding Size:** Reflection padding allows for flexible specification of padding sizes, enabling users to adapt the padding to different input sizes and network architectures easily.

7. **Reduction of Information Loss:** Unlike zero padding, which introduces constant values at the borders, reflection padding retains more information from the original input, thereby reducing information loss during convolution operations.

8. **Naturalness in Padding:** Reflection padding mimics the natural behavior of light reflections, making it more suitable for tasks involving image processing or computer vision, where preserving naturalness is desired.
hertschuh commented 6 months ago

@thesahibnanda , thank you for the PR.

Per this comment https://github.com/keras-team/tf-keras/issues/766#issuecomment-2025846551 , the chosen approach is to add a new fill_mode of "reflect" to the existing keras.ops.image.PadImages to make it very consistent with the existing API.

Can you modify this PR to move the logic to keras.ops.image.PadImages? Thanks!

thesahibnanda commented 6 months ago

I Will Do It, Thanks For Guidance