'Collapses CTC output to regular output.
Args:
ctc_labels: List of class labels including null characters to remove.
merge_dups: If True, Duplicate labels will be merged.
null_label: Label value to ignore.
All trailing zeros are removed!!
TODO(rays) This may become a problem with non-CTC models.
If using charset, this should not be a problem as zero is always space.
tf.pad can only append zero, so we have to be able to drop them, as a
non-ctc will have learned to output trailing zeros instead of trailing
nulls. This is awkward, as the stock ctc loss function requires that the
null character be num_classes-1.
Returns:
(List of) Labels with null characters removed.'

def _CodesFromCTC(self, ctc_labels, merge_dups, null_label):

    out_labels = []
    prev_label = (-1)
    zeros_needed = 0
    for label in ctc_labels:
        if (label == null_label):
            prev_label = (-1)
        elif ((label != prev_label) or (not merge_dups)):
            if (label == 0):
                zeros_needed += 1
            else:
                if (merge_dups and (zeros_needed > 0)):
                    out_labels.append(0)
                else:
                    out_labels += ([0] * zeros_needed)
                zeros_needed = 0
                out_labels.append(label)
            prev_label = label
    return out_labels
