RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 7, 7], but got 5-dimensional input of size [300, 1, 512, 512, 3] instead

Viewed 64

I am trying to do something similar to that shown in the following MONAI tutorial: https://github.com/Project-MONAI/tutorials/blob/master/2d_classification/mednist_tutorial.ipynb but keep getting the following error:

Expected 4-dimensional input for 4-dimensional weight [64, 1, 7, 7], but got 5-dimensional input of size [300, 1, 512, 512, 3] instead.

I followed everything the same way as done in the tutorial using my data which I accessed in the following way:

label_data = pd.read_csv('./dataset/label.csv')
label_data.set_index('file_name')

classes = ['no_tumor', 'meningioma_tumor', 'glioma_tumor', 'pituitary_tumor']
img_data = []
for index, info in label_data.iterrows():
    try:
        ArrImg = './dataset/image/{}'.format(info['file_name'])
        img_data.append([ArrImg, classes.index(info['label'])])
    except e:
        print(e)
X = []
y = []
for feature, label in img_data:
  X.append(feature)
  y.append(label)
    
X_1, X_test, y_1, y_test = train_test_split( X, y, test_size=0.15, random_state=42)
X_train, X_val, y_train, y_val = train_test_split( X_1, y_1, test_size=0.1764, random_state=42)

This is the only change made with respect to the tutorial; however, I get the error shown above. The images used are 512x512 greyscale images. Any help to resolve this issue would be appreciated.

1 Answers

That because your data already has the channel dim but you called AddChannel transform to add it. Please try EnsureChannelFirstd or AsChannelFirstd instead.

Thanks.

Related