def apply(self, sample: Sample) -> Sample: # Load the image and convert the class name into an index if self.mode in INPUT_PROCESSOR: sample = sample.new_inputs(cv2.imread(sample.inputs)) if self.mode in TARGETS_PROCESSOR: # Only load target if it is (expected to be) present sample = sample.new_targets( self.data_params.classes.index(sample.targets)) return sample
def to_sample(d: dict) -> Sample: return Sample(inputs={ Keys.InputSentence1: d['sentence1'].decode('utf-8'), Keys.InputSentence2: d['sentence2'].decode('utf-8') }, targets={Keys.Target: [d['label']]}, meta={'index': d['idx']})
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: [img.shape[0]] }).new_targets({ Keys.Targets: encoded, Keys.TargetsLength: [len(encoded)] })
def generate(self) -> Iterable[Sample]: # Generate the samples # First flatten all, since shuffling is performed during training (on each epoch anew) # Also shuffle in evaluation (no effect on the accuracy) but random examples will be displayed flat_samples = [] for k, filenames in self.params.image_files.items(): for fn in filenames: # Pass inputs and targets, meta data is optional but can be useful for debugging flat_samples.append(Sample(inputs=fn, targets=k, meta={'filename': fn, 'classname': k})) if self.mode in {PipelineMode.Training, PipelineMode.Evaluation}: shuffle(flat_samples) return flat_samples
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: 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 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))
def apply(self, sample: Sample) -> Sample: sample.outputs['sentence'] = ''.join(self.data_params.codec[i] for i in sample.outputs['decoded'] if i >= 0) return sample
def generate(self) -> Iterable[Sample]: return (Sample(inputs=fn) for fn in self.params.image_files)
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 generate(self) -> Iterable[Sample]: return (Sample(inputs=fn, targets=split_all_ext(fn)[0] + '.gt.txt', meta={'filename': fn}) for fn in self.params.image_files)
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: if self.mode in INPUT_PROCESSOR: sample = sample.new_inputs({Keys.Image: sample.inputs}) if self.mode in TARGETS_PROCESSOR: sample = sample.new_targets({Keys.Target: [sample.targets]}) return sample