gizmo
gizmo

Reputation: 29

Validation_split with BatchDataset

I am trying to split my dataset into validation and training. I was unable to call a validation subset in model.fit() as y data is not accepted for datasets, and the validation_split works only for tensors or numpy arrays. I checked the documentation for tensorflow, and there is no documentation of casting of BatchDataset to tensor, unless the neural network is altered itself, which I am unable to do as I am using the resnet architecture using keras.

The following errors showed up respectively:

 raise ValueError("`y` argument is not supported when using "
ValueError: `y` argument is not supported when using dataset as input.

 raise ValueError(
ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>]

Here is the code I am currently working on:

test = tf.keras.preprocessing.image_dataset_from_directory(
  "/Users/***/***/data-aug",
  labels="inferred",
  label_mode="categorical",
  color_mode="rgb",
  batch_size=32,
  image_size=(224, 224),
  shuffle=True,
  seed=123,
  interpolation="bilinear",
  follow_links=False,
  crop_to_aspect_ratio=True
)

H = model.fit(test,
              validation_split=0.2,
              epochs=200,
              shuffle=True,
              verbose=1,
              callbacks=[mc,es,pm])

Thank you for your time

Upvotes: 0

Views: 822

Answers (1)

Gerry P
Gerry P

Reputation: 8092

image_dataset_from_directory has a parameters called validation_split and subset that you can use to achieve your objective. code is below

train_data = tf.keras.preprocessing.image_dataset_from_directory(
  "/Users/***/***/data-aug",
  labels="inferred",
  label_mode="categorical",
  color_mode="rgb",
  batch_size=32,
  image_size=(224, 224),
  shuffle=True,
  seed=123,
  interpolation="bilinear",
  follow_links=False,
  crop_to_aspect_ratio=True
  validation_split=.2
  subset='training)
val_data=test = tf.keras.preprocessing.image_dataset_from_directory(
  "/Users/***/***/data-aug",
  labels="inferred",
  label_mode="categorical",
  color_mode="rgb",
  batch_size=32,
  image_size=(224, 224),
  shuffle=False,  
  interpolation="bilinear",
  follow_links=False,
  crop_to_aspect_ratio=True
  validation_split=.2
  subset='validation')

H = model.fit(train_data, validation_data=val_data, epochs=200,
              shuffle=True,verbose=1, callbacks=[mc,es,pm])

Upvotes: 1

Related Questions