【发布时间】:2020-04-14 20:47:09
【问题描述】:
当我尝试使用 3 个数据集文件夹运行此代码时,我遇到了这个问题。以前,此代码在数据集文件夹内只有 2 个文件夹即可完美运行,一个名为 normal,另一个名为 covid。但在这种情况下,我添加了另一个名为 pneumonia 的分类器,使其成为 3 类图像分类器。我是机器学习的新手,所以我对如何解决这个问题进行了很多研究,但每个解决方案都不同,代码也不同。我尝试了它们,但它们不起作用,这就是我寻求您帮助的原因。
此代码不属于我,它是 Adrian Rosebrock 代码,所有功劳归于他。它是关于在 COVID 或正常情况下对 X 射线图像进行分类,但改进此代码的想法添加了一个新类别来对具有正常(非 COVID)肺炎的图像进行分类。这就是我在数据集中添加一个新文件夹的原因。希望您能帮帮我,谢谢!
# USAGE
python train.py --dataset dataset
# import the necessary packages
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from imutils import paths
import numpy as np
import argparse
import cv2
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
help="path to input dataset")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output loss/accuracy plot")
ap.add_argument("-m", "--model", type=str, default="covid19.model",
help="path to output loss/accuracy plot")
args = vars(ap.parse_args())
# initialize the initial learning rate, number of epochs to train for,
# and batch size
INIT_LR = 1e-3
EPOCHS = 1
BS = 8
# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))
data = []
labels = []
# loop over the image paths
for imagePath in imagePaths:
# extract the class label from the filename
label = imagePath.split(os.path.sep)[-2]
# load the image, swap color channels, and resize it to be a fixed
# 224x224 pixels while ignoring aspect ratio
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image,(224, 224))
# update the data and labels lists, respectively
data.append(image)
labels.append(label)
# convert the data and labels to NumPy arrays while scaling the pixel
# intensities to the range [0, 255]
data = np.array(data) / 255.0
labels = np.array(labels)
# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.20, stratify=labels, random_state=42)
这是错误信息:
[INFO] loading images...
Traceback (most recent call last):
File "train_covid19.py", line 77, in <module>
test_size=0.20, stratify=labels, random_state=42)
File "C:\Users\KQ\anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 2143, in train_test_split
train, test = next(cv.split(X=arrays[0], y=stratify))
File "C:\Users\KQ\anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 1737, in split
y = check_array(y, ensure_2d=False, dtype=None)
File "C:\Users\KQ\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 574, in check_array
% (array.ndim, estimator_name))
ValueError: Found array with dim 3. Estimator expected <= 2.
【问题讨论】:
-
在错误之后出现的代码与问题无关(从未执行)并且应该不包含在此处,因为它只会造成不必要的混乱。此处发布的代码应该最少且仅与错误相关 - 请查看原因a wall of code isn't helpful
标签: machine-learning keras neural-network conv-neural-network