加载数据集-图像时遇到问题

时间:2020-01-21 15:58:15

标签: python arrays keras dataset

我正在尝试使用Keras建立用于植物病害检测的模型,但是当我尝试加载数据集并将其转换为数组时,似乎出现了此错误。该图像出现的错误是我的数据集中不存在的错误,我也不知道为什么。

EPOCHS = 25
INIT_LR = 1e-3
BS = 32
default_image_size = tuple((256, 256))
image_size = 0
directory_root = "C:\\Users\\vipek\\Desktop\\PlantVillage-Dataset\\raw\\color"
width=256
height=256
depth=3

image_list, label_list = [], []

try:
    print("[INFO] Loading images ...")
    root_dir = listdir(directory_root)
    for directory in root_dir :
        # remove .DS_Store from list
        if directory == ".DS_Store" :
            root_dir.remove(directory)

    for plant_folder in root_dir :
        plant_disease_folder_list = listdir(f"{directory_root}/{plant_folder}")
        
        for disease_folder in plant_disease_folder_list :
            # remove .DS_Store from list
            if disease_folder == ".DS_Store" :
                plant_disease_folder_list.remove(disease_folder)

        for plant_disease_folder in plant_disease_folder_list:
            print(f"[INFO] Processing {plant_disease_folder} ...")
            plant_disease_image_list = listdir(f"{directory_root}/{plant_folder}/{plant_disease_folder}/")
                
            for single_plant_disease_image in plant_disease_image_list :
                if single_plant_disease_image == ".DS_Store" :
                    plant_disease_image_list.remove(single_plant_disease_image)

            for image in plant_disease_image_list[:500]:
                image_directory = f"{directory_root}/{plant_folder}/{plant_disease_folder}/{image}"
                if image_directory.endswith(".jpg") == True or image_directory.endswith(".JPG") == True:
                    image_list.append(convert_image_to_array(image_directory))
                    label_list.append(plant_disease_folder)
    print("[INFO] Image loading completed")  
except Exception as e:
    print(f"Error : {e}")

这是我得到的输出:

[INFO]正在加载图像...

[INFO]处理00416648-be6e-4bd4-bc8d-82f43f8a7240 ___ GCREC_Bact.Sp 3110.JPG ...

错误:[WinError 267]目录名称无效:'C:\ Users \ vipek \ Desktop \ PlantVillage-Dataset \ raw \ color / Tomato ___ Bacterial_spot / 00416648-be6e-4bd4-bc8d-82f43f8a7240 ___ GCREC_Bact.Sp 3110.JPG / '

3 个答案:

答案 0 :(得分:2)

您完全可以加载任何图像吗?我认为您收到错误是因为目录无效,这意味着您的根目录已定义为C:\something\something,然后在某些时候切换到folder/folder/folder。您只需要使用反斜杠。另外,我很确定您的代码在指定文件类型(JPG)后添加了/。那不应该在那里。

希望有帮助。

答案 1 :(得分:0)

我会说00416648-be6e-4bd4-bc8d-82f43f8a7240___GCREC_Bact.Sp 3110.JPG中的空格导致了此错误

答案 2 :(得分:0)

我建议您使用Path Python 3库处理路径。我的猜测是该路径未被正确识别。基于Path库,我将在代码中更改几行,并在其中定义路径:

# header
from pathlib import Path

# ...

directory_root = Path("C:\\Users\\vipek\\Desktop\\PlantVillage-Dataset\\raw\\color")

# ...

plant_disease_folder_list = listdir(directory_root / plant_folder)

# ...

plant_disease_image_list = listdir(directory_root / plant_folder / plant_disease_folder)

并确保避免在plant_disease_image_list路径中使用最后一个'/'!