| 50 | Encoder.tokenizer = build_tokenizer(self.args) |
| 51 | |
| 52 | def encode(self, input_pair): |
| 53 | json_line, img_path = input_pair |
| 54 | data = json.loads(json_line) |
| 55 | key = "text" |
| 56 | text = data[key] |
| 57 | sentence_ids = Encoder.tokenizer.tokenize(text) |
| 58 | pad_len = self.args.pad_length |
| 59 | if len(sentence_ids) > 0 and self.args.append_eod: |
| 60 | sentence_ids = sentence_ids[:pad_len] |
| 61 | current_length = len(sentence_ids) |
| 62 | sentence_ids.extend([Encoder.tokenizer.eod for _ in range(max(0,pad_len-current_length))]) |
| 63 | |
| 64 | with open(img_path, "rb") as tf: |
| 65 | xs = bytearray(tf.read()) |
| 66 | img_pad = (4 - len(xs) % 4) % 4 |
| 67 | xs.extend([0 for _ in range(img_pad)]) |
| 68 | img_raw = np.frombuffer(xs, dtype=np.int32) |
| 69 | img_raw = np.insert(img_raw, 0, img_pad) |
| 70 | |
| 71 | return sentence_ids, img_raw, len(json_line) |
| 72 | |
| 73 | def get_args(): |
| 74 | parser = argparse.ArgumentParser() |