-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
127 lines (99 loc) · 3.16 KB
/
util.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from PIL import Image
from torch.utils.data import Dataset
import os
import torchvision.transforms as transforms
def cycle(iterable):
while True:
for x in iterable:
yield x
class EvalDataset(Dataset):
def __init__(self, data_list, image_dir):
self.data_list = data_list
self.image_dir = image_dir
self.transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
# transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # ImageNet标准化
])
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
item = self.data_list[idx]
# 加载图像
image_path = os.path.join(self.image_dir, item['image'])
image = Image.open(image_path).convert('RGB')
if self.transform:
image = self.transform(image)
# 获取caption
captions = item['caption']
return {
'images': image,
'captions': captions
}
def filter_dataset(dataset):
filtered_annotations = [ann for ann in dataset.annotation if ann['label'] != 'entailment']
dataset.annotation = filtered_annotations
return dataset
def train_collate_fn(batch):
# 定义图像转换
transform = transforms.Compose([
transforms.Resize(256),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
# transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # ImageNet标准化
])
images = []
captions = []
for item in batch:
image = item['image']
# caption = item['text_input']
# 转换图像
if isinstance(image, Image.Image):
image = transform(image)
images.append(image)
# captions.append(caption)
# 将图像堆叠成一个批次
images = torch.stack(images)
return {
'image': images,
'caption': None
}
def eval_collate_fn(batch):
# 定义图像转换
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
# transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # ImageNet标准化
])
images = []
captions = []
for item in batch:
image = item['image']
caption = item['text_input']
# 转换图像
if isinstance(image, Image.Image):
image = transform(image)
images.append(image)
captions.append(caption)
# 将图像堆叠成一个批次
images = torch.stack(images)
return {
'image': images,
'caption': captions
}
def custom_collate(batch):
images = torch.stack([item['images'] for item in batch])
captions = [item['captions'] for item in batch]
# 获取每个样本的caption数量
caption_lengths = [len(c) for c in captions]
return {
'images': images,
'captions': captions,
'caption_lengths': caption_lengths
}