I am having troubles with applying Data Augmentation when training the model. Specifically about the use of the fit_generator() method.
I have originally run my model succesfully without augmentation using the fit() method, however according to others it is recomended to use fit_generator(). It seems like both methods need the same input when it comes to images and labels, but I am getting the following ERROR when running the code below:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_35/139227558.py in <module>
105
106 # train the network
--> 107 model.fit_generator(aug.flow(train_ds, batch_size=batch_size),
108 validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
109 epochs=epochs)
/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
894 save_prefix=save_prefix,
895 save_format=save_format,
--> 896 subset=subset)
897
898 def flow_from_directory(self,
/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
472 save_format=save_format,
473 subset=subset,
--> 474 **kwargs)
475
476
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
119 y = y[split_idx:]
120
--> 121 self.x = np.asarray(x, dtype=self.dtype)
122 self.x_misc = x_misc
123 if self.x.ndim != 4:
/opt/conda/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
TypeError: float() argument must be a string or a number, not 'BatchDataset'
I have completed google in trying to fix the TypeError: float() argument must be a string or a number, not 'BatchDataset' error, but to no avail. Does anyone have suggestions as to move forward?
import pathlib
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
# Set data directory
data_dir = pathlib.Path("../input/validatedweaponsv6/images/")
# Set image size
img_height = 120
img_width = 120
# Hyperparameters
batch_size = 128
epochs = 50
learning_rate = 0.001
# Create the training dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="training",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
# Create the validation dataset
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="validation",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
# Create sequential model
model = Sequential([
# Preprocessing
layers.Rescaling(1./127.5, offset=-1,
input_shape=(img_height, img_width, 3)),
# Encoder
layers.Conv2D(8, 3, activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(16, 3, activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, activation='relu'),
# layers.Conv2D(2, 3, activation='relu'), ???
layers.Flatten(),
# Decoder
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(2, activation='softmax')
])
# Print the model to see the different output shapes
print(model.summary())
# Compile model
model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.SGD(learning_rate=learning_rate), metrics=['accuracy'])
# construct the training image generator for data augmentation
aug = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=20, zoom_range=0.15,
width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
horizontal_flip=True, fill_mode="nearest")
# train the network
model.fit_generator(aug.flow(train_ds, batch_size=batch_size),
validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
epochs=epochs)
# Print scores
score = model.evaluate(train_ds, verbose=0)
print('Validation loss:', score[0])
print('Validation accuracy:', score[1])
# Show loss and accuracy models
show_history(history)
Thank you for looking at my post! :)