如何解决PyCharm中无法从albucore.utils导入preserve_channel_dim的问题?
解决Albumentations导入错误:无法从'albucore.utils'导入'preserve_channel_dim'
运行Albumentations代码时出现导入错误:无法从'albucore.utils'导入名称'preserve_channel_dim',已安装Albumentations和Albucore库,报错代码如下:
import albumentations as A import matplotlib.pyplot as plt import cv2 transform = A.Compose([ A.RandomCrop(width=256, height=256), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(p=0.2), ]) import matplotlib.pyplot as plt def cv2_imshow(img): plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() image = cv2.imread("C:\\Users\\raj\\Documents\\image_yolo\\1.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2_imshow(image) transformed = transform(image=image) transformed_image = transformed["image"] cv2_imshow(transformed_image)
错误截图:
解决方案
- 版本兼容修复:该错误是Albumentations与Albucore版本不匹配导致的,执行以下命令卸载现有包并安装兼容版本:
pip uninstall -y albumentations albucore pip install albumentations==1.4.12 albucore==0.0.15 - 升级到最新稳定版:如果上述版本仍有问题,直接升级Albumentations到最新稳定版,它会自动适配兼容的Albucore版本:
pip install --upgrade albumentations - 检查虚拟环境:若使用虚拟环境,确认在目标环境中安装依赖并运行代码,避免全局环境和虚拟环境的包冲突。
内容的提问来源于stack exchange,提问作者rajdeocuda sing
相关产品推荐
相关产品推荐

