def apply(self, sample: Sample) -> Sample: assert self.data_params.downscale_factor > 0 # Not instantiated yet codec = self.data_params.codec # final preparation if self.mode in {PipelineMode.TRAINING, PipelineMode.EVALUATION}: text = np.array(codec.encode(sample.targets) if sample.targets else np.zeros((0,), dtype="int32")) else: text = None line = sample.inputs # gray or binary input, add missing axis if len(line.shape) == 2: line = np.expand_dims(line, axis=-1) # Validate if the line is valid for training if not self.is_valid_line( text, len(line) // self.data_params.downscale_factor, len(line), sample.meta.get("id", "Unknown Sample ID") ): return sample.new_invalid() if text is not None: sample = sample.new_targets( { "gt": np.asarray(text), "gt_len": np.asarray([len(text)]), "fold_id": np.asarray([sample.meta.get("fold_id", -1)]), } ) return sample.new_inputs({"img": line.astype(np.uint8), "img_len": np.asarray([len(line)])})
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