Function to apply histogram equalization to each image individually
def histogram_equalization(image, label):
# Convert the TensorFlow tensor to a NumPy array
image_np = image.numpy()
# Apply histogram equalization to each image in the batch
equalized_images = []
for img in image_np:
# Ensure the image is in the range 0-255 and type uint8
img = img.astype('uint8')
img = img.squeeze()
# Apply OpenCV's histogram equalization (which expects a 2D array of type uint8)
img_eq = cv2.equalizeHist(img)
img_eq = np.expand_dims(img_eq, axis=-1)
equalized_images.append(img_eq)
# Convert the list of equalized images back to a tensor
equalized_images = np.array(equalized_images)
return tf.convert_to_tensor(equalized_images, dtype=tf.float32), label
Apply histogram equalization to the dataset
train_data_eq = train_ds.map(lambda image, label: tf.py_function(histogram_equalization, [image, label], [tf.float32, tf.float32]))
The error is
ValueError Traceback (most recent call last)
Cell In[27], line 2
1 epochs=5
----> 2 history = model.fit(
3 train_data_eq,
4 validation_data=val_ds,
5 epochs=epochs
6 )
File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.traceback)
120 # To get the full stack trace, call:
121 # keras.config.disable_traceback_filtering()
→ 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.traceback)
120 # To get the full stack trace, call:
121 # keras.config.disable_traceback_filtering()
→ 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
ValueError: as_list() is not defined on an unknown TensorShape.