0

我正在训练 3D CNN 进行图像分类,但是我收到以下错误,我将 tensorflow 作为后端。运行 model.fit() 时,我不断收到此错误。

我查看了网上发布的大部分相关问题,但它们都集中在后端是 theaon 还是 tensorflow。其中一些建议扩展尺寸,但仍然不起作用,并且出现了其他一些问题。

我的模型

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)

我得到的错误:


 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)
4

1 回答 1

0

基于这些线,

X_train = X_train.reshape(-1,27,28,28)
X_test = X_train.reshape(-1,27,28,28)

看起来 OP 正在使用 3D 体积,其中每个体积都有形状(27, 28, 28)。它似乎缺少通道轴。解决方案是为单通道添加一个新维度。

X_train = X_train[..., np.newaxis]
X_test = X_test[..., np.newaxis]
于 2020-12-02T19:53:46.723 回答