简体   繁体   English

有人可以帮我解决这个输入错误吗?

[英]Can someone help me solve this input error?

I am training a 3D CNN for image classification, however I am getting the following error I have the tensorflow as the backend.我正在训练 3D CNN 进行图像分类,但是我收到以下错误,我将 tensorflow 作为后端。 I keep getting this error when it runs model.fit().当它运行 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.我查看了网上发布的大部分相关问题,但它们都集中在后端是theaon还是tensorflow。 Some of them suggests expand dimensions, but still doesn't work and some other problems showed up.其中一些建议扩展尺寸,但仍然不起作用,并且出现了其他一些问题。

My model我的 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) .看起来 OP 正在使用 3D 卷,其中每个卷的形状为(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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM