【问题标题】:ValueError: Filter must not be larger than the inputValueError:过滤器不能大于输入
【发布时间】:2016-09-30 07:08:19
【问题描述】:

我对机器学习很陌生,所以我在玩一些例子等。 代码中指定的图片尺寸为(28,28) 但由于某种原因,我不断收到相同的 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')

【问题讨论】:

  • 能否添加错误的回溯?
  • 我想问你使用的是 tensorflow 还是 theano。我在罐头示例中遇到了类似的错误,我通过在几个地方将 input_shape=(1,img_rows, img_cols) 更改为 input_shape=(img_rows, img_cols,1) 来修复它。

标签: python pandas machine-learning keras


【解决方案1】:

所以问题在于使用的卷积大小。卷积操作通常减少图像的维度。同样 - 每个池化操作都会减小大小。你有非常小的图像,但应用了为更大的图像设计的模型架构,因此在某个时候,在一个卷积/池化之后,你实际上有一个比后面的过滤器尺寸更小的输出图像,这是一个定义不明确的图像手术。

为了暂时解决问题 - 移除第二个卷积层和最大池化层,因为这些操作(带有提供的参数)无法在如此小的数据上执行。一般来说,您应该首先了解卷积是如何工作的,而不是应用其他人的模型,因为参数对于良好的性能至关重要 - 如果您应用的转换会大大降低分辨率 - 您将无法学习任何东西。因此,一旦您对卷积的工作原理有了一些直觉,您就可以返回并尝试不同的架构,但是没有一个“神奇”的方程式来计算架构,因此我无法为您提供“正常工作”的参数 - 从移除这些额外的卷积和池化,而不是在您对数据和模型有了更好的理解后返回并尝试其他可能性。

【讨论】:

  • 好的,所以我在去掉maxpooling和卷积层后尝试了这个程序,当我去掉maxpooling时程序成功执行了! maxpooling 到底是做什么的?
  • 它在图像中移动一个窗口并从窗口中获取最大值。通过为图像处理中的小平移提供一些不变性,它有效地降低了图像的分辨率
  • 但问题是第二次转换。它极大地降低了分辨率,以至于您的图像现在小于 5x5 像素。这就是为什么 5x5 窗口的最大池化失败的原因。换句话说,只是删除池而不是掩盖问题而不是解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多