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

Allow (n, 0) croppings on Cropping2d and 3d #4941

Merged
merged 2 commits into from Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions keras/layers/convolutional.py
Expand Up @@ -1740,11 +1740,41 @@ def get_output_shape_for(self, input_shape):

def call(self, x, mask=None):
if self.dim_ordering == 'th':
if self.cropping[0][1] == self.cropping[1][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:]
elif self.cropping[0][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1]]
elif self.cropping[1][1] == 0:
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:]
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1]]
elif self.dim_ordering == 'tf':
if self.cropping[0][1] == self.cropping[1][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:,
:]
elif self.cropping[0][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
:]
elif self.cropping[1][1] == 0:
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
:]
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
Expand Down Expand Up @@ -1827,12 +1857,96 @@ def get_output_shape_for(self, input_shape):

def call(self, x, mask=None):
if self.dim_ordering == 'th':
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:]
elif self.cropping[0][1] == 0:
return x[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[1][1] == 0:
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[2][1] == 0:
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:]
return x[:,
:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1]]
elif self.dim_ordering == 'tf':
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1],
:]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == 0:
return x[:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1],
:]
elif self.cropping[1][1] == 0:
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1],
:]
elif self.cropping[2][1] == 0:
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:,
:]
return x[:,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
Expand Down
18 changes: 18 additions & 0 deletions tests/keras/layers/test_convolutional.py
Expand Up @@ -666,6 +666,15 @@ def test_cropping_2d():
cropping[1][0]: -cropping[1][1],
:]
assert_allclose(np_output, expected_out)
# another correctness test (no cropping)
cropping = ((0, 0), (0, 0))
layer = convolutional.Cropping2D(cropping=cropping,
dim_ordering=dim_ordering)
layer.build(input.shape)
output = layer(K.variable(input))
np_output = K.eval(output)
# compare with input
assert_allclose(np_output, input)


def test_cropping_3d():
Expand Down Expand Up @@ -709,6 +718,15 @@ def test_cropping_3d():
cropping[2][0]: -cropping[2][1],
:]
assert_allclose(np_output, expected_out)
# another correctness test (no cropping)
cropping = ((0, 0), (0, 0), (0, 0))
layer = convolutional.Cropping3D(cropping=cropping,
dim_ordering=dim_ordering)
layer.build(input.shape)
output = layer(K.variable(input))
np_output = K.eval(output)
# compare with input
assert_allclose(np_output, input)

if __name__ == '__main__':
pytest.main([__file__])