我目前正在try 一个小的图像机器学习项目.我找到了这个人的Kaggle code,我试着从头开始复制它.然而,即使是在主体部分,我也已经面临一个错误.

我敢肯定,在我的结束上一定有一个本地化的问题,这是如何结束的,但我不知道是什么.

我的代码是:

#Import Libraries

#Data processing modules
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt
import cv2
#File directory modules
import glob as gb
import os
#Training and testing (machine learning) modules
import tensorflow as tf 
import keras

#Importing the images into the code

trainDataset = 'melanoma_cancer_dataset/train'
testDataset = 'melanoma_cancer_dataset/test'
predictionDataset = 'melanoma_cancer_dataset/skinTest'

#creating empty lists for the images to fall into for processing
training_List = []
testing_list = []
#making a classification dictionary for the two keys, benign and malignant
#used for inserting into the images
diction = {'benign' : 0, 'malignant' : 1}

#Read through the folder's length contents
for folder in os.listdir(trainDataset):
    data = gb.glob(pathname=str(trainDataset + folder + '/*.jpg'))
    print(f'{len(data)} in folder {folder}')
    #read the images, resize them in a uniform order, and store them in the empty lists
    for data in data:
        image = cv2.imread(data)
        imageList = cv2.resize(image(120,120))
        training_List.append(list(imageList))

笔记本的输出显示文件夹中存储了0 images/contents个.现在我有点怀疑这里发生了什么,我想知道一些答案.先谢谢你.我也在使用我自己的VScode.

这是我档案的截图:

file tree and notebooks

推荐答案

根据您的文件夹 struct 和您提供的代码,问题是您没有在文件夹路径的末尾放置尾随斜杠. 在提供的代码中,您试图将文件夹名称直接与路径连接.但是,如果您遗漏了斜杠或文件夹变量不包含尾随斜杠,则可能导致路径不正确.

更新路径如下:

trainDataset = 'melanoma_cancer_dataset/train/'
testDataset = 'melanoma_cancer_dataset/test/'
predictionDataset = 'melanoma_cancer_dataset/skinTest/'

你的代码在这里做什么:

for folder in os.listdir(trainDataset):
    data = gb.glob(pathname=str(trainDataset + folder + '/*.jpg'))

它将转到trainDataset的路径,然后使用os.listdir()列出那里的文件夹(命名为恶性和良性). 这些路径被连接以生成最终的图像路径:

data = gb.glob(pathname=str(trainDataset + folder + '/*.jpg'))

此外,行中有轻微语法错误:

imageList = cv2.resize(image(120,120))

应当

cv2.resize(image, (120, 120))

此外,您添加到training_List的方式可能是错误的.在添加之前,您需要将imageList转换为列表,或者如果您想保留图像数组 struct ,则直接添加imageList.

完整更新代码:

# Data processing modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import cv2
# File directory modules
import glob as gb
import os
# Training and testing (machine learning) modules
import tensorflow as tf
import keras

# Directories
trainDataset = 'melanoma_cancer_dataset/train/'
testDataset = 'melanoma_cancer_dataset/test/'
predictionDataset = 'melanoma_cancer_dataset/skinTest/'

# Empty list for the images
training_List = []
testing_list = []

# Classification dictionary
diction = {'benign': 0, 'malignant': 1}

# Read through the folder's contents
for folder in os.listdir(trainDataset):
    # Corrected the path pattern and added a slash
    data = gb.glob(pathname=str(trainDataset + folder + '/*.jpg'))
    print(f'{len(data)} in folder {folder}')
    # Read the images, resize them, and store them in the list
    for file_path in data:
        image = cv2.imread(file_path)
        # Corrected the resize function call
        imageList = cv2.resize(image, (120, 120))
        # Append the image array directly
        training_List.append(imageList)

print(f'Total images in training set: {len(training_List)}')

Python相关问答推荐

即使在可见的情况下也不相互作用

仿制药的类型铸造

当使用keras.utils.Image_dataset_from_directory仅加载测试数据集时,结果不同

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

查找两极rame中组之间的所有差异

将输入管道传输到正在运行的Python脚本中

Python虚拟环境的轻量级使用

如何获取numpy数组的特定索引值?

python中字符串的条件替换

Tkinter菜单自发添加额外项目

如何使用两个关键函数来排序一个多索引框架?

Odoo16:模板中使用的docs变量在哪里定义?

以异步方式填充Pandas 数据帧

如何过滤组s最大和最小行使用`transform`'

如何在一组行中找到循环?

使用python playwright从 Select 子菜单中 Select 值

如何在验证文本列表时使正则表达式无序?

PySpark:如何最有效地读取不同列位置的多个CSV文件

使用pythonminidom过滤XML文件

某些值的数值幂和**之间的差异