Fix array_to_datum/datum_to_array crashing on NumPy >= 2.0#7108
Open
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Open
Fix array_to_datum/datum_to_array crashing on NumPy >= 2.0#7108Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Conversation
python/caffe/io.py's datum (de)serializers call two NumPy APIs that
were deprecated and then removed in NumPy 2.0:
datum.data = arr.tostring() # line 76
return np.fromstring(datum.data, dtype=np.uint8).reshape # line 89
- numpy.ndarray.tostring was deprecated in favour of tobytes in NumPy
1.19 and removed in NumPy 2.0.
- numpy.fromstring for binary data was deprecated in favour of
frombuffer in NumPy 1.14 and removed in NumPy 2.0.
On any user running NumPy >= 2.0, both array_to_datum (save path) and
datum_to_array (load path for uint8 datums, e.g. image datasets) raise
AttributeError / AttributeError respectively, breaking pycaffe data
ingestion.
Switching to the explicit binary-safe replacements:
- arr.tostring() -> arr.tobytes()
- np.fromstring(buf, ...) -> np.frombuffer(buf, ...)
is exactly the migration NumPy's own deprecation notices recommend and
the behaviour matches the deprecated calls byte-for-byte (same bytes
out on write, same ndarray-view on read). Works on the NumPy 1.x
versions caffe already supported.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
python/caffe/io.py's datum (de)serializers still use two NumPy entry points that were deprecated and then removed in NumPy 2.0:numpy.ndarray.tostringwas deprecated in NumPy 1.19 in favour oftobytesand removed in NumPy 2.0.numpy.fromstringfor binary data was deprecated in NumPy 1.14 in favour offrombufferand removed in NumPy 2.0.On any user running NumPy ≥ 2.0,
array_to_datum(save path) anddatum_to_array(load path for uint8 datums, e.g. image datasets) raiseAttributeError: 'numpy.ndarray' object has no attribute 'tostring'andAttributeError: module 'numpy' has no attribute 'fromstring'respectively. This breaks pycaffe data ingestion for anyone on a modern scientific-Python stack.Fix
Switch to the binary-safe replacements NumPy's deprecation notices have been directing users to for years:
Both replacements are byte-for-byte equivalent to the deprecated calls:
ndarray.tobytes()produces the same bytes astostring()(the name was changed precisely because "tostring" misled users into expecting a Pythonstr), andnp.frombuffer(buf, dtype=np.uint8)reads the same bytes back into the same uint8 ndarray shape. Existing NumPy 1.x users are unaffected — bothtobytesandfrombufferhave been present since NumPy 1.9 and 1.14 respectively.Two one-token renames, no other changes.