You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

greedydecoder.py 1.9 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. modify GreedyDecoder to adapt to MindSpore
  17. """
  18. import numpy as np
  19. from deepspeech_pytorch.decoder import GreedyDecoder
  20. class MSGreedyDecoder(GreedyDecoder):
  21. """
  22. GreedyDecoder used for MindSpore
  23. """
  24. def process_string(self, sequence, size, remove_repetitions=False):
  25. """
  26. process string
  27. """
  28. string = ''
  29. offsets = []
  30. for i in range(size):
  31. char = self.int_to_char[sequence[i].item()]
  32. if char != self.int_to_char[self.blank_index]:
  33. if remove_repetitions and i != 0 and char == self.int_to_char[sequence[i - 1].item()]:
  34. pass
  35. elif char == self.labels[self.space_index]:
  36. string += ' '
  37. offsets.append(i)
  38. else:
  39. string = string + char
  40. offsets.append(i)
  41. return string, offsets
  42. def decode(self, probs, sizes=None):
  43. probs = probs.asnumpy()
  44. sizes = sizes.asnumpy()
  45. max_probs = np.argmax(probs, axis=-1)
  46. strings, offsets = self.convert_to_strings(max_probs, sizes, remove_repetitions=True, return_offsets=True)
  47. return strings, offsets