class_docstr: This class implements a model-specific automatic speech recognizer using the end-to-end speech recognizer
  DeepSpeech and PyTorch. It supports both version 2 and version 3 of DeepSpeech models as released at
  https://github.com/SeanNaren/deepspeech.pytorch.
  
  | Paper link: https://arxiv.org/abs/1512.02595
docstr: Initialization of an instance PyTorchDeepSpeech.
  
  :param model: DeepSpeech model.
  :param pretrained_model: The choice of pretrained model if a pretrained model is required. Currently this
                           estimator supports 3 different pretrained models consisting of `an4`, `librispeech`
                           and `tedlium`.
  :param filename: Name of the file.
  :param url: Download URL.
  :param use_half: Whether to use FP16 for pretrained model.
  :param optimizer: The optimizer used to train the estimator.
  :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                  gradient computation, e.g. with loss gradient computation. When set to True, this option is
                  only triggered if there are GPUs available.
  :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                    values are `O0`, `O1`, `O2`, and `O3`.
  :param decoder_type: Decoder type. Either `greedy` or `beam`. This parameter is only used when users want
                       transcription outputs.
  :param lm_path: Path to an (optional) kenlm language model for use with beam search. This parameter is only
                  used when users want transcription outputs.
  :param top_paths: Number of beams to be returned. This parameter is only used when users want transcription
                    outputs.
  :param alpha: The weight used for the language model. This parameter is only used when users want transcription
                outputs.
  :param beta: Language model word bonus (all words). This parameter is only used when users want transcription
               outputs.
  :param cutoff_top_n: Cutoff_top_n characters with highest probs in vocabulary will be used in beam search. This
                       parameter is only used when users want transcription outputs.
  :param cutoff_prob: Cutoff probability in pruning. This parameter is only used when users want transcription
                      outputs.
  :param beam_width: The width of beam to be used. This parameter is only used when users want transcription
                     outputs.
  :param lm_workers: Number of language model processes to use. This parameter is only used when users want
                     transcription outputs.
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
         maximum values allowed for features. If floats are provided, these will be used as the range of all
         features. If arrays are provided, each value will be considered the bound for a feature, thus
         the shape of clip values needs to match the total number of features.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
         used for data preprocessing. The first value will be subtracted from the input. The input will then
         be divided by the second one.
  :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                      if available otherwise run on CPU.
