已安装SciPy 1.5.3仍触发ImportError:TensorFlow图像生成代码报错排查与解决
Problem Description
I'm running an image generation script using TensorFlow and NumPy, but keep hitting an ImportError even though I've installed SciPy 1.5.3. Here's my code:
def img_gen(pair_list, file): image = [] for loc in pair_list: shutil.copy(loc, target_folder) img_gen = ImageDataGenerator(rescale=1 / 255) img_tranform = img_gen.flow_from_directory(temp_folder, target_size=(112, 112), batch_size=1, class_mode='sparse', shuffle=False, color_mode='rgb', seed=42) x_data, y_data = next(img_tranform) image.append(x_data) remove_file = os.path.join(target_folder,file) remove_file = remove_file.replace("\\", "/") os.remove(remove_file) return image img_class = [] img_pairs = [] for folders1, subfolders1, filenames1 in os.walk(orig_img_dir): for folders2, subfolders2, filenames2 in os.walk(segm_img_dir): for file1 in filenames1: for file2 in filenames2: if file1.endswith('.png') and file1==file2: audit_class = folders1.partition('\\') audit_class = audit_class[-1] img_class.append(audit_class) file1_loc = os.path.join(folders1, file1) file2_loc = os.path.join(folders2, file2) img_pair = [file1_loc, file2_loc] con_image = img_gen(img_pair, file1) img = tf.concat([con_image[0][0], con_image[1][0]], axis=-1) img_pairs.append(img)
When I run this, I get the following error:
C:\Program Files\WinPython64-3.8.6\python-3.8.6.amd64\lib\site-packages\keras_preprocessing\image\affine_transformations.py in apply_affine_transform(x, theta, tx, ty, shear, zx, zy, row_axis, col_axis, channel_axis, fill_mode, cval, order) 279 """ 280 if scipy is None: ---> 281 raise ImportError('Image transformations require SciPy. Install SciPy.') 282 transform_matrix = None ImportError: Image transformations require SciPy. Install SciPy.
I already have SciPy 1.5.3 installed, so what's causing this and how can I fix it?
Root Causes & Solutions
This error typically pops up when the keras_preprocessing library can't access your SciPy installation in the active Python environment. Here are the most likely fixes based on common scenarios:
1. SciPy isn't installed in your WinPython environment
You might have installed SciPy in a different Python environment, but your script is running in the WinPython 3.8.6 instance.
Fix:
- Open the WinPython Command Prompt from your installation folder
- Run
pip listto check if SciPy 1.5.3 appears in the list. If not, install it with:pip install scipy==1.5.3 - If it is listed, reinstall to fix potential corrupted files:
pip uninstall scipy -y pip install scipy==1.5.3
2. Version mismatch between keras_preprocessing and SciPy
Older versions of keras_preprocessing can have trouble detecting specific SciPy versions like 1.5.3.
Fix:
Upgrade the keras_preprocessing library to a version compatible with Python 3.8:
pip install --upgrade keras-preprocessing
3. Your script is using the wrong Python interpreter
Your system might be pointing to a different Python installation instead of the WinPython one you intended.
Fix:
Add these debug lines at the top of your script to verify the environment:
import scipy import sys print(f"Python executable path: {sys.executable}") print(f"SciPy version: {scipy.__version__}") print(f"SciPy installation path: {scipy.__file__}")
- If the path doesn't match your WinPython directory (
C:\Program Files\WinPython64-3.8.6\python-3.8.6.amd64\...), switch to the WinPython environment to run your script.
4. Missing underlying SciPy dependencies (rare)
SciPy relies on low-level libraries like OpenBLAS or MKL. If these are missing, keras_preprocessing might fail to detect SciPy.
Fix:
Use WinPython's built-in package manager to reinstall SciPy (it handles dependencies automatically) or download a pre-built wheel for Python 3.8/Windows 64-bit from PyPI and install it manually.
内容的提问来源于stack exchange,提问作者Raj Rajeshwari Prasad

