ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1)

ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1)

我是机器学习的新手,python 我尝试对患者是否患有癌症进行分类。我从 https://github.com/fahomid/ML-Tensorflow-Medical-Image/blob/master/tensorflow-model.py 中找到了一段代码 我有一个小数据集。训练集和测试集有两个患者目录,其中包含仅用于尝试的 dicom 文件。代码如下;

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
import pydicom
import numpy as np
import PIL

# Generate data from dicom file
dataset = [];
labels = [];
for root, dirs, files in os.walk("training_data/Cancer"):
    for file in files:
        if file.endswith(".dcm"):
            ds = pydicom.dcmread(os.path.join(root, file))
            dataset.append(ds.pixel_array)
            labels.append(1);

for root, dirs, files in os.walk("training_data/Normal"):
   for file in files:
        if file.endswith(".dcm"):
            ds = pydicom.dcmread(os.path.join(root, file))
            dataset.append(ds.pixel_array)
            labels.append(0)


dataset_size = len(dataset)
dataset = np.asarray(dataset)
labels = np.asarray(labels)

# create model
model = Sequential()
model.add(Dense(32, activation='tanh', input_shape=(512, 512)))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics= 
['accuracy'])
model.fit(dataset, labels, epochs=10, shuffle=True, batch_size=32)

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("\n\nModel saved to disk\n\n")
model.summary()

报错行如下;

model.fit(dataset, labels, epochs=10, shuffle=True, batch_size=32)

ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1)

感谢您的帮助。

代码的以下部分查找包含癌症正常数据的文件(扩展名为.dcm)人。它没有找到任何东西,所以它returns什么都没有。

# Generate data from dicom file
dataset = [];
labels = [];
for root, dirs, files in os.walk("training_data/Cancer"):
    for file in files:
        if file.endswith(".dcm"):
            ds = pydicom.dcmread(os.path.join(root, file))
            dataset.append(ds.pixel_array)
            labels.append(1);

for root, dirs, files in os.walk("training_data/Normal"):
   for file in files:
        if file.endswith(".dcm"):
            ds = pydicom.dcmread(os.path.join(root, file))
            dataset.append(ds.pixel_array)
            labels.append(0)

所以dataset变量的值为0labels变量的值为1。当调用 model.fit 方法时,它期望输入是 3 维的,形状为 (512, 512),但它只得到形状为 (0, 1).[=17= 的输入]