docstr_tok: ["Initialization","of","an","instance","PyTorchDeepSpeech",".",":","param","model",":","DeepSpeech","model",".",":","param","pretrained_model",":","The","choice","of","pretrained","model","if","a","pretrained","model","is","required",".","Currently","this","estimator","supports","3","different","pretrained","models","consisting","of","`","an4","`",",","`","librispeech","`","and","`","tedlium","`",".",":","param","filename",":","Name","of","the","file",".",":","param","url",":","Download","URL",".",":","param","use_half",":","Whether","to","use","FP16","for","pretrained","model",".",":","param","optimizer",":","The","optimizer","used","to","train","the","estimator",".",":","param","use_amp",":","Whether","to","use","the","automatic","mixed","precision","tool","to","enable","mixed","precision","training","or","gradient","computation",",","e.g",".","with","loss","gradient","computation",".","When","set","to","True",",","this","option","is","only","triggered","if","there","are","GPUs","available",".",":","param","opt_level",":","Specify","a","pure","or","mixed","precision","optimization","level",".","Used","when","use_amp","is","True",".","Accepted","values","are","`","O0","`",",","`","O1","`",",","`","O2","`",",","and","`","O3","`",".",":","param","decoder_type",":","Decoder","type",".","Either","`","greedy","`","or","`","beam","`",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","lm_path",":","Path","to","an","(","optional",")","kenlm","language","model","for","use","with","beam","search",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","top_paths",":","Number","of","beams","to","be","returned",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","alpha",":","The","weight","used","for","the","language","model",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","beta",":","Language","model","word","bonus","(","all","words",")",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","cutoff_top_n",":","Cutoff_top_n","characters","with","highest","probs","in","vocabulary","will","be","used","in","beam","search",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","cutoff_prob",":","Cutoff","probability","in","pruning",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","beam_width",":","The","width","of","beam","to","be","used",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","lm_workers",":","Number","of","language","model","processes","to","use",".","This","parameter","is","only","used","when","users","want","transcription","outputs",".",":","param","clip_values",":","Tuple","of","the","form","`","(","min",",","max",")","`","of","floats","or","`","np.ndarray","`","representing","the","minimum","and","maximum","values","allowed","for","features",".","If","floats","are","provided",",","these","will","be","used","as","the","range","of","all","features",".","If","arrays","are","provided",",","each","value","will","be","considered","the","bound","for","a","feature",",","thus","the","shape","of","clip","values","needs","to","match","the","total","number","of","features",".",":","param","preprocessing_defences",":","Preprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","postprocessing_defences",":","Postprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","preprocessing",":","Tuple","of","the","form","`","(","subtrahend",",","divisor",")","`","of","floats","or","`","np.ndarray","`","of","values","to","be","used","for","data","preprocessing",".","The","first","value","will","be","subtracted","from","the","input",".","The","input","will","then","be","divided","by","the","second","one",".",":","param","device_type",":","Type","of","device","to","be","used","for","model","and","tensors",",","if","`","cpu","`","run","on","CPU",",","if","`","gpu","`","run","on","GPU","if","available","otherwise","run","on","CPU","."]
code: def __init__(
      self,
      model: Optional["DeepSpeech"] = None,
      pretrained_model: Optional[str] = None,
      filename: Optional[str] = None,
      url: Optional[str] = None,
      use_half: bool = False,
      optimizer: Optional["torch.optim.Optimizer"] = None,  # type: ignore
      use_amp: bool = False,
      opt_level: str = "O1",
      decoder_type: str = "greedy",
      lm_path: str = "",
      top_paths: int = 1,
      alpha: float = 0.0,
      beta: float = 0.0,
      cutoff_top_n: int = 40,
      cutoff_prob: float = 1.0,
      beam_width: int = 10,
      lm_workers: int = 4,
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = None,
      device_type: str = "gpu",
      verbose: bool = True,
  ):
      """
      Initialization of an instance PyTorchDeepSpeech.
  
      :param model: DeepSpeech model.
      :param pretrained_model: The choice of pretrained model if a pretrained model is required. Currently this
                               estimator supports 3 different pretrained models consisting of `an4`, `librispeech`
                               and `tedlium`.
      :param filename: Name of the file.
      :param url: Download URL.
      :param use_half: Whether to use FP16 for pretrained model.
      :param optimizer: The optimizer used to train the estimator.
      :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                      gradient computation, e.g. with loss gradient computation. When set to True, this option is
                      only triggered if there are GPUs available.
      :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                        values are `O0`, `O1`, `O2`, and `O3`.
      :param decoder_type: Decoder type. Either `greedy` or `beam`. This parameter is only used when users want
                           transcription outputs.
      :param lm_path: Path to an (optional) kenlm language model for use with beam search. This parameter is only
                      used when users want transcription outputs.
      :param top_paths: Number of beams to be returned. This parameter is only used when users want transcription
                        outputs.
      :param alpha: The weight used for the language model. This parameter is only used when users want transcription
                    outputs.
      :param beta: Language model word bonus (all words). This parameter is only used when users want transcription
                   outputs.
      :param cutoff_top_n: Cutoff_top_n characters with highest probs in vocabulary will be used in beam search. This
                           parameter is only used when users want transcription outputs.
      :param cutoff_prob: Cutoff probability in pruning. This parameter is only used when users want transcription
                          outputs.
      :param beam_width: The width of beam to be used. This parameter is only used when users want transcription
                         outputs.
      :param lm_workers: Number of language model processes to use. This parameter is only used when users want
                         transcription outputs.
      :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
             maximum values allowed for features. If floats are provided, these will be used as the range of all
             features. If arrays are provided, each value will be considered the bound for a feature, thus
             the shape of clip values needs to match the total number of features.
      :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
      :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
      :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
             used for data preprocessing. The first value will be subtracted from the input. The input will then
             be divided by the second one.
      :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                          if available otherwise run on CPU.
      """
      import torch
      from deepspeech_pytorch.model import DeepSpeech
      from deepspeech_pytorch.configs.inference_config import LMConfig
      from deepspeech_pytorch.enums import DecoderType
      from deepspeech_pytorch.utils import load_decoder, load_model
  
      # Super initialization
      super().__init__(
          model=None,
          clip_values=clip_values,
          channels_first=None,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
      )
  
      # Check DeepSpeech version
      if (
          str(DeepSpeech.__base__)
          == "<class 'torch.nn.modules.module.Module'>"
      ):
          self._version = 2
      elif str(DeepSpeech.__base__) in [
          "<class 'pytorch_lightning.core.lightning.LightningModule'>",
          "<class 'pytorch_lightning.core.module.LightningModule'>",
      ]:
          self._version = 3
      else:
          raise NotImplementedError(
              "Only DeepSpeech version 2 and DeepSpeech version 3 are currently supported."
          )
  
      self.verbose = verbose
  
      # Check clip values
      if self.clip_values is not None:
          if not np.all(self.clip_values[0] == -1):  # pragma: no cover
              raise ValueError(
                  "This estimator requires normalized input audios with clip_vales=(-1, 1)."
              )
          if not np.all(self.clip_values[1] == 1):  # pragma: no cover
              raise ValueError(
                  "This estimator requires normalized input audios with clip_vales=(-1, 1)."
              )
  
      # Check postprocessing defences
      if self.postprocessing_defences is not None:  # pragma: no cover
          raise ValueError(
              "This estimator does not support `postprocessing_defences`."
          )
  
      # Set cpu/gpu device
      self._device: torch.device
      if device_type == "cpu" or not torch.cuda.is_available():
          self._device = torch.device("cpu")
      else:  # pragma: no cover
          cuda_idx = torch.cuda.current_device()
          self._device = torch.device(f"cuda:{cuda_idx}")
  
      self._input_shape = None
  
      # Load model
      if model is None:
          if self._version == 2:
              if pretrained_model == "an4":  # pragma: no cover
                  filename, url = (
                      "an4_pretrained_v2.pth",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/v2.0/an4_pretrained_v2.pth",
                  )
  
              elif pretrained_model == "librispeech":
                  filename, url = (
                      "librispeech_pretrained_v2.pth",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/v2.0/"
                      "librispeech_pretrained_v2.pth",
                  )
  
              elif pretrained_model == "tedlium":  # pragma: no cover
                  filename, url = (
                      "ted_pretrained_v2.pth",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/v2.0/ted_pretrained_v2.pth",
                  )
  
              elif pretrained_model is None:  # pragma: no cover
                  # If model is None and no pretrained model is selected, then we need to have parameters filename
                  # and url to download, extract and load the automatic speech recognition model
                  if filename is None or url is None:
                      filename, url = (
                          "librispeech_pretrained_v2.pth",
                          "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/v2.0/"
                          "librispeech_pretrained_v2.pth",
                      )
  
              else:  # pragma: no cover
                  raise ValueError(
                      f"The input pretrained model {pretrained_model} is not supported."
                  )
  
              # Download model
              model_path = get_file(
                  filename=filename,
                  path=config.ART_DATA_PATH,
                  url=url,
                  extract=False,
                  verbose=self.verbose,
              )
  
              # Then load model
              self._model = load_model(
                  device=self._device,
                  model_path=model_path,
                  use_half=use_half,
              )
  
          else:
              if pretrained_model == "an4":  # pragma: no cover
                  filename, url = (
                      "an4_pretrained_v3.ckpt",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/V3.0/an4_pretrained_v3.ckpt",
                  )
  
              elif pretrained_model == "librispeech":
                  filename, url = (
                      "librispeech_pretrained_v3.ckpt",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/V3.0/"
                      "librispeech_pretrained_v3.ckpt",
                  )
  
              elif pretrained_model == "tedlium":  # pragma: no cover
                  filename, url = (
                      "ted_pretrained_v3.ckpt",
                      "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/V3.0/ted_pretrained_v3.ckpt",
                  )
  
              elif pretrained_model is None:  # pragma: no cover
                  # If model is None and no pretrained model is selected, then we need to have parameters filename and
                  # url to download, extract and load the automatic speech recognition model
                  if filename is None or url is None:
                      filename, url = (
                          "librispeech_pretrained_v3.ckpt",
                          "https://github.com/SeanNaren/deepspeech.pytorch/releases/download/V3.0/"
                          "librispeech_pretrained_v3.ckpt",
                      )
  
              else:  # pragma: no cover
                  raise ValueError(
                      f"The input pretrained model {pretrained_model} is not supported."
                  )
  
              # Download model
              model_path = get_file(
                  filename=filename,
                  path=config.ART_DATA_PATH,
                  url=url,
                  extract=False,
                  verbose=self.verbose,
              )
  
              # Then load model
              self._model = load_model(
                  device=self._device, model_path=model_path
              )
  
      else:
          self._model = model
  
          # Push model to the corresponding device
          self._model.to(self._device)
  
      # Set the loss function
      if self._version == 2:
          from warpctc_pytorch import CTCLoss
  
          self.criterion = CTCLoss()
      else:
          self.criterion = self._model.criterion
  
      # Save first version of the optimizer
      self._optimizer = optimizer
      self._use_amp = use_amp
      self._opt_level = opt_level
  
      # Now create a decoder
      # Create the language model config first
      lm_config = LMConfig()
  
      # Then setup the config
      if decoder_type == "greedy":
          lm_config.decoder_type = DecoderType.greedy
      elif decoder_type == "beam":
          lm_config.decoder_type = DecoderType.beam
      else:
          raise ValueError(
              f"Decoder type {decoder_type} currently not supported."
          )
  
      lm_config.lm_path = lm_path
      lm_config.top_paths = top_paths
      lm_config.alpha = alpha
      lm_config.beta = beta
      lm_config.cutoff_top_n = cutoff_top_n
      lm_config.cutoff_prob = cutoff_prob
      lm_config.beam_width = beam_width
      lm_config.lm_workers = lm_workers
      self.lm_config = lm_config
  
      # Create the decoder with the lm config
      self.decoder = load_decoder(
          labels=self._model.labels, cfg=lm_config
      )
  
      # Setup for AMP use
      if self.use_amp:  # pragma: no cover
          from apex import amp  # pylint: disable=E0611
  
          if self.optimizer is None:
              logger.warning(
                  "An optimizer is needed to use the automatic mixed precision tool, but none for provided. "
                  "A default optimizer is used."
              )
  
              # Create the optimizers
              parameters = self._model.parameters()
              self._optimizer = torch.optim.SGD(parameters, lr=0.01)
  
          if self._device.type == "cpu":
              enabled = False
          else:
              enabled = True
  
          self._model, self._optimizer = amp.initialize(
              models=self._model,
              optimizers=self._optimizer,
              enabled=enabled,
              opt_level=opt_level,
              loss_scale=1.0,
          )
  
imports: import logging
  from typing import TYPE_CHECKING, List, Optional, Tuple, Union
  from pkg_resources import packaging
  import numpy
  from art import config
  from art.estimators.pytorch import PyTorchEstimator
  from art.estimators.speech_recognition.speech_recognizer import SpeechRecognizerMixin, PytorchSpeechRecognizerMixin
  from art.utils import get_file
label_desc: Use image node_id 1 to create a new PyTorchDeepSpeech object from inherited base classes: PytorchSpeechRecognizerMixin, SpeechRecognizerMixin, PyTorchEstimator with example: obj = PyTorchDeepSpeech(model, pretrained_model, filename, url, use_half, optimizer, use_amp, opt_level, decoder_type, lm_path, top_paths, alpha, beta, cutoff_top_n, cutoff_prob, beam_width, lm_workers, clip_values, preprocessing_defences, postprocessing_defences, preprocessing, device_type, verbose)