-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHMNIST.py
136 lines (113 loc) · 4.85 KB
/
CHMNIST.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
import torchvision.transforms as tf
import torch
from PIL import Image
from sklearn.model_selection import train_test_split
import os
from torch.utils.data import DataLoader
import numpy as np
CHMNIST_TRANSFORMS = tf.Compose([
tf.ToTensor(),
tf.Resize((64, 64)),
tf.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
Birds_TRANSFORMS = tf.Compose([
tf.ToTensor(),
tf.Resize((128, 128)),
tf.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
class CHMNIST_client_test(torch.utils.data.Dataset):
def __init__(self, num_client, transform=CHMNIST_TRANSFORMS):
np.random.seed(2021)
self.dataset = CHMNIST_client_allclass(train=False)
self.transform = transform
self.pos_index = np.where(np.array(self.dataset.targets) == num_client)[0]
self.neg_index = np.random.choice(np.where(np.array(self.dataset.targets) != num_client)[0], len(self.pos_index))
# print(np.r_[self.pos_index, self.neg_index])
self.data = np.array(self.dataset.images)[np.r_[self.pos_index, self.neg_index]]
# print(self.data)
self.label = np.array(self.dataset.targets)[np.r_[self.pos_index, self.neg_index]]
def __getitem__(self, item):
img_fn = self.data[item]
label = self.label[item]
img = Image.open(img_fn)
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.pos_index)*2
class CHMNIST_client(torch.utils.data.Dataset):
def __init__(self, num_client,root ='/home/yuchen/Projects/CHMNIST',train=True, download=True, transform = CHMNIST_TRANSFORMS):
# self.root = '/home/yuchen/Projects/BioID'
self.images = []
self.root = root
self.targets = []
self.label = num_client
self.train = train
self.download = download
self.transform = transform
x_train, x_test, y_train, y_test = self._train_test_split()
if self.train:
self._setup_dataset(x_train, y_train)
else:
self._setup_dataset(x_test, y_test)
# self.transforms = Birds_TRANSFORMS_TEST
def _train_test_split(self):
img_names = []
img_label = []
for i, folder_name in enumerate(os.listdir(self.root)):
for j, img_name in enumerate(os.listdir(self.root + '/' +folder_name)):
if int(int(folder_name[0:2])-1) == self.label:
img_names.append(os.path.join(self.root+'/', folder_name, img_name))
img_label.append(self.label)
x_train,x_test, y_train, y_test = train_test_split(img_names, img_label, train_size=0.8,
random_state=1)
return x_train, x_test, y_train, y_test
def _setup_dataset(self, x, y):
self.images = x
self.targets = y
def __getitem__(self, item):
img_fn = self.images[item]
label = self.targets[item]
img = Image.open(img_fn)
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.images)
class CHMNIST_client_allclass(torch.utils.data.Dataset):
def __init__(self, root ='/home/yuchen/Projects/CHMNIST',train=True, download=True, transform = CHMNIST_TRANSFORMS):
# self.root = '/home/yuchen/Projects/BioID'
self.images = []
self.root = root
self.targets = []
self.train = train
self.download = download
self.transform = transform
x_train, x_test, y_train, y_test = self._train_test_split()
if self.train:
self._setup_dataset(x_train, y_train)
else:
self._setup_dataset(x_test, y_test)
# self.transforms = Birds_TRANSFORMS_TEST
def _train_test_split(self):
img_names = []
img_label = []
for i, folder_name in enumerate(os.listdir(self.root)):
for j, img_name in enumerate(os.listdir(self.root + '/' +folder_name)):
img_names.append(os.path.join(self.root+'/', folder_name, img_name))
img_label.append(int(folder_name[0:2])-1)
x_train,x_test, y_train, y_test = train_test_split(img_names, img_label, train_size=0.8,
random_state=1)
return x_train, x_test, y_train, y_test
def _setup_dataset(self, x, y):
self.images = x
self.targets = y
def __getitem__(self, item):
img_fn = self.images[item]
label = self.targets[item]
img = Image.open(img_fn)
if self.transform:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.images)