This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-807] Support integer label type in ctc_loss operator #12468
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f0a757b
Support integer type in ctc_loss
apeforest 7af7274
Support any data type in ctc_loss operator
apeforest 5e99e7e
Enable integer type in labels and fix lint errors
apeforest eb30964
Fix compilation error in GPU
apeforest 774c61b
Add unit tests
apeforest d9dc6e6
Merge remote-tracking branch 'upstream/master' into bugfix/ctc_loss_i…
apeforest 59f48f2
Undo indentation
apeforest 1b3d141
Undo blank line
apeforest 299b1e7
Undo blank line
apeforest ec5cc3c
Add unit test for large number of classes
apeforest 59e5d7c
Merge branch 'bugfix/ctc_loss_integer' of https://github.com/apefores…
apeforest c8b7cd4
move unit tests to test_operator.py per reviewer advice
apeforest 973daca
update unit test
apeforest 217069e
update unit test
apeforest 4574c7c
update unit test using random seed
apeforest fa61a0a
Update unit test
apeforest 3fbb3f5
Fix unit test difference Python2 and Python3
apeforest 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
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 |
---|---|---|
|
@@ -244,6 +244,64 @@ def assert_match(inputs, x, y, threshold, is_ascend=False): | |
assert_match([[0.5, 0.6], [0.1, 0.2], [0.3, 0.4]], [1, -1, 0], [2, 0], 1e-12, False) | ||
assert_match([[0.5, 0.6], [0.1, 0.2], [0.3, 0.4]], [-1, 0, 1], [1, 2], 100, True) | ||
|
||
def test_ctc_loss_op(): | ||
batch_size = 10 | ||
seq_len = 5 | ||
label_len = 3 | ||
num_classes = 6 | ||
np.random.seed(1) | ||
x = np.random.uniform(size=(seq_len, batch_size, num_classes)) | ||
y = np.random.randint(0, num_classes, size=(batch_size, label_len)) | ||
|
||
def test_cpu(x, y): | ||
data = mx.nd.array(x, ctx=mx.cpu(0)) | ||
label = mx.nd.array(y, ctx=mx.cpu(0)) | ||
loss = mx.nd.contrib.ctc_loss(data=data, label=label) | ||
loss = mx.nd.make_loss(loss) | ||
expected_output = [9.604521, 7.096151, 4.906869, 5.5237527, 5.9895644, 5.584548, | ||
5.528411, 5.765914, 6.740701, 5.2625823] | ||
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. This testing strategy (i.e. compare the output from random input and labels with fixed seed from recorded output) is not meaningful and does not guarantee anything. It merely increases the line coverage. 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. Did not notice the unit test in test_operator.py. I have removed this one. |
||
assert np.isclose(loss.asnumpy(), expected_output).all() | ||
|
||
def test_gpu(x, y): | ||
data = mx.nd.array(x, ctx=mx.gpu(0)) | ||
label = mx.nd.array(y, ctx=mx.gpu(0)) | ||
loss = mx.nd.contrib.ctc_loss(data=data, label=label) | ||
loss = mx.nd.make_loss(loss) | ||
expected_output = [9.604521, 7.096151, 4.906869, 5.5237527, 5.9895644, 5.584548, | ||
5.528411, 5.765914, 6.740701, 5.2625823] | ||
assert np.isclose(loss.asnumpy(), expected_output).all() | ||
|
||
def test_integer_label(x, y): | ||
data = mx.nd.array(x, ctx=mx.cpu(0)) | ||
label = mx.nd.array(y, ctx=mx.cpu(0), dtype=np.int32) | ||
loss = mx.nd.contrib.ctc_loss(data=data, label=label) | ||
loss = mx.nd.make_loss(loss) | ||
expected_output = [9.604521, 7.096151, 4.906869, 5.5237527, 5.9895644, 5.584548, | ||
5.528411, 5.765914, 6.740701, 5.2625823] | ||
assert np.isclose(loss.asnumpy(), expected_output).all() | ||
|
||
def test_large_classes(): | ||
batch_size = 1024 | ||
seq_len = 35 | ||
label_len = 10 | ||
num_classes = 6000 | ||
x = np.random.uniform(size=(seq_len, batch_size, num_classes)) | ||
y = np.random.randint(0, num_classes, size=(batch_size, label_len)) | ||
|
||
data = mx.nd.array(x, ctx=mx.gpu(0)) | ||
label = mx.nd.array(y, ctx=mx.gpu(0)) | ||
loss = mx.nd.contrib.ctc_loss(data=data, label=label) | ||
loss = mx.nd.make_loss(loss) | ||
expected_output_sum = 282733.95318603516 | ||
assert np.isclose(sum(loss.asnumpy(), expected_output_sum)) | ||
|
||
test_cpu(x, y) | ||
test_integer_label(x, y) | ||
if default_context().device_type == 'gpu': | ||
test_gpu(x, y) | ||
test_large_classes() | ||
|
||
|
||
if __name__ == '__main__': | ||
import nose | ||
nose.runmodule() |
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.
CTC loss tests can be found at https://github.com/apache/incubator-mxnet/blob/master/tests/python/unittest/test_operator.py#L4500, and integration at https://github.com/apache/incubator-mxnet/blob/master/tests/python/unittest/test_loss.py#L186. Test cases are from hand calculated examples.
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.
Feel free to add test cases for large labels there.
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.
Thanks for the reference. Moving the unit test there.