-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
data load for inference #788
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,16 +31,18 @@ class SampleInfo(object): | |
label_bin_path (str): File containing the label data. | ||
label_size (int): Byte count of the sample's label data. | ||
label_frame_num (int): Label number of the sample. | ||
sample_name (str): key of the sample | ||
""" | ||
|
||
def __init__(self, feature_bin_path, feature_start, feature_size, | ||
feature_frame_num, feature_dim, label_bin_path, label_start, | ||
label_size, label_frame_num): | ||
label_size, label_frame_num, sample_name): | ||
self.feature_bin_path = feature_bin_path | ||
self.feature_start = feature_start | ||
self.feature_size = feature_size | ||
self.feature_frame_num = feature_frame_num | ||
self.feature_dim = feature_dim | ||
self.sample_name = sample_name | ||
|
||
self.label_bin_path = label_bin_path | ||
self.label_start = label_start | ||
|
@@ -102,24 +104,32 @@ def generate_sample_info_list(self): | |
feature_bin_path = self._feature_bin_paths[block_idx] | ||
feature_desc_path = self._feature_desc_paths[block_idx] | ||
|
||
label_desc_lines = open(label_desc_path).readlines() | ||
feature_desc_lines = open(feature_desc_path).readlines() | ||
label_desc_lines = None | ||
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. Seems that |
||
if label_desc_path != "": | ||
label_desc_lines = open(label_desc_path).readlines() | ||
|
||
sample_num = int(label_desc_lines[0].split()[1]) | ||
assert sample_num == int(feature_desc_lines[0].split()[1]) | ||
sample_num = int(feature_desc_lines[0].split()[1]) | ||
if label_desc_path != "": | ||
assert sample_num == int(label_desc_lines[0].split()[1]) | ||
|
||
for i in xrange(sample_num): | ||
feature_desc_split = feature_desc_lines[i + 1].split() | ||
sample_name = feature_desc_split[0] | ||
feature_start = int(feature_desc_split[2]) | ||
feature_size = int(feature_desc_split[3]) | ||
feature_frame_num = int(feature_desc_split[4]) | ||
feature_dim = int(feature_desc_split[5]) | ||
|
||
label_desc_split = label_desc_lines[i + 1].split() | ||
label_start = int(label_desc_split[2]) | ||
label_size = int(label_desc_split[3]) | ||
label_frame_num = int(label_desc_split[4]) | ||
assert feature_frame_num == label_frame_num | ||
label_start = -1 | ||
label_size = -1 | ||
label_frame_num = feature_frame_num | ||
if label_desc_path != "": | ||
label_desc_split = label_desc_lines[i + 1].split() | ||
label_start = int(label_desc_split[2]) | ||
label_size = int(label_desc_split[3]) | ||
label_frame_num = int(label_desc_split[4]) | ||
assert feature_frame_num == label_frame_num | ||
|
||
if self._split_sentence_threshold == -1 or \ | ||
self._split_perturb == -1 or \ | ||
|
@@ -129,7 +139,7 @@ def generate_sample_info_list(self): | |
SampleInfo(feature_bin_path, feature_start, | ||
feature_size, feature_frame_num, feature_dim, | ||
label_bin_path, label_start, label_size, | ||
label_frame_num)) | ||
label_frame_num, sample_name)) | ||
#split sentence | ||
else: | ||
cur_frame_pos = 0 | ||
|
@@ -150,7 +160,7 @@ def generate_sample_info_list(self): | |
* feature_dim * 4, cur_frame_len * feature_dim * | ||
4, cur_frame_len, feature_dim, label_bin_path, | ||
label_start + cur_frame_pos * 4, cur_frame_len * | ||
4, cur_frame_len)) | ||
4, cur_frame_len, sample_name)) | ||
|
||
remain_frame_num -= cur_frame_len | ||
cur_frame_pos += cur_frame_len | ||
|
@@ -187,7 +197,7 @@ class AsyncDataReader(object): | |
|
||
def __init__(self, | ||
feature_file_list, | ||
label_file_list, | ||
label_file_list="", | ||
drop_frame_len=512, | ||
proc_num=10, | ||
sample_buffer_size=1024, | ||
|
@@ -221,16 +231,25 @@ def __init__(self, | |
def generate_bucket_list(self, is_shuffle): | ||
if self._block_info_list is None: | ||
block_feature_info_lines = open(self._feature_file_list).readlines() | ||
block_label_info_lines = open(self._label_file_list).readlines() | ||
assert len(block_feature_info_lines) == len(block_label_info_lines) | ||
self._block_info_list = [] | ||
for i in xrange(0, len(block_feature_info_lines), 2): | ||
block_info = (block_feature_info_lines[i], | ||
block_feature_info_lines[i + 1], | ||
block_label_info_lines[i], | ||
block_label_info_lines[i + 1]) | ||
self._block_info_list.append( | ||
map(lambda line: line.strip(), block_info)) | ||
if self._label_file_list != "": | ||
block_label_info_lines = open(self._label_file_list).readlines() | ||
#block_label_info_lines = open(self._label_file_list).readlines() | ||
assert len(block_feature_info_lines) == len( | ||
block_label_info_lines) | ||
for i in xrange(0, len(block_feature_info_lines), 2): | ||
block_info = (block_feature_info_lines[i], | ||
block_feature_info_lines[i + 1], | ||
block_label_info_lines[i], | ||
block_label_info_lines[i + 1]) | ||
self._block_info_list.append( | ||
map(lambda line: line.strip(), block_info)) | ||
else: | ||
for i in xrange(0, len(block_feature_info_lines), 2): | ||
block_info = (block_feature_info_lines[i], | ||
block_feature_info_lines[i + 1], "", "") | ||
self._block_info_list.append( | ||
map(lambda line: line.strip(), block_info)) | ||
|
||
if is_shuffle: | ||
self._rng.shuffle(self._block_info_list) | ||
|
@@ -318,19 +337,24 @@ def read_bytes(fpath, start, size): | |
sample_info.feature_dim, | ||
len(feature_bytes)) | ||
|
||
label_bytes = read_bytes(sample_info.label_bin_path, | ||
sample_info.label_start, | ||
sample_info.label_size) | ||
|
||
assert sample_info.label_frame_num * 4 == len(label_bytes), ( | ||
sample_info.label_bin_path, sample_info.label_array, | ||
len(label_bytes)) | ||
|
||
label_array = struct.unpack('I' * sample_info.label_frame_num, | ||
label_bytes) | ||
label_data = np.array( | ||
label_array, dtype='int64').reshape( | ||
(sample_info.label_frame_num, 1)) | ||
if sample_info.label_bin_path != "": | ||
label_bytes = read_bytes(sample_info.label_bin_path, | ||
sample_info.label_start, | ||
sample_info.label_size) | ||
|
||
assert sample_info.label_frame_num * 4 == len( | ||
label_bytes), (sample_info.label_bin_path, | ||
sample_info.label_array, | ||
len(label_bytes)) | ||
|
||
label_array = struct.unpack( | ||
'I' * sample_info.label_frame_num, label_bytes) | ||
label_data = np.array( | ||
label_array, dtype='int64').reshape( | ||
(sample_info.label_frame_num, 1)) | ||
else: | ||
label_data = np.zeros( | ||
(sample_info.label_frame_num, 1), dtype='int64') | ||
|
||
feature_frame_num = sample_info.feature_frame_num | ||
feature_dim = sample_info.feature_dim | ||
|
@@ -340,12 +364,11 @@ def read_bytes(fpath, start, size): | |
feature_data = np.array( | ||
feature_array, dtype='float32').reshape(( | ||
sample_info.feature_frame_num, sample_info.feature_dim)) | ||
|
||
sample_data = (feature_data, label_data) | ||
sample_data = (feature_data, label_data, | ||
sample_info.sample_name) | ||
for transformer in self._transformers: | ||
# @TODO(pkuyym) to make transfomer only accept feature_data | ||
sample_data = transformer.perform_trans(sample_data) | ||
|
||
while order_id != out_order[0]: | ||
time.sleep(0.001) | ||
|
||
|
@@ -395,24 +418,26 @@ def conv_to_shared(ndarray): | |
batch_samples.append(sample) | ||
lod.append(lod[-1] + sample[0].shape[0]) | ||
if len(batch_samples) == batch_size: | ||
feature, label = batch_to_ndarray(batch_samples, lod) | ||
feature, label, name_lst = batch_to_ndarray( | ||
batch_samples, lod) | ||
|
||
feature = conv_to_shared(feature) | ||
label = conv_to_shared(label) | ||
lod = conv_to_shared(np.array(lod).astype('int64')) | ||
|
||
batch_queue.put((feature, label, lod)) | ||
batch_queue.put((feature, label, lod, name_lst)) | ||
batch_samples = [] | ||
lod = [0] | ||
|
||
if len(batch_samples) >= minimum_batch_size: | ||
(feature, label) = batch_to_ndarray(batch_samples, lod) | ||
(feature, label, name_lst) = batch_to_ndarray(batch_samples, | ||
lod) | ||
|
||
feature = conv_to_shared(feature) | ||
label = conv_to_shared(label) | ||
lod = conv_to_shared(np.array(lod).astype('int64')) | ||
|
||
batch_queue.put((feature, label, lod)) | ||
batch_queue.put((feature, label, lod, name_lst)) | ||
|
||
batch_queue.put(EpochEndSignal()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
key
--->Key
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.
fix