Converts CTC labels from dense to sparse. Arguments: labels: dense CTC labels. label_lengths: length of the labels. Returns: A sparse tensor representation of the labels.
(labels, label_lengths)
| 5556 | |
| 5557 | @keras_export('keras.backend.ctc_label_dense_to_sparse') |
| 5558 | def ctc_label_dense_to_sparse(labels, label_lengths): |
| 5559 | """Converts CTC labels from dense to sparse. |
| 5560 | |
| 5561 | Arguments: |
| 5562 | labels: dense CTC labels. |
| 5563 | label_lengths: length of the labels. |
| 5564 | |
| 5565 | Returns: |
| 5566 | A sparse tensor representation of the labels. |
| 5567 | """ |
| 5568 | label_shape = array_ops.shape(labels) |
| 5569 | num_batches_tns = array_ops.stack([label_shape[0]]) |
| 5570 | max_num_labels_tns = array_ops.stack([label_shape[1]]) |
| 5571 | |
| 5572 | def range_less_than(old_input, current_input): |
| 5573 | return array_ops.expand_dims( |
| 5574 | math_ops.range(array_ops.shape(old_input)[1]), 0) < array_ops.fill( |
| 5575 | max_num_labels_tns, current_input) |
| 5576 | |
| 5577 | init = math_ops.cast( |
| 5578 | array_ops.fill([1, label_shape[1]], 0), dtypes_module.bool) |
| 5579 | dense_mask = functional_ops.scan( |
| 5580 | range_less_than, label_lengths, initializer=init, parallel_iterations=1) |
| 5581 | dense_mask = dense_mask[:, 0, :] |
| 5582 | |
| 5583 | label_array = array_ops.reshape( |
| 5584 | array_ops.tile(math_ops.range(0, label_shape[1]), num_batches_tns), |
| 5585 | label_shape) |
| 5586 | label_ind = array_ops.boolean_mask(label_array, dense_mask) |
| 5587 | |
| 5588 | batch_array = array_ops.transpose( |
| 5589 | array_ops.reshape( |
| 5590 | array_ops.tile(math_ops.range(0, label_shape[0]), max_num_labels_tns), |
| 5591 | reverse(label_shape, 0))) |
| 5592 | batch_ind = array_ops.boolean_mask(batch_array, dense_mask) |
| 5593 | indices = array_ops.transpose( |
| 5594 | array_ops.reshape(concatenate([batch_ind, label_ind], axis=0), [2, -1])) |
| 5595 | |
| 5596 | vals_sparse = array_ops.gather_nd(labels, indices) |
| 5597 | |
| 5598 | return sparse_tensor.SparseTensor( |
| 5599 | math_ops.cast(indices, dtypes_module.int64), vals_sparse, |
| 5600 | math_ops.cast(label_shape, dtypes_module.int64)) |
| 5601 | |
| 5602 | |
| 5603 | @keras_export('keras.backend.ctc_batch_cost') |
no test coverage detected