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

Created a function to_data_format to abstract the shape and data_format handling. #10781

Merged
merged 4 commits into from
Aug 5, 2018
Merged

Created a function to_data_format to abstract the shape and data_format handling. #10781

merged 4 commits into from
Aug 5, 2018

Conversation

gabrieldemarmiesse
Copy link
Contributor

Summary

The keras codebase has this pattern which occurs quite frequently:

if data_format == 'channels_first':
     shape = ....
else:
     shape = ....

I believe some refactoring could be done, and it could lead to more readable and less error-prone code.
I made a function for it, I didn't use it in all the places that I could though. I wanted to keep this PR small. I will replace more code in the future if this PR is merged.

Related Issues

PR Overview

  • Made a new function to_data_format.
  • Removed it from the docs.
  • Used it at a few places to show how it would work.

More refactoring using this function will come in future PRs.

  • 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)

@taehoonlee
Copy link
Contributor

Good idea, @gabrieldemarmiesse. However, a data_format is orthogonal to backends. How about moving to_data_format from keras/backend/common.py to keras/utils/generic_utils.py?

@gabrieldemarmiesse
Copy link
Contributor Author

Sure thing.

@@ -532,3 +532,51 @@ def slice_arrays(arrays, start=None, stop=None):
return arrays[start:stop]
else:
return [None]


def to_data_format(values, data_format, skip=1):
Copy link
Collaborator

@fchollet fchollet Jul 31, 2018

Choose a reason for hiding this comment

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

May I suggest the following signature:

pattern = [batch_size, steps, height, width, dimensions]
transposed_shape = transpose_shape(shape, target_format=data_format, spatial_axes=(2, 3))

I think that would be more readable and explicit than "to_data_format" and "skip".

Also, it would be more readable to explicitly pass keyword arguments when calling the function, e.g.

transpose_shape(shape, target_format=data_format, spatial_axes=(2, 3))
rather than
transpose_shape(shape, data_format, (2, 3))

@gabrieldemarmiesse
Copy link
Contributor Author

gabrieldemarmiesse commented Aug 1, 2018

I changed the signature to:

transpose_shape(shape, data_format, spatial_axes=(2, 3))

I don't think target_format needs to be passed explicitly since the name of the variable passed is always data_format or something really similar. I think it's already understandable that data_format is the target format.

If you still think that this should be called with target_format=... just tell me and I'll change it.

Copy link
Collaborator

@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!

@fchollet fchollet merged commit 1068e17 into keras-team:master Aug 5, 2018
@gabrieldemarmiesse gabrieldemarmiesse deleted the symplifying_data_format branch September 18, 2018 06:33
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.

3 participants