1 """This script converts blobproto instances to numpy arrays.
2 """
4 from caffe.proto import caffe_pb2
5 import numpy as np
7 def blobproto_to_array(blob):
8 arr = np.array(blob.data).reshape(blob.num(), blob.channels(), blobs.height(),
9 blobs.width())
10 return arr
12 def array_to_blobproto(arr):
13 if arr.ndim != 4:
14 raise ValueError('Incorrect array shape.')
15 blob = caffe_pb2.BlobProto()
16 blob.num, blob.channels, blob.height, blob.width = arr.shape;
17 blob.data.extend(arr.flat)
18 return blob
20 def array_to_datum(arr):
21 if arr.ndim != 3:
22 raise ValueError('Incorrect array shape.')
23 if arr.dtype != np.uint8:
24 raise TypeError('Input array has to be of type uint8.')
25 datum = caffe_pb2.Datum()
26 datum.channels, datum.height, datum.width = arr.shape
27 datum.data = arr.tostring()
28 return datum