-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader_v3.py
63 lines (50 loc) · 2.15 KB
/
data_loader_v3.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
import os
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image
"""
args: path
"""
# train transformer
train_transformer = transforms.Compose([
transforms.Resize(256),
transforms.RandomCrop(256),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
# evl and test transformer
eval_transformer = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor()])
class BuildingDataset(Dataset):
def __init__(self, data_dir, transform):
self.transform = transform
self.images = []
self.labels = []
self.root = os.listdir(data_dir)
self.filenames = [os.path.join(data_dir, f) for f in self.root]
for i, file in enumerate(self.filenames):
for image in os.listdir(file):
src = os.path.join(file, image)
self.images.append(src)
self.labels.append(i)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = Image.open(self.images[idx])
image = self.transform(image)
return image, self.labels[idx]
# load a train, val, text in mini-batch size
def fetch_dataloader(types, data_dir, params, device='cpu'):
dataloaders = {}
for split in ['Building_labeled_train_data', 'Building_labeled_val_data', 'Building_labeled_test_data']:
if split in types:
path = os.path.join(data_dir, "{}".format(split))
# use the train_transformer if training data, else use eval_transformer without random flip
if split == 'train':
dl = DataLoader(BuildingDataset(path, train_transformer), batch_size=params.batch_size, shuffle=True,
num_workers=params.num_workers, pin_memory=params.cuda)
else:
dl = DataLoader(BuildingDataset(path, eval_transformer), batch_size=params.batch_size, shuffle=False,
num_workers=params.num_workers, pin_memory=params.cuda)
dataloaders[split] = dl
return dataloaders