0

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
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Sean
  • 79
  • 7
  • 1
    it would be helpful to know what errors those are – Christoph Rackwitz Dec 27 '20 at 12:16
  • i get the value error, but if i use tf.expand_dims(img_tensor, 2), i get another error – Sean Dec 27 '20 at 13:55
  • 1
    Does this answer your question? [ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)](https://stackoverflow.com/questions/43977463/valueerror-could-not-broadcast-input-array-from-shape-224-224-3-into-shape-2) – Christoph Rackwitz Dec 27 '20 at 22:41
  • Unfortunately it doesn't help solve the problem at hand – Sean Dec 28 '20 at 16:17
  • If you could let us know how you are defining and compiling your model to find out cause of NaN loss while training. –  Mar 04 '22 at 12:58

0 Answers0