-
Notifications
You must be signed in to change notification settings - Fork 1
/
fedbn_data_utils.py
117 lines (91 loc) · 4.68 KB
/
fedbn_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
import sys, os
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)
import numpy as np
import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms
from PIL import Image
import os
class DigitsDataset(Dataset):
def __init__(self, data_path, channels, percent=0.1, filename=None, train=True, transform=None):
if filename is None:
if train:
if percent >= 0.1:
for part in range(int(percent*10)):
if part == 0:
self.images, self.labels = np.load(os.path.join(data_path, 'partitions/train_part{}.pkl'.format(part)), allow_pickle=True)
else:
images, labels = np.load(os.path.join(data_path, 'partitions/train_part{}.pkl'.format(part)), allow_pickle=True)
self.images = np.concatenate([self.images,images], axis=0)
self.labels = np.concatenate([self.labels,labels], axis=0)
else:
self.images, self.labels = np.load(os.path.join(data_path, 'partitions/train_part0.pkl'), allow_pickle=True)
data_len = int(self.images.shape[0] * percent*10)
self.images = self.images[:data_len]
self.labels = self.labels[:data_len]
else:
self.images, self.labels = np.load(os.path.join(data_path, 'test.pkl'), allow_pickle=True)
else:
self.images, self.labels = np.load(os.path.join(data_path, filename), allow_pickle=True)
self.transform = transform
self.channels = channels
self.labels = self.labels.astype(np.long).squeeze()
def __len__(self):
return self.images.shape[0]
def __getitem__(self, idx):
image = self.images[idx]
label = self.labels[idx]
if self.channels == 1:
image = Image.fromarray(image, mode='L')
elif self.channels == 3:
image = Image.fromarray(image, mode='RGB')
else:
raise ValueError("{} channel is not allowed.".format(self.channels))
if self.transform is not None:
image = self.transform(image)
label = torch.tensor(label, dtype=torch.long)
return image, label
class OfficeDataset(Dataset):
def __init__(self, base_path, site, train=True, transform=None):
if train:
self.paths, self.text_labels = np.load('{}/office_caltech_10/{}_train.pkl'.format(base_path, site), allow_pickle=True)
else:
self.paths, self.text_labels = np.load('{}/office_caltech_10/{}_test.pkl'.format(base_path, site), allow_pickle=True)
label_dict={'back_pack':0, 'bike':1, 'calculator':2, 'headphones':3, 'keyboard':4, 'laptop_computer':5, 'monitor':6, 'mouse':7, 'mug':8, 'projector':9}
self.labels = [label_dict[text] for text in self.text_labels]
self.transform = transform
self.base_path = base_path if base_path is not None else '../data'
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
img_path = os.path.join(self.base_path, self.paths[idx])
label = self.labels[idx]
image = Image.open(img_path)
if len(image.split()) != 3:
image = transforms.Grayscale(num_output_channels=3)(image)
if self.transform is not None:
image = self.transform(image)
label = torch.tensor(label, dtype=torch.long)
return image, label
class DomainNetDataset(Dataset):
def __init__(self, base_path, site, train=True, transform=None):
if train:
self.paths, self.text_labels = np.load('../data/DomainNet/{}_train.pkl'.format(site), allow_pickle=True)
else:
self.paths, self.text_labels = np.load('../data/DomainNet/{}_test.pkl'.format(site), allow_pickle=True)
label_dict = {'bird':0, 'feather':1, 'headphones':2, 'ice_cream':3, 'teapot':4, 'tiger':5, 'whale':6, 'windmill':7, 'wine_glass':8, 'zebra':9}
self.labels = [label_dict[text] for text in self.text_labels]
self.transform = transform
self.base_path = base_path if base_path is not None else '../data'
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
img_path = os.path.join(self.base_path, self.paths[idx])
label = self.labels[idx]
image = Image.open(img_path)
if len(image.split()) != 3:
image = transforms.Grayscale(num_output_channels=3)(image)
if self.transform is not None:
image = self.transform(image)
return image, label