I am training a 3D CNN for image classification, however I am getting the following error I have the tensorflow as the backend. I keep getting this error when it runs model.fit().
I checked out most of the related problems posted online, but they all kind of focus on whether it's theaon or tensorflow as the backend. Some of them suggests expand dimensions, but still doesn't work and some other problems showed up.
My model
from keras.models import Sequential, Model
from keras.losses import categorical_crossentropy
def get_model_compiled(shapeinput, num_class):
clf = Sequential()
clf.add(Conv3D(32, kernel_size=(3, 3, 1), input_shape=shapeinput))
clf.add(BatchNormalization())
clf.add(Activation('relu'))
clf.add(Conv3D(64, (5, 5, 16)))
clf.add(BatchNormalization())
clf.add(Activation('relu'))
clf.add(MaxPooling3D(pool_size=(2, 2, 2)))
clf.add(GlobalAveragePooling3D())
clf.add(Dense(64, kernel_regularizer=regularizers.l2(0)))
clf.add(Dense(num_class, activation='softmax'))
clf.compile(loss=categorical_crossentropy, optimizer=Adam(lr=0.001), metrics=['accuracy'])
return clf
import argparse
import numpy as np
import sys
import pickle
from sklearn.metrics import accuracy_score
sys.path.insert(0, "lib")
import h5py
f=h5py.File('IP28-28-27.h5','r')
train_images=f['data'][:]
train_labels=f['label'][:]
f.close()
train_labels = np.argmax(train_labels,1)
indices = np.arange(train_images.shape[0])
shuffled_indices = np.random.permutation(indices)
images = train_images[shuffled_indices]
labels = train_labels[shuffled_indices]
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.8,
random_state=345)
n_classes = labels.max() + 1
i_labeled = []
for c in range(n_classes):
i = indices[labels==c][:5]##change sample number
i_labeled += list(i)
X_train = images[i_labeled]
X_train = X_train.reshape(-1,27,28,28)
y_train = labels[i_labeled]
X_test = images[i_labeled]
X_test = X_train.reshape(-1,27,28,28)
y_test = labels[i_labeled]
filepath = "best-model_ip.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
import time
import datetime
import collections
inputshape = X_train.shape
clf = get_model_compiled(inputshape, num_class=16)
history = clf.fit(x=X_train, y=y_train, batch_size=32, epochs=50, callbacks=callbacks_list)
The error I am getting:
ValueError Traceback (most recent call last)
<ipython-input-36-a7e7b3215008> in <module>
59 inputshape = X_train.shape
60 clf = get_model_compiled(inputshape, num_class=16)
61 history = clf.fit(x=X_train, y=y_train, batch_size=32, epochs=50, callbacks=callbacks_list)
62 toc1 = time.clock()
63 print(' Training Time: ', toc1 - tic1)
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size,
epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight,
sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self,
x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
751 exception_prefix='input')
752
753 if y is not None:
~/anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in
standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
126 ': expected ' + names[i] + ' to have ' +
127 str(len(shape)) + ' dimensions, but got array
128 'with shape ' + str(data_shape))
129 if not check_batch_axis:
130 data_shape = data_shape[1:]
ValueError: Error when checking input: expected conv3d_15_input to have 5 dimensions, but got
array with shape (80, 27, 28, 28)
Base on these lines,
X_train = X_train.reshape(-1,27,28,28)
X_test = X_train.reshape(-1,27,28,28)
it looks like OP is using 3D volumes, where each volume has the shape (27, 28, 28)
. It seems to be missing the channel axis. The solution is to add a new dimension for the single channel.
X_train = X_train[..., np.newaxis]
X_test = X_test[..., np.newaxis]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.