def apply(self, sample: Sample) -> Sample: if sample.targets and 'gt' in sample.targets: sample.targets['sentence'] = "".join( self.params.codec.decode(sample.targets['gt'])) if sample.outputs: outputs = self.ctc_decoder.decode( sample.outputs['softmax'].astype(float)) outputs.labels = list(map(int, outputs.labels)) outputs.sentence = "".join(self.params.codec.decode( outputs.labels)) sample = sample.new_outputs(outputs) return sample
def apply(self, sample: Sample) -> Sample: if sample.targets and 'gt' in sample.targets: sample.targets['sentence'] = "".join(self.params.codec.decode(sample.targets['gt'])) if sample.outputs: def decode(suffix): outputs = self.ctc_decoder.decode(sample.outputs['softmax' + suffix].astype(float)) outputs.labels = list(map(int, outputs.labels)) outputs.sentence = "".join(self.params.codec.decode(outputs.labels)) return outputs outputs = decode("") outputs.voter_predictions = [] for i in range(self.params.ensemble_): outputs.voter_predictions.append(decode(f"_{i}")) sample = sample.new_outputs(outputs) return sample
def apply(self, sample: Sample) -> Sample: targets: str = sample.targets outputs: str = sample.outputs meta = sample.meta if isinstance(outputs, Prediction): prediction: Prediction = outputs prediction.sentence = self._apply_single(prediction.sentence, meta) return sample elif isinstance(targets, dict) and 'sentence' in targets: targets['sentence'] = self._apply_single(targets['sentence'], meta) return sample elif isinstance(outputs, dict) and 'sentence' in outputs: outputs['sentence'] = self._apply_single(outputs['sentence'], meta) return sample else: if targets: sample = sample.new_targets(self._apply_single(targets, meta)) if outputs: sample = sample.new_outputs(self._apply_single(outputs, meta)) return sample