Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions python/caffe/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def blobprotovector_str_to_arraylist(str):
return [blobproto_to_array(blob) for blob in vec.blobs]


def array_to_datum(arr, label=0):
def array_to_datum(arr, label=None):
"""Converts a 3-dimensional array to datum. If the array has dtype uint8,
the output data will be encoded as a string. Otherwise, the output data
will be stored in float format.
Expand All @@ -76,7 +76,8 @@ def array_to_datum(arr, label=0):
datum.data = arr.tostring()
else:
datum.float_data.extend(arr.flat)
datum.label = label
if label is not None:
datum.label = label
return datum


Expand Down
15 changes: 15 additions & 0 deletions python/caffe/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ def test_scalar(self):

arr = caffe.io.blobproto_to_array(blob)
self.assertEqual(arr, 123)


class TestArrayToDatum(unittest.TestCase):

def test_label_none_size(self):
# Set label
d1 = caffe.io.array_to_datum(
np.ones((10,10,3)), label=1)
# Don't set label
d2 = caffe.io.array_to_datum(
np.ones((10,10,3)))
# Not setting the label should result in a smaller object
self.assertGreater(
len(d1.SerializeToString()),
len(d2.SerializeToString()))