def apply(self, sample: Sample) -> Sample: inputs = sample.inputs.copy() inputs["img"] = inputs["img"] / 255 if self.params.center: inputs["img"] = (inputs["img"] - 0.5) * 2 return sample.new_inputs(inputs)
def apply(self, sample: Sample) -> Sample: img = sample.inputs.transpose() encoded = [self.data_params.codec.index(c) for c in sample.targets] return sample.new_inputs({ Keys.Image: img, Keys.ImageLength: np.array([img.shape[0]]) }).new_targets({ Keys.Targets: np.array(encoded), Keys.TargetsLength: np.array([len(encoded)]) })
def apply(self, sample: Sample) -> Sample: def encode_sentences(sentence1, sentence2): tokens1 = list(self.tokenizer.tokenize(sentence1)) + [ self.tokenizer.sep_token ] tokens2 = list(self.tokenizer.tokenize(sentence2)) + [ self.tokenizer.sep_token ] return [self.tokenizer.cls_token] + tokens1 + tokens2, [ 0 ] + [0] * len(tokens1) + [1] * len(tokens2) word_ids, type_ids = encode_sentences( sample.inputs[Keys.InputSentence1], sample.inputs[Keys.InputSentence2]) word_ids = self.tokenizer.convert_tokens_to_ids(word_ids) return sample.new_inputs({ Keys.InputWordIds: np.asarray(word_ids), Keys.InputMask: np.full(fill_value=1, shape=[len(word_ids)], dtype=np.int32), Keys.InputTypeIds: np.asarray(type_ids, dtype=np.int32), })
def make_sample(self, file_id: str): sample = Sample( inputs={ k: self.parsed_files[k][file_id] for k in self._input_keys }, targets={ k: self.parsed_files[k][file_id] for k in self._target_keys }, meta={ "id": file_id, **{ k + "_filename": v[file_id] for k, v in self.parsed_files.items() } }, ) if len(sample.inputs) == 1: sample = sample.new_inputs(list(sample.inputs.values())[0]) if len(sample.targets) == 1: sample = sample.new_targets(list(sample.targets.values())[0]) return sample
def apply(self, sample: Sample) -> Sample: return sample.new_inputs( cv2.resize( sample.inputs, (self.data_params.image_height, self.data_params.image_width)))
def apply(self, sample: Sample) -> Sample: return sample.new_inputs(sample.inputs + self.params.v)
def apply(self, sample: Sample) -> Sample: return sample.new_inputs({"n": np.asarray([sample.inputs])}).new_targets( {"n": np.asarray([sample.targets])} )
def apply(self, sample: Sample) -> Sample: img = cv2.imread(sample.inputs, flags=cv2.IMREAD_GRAYSCALE) with open(sample.targets) as f: txt = f.read().strip() return sample.new_inputs(img).new_targets(txt)
def apply(self, sample: Sample) -> Sample: assert self.data_params.height > 0 # Not initialized return sample.new_inputs( scale_to_h(sample.inputs, self.data_params.height))