Views
No views yet
def call(self, inputs):
return tf.image.rgb_to_grayscale(inputs)def call(self, inputs):
# Ensure input has a channel dimension
if inputs.shape.rank == 3: # Shape: (None, 224, 224)
inputs = tf.expand_dims(inputs, axis=-1) # Shape: (None, 224, 224, 1)
return tf.image.grayscale_to_rgb(inputs)
def compute_output_shape(self, input_shape):
return input_shape[:-1] + (3,) # Output has 3 channelsdef call(self, inputs):
if tf.executing_eagerly():
# PIL-based enhancements (for debugging and visualization)
img = tf.keras.preprocessing.image.array_to_img(inputs[0]) # Access first image in batch
brightness_factor = 1 + (34 / 100)
x_enhanced = ImageEnhance.Brightness(img).enhance(brightness_factor)
contrast_factor = 1 + (30 / 100)
x_enhanced = ImageEnhance.Contrast(x_enhanced).enhance(contrast_factor)
sharpness_factor = 1 + (70 / 100)
x_enhanced = ImageEnhance.Sharpness(x_enhanced).enhance(sharpness_factor)
x_enhanced = x_enhanced.filter(ImageFilter.MedianFilter(size=1))
# Convert back to tensor
x_enhanced = tf.keras.preprocessing.image.img_to_array(x_enhanced)
x_enhanced = tf.expand_dims(x_enhanced, 0) # Restore batch dimension
x_enhanced = tf.cast(x_enhanced, inputs.dtype)
else:
# Placeholder for TensorFlow-based enhancements during training
x_enhanced = inputs
return x_enhanceddef call(self, inputs):
# Placeholder for stroke width computation logic
return inputs # Check the number of channels
num_channels = inputs.shape[-1]
# Define Roberts Cross kernels
kernel_x = tf.constant([[1, 0], [0, -1]], dtype=tf.float32)
kernel_y = tf.constant([[0, 1], [-1, 0]], dtype=tf.float32)
# Reshape kernels to match the number of input channels
kernel_x = tf.reshape(kernel_x, [2, 2, 1, 1])
kernel_y = tf.reshape(kernel_y, [2, 2, 1, 1])
# Apply convolution using Roberts Cross kernels for each channel
grad_x = [tf.nn.conv2d(inputs_float32[..., i:i+1], kernel_x, strides=[1, 1, 1, 1], padding='SAME') for i in range(num_channels)]
grad_y = [tf.nn.conv2d(inputs_float32[..., i:i+1], kernel_y, strides=[1, 1, 1, 1], padding='SAME') for i in range(num_channels)]
# Concatenate the gradients along the channel dimension (if more than one channel)
grad_x = tf.concat(grad_x, axis=-1)
grad_y = tf.concat(grad_y, axis=-1)
# Compute the gradient magnitude
grad_magnitude = tf.sqrt(tf.square(grad_x) + tf.square(grad_y))
# Normalize the gradient magnitudes
grad_magnitude /= tf.reduce_max(grad_magnitude)
# Return the result with the same number of channels as input
return grad_magnitude
def compute_output_shape(self, input_shape):
return input_shapedef call(self, inputs):
# Ensure inputs have the shape (batch_size, height, width, 1) for grayscale
if inputs.shape[-1] != 1:
raise ValueError("Input must be a grayscale image with a single channel.")
# Define the kernel for dilation and erosion
kernel = tf.ones(tuple(self.kernel_size) + (1,), dtype=tf.float32)
# Perform dilation
dilated_image = tf.image.extract_patches(
images=inputs,
sizes=[1, self.kernel_size[0], self.kernel_size[1], 1],
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
padding='SAME'
)
dilated_image = tf.reduce_max(dilated_image, axis=-1, keepdims=True)
# Perform erosion
eroded_image = tf.image.extract_patches(
images=dilated_image,
sizes=[1, self.kernel_size[0], self.kernel_size[1], 1],
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
padding='SAME'
)
eroded_image = tf.reduce_min(eroded_image, axis=-1, keepdims=True)
return eroded_image
def compute_output_shape(self, input_shape):
return input_shape # Ensure output shape matches input shapedef call(self, inputs):
squeezed = tf.squeeze(inputs, axis=self.axis) # Remove extra dimensions
# If squeezed shape has 3 dimensions and we have grayscale images, restore the channel dimension
if len(squeezed.shape) == 3: # (batch, height, width) for grayscale image
restored = tf.expand_dims(squeezed, axis=-1) # (batch_size, 224, 224, 1)
else:
restored = squeezed # No expansion needed, already 4D
return restored# Handle RGBA images (if the image has an alpha channel)
if img_array.shape[-1] == 4:
img_array = img_array[..., :3]
img_array = np.expand_dims(img_array, 0) / 255.0
return img_array# Optionally print predictions for debugging
print(f"Prediction probabilities: {predictions[0]}")
class_label = class_labels[predicted_class_index]
diagnostic_prompt = diagnostic_messages[class_label]
return f"This handwriting is \"{class_label}\".\n\n{diagnostic_prompt}"