forked from Swastik3/DenseTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
205 lines (171 loc) · 7.53 KB
/
dataloader.py
File metadata and controls
205 lines (171 loc) · 7.53 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import torch
from torch.utils.data import Dataset, DataLoader, Subset, DistributedSampler
from torchvision import transforms
from PIL import Image
import pickle
import random
class CustomDataset(Dataset):
def __init__(self, image_dir, label_file, transform=None, cache_file='valid_indices_cache.pkl', shuffle=True):
self.image_dir = image_dir
self.transform = transform
self.cache_file = cache_file
with open(label_file, 'r', encoding='utf-8') as f:
self.labels = f.readlines()
self.valid_indices = self._load_or_create_valid_indices()
self.shuffle = True
def _load_or_create_valid_indices(self):
if os.path.exists(self.cache_file):
with open(self.cache_file, 'rb') as f:
valid_indices = pickle.load(f)
else:
valid_indices = self._get_valid_indices()
with open(self.cache_file, 'wb') as f:
pickle.dump(valid_indices, f)
return valid_indices
def _get_valid_indices(self):
valid_indices = []
for idx in range(len(self.labels)):
img_name = os.path.join(self.image_dir, f"{idx:07d}.png")
if os.path.exists(img_name):
valid_indices.append(idx)
return valid_indices
def __len__(self):
return len(self.valid_indices)
def __getitem__(self, idx):
actual_idx = self.valid_indices[idx]
img_name = os.path.join(self.image_dir, f"{actual_idx:07d}.png")
image = Image.open(img_name).convert('RGB')
label = self.labels[actual_idx].strip()
image = self.resize_and_pad(image, 800, 400)
if self.transform:
image = self.transform(image)
else:
transform = transforms.Compose([
transforms.ToTensor()
])
image = transform(image)
return image, label
def resize_and_pad(self, image, target_width, target_height):
# Calculate the ratio to maintain the aspect ratio
original_width, original_height = image.size
ratio = min(target_width / original_width, target_height / original_height)
# Resize the image while maintaining the aspect ratio
new_size = (int(original_width * ratio), int(original_height * ratio))
resized_image = image.resize(new_size, Image.LANCZOS)
# Create a new image with the specified target size and a white background
new_image = Image.new("RGB", (target_width, target_height), (255, 255, 255))
# Calculate the position to paste the resized image on the white background
paste_x = (target_width - new_size[0]) // 2
paste_y = (target_height - new_size[1]) // 2
# Paste the resized image onto the white background
new_image.paste(resized_image, (paste_x, paste_y))
return new_image
class CustomDataLoader:
def __init__(self, image_dir, label_file, process_rank, num_processes, transform=None, cache_file='valid_indices_cache.pkl', shuffle=True, batch_size=1, num_workers=1, sampler=None):
self.dataset = CustomDataset(image_dir=image_dir, label_file=label_file, transform=transform, cache_file=cache_file)
self.batch_size = batch_size
self.num_workers = num_workers
self.shuffle = shuffle
self.process_rank = process_rank
self.num_processes = num_processes
# Create a DistributedSampler for multi-GPU training
self.sampler = torch.utils.data.distributed.DistributedSampler(
self.dataset,
num_replicas=self.num_processes,
rank=self.process_rank,
shuffle=self.shuffle)
def __len__(self):
return len(self.dataset)
def __iter__(self):
if self.sampler is not None:
dataloader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers,
sampler=self.sampler, pin_memory=True, )
else:
dataloader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=self.shuffle, num_workers=self.num_workers, pin_memory=True)
return iter(dataloader)
def get_epoch(self):
return self.sampler.epoch
def set_epoch(self, epoch):
self.sampler.set_epoch(epoch)
class SubsetCustomDataLoader:
def __init__(self, image_dir, label_file, process_rank, num_processes, subset_size,
transform=None, cache_file='valid_indices_cache.pkl', shuffle=True,
sampler=None,
batch_size=1, num_workers=1, seed=42):
# Initialize the full dataset
self.full_dataset = CustomDataset(image_dir=image_dir, label_file=label_file,
transform=transform, cache_file=cache_file)
# Create a subset of the dataset
total_size = len(self.full_dataset)
subset_size = min(subset_size, total_size)
# Use a fixed seed for reproducibility
random.seed(seed)
subset_indices = random.sample(range(total_size), subset_size)
self.dataset = Subset(self.full_dataset, subset_indices)
self.batch_size = batch_size
self.num_workers = num_workers
self.shuffle = shuffle
self.process_rank = process_rank
self.num_processes = num_processes
# Create a DistributedSampler for multi-GPU training
self.sampler = DistributedSampler(
self.dataset,
num_replicas=self.num_processes,
rank=self.process_rank,
shuffle=self.shuffle
)
def __len__(self):
return len(self.dataset)
def __iter__(self):
if self.sampler is not None:
dataloader = DataLoader(
self.dataset,
batch_size=self.batch_size,
shuffle=False, # Shuffle is handled by DistributedSampler
num_workers=self.num_workers,
sampler=self.sampler,
pin_memory=True, # This can speed up data transfer to GPU
)
return iter(dataloader)
def get_epoch(self):
return self.sampler.epoch
def set_epoch(self, epoch):
self.sampler.set_epoch(epoch)
# for parallel training
def dist_sampler(ddp, ddp_rank, ddp_world_size):
if ddp:
# Training sampler
train_dataset = CustomDataset(
image_dir='./data/UniMER-1M/images',
label_file='./data/UniMER-1M/train.txt',
cache_file='valid_indices_cache.pkl'
)
train_sampler = DistributedSampler(
train_dataset,
num_replicas=ddp_world_size,
rank=ddp_rank,
shuffle=True
)
# Validation sampler
val_dataset_cpe = CustomDataset(
image_dir='./data/UniMER-Test/cpe/',
label_file='./data/UniMER-Test/cpe.txt',
cache_file='valid_indices_val_cpe.pkl'
)
val_dataset_spe = CustomDataset(
image_dir='./data/UniMER-Test/spe/',
label_file='./data/UniMER-Test/spe.txt',
cache_file='valid_indices_val_spe.pkl'
)
val_dataset = torch.utils.data.ConcatDataset([val_dataset_cpe, val_dataset_spe])
val_sampler = DistributedSampler(
val_dataset,
num_replicas=ddp_world_size,
rank=ddp_rank,
shuffle=False # Usually, we don't shuffle the validation set
)
else:
train_sampler = None
val_sampler = None
return train_sampler, val_sampler