I've tried modifying the code with the pre_process function and the color_mode but each time a different error pops up.
def pre_process(img):
ret, mask = cv2. threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
return mask_inv
train = ImageDataGenerator(rescale=1/255, vertical_flip=True,preprocessing_function=pre_process)
test = ImageDataGenerator(rescale=1/255, vertical_flip=True,preprocessing_function=pre_process)
train_dataset = train.flow_from_directory(path,
target_size=(512,512),
batch_size = 32,
class_mode = 'binary',
color_mode = 'grayscale',
shuffle = True
)
test_dataset = test.flow_from_directory(path,
target_size=(512,512),
batch_size =32,
class_mode = 'binary',
color_mode = 'grayscale',
shuffle = True
)
ValueError: could not broadcast input array from shape (512,512) into shape (512,512,1)..... It's what i get from the above code.
However, if i used the pre_process method below, there is no error, but my loss is always NAN while training.
def pre_process(img):
ret, mask = cv2. threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
img_expanded = tf.expand_dims(mask_inv, 2)
return img_expanded