Skip to content

Commit

Permalink
Merge pull request #3479 from helinwang/recordio
Browse files Browse the repository at this point in the history
Fix local recordio reader
  • Loading branch information
helinwang committed Aug 15, 2017
2 parents 33d502e + b95668d commit 245f622
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
12 changes: 8 additions & 4 deletions python/paddle/v2/reader/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def reader():
return reader


def recordio_local(paths, buf_size=100):
def recordio(paths, buf_size=100):
"""
Creates a data reader from given RecordIO file paths separated by ",",
glob pattern is supported.
Expand All @@ -67,15 +67,19 @@ def recordio_local(paths, buf_size=100):

import recordio as rec
import paddle.v2.reader.decorator as dec
import cPickle as pickle

def reader():
a = ','.join(paths)
f = rec.reader(a)
if isinstance(paths, basestring):
path = paths
else:
path = ",".join(paths)
f = rec.reader(path)
while True:
r = f.read()
if r is None:
break
yield r
yield pickle.loads(r)
f.close()

return dec.buffered(reader, buf_size)
Expand Down
22 changes: 22 additions & 0 deletions python/paddle/v2/reader/tests/creator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,27 @@ def test_text_file(self):
self.assertEqual(e, str(idx * 2) + " " + str(idx * 2 + 1))


class TestRecordIO(unittest.TestCase):
def do_test(self, path):
reader = paddle.v2.reader.creator.recordio(path)
idx = 0
for e in reader():
if idx == 0:
self.assertEqual(e, (1, 2, 3))
elif idx == 1:
self.assertEqual(e, (4, 5, 6))
idx += 1
self.assertEqual(idx, 2)

def test_recordIO(self):
self.do_test(
os.path.join(
os.path.dirname(__file__), "test_reader_recordio.dat"))
self.do_test([
os.path.join(
os.path.dirname(__file__), "test_reader_recordio.dat")
])


if __name__ == '__main__':
unittest.main()
Binary file not shown.
2 changes: 1 addition & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
requests==2.9.2
numpy>=1.12
protobuf==3.1
recordio
recordio>=0.1.0
matplotlib
rarfile
scipy>=0.19.0
Expand Down

0 comments on commit 245f622

Please sign in to comment.