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

Added the mode "bilinear" in the upscaling2D layer. #10994

Merged
merged 15 commits into from
Aug 30, 2018
Merged

Added the mode "bilinear" in the upscaling2D layer. #10994

merged 15 commits into from
Aug 30, 2018

Conversation

gabrieldemarmiesse
Copy link
Contributor

@gabrieldemarmiesse gabrieldemarmiesse commented Aug 25, 2018

Summary

Thanks @ahundt for your work. I was able to get Theano working and be compatible with what was doing tensorflow, but only for (2, 2) upscaling. Other factors like (4, 4) show different results for the theano's implementation and the tensorflow's implementation.
I also couldn't figure out how to use other factors like (3, 2) with theano. Which is why, with the Theano backend, for implementation and backend compatibility reasons, only (2, 2) is implemented.

Related Issues

See @ahundt 's PR: #9303

PR Overview

  • This PR requires new unit tests [y/n] (make sure tests are included)
  • This PR requires to update the documentation [y/n] (make sure the docs are up-to-date)
  • This PR is backwards compatible [y/n]
  • This PR changes the current API [y/n] (all API changes need to be approved by fchollet)

Copy link
Contributor

@ahundt ahundt left a comment

Choose a reason for hiding this comment

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

One small thing, otherwise looks good

else:
raise ValueError('CNTK Backend: Invalid data_format:', data_format)
raise NotImplementedError
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the existing behavior better here?

raise ValueError('CNTK Backend: Invalid data_format:', data_format)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already have this message when giving as an input a wrong data_format. But it's true that the NotImplementedError is not clear at all. Let me change that.

fchollet
fchollet previously approved these changes Aug 26, 2018
Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@@ -862,6 +862,45 @@ def test_upsampling_2d():
assert_allclose(np_output, expected_out)


@pytest.mark.skipif((K.backend() == 'cntk'),
reason="cntk does not support it yet")
Copy link
Member

Choose a reason for hiding this comment

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

Use ' for string delimitation, for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, my bad.

@staticmethod
def _helper_bilinear(height_factor, width_factor):
x_shape = (2, 3, 4, 5)
for data_format in ['channels_first', 'channels_last']:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to make data_format as argument of _helper_bilinear. Then parameterize test_resize_images_bilinear with data_format. Otherwise, you may not be able to test both data_format with pytest.raises.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! I'll change that.

@@ -19,6 +19,7 @@
except ImportError:
from theano.sandbox.softsign import softsign as T_softsign

import warnings
Copy link
Contributor

Choose a reason for hiding this comment

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

Not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. Let me remove that.

@gabrieldemarmiesse
Copy link
Contributor Author

It seems that tests/test_multiprocessing.py::test_multiprocessing_predict_error is failing. I don't think it's related to this PR.

@@ -862,6 +862,45 @@ def test_upsampling_2d():
assert_allclose(np_output, expected_out)


@pytest.mark.skipif((K.backend() == 'cntk'),
Copy link
Member

Choose a reason for hiding this comment

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

Please make this test parameterized (see #10975)

@fchollet fchollet merged commit cc3521e into keras-team:master Aug 30, 2018
jlherren added a commit to jlherren/keras that referenced this pull request Sep 3, 2018
* keras/master: (327 commits)
  Added in_train_phase and in_test_phase in the numpy backend. (keras-team#11061)
  Make sure the data_format argument defaults to ‘chanels_last’ for all 1D sequence layers.
  Speed up backend tests (keras-team#11051)
  Skipped some duplicated tests. (keras-team#11049)
  Used decorators and WITH_NP to avoid tests duplication. (keras-team#11050)
  Cached the theano compilation directory. (keras-team#11048)
  Removing duplicated backend tests. (keras-team#11037)
  [P, RELNOTES] Conv2DTranspose supports dilation (keras-team#11029)
  Doc Change: Change in shape for CIFAR Datasets (keras-team#11043)
  Fix line too long in mnist_acgan (keras-team#11040)
  Enable using last incomplete minibatch (keras-team#8344)
  Better UX (keras-team#11039)
  Update lstm text generation example (keras-team#11038)
  fix a bug, load_weights doesn't return anything (keras-team#11031)
  Speeding up the tests by reducing the number of K.eval(). (keras-team#11036)
  [P] Expose monitor value getter for easier subclass (keras-team#11002)
  [RELNOTES] Added the mode "bilinear" in the upscaling2D layer. (keras-team#10994)
  Separate pooling test from convolutional test and parameterize test case (keras-team#10975)
  Fix issue with non-canonical TF version name format.
  Allow TB callback to display float values.
  ...
@gabrieldemarmiesse gabrieldemarmiesse deleted the upsampling_bilinear branch September 18, 2018 06:34
@ahundt
Copy link
Contributor

ahundt commented Sep 27, 2018

cool! thanks for pushing this through @gabrieldemarmiesse!

@gabrieldemarmiesse
Copy link
Contributor Author

You're welcome!

@bonlime
Copy link
Contributor

bonlime commented Oct 16, 2018

@gabrieldemarmiesse
tf.image.resize_image has an align_corners parameter docs, which is impossble to pass using keras upsampling layer. But Theano doesn't have it. Is it possible to add support for this parameter for TF users?

@SummitKwan
Copy link

Hi thank you for the hard work! I have a question about the tensorflow implementation of resize_images.

The current implementation in keras uses tf.image.resize_images, which suffers from a list of problems depending on the version:

  1. the bilinear interpolation have alignment problems: https://hackernoon.com/how-tensorflows-tf-image-resize-stole-60-days-of-my-life-aba5eb093f35
  2. the tf.image.resize_images (for nearest neighbor) is not as efficient as tf.tile + tf.reshape, as is tested in Suggestion for efficient upsample+conv2d and conv2d+pool tensorflow/tensorflow#18711 (comment)
  3. For 'channel_first' option in K.resize_images, the tensor has to go through 2 extra layers of transpose, it is going to slow down the operation?
  4. It seems to me that tf.images.resize_images are designed for image pre-processing, not for intermediate layers (in early days, this op only runs on CPU, not GPU) are too generic for the simple goal of scaling the tensor by integer times, and tf.tile+tf.reshape sounds more reasonable for nearest, and tf.tile+tf.reshape+tf.conv2d (with pre-defined kernel) seems more reasonable for bilinear option.

In order not to break the previous code, would that make sense to add an option like 'nearest_by_tile', which uses the K.repeat_elements as in the CNTK back-end? Than you for your consideration and please correct me if I mis-understood things here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants