shivangibithel commited on
Commit
de8fc8a
1 Parent(s): 72fe4a7

Upload Flickr8k.py

Browse files
Files changed (1) hide show
  1. Flickr8k.py +53 -0
Flickr8k.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import datasets
4
+ import json
5
+ from huggingface_hub import hf_hub_url
6
+
7
+ _INPUT_CSV = 'captions.txt'
8
+ _INPUT_IMAGES = "Images"
9
+ _REPO_ID = "shivangibithel/flickr8k"
10
+ _JSON_KEYS = ['raw', 'sentids']
11
+
12
+ class Dataset(datasets.GeneratorBasedBuilder):
13
+ VERSION = datasets.Version("1.1.0")
14
+ BUILDER_CONFIGS = [
15
+ datasets.BuilderConfig(name="ALL", version=VERSION, description="all"),
16
+ ]
17
+
18
+ def _info(self):
19
+ return datasets.DatasetInfo(
20
+ features=datasets.Features(
21
+ {
22
+ "image": datasets.Image(),
23
+ "caption": [datasets.Value('string')],
24
+ "image_filename": datasets.Value("string"),
25
+ }
26
+ ),
27
+ task_templates=[],
28
+ )
29
+
30
+ def _split_generators(self, dl_manager):
31
+ """Returns SplitGenerators."""
32
+
33
+ repo_id = _REPO_ID
34
+ data_dir = dl_manager.download_and_extract({
35
+ "examples_csv": hf_hub_url(repo_id=repo_id, repo_type='dataset', filename=_INPUT_CSV),
36
+ "images_dir": hf_hub_url(repo_id=repo_id, repo_type='dataset', filename=f"{_INPUT_IMAGES}.zip")
37
+ })
38
+
39
+ return [datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs=data_dir)]
40
+
41
+
42
+ def _generate_examples(self, examples_csv, images_dir):
43
+ """Yields examples."""
44
+ df = pd.read_csv(examples_csv, delimiter=',')
45
+ for c in _JSON_KEYS:
46
+ df[c] = df[c].apply(json.loads)
47
+
48
+ for r_idx, r in df.iterrows():
49
+ r_dict = r.to_dict()
50
+ image_path = os.path.join(images_dir, _INPUT_IMAGES, r_dict['filename'])
51
+ r_dict['image'] = image_path
52
+ r_dict['caption'] = r_dict.pop('raw')
53
+ yield r_idx, r_dict