-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader.py
167 lines (138 loc) · 3.95 KB
/
data_loader.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
167
import os
import random
from glob import glob
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision.transforms as transforms
random.seed(42)
torch.manual_seed(42)
classes = [
"back_pack",
"bike",
"bike_helmet",
"bookcase",
"bottle",
"calculator",
"desk_chair",
"desk_lamp",
"desktop_computer",
"file_cabinet",
"headphones",
"keyboard",
"laptop_computer",
"letter_tray",
"mobile_phone",
"monitor",
"mouse",
"mug",
"paper_notebook",
"pen",
"phone",
"printer",
"projector",
"punchers",
"ring_binder",
"ruler",
"scissors",
"speaker",
"stapler",
"tape_dispenser",
"trash_can",
]
class Amazon(torch.utils.data.Dataset):
def __init__(self, path, transforms=None, batch_size=16):
self.path = path
self.files = glob(os.path.join(path, "**", "*.jpg"), recursive=True)
self.transforms = transforms
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
file = self.files[idx]
img = Image.open(file)
label = file.split(self.path)[-1].split("/images/")[-1].split("/")[0]
label = classes.index(label)
if self.transforms is not None:
img = self.transforms(img)
return img, label
class Webcam(torch.utils.data.Dataset):
def __init__(self, path, transforms=None, batch_size=16):
self.path = path
self.files = glob(os.path.join(path, "**", "*.jpg"), recursive=True)
self.transforms = transforms
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
file = self.files[idx]
img = Image.open(file)
label = file.split(self.path)[-1].split("/images/")[-1].split("/")[0]
label = classes.index(label)
if self.transforms is not None:
img = self.transforms(img)
return img, label
class DSLR(torch.utils.data.Dataset):
def __init__(self, path, transforms=None, batch_size=16):
self.path = path
self.files = glob(os.path.join(path, "**", "*.jpg"), recursive=True)
self.transforms = transforms
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
file = self.files[idx]
img = Image.open(file)
label = file.split(self.path)[-1].split("/images/")[-1].split("/")[0]
label = classes.index(label)
if self.transforms is not None:
img = self.transforms(img)
return img, label
# set up the 6 different loaders
root = "/home/gmvincen/class_work/ece_792/Unsupervised-Domain-Adaptation/data"
transform = transforms.Compose(
[
transforms.Resize(224),
transforms.ToTensor(),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]
)
amazon_source = torch.utils.data.DataLoader(
Amazon(path=os.path.join(root, "amazon"), transforms=transform),
batch_size=16,
shuffle=True,
num_workers=8,
pin_memory=False,
)
amazon_target = torch.utils.data.DataLoader(
Amazon(path=os.path.join(root, "amazon"), transforms=transform),
batch_size=16,
shuffle=False,
num_workers=8,
pin_memory=False,
)
webcam_source = torch.utils.data.DataLoader(
Webcam(path=os.path.join(root, "webcam"), transforms=transform),
batch_size=16,
shuffle=True,
num_workers=8,
pin_memory=False,
)
webcam_target = torch.utils.data.DataLoader(
Webcam(path=os.path.join(root, "webcam"), transforms=transform),
batch_size=16,
shuffle=False,
num_workers=8,
pin_memory=False,
)
dslr_source = torch.utils.data.DataLoader(
DSLR(path=os.path.join(root, "dslr"), transforms=transform),
batch_size=16,
shuffle=True,
num_workers=8,
pin_memory=False,
)
dslr_target = torch.utils.data.DataLoader(
DSLR(path=os.path.join(root, "dslr"), transforms=transform),
batch_size=16,
shuffle=False,
num_workers=8,
pin_memory=False,
)