0
votes

I am new in machine learning and python and I try to make classification whether a patient cancer or not. 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. Training and test set have two patient directories that have dicom files just for trying. 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)

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

Thanks for your help.

1

1 Answers

0
votes

The following section of your code looks for files (with.dcm extension) containing the data for Cancer and Normal person. 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. 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).