Convert a blob proto to an array. In default, we will just return the data, unless return_diff is True, in which case we will return the diff.
(blob, return_diff=False)
| 16 | |
| 17 | ## proto / datum / ndarray conversion |
| 18 | def blobproto_to_array(blob, return_diff=False): |
| 19 | """ |
| 20 | Convert a blob proto to an array. In default, we will just return the data, |
| 21 | unless return_diff is True, in which case we will return the diff. |
| 22 | """ |
| 23 | # Read the data into an array |
| 24 | if return_diff: |
| 25 | data = np.array(blob.diff) |
| 26 | else: |
| 27 | data = np.array(blob.data) |
| 28 | |
| 29 | # Reshape the array |
| 30 | if blob.HasField('num') or blob.HasField('channels') or blob.HasField('height') or blob.HasField('width'): |
| 31 | # Use legacy 4D shape |
| 32 | return data.reshape(blob.num, blob.channels, blob.height, blob.width) |
| 33 | else: |
| 34 | return data.reshape(blob.shape.dim) |
| 35 | |
| 36 | def array_to_blobproto(arr, diff=None): |
| 37 | """Converts a N-dimensional array to blob proto. If diff is given, also |
no test coverage detected