-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Add convert function #2407
Add convert function #2407
Changes from 11 commits
d7bde1f
f23ee80
e3a37a7
c079437
cf1018d
48e5574
09c8bf2
8f5805d
94dbe44
9011f9e
96a56b9
f904e79
283bdc5
77c4dce
46ccfc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,3 +149,47 @@ def reader(): | |
yield line | ||
|
||
return reader | ||
|
||
|
||
def convert(output_path, eader, num_shards, name_prefix): | ||
import recordio | ||
import cPickle as pickle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add cPickle installation with fixed version in https://github.com/PaddlePaddle/Paddle/blob/develop/Dockerfile#L55 . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python 2.7 and Python 3.4 include the pickle and cPickle modules already. But I didn't find official answer, there is a answer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, thanks! |
||
""" | ||
Convert data from reader to recordio format files. | ||
|
||
:param output_path: directory in which output files will be saved. | ||
:param reader: a data reader, from which the convert program will read data instances. | ||
:param num_shards: the number of shards that the dataset will be partitioned into. | ||
:param name_prefix: the name prefix of generated files. | ||
""" | ||
|
||
def open_needs(idx): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
n = "%s/%s-%05d" % (output_path, name_prefix, idx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should follow the format in the design doc:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
w = recordio.writer(n) | ||
f = open(n, "w") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
idx += 1 | ||
|
||
return w, f, idx | ||
|
||
def close_needs(w, f): | ||
if w is not None: | ||
w.close() | ||
|
||
if f is not None: | ||
f.close() | ||
|
||
idx = 0 | ||
w = None | ||
f = None | ||
|
||
for i, d in enumerate(reader()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I should have mentioned earlier, please consider this issue: #1915 To randomize, maybe we could have a shuffle_buffer_size as optional parameter. read until the buffer is full, shuffle and then write to RecordIO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if w is None: | ||
w, f, idx = open_needs(idx) | ||
|
||
w.write(pickle.dumps(d, pickle.HIGHEST_PROTOCOL)) | ||
|
||
if i % num_shards == 0 and i >= num_shards: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的逻辑我不是很明白,假设一共有N个shard,这里的逻辑是每N个record,写入下一个shard。不应该是每一个record写入下一个shard吗? 是不是考虑这样: var writers []writer
// fill writer
writer[i%num_shards].Write(record)
// close all writer once everything is done. Don't close and create a new writer frequently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done.一开始想错了,把 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一开始想如果文件数目比较多,而记录的个数比较少。那样的话会生成空文件。 |
||
close_needs(w, f) | ||
w, f, idx = open_needs(idx) | ||
|
||
close_needs(w, f) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,22 @@ def test_cluster_file_reader(self): | |
for idx, e in enumerate(reader()): | ||
self.assertEqual(e, str("0")) | ||
|
||
def test_convert(self): | ||
def test_reader(): | ||
def reader(): | ||
for x in xrange(10): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test will break when 10 is changed to 4. According to line 191 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
yield x | ||
|
||
return reader | ||
|
||
path = tempfile.mkdtemp() | ||
|
||
paddle.v2.dataset.common.convert(path, | ||
test_reader(), 4, 'random_images') | ||
|
||
files = glob.glob(temp_path + '/random_images-*') | ||
self.assertEqual(len(files), 3) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eader -> reader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done