Allow old-style shape in blobproto_to_array#3200
Conversation
|
Thanks @lukeyeager, sorry for neglecting to think about this in #3170. I think the logic should be reversed though -- i.e., should check if any of the legacy fields are present and use them if so, othereise use the shape. This way, a BlobProto with neither the legacy fields nor a shape will be interpreted as having an empty shape (a scalar). This is consistent with the behavior of Blob::FromProto. |
13503f7 to
4376d17
Compare
|
I've updated the logic and added a third test. |
python/caffe/io.py
Outdated
There was a problem hiding this comment.
This wasn't quite what I meant -- this will give a 1D array, not a 0D array (scalar). You'd still want to return data.reshape(blob.shape.dim), or equivalently return data.reshape(()). The invariant we want is len(data.shape) == len(blob.shape.dim) (unless any of the legacy fields are present). It will only work in the case where len(data) == 1, but that's okay since a BlobProto with no shape field (and no legacy dims) is kind of an odd corner case.
There was a problem hiding this comment.
Well I'm not letting it fall back to this worthless error:
File "/home/tranlaman/BLVC-caffe/python/caffe/io.py", line 26, in blobproto_to_array
return np.array(blob.data).reshape(*blob.shape.dim)
TypeError: function takes exactly 1 argument (0 given)
How about raising a ValueError?
if len(data) != 1:
raise ValueError('blob has no shape')
# Return scalar
return data[0]There was a problem hiding this comment.
If you remove the * (reshape(blob.shape.dim)) it should work? ValueError seems okay to me too, though.
There was a problem hiding this comment.
Oh yeah, that works. Good call.
4376d17 to
75e859a
Compare
|
Updated logic again and added a fourth test. |
|
Thanks for the fixes and tests @lukeyeager! LGTM. |
Allow old-style shape in blobproto_to_array
Fixes #3199
Bug introduced in #3170