[英]ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1)
I am new in machine learning and python and I try to make classification whether a patient cancer or not. 我是机器学习和python的新手,无论是否患癌症,我都会尝试进行分类。 I found a piece of code from https://github.com/fahomid/ML-Tensorflow-Medical-Image/blob/master/tensorflow-model.py I have a small dataset.
我从https://github.com/fahomid/ML-Tensorflow-Medical-Image/blob/master/tensorflow-model.py找到了一段代码。我有一个小的数据集。 Training and test set have two patient directories that have dicom files just for trying.
训练和测试集有两个患者目录,其中包含dicom文件供尝试使用。 The code is as following;
代码如下:
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()
The line of error message is as following; 错误消息行如下:
model.fit(dataset, labels, epochs=10, shuffle=True, batch_size=32)
model.fit(数据集,标签,时期= 10,随机播放= True,batch_size = 32)
ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1)
ValueError:检查输入时出错:预期density_11_input具有3维,但数组的形状为(0,1)
Thanks for your help. 谢谢你的帮助。
The following section of your code looks for files (with .dcm
extension) containing the data for Cancer and Normal person. 代码的以下部分查找包含癌症和正常人数据的文件(扩展名为
.dcm
)。 It does NOT find any, So it returns nothing. 它没有找到任何,所以它没有返回。
# 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)
So the value of dataset
variable is 0 , and labels
variable is 1 . 因此,
dataset
变量的值为0 , labels
变量为1 。 And when the model.fit
method is called, it expects the input to be 3 dimensions with the shape of (512, 512)
, but it only gets the input with the shape of (0, 1)
. 并且当调用
model.fit
方法时,它期望输入为(512, 512)
形状的3维,但仅获得形状为(0, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.