forked from PaddlePaddle/PaddleMIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coco_clip.py
84 lines (72 loc) · 2.79 KB
/
coco_clip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
import json
import os
from paddle.utils.download import get_path_from_url
from paddlemix.utils.env import DATA_HOME
from paddlemix.utils.log import logger
__all__ = ["CaptionCLIP"]
from .dataset import DatasetBuilder
class CaptionCLIP(DatasetBuilder):
URL = "https://bj.bcebos.com/v1/paddlenlp/datasets/paddlemix/coco.tar"
META_INFO = collections.namedtuple("META_INFO", ("images", "annotations", "images_md5", "annotations_md5"))
MD5 = ""
SPLITS = {
"train": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_train.json"),
"",
"",
),
"val": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_val.json"),
"",
"",
),
"test": META_INFO(
os.path.join("coco", "images"),
os.path.join("coco", "annotations/coco_karpathy_test.json"),
"",
"",
),
}
def _get_data(self, mode, **kwargs):
logger.info("default dataset root is {}".format(DATA_HOME))
images, annotations, image_hash, anno_hash = self.SPLITS[mode]
image_fullname = os.path.join(DATA_HOME, images)
anno_fullname = os.path.join(DATA_HOME, annotations)
if not os.path.exists(image_fullname) or not os.path.exists(anno_fullname):
get_path_from_url(self.URL, DATA_HOME)
return image_fullname, anno_fullname, mode
def _gen_image_id(self, anno):
img_ids = {}
n = 0
for ann in anno:
img_id = ann["image_id"]
if img_id not in img_ids.keys():
img_ids[img_id] = n
n += 1
return img_ids
def _read(self, filename, *args):
image_root, anno_path, mode = filename
annotations = json.load(open(anno_path, "r"))
for ann in annotations:
image_path = os.path.join(image_root, ann["image"])
yield_data = {"image": image_path}
if mode == "train":
# only train mode has text input
yield_data["text"] = ann["caption"]
yield yield_data