ValueError异常:过滤器必须不大于输入
大我非常新的机器学习,所以我用的例子和这样的玩弄。 代码中指定的图像尺寸为(28,28) 但由于某些原因我不断收到同样的ValueError异常 我无法弄清楚为什么发生这种情况。ValueError异常:过滤器必须不大于输入
下面的代码:
import pandas as pd
import numpy as np
np.random.seed(1337) # for reproducibility
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
# input image dimensions
img_rows, img_cols = 28, 28
batch_size = 128 # Number of images used in each optimization step
nb_classes = 10 # One class per digit
nb_epoch = 35 # Number of times the whole data is used to learn
# Read the train and test datasets
train = pd.read_csv("../input/train.csv").values
test = pd.read_csv("../input/test.csv").values
# Reshape the data to be used by a Theano CNN. Shape is
# (nb_of_samples, nb_of_color_channels, img_width, img_heigh)
X_train = train[:, 1:].reshape(train.shape[0], 1, img_rows, img_cols)
X_test = test.reshape(test.shape[0], 1, img_rows, img_cols)
y_train = train[:, 0] # First data is label (already removed from X_train)
# Make the value floats in [0;1] instead of int in [0;255]
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 (ie one-hot vectors)
Y_train = np_utils.to_categorical(y_train, nb_classes)
#Display the shapes to check if everything's ok
print('X_train shape:', X_train.shape)
print('Y_train shape:', Y_train.shape)
print('X_test shape:', X_test.shape)
model = Sequential()
# For an explanation on conv layers see http://cs231n.github.io/convolutional-networks/#conv
# By default the stride/subsample is 1
# border_mode "valid" means no zero-padding.
# If you want zero-padding add a ZeroPadding layer or, if stride is 1 use border_mode="same"
model.add(Convolution2D(12, 5, 5, border_mode='valid',input_shape=(1,img_rows, img_cols)))
model.add(Activation('relu'))
# For an explanation on pooling layers see http://cs231n.github.io/convolutional-networks/#pool
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.15))
model.add(Convolution2D(24, 5, 5))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.15))
# Flatten the 3D output to 1D tensor for a fully connected layer to accept the input
model.add(Flatten())
model.add(Dense(180))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes)) #Last layer with one output per class
model.add(Activation('softmax')) #We want a score simlar to a probability for each class
# The function to optimize is the cross entropy between the true label and the output (softmax) of the model
# We will use adadelta to do the gradient descent see http://cs231n.github.io/neural-networks-3/#ada
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=["accuracy"])
# Make the model learn
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)
# Predict the label for X_test
yPred = model.predict_classes(X_test)
# Save prediction in file for Kaggle submission
np.savetxt('mnist-pred.csv', np.c_[range(1,len(yPred)+1),yPred], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d')
所以,问题是所使用的卷积大小。卷积操作通常是减少图像的尺寸。同样 - 每个池操作都会减小大小。你有非常小的图像,但应用了模型架构,它被设计为一个更大的模型,因此在某一点上,在一个卷积/汇集之后,实际上有一个较小的输出图像比一个后续的滤波器大小,这是一个不明确的操作。
为了即暂时解决这个问题 - 除去第二卷积和maxpooling层,因为这些操作(与设置参数)不能在这样小的数据进行的。一般来说,首先应该了解卷积是如何工作的,而不应用别人的模型,因为这些参数对于提高性能至关重要 - 如果应用可将分辨率降低到很低的转换,您将无法学习任何东西。因此,一旦你有直觉知道卷积如何工作,你可以回去尝试不同的架构,但是没有人能够找出这个架构的“神奇”方程,因此我无法为你提供“正常工作”的参数 - 从删除这个额外的卷积和池,并且一旦你更好地理解你的数据和模型,就回去尝试其他的可能性。
好吧,所以我在删除maxpooling和convolution层后都试过了程序,当我删除maxpooling时程序成功执行! maxpooling究竟做了什么? – user3430238
它通过图像移动窗口并从窗口获取最大值。有效地通过给图像处理中的小翻译提供一些不变性来降低图像的分辨率 – lejlot
但是,第二个转化是问题。它极大地降低了分辨率,使图像现在小于5x5像素。这就是为什么max pooling 5x5窗口失败。换句话说,除去解决问题并不是解决问题 – lejlot
你可以添加错误的追溯? – hashcode55
我会问你是否使用tensorflow或theano。我有一个罐头例的类似的错误,我通过改变input_shape =(1,img_rows,img_cols)固定在它几个地方到input_shape =(img_rows,img_cols,1)。 – user1269942