Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't force datum.label=0 in array_to_datum #3749

Merged
merged 1 commit into from
Apr 20, 2016
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
Don't force datum.label=0 in array_to_datum
  • Loading branch information
lukeyeager committed Feb 29, 2016
commit c1c559c2cb98d6de955f1d469c6104cb265f5dc5
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()))