Home > database >  TypeError: float() argument must be a string or a number, not 'BatchDataset' when data aug
TypeError: float() argument must be a string or a number, not 'BatchDataset' when data aug

Time:11-23

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! :)

CodePudding user response:

First, the article you referred to is 3 years old and is a bit outdated. Starting from tensorflow 2.1.0, the .fit method accepts generators too, and currently it fully replaced .fit_generator. I suggest you to update your tensorflow if possible.

Second, the error seems to be not in the fit_generator method, but in the way you define the datasets. They just first called in fit_generator, and that's why the error message trace you back there.

As of the error itself, I don't understand the part of nesting the generators, and I think it can cause problems here. You're trying to pass batched dataset gotten from tf.keras.utils.image_dataset_from_directory to another generator, which seems to be impossible.

If I understood correctly, you have only one label on each image, and images of each class are stored in separate folders, so I suggest you to use the flow_from_directory method of tf.keras.preprocessing.image.ImageDataGenerator directly. This generator will both read and augment the images, so you can drop the tf.keras.utils.image_dataset_from_directory part.

To use this generator, you need to have images in the form:

  • root_directory
    • class1 folder
    • class2 folder
    • etc

and your code will be something like this:

gen = tf.keras.preprocessing.image.ImageDataGenerator( #desired augmentation, ...) 
train_generator = gen.flow_from_directory(directory = root_directory,
target_size=(256, 256), classes= *list of class names*,
class_mode='categorical', batch_size=32, shuffle=True, ...)
model.fit(train_generator, ...)

You can pass "validation_split" argument too to get separate datasets for training and validation. Read more about the ImageDataGenerator and flow_from_directory method in the official documentation.

  • Related