Reputation: 23256
To perform image-preprocessing, I was trying to use the ImageDataGenerator class from Keras. Here is how used it:
data_generator = keras.preprocessing.image.ImageDataGenerator(
rotation_range = 60,
width_shift_range = 0.1,
height_shift_range = 0.1,
brightness_range = [0.5, 1.5],
shear_range = 0.01,
zoom_range = [0, 1],
horizontal_flip = True,
vertical_flip = True,
preprocessing_function = preprocess_other
)
The preprocessing_function
attribute has been assigned a function named preprocess_other
as defined below:
def preprocess_other(image):
flip = np.random.random()
if flip > 0.5:
# Add noise
blank_image = np.zeros(image.shape, np.uint8)
cv2.randn(blank_image, 0, 5)
noisy_image = cv2.add(image, blank_image)
return noisy_image
else:
# Return the original image
return image
The role of this function is to add noise to an image with a probability of 0.5.
As I start the training process (training the CNN), it works for a few seconds but fails due to some error with the preprocess_other
function with an error saying:
error: OpenCV(3.4.3) /io/opencv/modules/core/src/arithm.cpp:683:
error: (-5:Bad argument) When the input arrays in
add/subtract/multiply/divide functions have different types,
the output array type must be explicitly specified in function
'arithm_op'
I debugged but could not understand the reason for it. Am I trying to add the noise in an incorrect way? How could I correct this error?
Upvotes: 1
Views: 106
Reputation: 13611
The problem is that image
and blank_image
have different types.
You can change:
blank_image = np.zeros(image.shape, np.uint8)
to:
blank_image = np.zeros(image.shape, image.dtype)
or to:
blank_image = np.zeros_like(image)
Upvotes: 1