Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to infer the output shape for Deconvolution2D layer #4

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion keras/backend/theano_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,19 @@ def deconv2d(x, kernel, output_shape, strides=(1, 1),
kernel = kernel.dimshuffle((1, 0, 2, 3))
th_border_mode = _preprocess_border_mode(border_mode)
np_kernel = kernel.eval()
filter_shape = _preprocess_filter_shape(dim_ordering, filter_shape)
filter_shape = _preprocess_filter_shape(dim_ordering, shape(kernel))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a kind of inconsistency here - we receive the filter_shape as a parameter, after that we reshape the kernel itself (shape should change) but leave filter_shape intact and pass it to the AbstractConv2d_gradInputs then.

Probably we could totally remove filter_shape from the parameters, if there are no other reasons to use it.


_t = ()
for v in output_shape:
try:
_v = v.eval()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I stuck probably the same issue that @yaringal mentioned - if we infer output_shape, it becomes the tuple of Theano variables/expressions, which Theano couldn't accept as an input due to some reasons.
So I try to evaluate them, but here I get another error:

MissingInputError: An input of the graph, used to compute Shape(input_25), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.

I'm not an expert in Theano, so have no idea how to fix it quick.

except AttributeError as e:
_v = v
print(e.__str__())
_t = _t + (v, )
output_shape = _t

print("imshp {} kshp {} subsample {} border_mode {} output_shape {}".format(output_shape, filter_shape, strides, th_border_mode, output_shape[2:]))

op = T.nnet.abstract_conv.AbstractConv2d_gradInputs(imshp=output_shape,
kshp=filter_shape,
Expand Down
19 changes: 13 additions & 6 deletions keras/layers/convolutional.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,11 @@ class Deconvolution2D(Convolution2D):
provide the keyword argument `input_shape`
(tuple of integers, does not include the sample axis),
e.g. `input_shape=(3, 128, 128)` for 128x128 RGB pictures.

# References
- [Deconvolutional Networks](http://www.matthewzeiler.com/pubs/cvpr2010/cvpr2010.pdf)
'''
def __init__(self, nb_filter, nb_row, nb_col, output_shape,
def __init__(self, nb_filter, nb_row, nb_col, output_shape=None,
init='glorot_uniform', activation='linear', weights=None,
border_mode='valid', subsample=(1, 1),
dim_ordering=K.image_dim_ordering(),
Expand All @@ -395,7 +398,7 @@ def __init__(self, nb_filter, nb_row, nb_col, output_shape,
bias=True, **kwargs):

if border_mode not in {'valid', 'same'}:
raise Exception('Invalid border mode for AtrousConv2D:', border_mode)
raise Exception('Invalid border mode for Deconvolution2D:', border_mode)

self.output_shape_ = output_shape

Expand All @@ -418,10 +421,12 @@ def get_output_shape_for(self, input_shape):
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)

rows = conv_input_length(rows, self.nb_row,
self.border_mode, self.subsample[0])
cols = conv_input_length(cols, self.nb_col,
self.border_mode, self.subsample[1])
if self.border_mode == 'same':
rows = rows * self.subsample[0]
cols = cols * self.subsample[1]
elif self.border_mode == 'valid':
rows = (rows - 1) * self.subsample[0] + self.nb_row
cols = (cols - 1) * self.subsample[1] + self.nb_col

if self.dim_ordering == 'th':
return (input_shape[0], self.nb_filter, rows, cols)
Expand All @@ -431,6 +436,8 @@ def get_output_shape_for(self, input_shape):
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)

def call(self, x, mask=None):
if not self.output_shape_:
self.output_shape_ = self.get_output_shape_for(K.shape(x))
output = K.deconv2d(x, self.W, self.output_shape_,
strides=self.subsample,
border_mode=self.border_mode,
Expand Down
1 change: 1 addition & 0 deletions tests/dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy
62 changes: 37 additions & 25 deletions tests/keras/layers/test_convolutional.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,48 @@ def test_deconvolution_2d():
stack_size = 3
nb_row = 10
nb_col = 6
nb_filter_rows = 3
nb_filter_cols = 3

for border_mode in ['valid', 'same']:
for subsample in [(1, 1), (2, 2)]:
if border_mode == 'same' and subsample != (1, 1):
continue
for infer_output_shape in [False, True]:
print("border_mode: {} subsample: {} infer output shape: {}".
format(border_mode, subsample, infer_output_shape))
if border_mode == 'same' and subsample != (1, 1):
continue

rows = conv_input_length(nb_row, 3, border_mode, subsample[0])
cols = conv_input_length(nb_col, 3, border_mode, subsample[1])
layer_test(convolutional.Deconvolution2D,
kwargs={'nb_filter': nb_filter,
'nb_row': 3,
'nb_col': 3,
'output_shape': (nb_samples, nb_filter, rows, cols),
'border_mode': border_mode,
'subsample': subsample},
input_shape=(nb_samples, stack_size, nb_row, nb_col),
fixed_batch_size=True)
# rows = conv_input_length(nb_row, 3, border_mode, subsample[0])
# cols = conv_input_length(nb_col, 3, border_mode, subsample[1])
if border_mode == 'same':
rows = nb_row * subsample[0]
cols = nb_col * subsample[1]
elif border_mode == 'valid':
rows = (nb_row - 1) * subsample[0] + nb_filter_rows
cols = (nb_col - 1) * subsample[1] + nb_filter_cols

layer_test(convolutional.Deconvolution2D,
kwargs={'nb_filter': nb_filter,
'nb_row': 3,
'nb_col': 3,
'output_shape': (nb_samples, nb_filter, rows, cols),
'border_mode': border_mode,
'W_regularizer': 'l2',
'b_regularizer': 'l2',
'activity_regularizer': 'activity_l2',
'subsample': subsample},
input_shape=(nb_samples, stack_size, nb_row, nb_col),
fixed_batch_size=True)
layer_test(convolutional.Deconvolution2D,
kwargs={'nb_filter': nb_filter,
'nb_row': nb_filter_rows,
'nb_col': nb_filter_cols,
'output_shape': None if infer_output_shape else (nb_samples, nb_filter, rows, cols),
'border_mode': border_mode,
'subsample': subsample},
input_shape=(nb_samples, stack_size, nb_row, nb_col),
fixed_batch_size=True)

layer_test(convolutional.Deconvolution2D,
kwargs={'nb_filter': nb_filter,
'nb_row': nb_filter_rows,
'nb_col': nb_filter_cols,
'output_shape': None if infer_output_shape else (nb_samples, nb_filter, rows, cols),
'border_mode': border_mode,
'W_regularizer': 'l2',
'b_regularizer': 'l2',
'activity_regularizer': 'activity_l2',
'subsample': subsample},
input_shape=(nb_samples, stack_size, nb_row, nb_col),
fixed_batch_size=True)


@keras_test
Expand Down