Ubuntu系统Python3.6中flow_from_directory报错不存在且不接受参数
flow_from_directory not found error in Ubuntu + Python 3.6 Hey there! Let's break down why you're seeing the "Can't use 'flow_from_directory' because it doesn't seems to exist" error, and how to fix it. This is a common gotcha when starting out with image data generators in Python, so don't worry—we'll get you sorted.
Common Causes & Fixes
1. You're not calling the method on an ImageDataGenerator instance
flow_from_directory isn't a standalone function—it's a method of the ImageDataGenerator class (part of Keras/TensorFlow Keras). If you're trying to call it directly like flow_from_directory(...) instead of first creating an instance of the generator, Python won't recognize it.
Here's the correct way to use it:
# First import the right class from tensorflow.keras.preprocessing.image import ImageDataGenerator # Or if you're using standalone Keras: from keras.preprocessing.image import ImageDataGenerator # Create an instance of ImageDataGenerator datagen = ImageDataGenerator(rescale=1./255) # Add any preprocessing params you need # Now call flow_from_directory on the instance train_generator = datagen.flow_from_directory( 'path/to/your/train/directory', target_size=(150, 150), # Adjust to your image size batch_size=32, class_mode='binary' # Use 'categorical' for multi-class tasks )
2. You've imported the wrong library or class
Double-check your import statement. If you're importing from a different module (like a custom data generator or another library), the class won't have the flow_from_directory method. Make sure you're importing exactly:
from tensorflow.keras.preprocessing.image import ImageDataGenerator(for TensorFlow-integrated Keras)- OR
from keras.preprocessing.image import ImageDataGenerator(for standalone Keras)
3. Your TensorFlow/Keras version is incompatible with Python 3.6
Python 3.6 is an older version, so you need to use a compatible release of TensorFlow/Keras:
- TensorFlow versions up to 2.5.x support Python 3.6 (versions 2.6+ drop Python 3.6 support)
- Standalone Keras versions up to 2.4.3 work with Python 3.6
To check your current version:
pip show tensorflow # For TensorFlow Keras # OR pip show keras # For standalone Keras
If you have an incompatible version, install a compatible one with:
pip install tensorflow==2.5.0 --user # OR pip install keras==2.4.3 --user
4. You might have a typo in the method name
Double-check that you're writing flow_from_directory correctly—capitalization and underscores matter! It's easy to mistype as flow_from_dir or FlowFromDirectory, which would cause the "doesn't exist" error.
Next Steps if You're Still Stuck
If none of the above fixes work, share a snippet of your code (the import line and the part where you try to use flow_from_directory)—that will help pinpoint exactly what's going wrong.
内容的提问来源于stack exchange,提问作者Pierre Riedinger

