-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_Utils.py
70 lines (47 loc) · 2.24 KB
/
Data_Utils.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
#this code is modified from 'https://github.com/gumusserv/CLIP-SalGan'
from torch.utils.data import Dataset, DataLoader
from PIL import Image
import random
import torch
import clip
import json
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
class SaliencyDatasetWithText(Dataset):
def __init__(self, image_paths, target_paths, text_sequences, transform=None):
self.image_paths = image_paths
self.target_paths = target_paths
self.text_sequences = []
for i in range(len(image_paths)):
text_tokens = clip.tokenize([text_sequences[i]]).to(device)
with torch.no_grad():
text_features = model.encode_text(text_tokens)
self.text_sequences.append(text_features)
self.transform = transform
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
image = Image.open(self.image_paths[idx]).convert('RGB')
target = Image.open(self.target_paths[idx]).convert('L')
text = self.text_sequences[idx]
if self.transform:
image = self.transform(image)
target = self.transform(target)
text_tensor = torch.tensor(text, dtype=torch.long)
return image, target, text_tensor
def split_dataset(image_paths, target_paths, text_descriptions, train_ratio=0.7, val_ratio=0.15):
combined = list(zip(image_paths, target_paths, text_descriptions))
random.shuffle(combined)
total_images = len(combined)
train_size = int(total_images * train_ratio)
val_size = int(total_images * val_ratio)
train_data = combined[:train_size]
val_data = combined[train_size:train_size + val_size]
test_data = combined[train_size + val_size:]
with open('test_data_list_total.json', 'w') as json_file:
json.dump(test_data, json_file)
return train_data, val_data, test_data
def create_dataloader(data, transform, batch_size = 32, shuffle=True):
image_paths, target_paths, text_descriptions = zip(*data)
dataset = SaliencyDatasetWithText(list(image_paths), list(target_paths), list(text_descriptions), transform=transform)
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle)