当我试图使用dataset的3个文件夹运行此代码时,出现了此问题。以前,这段代码在dataset文件夹中只运行了两个文件夹,一个叫做normal,另一个叫做covid。但在本例中,我添加了另一个名为pneumonia的分类器,使其成为一个3类图像分类器。我是机器学习方面的新手,所以我研究了很多关于如何解决这个问题的方法,但是每个解决方案都是不同的,而且代码也不一样。我试过了,但都没用,所以我才找你帮忙。
这个密码不属于我,是禤浩焯罗斯布罗克的代码,所有的功劳都归于他。这是关于在COVID或正常情况下对X射线图像进行分类,但是改进这个代码的想法增加了一个新的类别来对正常(非COVID)肺炎的图像进行分类。这就是为什么我在dataset中添加了一个新文件夹。希望你能帮我,谢谢!
# 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.发布于 2020-04-14 21:29:21
我认为你不需要LabelBinarizer和to_categorical。他们做同样的事,所以你只需要一个或另一个。尝试删除对to_categorical的调用。
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels) # Remove this line.您还需要更新模型中的类别数。
# Change the size from 2 to 3.
headModel = Dense(3, activation="softmax")(headModel)为了避免每次添加或删除类别时都需要更改,您可以计算唯一的标签。
n_labels = len(set(labels))
headModel = Dense(n_labels, activation="softmax")(headModel)更新
还请注意,to_categorical将只在整数标签上工作。这使得它更像OneHotEncoder而不是LabelBinarizer。
这是所谓的一切。
labels = [0, 1, 0, 2]
lb = LabelBinarizer()
binarized = lb.fit_transform(labels)
binarized
# array([[1, 0, 0],
# [0, 1, 0],
# [1, 0, 0],
# [0, 0, 1]])
to_categorical(labels)
# array([[1., 0., 0.],
# [0., 1., 0.],
# [1., 0., 0.],
# [0., 0., 1.]], dtype=float32)
to_categorical(binarized)
# array([[[0., 1.],
# [1., 0.],
# [1., 0.]],
#
# [[1., 0.],
# [0., 1.],
# [1., 0.]],
#
# [[0., 1.],
# [1., 0.],
# [1., 0.]],
#
# [[1., 0.],
# [1., 0.],
# [0., 1.]]], dtype=float32)注意标签的三维输出,因为它尝试对一个热编码数据的三个部分进行一个热编码,添加一个train_test_split不知道如何处理的额外维度。
这就是为什么你有一个ValueError: Found array with dim 3.
https://stackoverflow.com/questions/61216957
复制相似问题