0

I'm trying to train my own images with CNN.

However, I split train/validation/test data, and run model. These are my code.

print("train images : {} / train labels : {}".format(train_image.shape, train_label.shape))
print("val images : {} / val labels : {}".format(val_image.shape, val_label.shape))
print("test images : {} / test labels : {}".format(test_image.shape, test_label.shape))

train images : (504, 255, 255, 3) / train labels : (504,)
val images : (127, 255, 255, 3) / val labels : (127,)
test images : (158, 255, 255, 3) / test labels : (158,)

import tensorflow as tf
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(255, 255, 3)),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation='relu'),
  tf.keras.layers.Dense(1, activation='sigmoid')
])

model.summary()

enter image description here

from tensorflow.keras.optimizers import RMSprop

model.compile(optimizer=RMSprop(lr=0.001),
            loss='binary_crossentropy',
            metrics = ['accuracy'])

history = model.fit(train_image, train_label,
                    epochs=100,
                    validation_data = (val_image, val_label),
                    validation_steps=50,
                    verbose=2)

enter image description here

As you can see, validation set works only at first time. How can I fix this?

엄태선
  • 13
  • 3
  • I believe it's related to the use of `validation_steps=50`. – Mohamed abdelmagid Oct 01 '21 at 07:01
  • I searched what is validation_steps, but still no clue. Can you explain it with more details? – 엄태선 Oct 01 '21 at 07:12
  • from the [docs](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit): "only relevant if `validation_data` is provided **and** is a `tf.data` dataset.", to use `validation_steps` youn need tf.Data not tuple. – Mohamed abdelmagid Oct 01 '21 at 07:13
  • If i understood the docs correctly, you need to create a `tf.Data.Dataset` object out of your validation set and add the repeat function, then continue, or leave it as tuple, but remove the `validation_steps` parameter, or simply use `validation_split` if there is nothing special about how the validation set is picked. – Mohamed abdelmagid Oct 01 '21 at 07:21
  • I remove **validation_steps** and it works as I wanted. Maybe I try to create **tf.Data.Dataset**. Thanks. And one more thing. Does my ID looks korean?? – 엄태선 Oct 01 '21 at 07:49
  • You are welcome, I'll create an answer now, and Yes, it does look Korean. – Mohamed abdelmagid Oct 01 '21 at 07:50
  • one more thing, it's `tf.data.Dataset` i miss-spilled it in the previous comment, and fixed it in the answer. – Mohamed abdelmagid Oct 01 '21 at 07:55

1 Answers1

0

the problem is using validation_steps while the dataset is not of type tf.data.Dataset, as stated in the docs:

validation_steps: Only relevant if validation_data is provided and is a tf.data dataset ...

You can solve this by either

  1. Changing your validation data to be of type tf.data.Dataset (docs) and add repeat to have the validation continue without the data being over.

  2. Remove the validation_steps attribute, this will have it run the validation at each epoch.