Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-360]auto convert str to bytes in img.imdecode when py3 #10697

Merged
merged 5 commits into from
Sep 21, 2018

Conversation

yajiedesign
Copy link
Contributor

@yajiedesign yajiedesign commented Apr 26, 2018

Description

auto convert str to bytes in img.imdecode when py3

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@@ -132,6 +134,9 @@ def imdecode(buf, *args, **kwargs):
<NDArray 224x224x3 @cpu(0)>
"""
if not isinstance(buf, nd.NDArray):
if sys.version_info[0] == 3:
if isinstance(buf, str):
buf = bytes(buf, "ascii")
Copy link
Contributor

Choose a reason for hiding this comment

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

could you add a test?
also we have mx.base.py_str. Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mx.base.py_str is 'utf8' encoding,I'm not sure it can work.

Copy link
Contributor

@piiswrong piiswrong Apr 30, 2018

Choose a reason for hiding this comment

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

how about using buffer?

Copy link
Member

Choose a reason for hiding this comment

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

This case really shouldn't happen, because in this case the API is underspecified.

String is always tied to an encoding, and if the string comes from the user, it could have been decoded with any encoding/charset, so the 'ascii' assumption can easily be wrong.

Copy link
Contributor Author

@yajiedesign yajiedesign May 2, 2018

Choose a reason for hiding this comment

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

@szha or I change it into a friendly error message. prompt user convet it himself.

Copy link
Member

Choose a reason for hiding this comment

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

OK

@piiswrong
Copy link
Contributor

Is this going to cause error if no converting is done?

@yajiedesign
Copy link
Contributor Author

yajiedesign commented Apr 27, 2018

@piiswrong np.frombuffer receive buffer like param,but in python3 str not is buffer like.will trigger TypeError.

@piiswrong
Copy link
Contributor

I can merge this work around first. But I think we should have a mx.nd.frombuffer that uses CAPI directly.

BTW why would buf be str in python3? open('xxx', 'b').read() produces bytes in py3 right?

@yajiedesign
Copy link
Contributor Author

This conversion is used to be compatible with python2,If someone saves data with python2 and reads it with python3, it will go wrong.

@chinakook
Copy link
Contributor

It's a bug of numpy -- numpy/numpy#8933

@szha szha requested review from zhreshold and removed request for szha May 21, 2018 19:53
@lupesko
Copy link
Contributor

lupesko commented Aug 6, 2018

@piiswrong are you still planning to merge this?

@lupesko
Copy link
Contributor

lupesko commented Aug 21, 2018

@piiswrong @zhreshold bouncing for a review. thanks!

@zhreshold
Copy link
Member

It is still relevant now?

@yajiedesign
Copy link
Contributor Author

@zhreshold Numpy has not been fixed. This examination should be useful.

@lupesko
Copy link
Contributor

lupesko commented Sep 14, 2018

Adding @sandeep-krishnamurthy @Roshrini @vandanavk for review.
@mxnet-label-bot [pr-awaiting-review]

@marcoabreu marcoabreu added the pr-awaiting-review PR is waiting for code review label Sep 14, 2018
Copy link
Contributor

@lupesko lupesko left a comment

Choose a reason for hiding this comment

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

Some minor wording nit.

@@ -133,6 +135,9 @@ def imdecode(buf, *args, **kwargs):
<NDArray 224x224x3 @cpu(0)>
"""
if not isinstance(buf, nd.NDArray):
if sys.version_info[0] == 3 and not isinstance(buf, (bytes, np.ndarray)):
raise ValueError('buf must bytes or numpy.ndarray,'
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: suggest rephrasing a bit to "buf must be of type bytes or numpy.ndarray"

@@ -133,6 +135,9 @@ def imdecode(buf, *args, **kwargs):
<NDArray 224x224x3 @cpu(0)>
"""
if not isinstance(buf, nd.NDArray):
if sys.version_info[0] == 3 and not isinstance(buf, (bytes, np.ndarray)):
raise ValueError('buf must bytes or numpy.ndarray,'
'if you input str,please convert it to bytes')
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: suggest rephrase: "if you would like to input type str, please convert to bytes"

@yajiedesign
Copy link
Contributor Author

@lupesko done

Copy link
Contributor

@sandeep-krishnamurthy sandeep-krishnamurthy left a comment

Choose a reason for hiding this comment

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

LGTM.
This is still relevant edge case numpy/numpy#8933 as pointed out by @chinakook

@sandeep-krishnamurthy sandeep-krishnamurthy added pr-awaiting-merge Review and CI is complete. Ready to Merge and removed pr-awaiting-review PR is waiting for code review labels Sep 14, 2018
@sandeep-krishnamurthy
Copy link
Contributor

@szha @piiswrong - ping for any other concerns.

@sandeep-krishnamurthy
Copy link
Contributor

@zhreshold - This is good to go, if it looks fine for you.

@zhreshold zhreshold merged commit d4991a0 into apache:master Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants