MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / random_mask

Method random_mask

openrec/preprocess/mdiff_label_encode.py:41–61  ·  view source on GitHub ↗
(self, text)

Source from the content-addressed store, hash-verified

39 self.sample_num = sample_num
40
41 def random_mask(self, text):
42 l = len(text)
43 p_mask = random.random()
44
45 noisy_batch = text[:]
46 masked_indices = [False] * l
47 none_pad_indices = []
48 for i in range(l):
49 if random.random() < p_mask and text[i] != self.dict[self.PAD]:
50 noisy_batch[i] = self.dict[self.MASK]
51 masked_indices[i] = True
52 if text[i] != self.dict[self.PAD]:
53 none_pad_indices.append(i)
54 if noisy_batch[i] == self.dict[self.PAD]:
55 noisy_batch[i] = self.dict[self.MASK]
56
57 if not any(masked_indices) and len(none_pad_indices) > 0:
58 idx = random.choice(none_pad_indices)
59 noisy_batch[idx] = self.dict[self.MASK]
60 masked_indices[idx] = True
61 return noisy_batch, masked_indices
62
63 def full_mask(self, text):
64 noisy_batch = [self.dict[self.MASK]] * (self.max_text_len + 1)

Callers 3

forward_processMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected