2016-09-30 8 views
-1

機械学習にはかなり新しいので、例などで遊んでいます。 コードに指定されている画像サイズは(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') 
+0

エラーのトレースバックを追加できますか? – hashcode55

+0

テンソルフローやテアノを使っているかどうか聞いてみましょう。私は缶詰めされた例と同様のエラーがあり、input_shape =(1、img_rows、img_cols)をいくつかの場所でinput_shape =(img_rows、img_cols、1)に変更して修正しました。 – user1269942

答えて

1

だから、問題は、使用畳み込みサイズです。畳み込み演算は通常、画像のを縮小します。同様に、各プーリング操作によってサイズが小さくなります。あなたは非常に小さい画像を持っていますが、より大きなもののために設計されたモデルアーキテクチャを適用しています。したがって、ある時点で、コンボリューション/プールの後に次のフィルタサイズよりも小さな出力画像があります。操作。

問題を一時的に修正する - このような小さなデータに対しては、これらの操作(パラメータを指定して)を実行できないため、2番目の畳み込みレイヤーとmaxpoolingレイヤーを削除します。一般的に、畳み込みの仕組みを理解し、elsesモデルを適用しないでください。なぜなら、パラメータが優れたパフォーマンスにとって重要であるからです。解像度を大幅に下げる変換を適用すると、何も学ぶことができなくなります。したがって、畳み込みの仕組みを理解したら、別のアーキテクチャを試してみることもできますが、アーキテクチャを把握する "魔法の"方程式はないので、 "うまくいく"というパラメータを提供することはできません。この追加の畳み込みとプールを削除し、データとモデルをよりよく理解したらもう一度やり直してみてください。

+0

これで、maxpoolingとconvolutionの両方のレイヤーを削除してからプログラムを試しました。プログラムはmaxpoolingを削除したときに正常に実行されました! maxpoolingは正確に何をしますか? – user3430238

+1

ウィンドウを画像内に移動し、ウィンドウから最大値をとります。効率的には、画像処理で小さな翻訳に不変性を与えることによって画像の解像度を下げる – lejlot

+1

しかし、事は第2のconvが問題です。解像度が極端に低下し、画像が5×5ピクセルよりも小さくなりました。これは最大5×5ウィンドウのプールに失敗した理由です。言い換えれば、プーリングだけを取り除くことは、それを解決するよりむしろ問題を隠すことになります – lejlot

関連する問題