2

我正在使用 tensorflow 2.0 API,我从所有图像路径创建了一个数据集,如下例所示

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)

但是,当我运行此代码以使用 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)

错误:

 /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'
4

1 回答 1

5

tf.keras.preprocessing.image.ImageDataGenerator不适用于tf.data.Dataset对象,它被设计用于处理普通的旧图像。

如果您想应用扩充,您必须使用tf.data.Dataset对象本身(通过各种.map调用),或者您可以tf.data.Dataset在使用tf.keras.preprocessing.image.ImageDataGenerator.

于 2019-04-10T15:27:29.737 回答