-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdata.py
128 lines (113 loc) · 5.67 KB
/
data.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
import numpy as np
import argparse
import os
import torch
import torch.optim
import torch.utils.data
import torchvision
import torchvision.transforms as transforms
from torchvision.transforms import ToTensor, Normalize, Compose, Lambda
def get_data(args: argparse.Namespace):
"""
Load the proper dataset based on the parsed arguments
:param args: The arguments in which is specified which dataset should be used
:return: a 5-tuple consisting of:
- The train data set
- The project data set (usually train data set without augmentation)
- The test data set
- a tuple containing all possible class labels
- a tuple containing the shape (depth, width, height) of the input images
"""
if args.dataset =='CUB-200-2011':
return get_birds(True, './data/CUB_200_2011/dataset/train_corners', './data/CUB_200_2011/dataset/train_crop', './data/CUB_200_2011/dataset/test_full')
if args.dataset == 'CARS':
return get_cars(True, './data/cars/dataset/train', './data/cars/dataset/train', './data/cars/dataset/test')
raise Exception(f'Could not load data set "{args.dataset}"!')
def get_dataloaders(args: argparse.Namespace):
"""
Get data loaders
"""
# Obtain the dataset
trainset, projectset, testset, classes, shape = get_data(args)
c, w, h = shape
# Determine if GPU should be used
cuda = not args.disable_cuda and torch.cuda.is_available()
trainloader = torch.utils.data.DataLoader(trainset,
batch_size=args.batch_size,
shuffle=True,
pin_memory=cuda
)
projectloader = torch.utils.data.DataLoader(projectset,
# batch_size=args.batch_size,
batch_size=int(args.batch_size/4), #make batch size smaller to prevent out of memory errors during projection
shuffle=False,
pin_memory=cuda
)
testloader = torch.utils.data.DataLoader(testset,
batch_size=args.batch_size,
shuffle=False,
pin_memory=cuda
)
print("Num classes (k) = ", len(classes), flush=True)
return trainloader, projectloader, testloader, classes, c
def get_birds(augment: bool, train_dir:str, project_dir: str, test_dir:str, img_size = 224):
shape = (3, img_size, img_size)
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
normalize = transforms.Normalize(mean=mean,std=std)
transform_no_augment = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.ToTensor(),
normalize
])
if augment:
transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomOrder([
transforms.RandomPerspective(distortion_scale=0.2, p = 0.5),
transforms.ColorJitter((0.6,1.4), (0.6,1.4), (0.6,1.4), (-0.02,0.02)),
transforms.RandomHorizontalFlip(),
transforms.RandomAffine(degrees=10, shear=(-2,2),translate=[0.05,0.05]),
]),
transforms.ToTensor(),
normalize,
])
else:
transform = transform_no_augment
trainset = torchvision.datasets.ImageFolder(train_dir, transform=transform)
projectset = torchvision.datasets.ImageFolder(project_dir, transform=transform_no_augment)
testset = torchvision.datasets.ImageFolder(test_dir, transform=transform_no_augment)
classes = trainset.classes
for i in range(len(classes)):
classes[i]=classes[i].split('.')[1]
return trainset, projectset, testset, classes, shape
def get_cars(augment: bool, train_dir:str, project_dir: str, test_dir:str, img_size = 224):
shape = (3, img_size, img_size)
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
normalize = transforms.Normalize(mean=mean,std=std)
transform_no_augment = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.ToTensor(),
normalize
])
if augment:
transform = transforms.Compose([
transforms.Resize(size=(img_size+32, img_size+32)), #resize to 256x256
transforms.RandomOrder([
transforms.RandomPerspective(distortion_scale=0.5, p = 0.5),
transforms.ColorJitter((0.6,1.4), (0.6,1.4), (0.6,1.4), (-0.4,0.4)),
transforms.RandomHorizontalFlip(),
transforms.RandomAffine(degrees=15,shear=(-2,2)),
]),
transforms.RandomCrop(size=(img_size, img_size)), #crop to 224x224
transforms.ToTensor(),
normalize,
])
else:
transform = transform_no_augment
trainset = torchvision.datasets.ImageFolder(train_dir, transform=transform)
projectset = torchvision.datasets.ImageFolder(project_dir, transform=transform_no_augment)
testset = torchvision.datasets.ImageFolder(test_dir, transform=transform_no_augment)
classes = trainset.classes
return trainset, projectset, testset, classes, shape