-
Notifications
You must be signed in to change notification settings - Fork 19
/
data_utils.py
166 lines (135 loc) · 5.98 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
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
from torch.utils.data import Dataset
from PIL import Image
from torchvision import datasets, transforms
import os
import json
import torch
from random_erasing import RandomErasing
class ImageDataset(Dataset):
def __init__(self, imgs, transform = None):
self.imgs = imgs
self.transform = transform
def __len__(self):
return len(self.imgs)
def __getitem__(self, index):
data,label = self.imgs[index]
return self.transform(Image.open(data)), label
class Data():
def __init__(self, datasets, data_dir, batch_size, erasing_p, color_jitter, train_all):
self.datasets = datasets.split(',')
self.batch_size = batch_size
self.erasing_p = erasing_p
self.color_jitter = color_jitter
self.data_dir = data_dir
self.train_all = '_all' if train_all else ''
def transform(self):
transform_train = [
transforms.Resize((256,128), interpolation=3),
transforms.Pad(10),
transforms.RandomCrop((256,128)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
transform_val = [
transforms.Resize(size=(256,128),interpolation=3),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
if self.erasing_p > 0:
transform_train = transform_train + [RandomErasing(probability=self.erasing_p, mean=[0.0, 0.0, 0.0])]
if self.color_jitter:
transform_train = [transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0)] + transform_train
self.data_transforms = {
'train': transforms.Compose(transform_train),
'val': transforms.Compose(transform_val),
}
def preprocess_kd_data(self, dataset):
loader, image_dataset = self.preprocess_one_train_dataset(dataset)
self.kd_loader = loader
def preprocess_one_train_dataset(self, dataset):
"""preprocess a training dataset, construct a data loader.
"""
data_path = os.path.join(self.data_dir, dataset, 'pytorch')
data_path = os.path.join(data_path, 'train' + self.train_all)
image_dataset = datasets.ImageFolder(data_path)
loader = torch.utils.data.DataLoader(
ImageDataset(image_dataset.imgs, self.data_transforms['train']),
batch_size=self.batch_size,
shuffle=True,
num_workers=2,
pin_memory=False)
return loader, image_dataset
def preprocess_train(self):
"""preprocess training data, constructing train loaders
"""
self.train_loaders = {}
self.train_dataset_sizes = {}
self.train_class_sizes = {}
self.client_list = []
for dataset in self.datasets:
self.client_list.append(dataset)
loader, image_dataset = self.preprocess_one_train_dataset(dataset)
self.train_dataset_sizes[dataset] = len(image_dataset)
self.train_class_sizes[dataset] = len(image_dataset.classes)
self.train_loaders[dataset] = loader
print('Train dataset sizes:', self.train_dataset_sizes)
print('Train class sizes:', self.train_class_sizes)
def preprocess_test(self):
"""preprocess testing data, constructing test loaders
"""
self.test_loaders = {}
self.gallery_meta = {}
self.query_meta = {}
for test_dir in self.datasets:
test_dir = 'data/'+test_dir+'/pytorch'
dataset = test_dir.split('/')[1]
gallery_dataset = datasets.ImageFolder(os.path.join(test_dir, 'gallery'))
query_dataset = datasets.ImageFolder(os.path.join(test_dir, 'query'))
gallery_dataset = ImageDataset(gallery_dataset.imgs, self.data_transforms['val'])
query_dataset = ImageDataset(query_dataset.imgs, self.data_transforms['val'])
self.test_loaders[dataset] = {key: torch.utils.data.DataLoader(
dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=8,
pin_memory=True) for key, dataset in {'gallery': gallery_dataset, 'query': query_dataset}.items()}
gallery_cameras, gallery_labels = get_camera_ids(gallery_dataset.imgs)
self.gallery_meta[dataset] = {
'sizes': len(gallery_dataset),
'cameras': gallery_cameras,
'labels': gallery_labels
}
query_cameras, query_labels = get_camera_ids(query_dataset.imgs)
self.query_meta[dataset] = {
'sizes': len(query_dataset),
'cameras': query_cameras,
'labels': query_labels
}
print('Query Sizes:', self.query_meta[dataset]['sizes'])
print('Gallery Sizes:', self.gallery_meta[dataset]['sizes'])
def preprocess(self):
self.transform()
self.preprocess_train()
self.preprocess_test()
self.preprocess_kd_data('cuhk02')
def get_camera_ids(img_paths):
"""get camera id and labels by image path
"""
camera_ids = []
labels = []
for path, v in img_paths:
filename = os.path.basename(path)
if filename[:3]!='cam':
label = filename[0:4]
camera = filename.split('c')[1]
camera = camera.split('s')[0]
else:
label = filename.split('_')[2]
camera = filename.split('_')[1]
if label[0:2]=='-1':
labels.append(-1)
else:
labels.append(int(label))
camera_ids.append(int(camera[0]))
return camera_ids, labels