Skip to content

Commit

Permalink
Merge pull request #3749 from lukeyeager/bvlc/array_to_datum-default-…
Browse files Browse the repository at this point in the history
…label

Don't force datum.label=0 in array_to_datum
  • Loading branch information
shelhamer committed Apr 20, 2016
2 parents 045e5ba + c1c559c commit 5166583
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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()))

0 comments on commit 5166583

Please sign in to comment.