嗨,我有这段代码,在检查输入时似乎给了我一个错误:预期conv2d_24_input具有形状(28,28,1)但得到了形状为(1,28,28)的数组。您能否建议我是什么导致了此错误。
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils
import theano
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#batch size to train
batch_size = 50
# number of epochs to train
nb_epoch = 20
#number of output classes
nb_classes = 10
#number of epochs to train
nb_train = 20
#input image dimension
img_rows,img_cols = 28,28
#number of convolution filters
nb_filters = 32
#size of pooling area fro max pool
nb_pool = 2
#convolution kernel size
nb_conv = 3
#data shuffeled and split between train and test data
(X_train,y_train),(X_test,y_test)= mnist.load_data()
#reshape the data
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
i = 100
plt.imshow(X_train[i, 0], interpolation='nearest')
print("label : ", Y_train[i,:])
model = Sequential()
model.add(Convolution2D(nb_filters, (nb_conv, nb_conv),
border_mode='valid',
input_shape=(img_rows, img_cols,1)))
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(nb_filters, (nb_conv, nb_conv)))
convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
#%%
hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_split=0.2)
#%%
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print(model.predict_classes(X_test[1:5]))
print(Y_test[1:5])
def plot_filters(layer, x,y):
filters = layer.W.get_value()
fig = plt.figure()
for j in range(len(filters)):
ax = fig.add_subplot(y,x,j+1)
ax.matshow(filters[j][0],cmap =matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout
return plt
#plot the first convolution layer filters
plot_filters(model.layers[0],8,4)
#Visualising intermediate layers
output_layer = model.layers[1].get_output()
output_fn = theano.function([model.layers[0].get_input()], output_layer)
#the input imageinput_image=X_train[0:1,:,:,:]
input_image=X_train[0:1,:,:,:]
print(input_image.shape)
# Rearrange dimension so we can plot the result
output_image = np.rollaxis(np.rollaxis(output_image, 3, 1), 3, 1)
print(output_image.shape)
fig = plt.figure(figsize=(16,8))
for i in range(32):
plt.subplot(1,10,i+1)
plt.imshow(im[0,:,:,i],interpolation ='nearest') #to see the first filter
plt.axis('off')
我怀疑此错误是在重新排列尺寸时引起的。我该如何解决。