diff --git a/examples/mnist_cnn.py b/examples/mnist_cnn.py index 92ea0bc6f..d7a4d9b30 100644 --- a/examples/mnist_cnn.py +++ b/examples/mnist_cnn.py @@ -51,7 +51,7 @@ Y_test = np_utils.to_categorical(y_test, nb_classes) model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, - border_mode='same', + border_mode='valid', input_shape=(1, img_rows, img_cols))) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) diff --git a/keras/backend/tensorflow_backend.py b/keras/backend/tensorflow_backend.py index 12271e669..74ff8a3d5 100644 --- a/keras/backend/tensorflow_backend.py +++ b/keras/backend/tensorflow_backend.py @@ -502,7 +502,8 @@ def dropout(x, level, seed=None): # CONVOLUTIONS -def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th'): +def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th', + image_shape=None, filter_shape=None): ''' Run on cuDNN if available. border_mode: string, "same" or "valid". diff --git a/keras/backend/theano_backend.py b/keras/backend/theano_backend.py index 3d34262bb..e8418e7ce 100644 --- a/keras/backend/theano_backend.py +++ b/keras/backend/theano_backend.py @@ -515,7 +515,8 @@ def dropout(x, level, seed=None): # CONVOLUTIONS -def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th'): +def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th', + image_shape=None, filter_shape=None): ''' Run on cuDNN if available. border_mode: string, "same" or "valid". @@ -532,6 +533,12 @@ def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th'): # TF kernel shape: (rows, cols, input_depth, depth) x = x.dimshuffle((0, 3, 1, 2)) kernel = kernel.dimshuffle((3, 2, 0, 1)) + if image_shape: + image_shape = (image_shape[0], image_shape[3], + image_shape[1], image_shape[2]) + if filter_shape: + filter_shape = (filter_shape[3], filter_shape[2], + filter_shape[0], filter_shape[1]) if _on_gpu() and dnn.dnn_available(): if border_mode == 'same': @@ -558,7 +565,9 @@ def conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th'): conv_out = T.nnet.conv.conv2d(x, kernel, border_mode=th_border_mode, - subsample=strides) + subsample=strides, + image_shape=image_shape, + filter_shape=filter_shape) if border_mode == 'same': shift_x = (kernel.shape[2] - 1) // 2 shift_y = (kernel.shape[3] - 1) // 2 diff --git a/keras/layers/convolutional.py b/keras/layers/convolutional.py index e1d49093c..53a28a5bc 100644 --- a/keras/layers/convolutional.py +++ b/keras/layers/convolutional.py @@ -93,7 +93,8 @@ class Convolution1D(Layer): X = K.expand_dims(X, -1) # add a dimension of the right X = K.permute_dimensions(X, (0, 2, 1, 3)) conv_out = K.conv2d(X, self.W, strides=self.subsample, - border_mode=self.border_mode, dim_ordering='th') + border_mode=self.border_mode, + dim_ordering='th') output = conv_out + K.reshape(self.b, (1, self.nb_filter, 1, 1)) output = self.activation(output) @@ -212,7 +213,9 @@ class Convolution2D(Layer): X = self.get_input(train) conv_out = K.conv2d(X, self.W, strides=self.subsample, border_mode=self.border_mode, - dim_ordering=self.dim_ordering) + dim_ordering=self.dim_ordering, + image_shape=self.input_shape, + filter_shape=self.W_shape) output = conv_out + K.reshape(self.b, (1, self.nb_filter, 1, 1)) output = self.activation(output)