Kero
Kero

Reputation: 1944

How to use tensorflow2.0 dataset with keras ImageDataGenerator

i am using tensorflow 2.0 API where i created a dataset from all image paths like example below

X_train, X_test, y_train, y_test = train_test_split(all_image_paths, all_image_labels, test_size=0.20, random_state=32)

path_train_ds = tf.data.Dataset.from_tensor_slices(X_train)
image_train_ds = path_train_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)

However, i am getting error when i ran this code to apply some agumentation using keras ImageDataGenerator

datagen=tf.keras.preprocessing.image.ImageDataGenerator(featurewise_center=True,
        featurewise_std_normalization=True,
        rotation_range=20,
        width_shift_range=0.2,
        height_shift_range=0.2,
        horizontal_flip=True)
datagen.fit(image_train_ds)

Error:

 /usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py in fit(self, x, augment, rounds, seed)
    907             seed: Int (default: None). Random seed.
    908        """
--> 909         x = np.asarray(x, dtype=self.dtype)
    910         if x.ndim != 4:
    911             raise ValueError('Input to `.fit()` should have rank 4. '

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    499 
    500     """
--> 501     return array(a, dtype, copy=False, order=order)
    502 
    503 

TypeError: float() argument must be a string or a number, not 'ParallelMapDataset'

Upvotes: 2

Views: 797

Answers (1)

nessuno
nessuno

Reputation: 27042

tf.keras.preprocessing.image.ImageDataGenerator does not work with a tf.data.Dataset object, it has been designed to work with plain old images.

If you want to apply augmentation you have to use the tf.data.Dataset object itself (via various .map call) or you can create a tf.data.Dataset object after having created an augmented dataset using tf.keras.preprocessing.image.ImageDataGenerator.

Upvotes: 5

Related Questions