add usage
Browse files
README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
see [read_pyarrow.py](https://gist.github.com/csarron/df712e53c9e0dcaad4eb6843e7a3d51c#file-read_pyarrow-py) for how to read one pyarrow file.
|
| 2 |
+
|
| 3 |
+
example PyTorch dataset:
|
| 4 |
+
|
| 5 |
+
```python
|
| 6 |
+
from torch.utils.data import Dataset
|
| 7 |
+
|
| 8 |
+
class ImageCaptionArrowDataset(Dataset):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
dataset_file,
|
| 12 |
+
tokenizer,
|
| 13 |
+
):
|
| 14 |
+
|
| 15 |
+
import pyarrow as pa
|
| 16 |
+
|
| 17 |
+
data = [pa.ipc.open_file(pa.memory_map(f, "rb")).read_all() for f in glob.glob(dataset_file)]
|
| 18 |
+
self.data = pa.concat_tables(data)
|
| 19 |
+
# do other initialization, like init image preprocessing fn,
|
| 20 |
+
|
| 21 |
+
def __getitem__(self, index):
|
| 22 |
+
# item_id = self.data["id"][index].as_py()
|
| 23 |
+
text = self.data["text"][index].as_py() # get text
|
| 24 |
+
if isinstance(text, list):
|
| 25 |
+
text = random.choice(text)
|
| 26 |
+
|
| 27 |
+
img_bytes = self.data["image"][index].as_py() # get image bytes
|
| 28 |
+
|
| 29 |
+
# do some processing with image and text, return the features
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# img_feat = self.image_bytes_to_tensor(img_bytes)
|
| 33 |
+
# inputs = self.tokenizer(
|
| 34 |
+
# text,
|
| 35 |
+
# padding="max_length",
|
| 36 |
+
# max_length=self.max_text_len,
|
| 37 |
+
# truncation=True,
|
| 38 |
+
# return_token_type_ids=True,
|
| 39 |
+
# return_attention_mask=True,
|
| 40 |
+
# add_special_tokens=True,
|
| 41 |
+
# return_tensors="pt",
|
| 42 |
+
# )
|
| 43 |
+
# input_ids = inputs.input_ids.squeeze(0)
|
| 44 |
+
# attention_mask = inputs.attention_mask.squeeze(0)
|
| 45 |
+
# return {
|
| 46 |
+
# # "item_ids": item_id,
|
| 47 |
+
# "text_ids": input_ids,
|
| 48 |
+
# "input_ids": input_ids,
|
| 49 |
+
# "text_masks": attention_mask,
|
| 50 |
+
# "pixel_values": img_feat,
|
| 51 |
+
# }
|
| 52 |
+
def __len__(self):
|
| 53 |
+
return len(self.data)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
```
|