Upload chinese_food_caption.py
Browse files- chinese_food_caption.py +82 -0
chinese_food_caption.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import json
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
from datasets.tasks import QuestionAnsweringExtractive
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
logger = datasets.logging.get_logger(__name__)
|
10 |
+
|
11 |
+
|
12 |
+
_CITATION = """\
|
13 |
+
@article{tobedetermined,
|
14 |
+
author = {Zihao},
|
15 |
+
title = "{Food Images}",
|
16 |
+
journal = {Nah},
|
17 |
+
year = 2022,
|
18 |
+
eid = {arXiv:Nah},
|
19 |
+
pages = {arXiv:Nah},
|
20 |
+
archivePrefix = {arXiv},
|
21 |
+
eprint = {Nah},
|
22 |
+
}
|
23 |
+
"""
|
24 |
+
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
For finetunning stable diffuser with chinese food images
|
27 |
+
"""
|
28 |
+
|
29 |
+
_URL = "https://huggingface.co/datasets/zmao/chinese_food_caption/resolve/main/chinese_food_caption.tar.gz"
|
30 |
+
|
31 |
+
|
32 |
+
class FoodCaption(datasets.GeneratorBasedBuilder):
|
33 |
+
"""Chinese_food image and captions Dataset. Version 1.1."""
|
34 |
+
|
35 |
+
def _info(self):
|
36 |
+
return datasets.DatasetInfo(
|
37 |
+
description=_DESCRIPTION,
|
38 |
+
features=datasets.Features(
|
39 |
+
{
|
40 |
+
"text": datasets.Value("string"),
|
41 |
+
"image": datasets.Image(),
|
42 |
+
|
43 |
+
}
|
44 |
+
),
|
45 |
+
# No default supervised_keys (as we have to pass both question
|
46 |
+
# and context as input).
|
47 |
+
supervised_keys=None,
|
48 |
+
homepage="https://huggingface.co/datasets/zmao/food_img_caption",
|
49 |
+
citation=_CITATION,
|
50 |
+
)
|
51 |
+
|
52 |
+
def _split_generators(self, dl_manager):
|
53 |
+
path = dl_manager.download(_URL)
|
54 |
+
image_iters = dl_manager.iter_archive(path)
|
55 |
+
|
56 |
+
return [
|
57 |
+
datasets.SplitGenerator(
|
58 |
+
name=datasets.Split.TRAIN,
|
59 |
+
gen_kwargs={
|
60 |
+
"images": image_iters
|
61 |
+
}
|
62 |
+
),
|
63 |
+
]
|
64 |
+
|
65 |
+
def _generate_examples(self, images):
|
66 |
+
"""This function returns the examples in the raw (text) form."""
|
67 |
+
idx = 0
|
68 |
+
|
69 |
+
#iterate through images:
|
70 |
+
for filepath, image in images:
|
71 |
+
|
72 |
+
text = filepath[14:-4]
|
73 |
+
text = text.replace('-',' ')
|
74 |
+
text = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", text)
|
75 |
+
text = text.strip()
|
76 |
+
|
77 |
+
yield idx, {
|
78 |
+
"image" : {"path": filepath, "bytes":image.read()},
|
79 |
+
"text": text
|
80 |
+
}
|
81 |
+
idx += 1
|
82 |
+
|