Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thesahibnanda
Copy link

Addressed Issue #766 (Feature Request)

Changes Made

  • Added ReflectionPadding1D, ReflectionPadding2D and ReflectionPadding3D to support Reflection Padding Layer in Tensorflow's Keras API
  • Implemented test cases for the same
  • Added the ReflectionPadding Functionality in BUILD file

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))
  1. ReflectionPadding2D:
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
Copy link
Contributor

@thesahibnanda , thank you for the PR.

Per this comment #766 (comment) , 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
Copy link
Author

I Will Do It, Thanks For Guidance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants