module 'tensorflow.keras.utils' has no attribute 'image_dataset_from_directory'
时间: 2023-10-03 21:07:28 浏览: 190
浅谈keras中的keras.utils.to_categorical用法
This error message typically occurs when using an outdated version of TensorFlow. The method `image_dataset_from_directory` was introduced in TensorFlow 2.2.0.
To resolve this error, you can update your TensorFlow version to 2.2.0 or later. You can do this by running `!pip install --upgrade tensorflow` in your Python environment.
Alternatively, you can use the `ImageDataGenerator` class from `tensorflow.keras.preprocessing.image` to generate image data from directories. Here's an example:
```python
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_generator = ImageDataGenerator().flow_from_directory(
'path/to/train_directory',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
test_generator = ImageDataGenerator().flow_from_directory(
'path/to/test_directory',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
```
This will generate image data from the directories specified and return generator objects that can be used for training and testing.
阅读全文