-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_model_loading.py
204 lines (178 loc) · 7.88 KB
/
data_model_loading.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import yaml
from yaml.loader import SafeLoader
import torch
import torchvision
import pandas as pd
import numpy as np
import os
from PIL import Image, ImageDraw
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import pickle
import random
import time,json
import copy,sys
from sklearn.preprocessing import label_binarize
from sklearn.metrics import classification_report,auc,roc_curve,precision_recall_fscore_support
import warnings
warnings.filterwarnings("ignore")
def load_dataset(config):
"""
dataset_name
pin_memory
n_clients
n_workers
batch_size
"""
each_client_dataloader = []
dataset = config['dataset']
dataset_path = config['dataset_path']
pin_memory = config['pin_memory']
n_clients = config['n_clients']
n_workers = config['n_workers']
img_size = config['img_size']
batch_size = config['batch_size']
iid = config['iid']
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(img_size),
torchvision.transforms.ToTensor()
])
if dataset == 'cifar10':
if config.get("augment", False):
print("augmenting dataset")
transform = torchvision.transforms.Compose([
transform,
torchvision.transforms.RandomHorizontalFlip(0.6)
])
if config.get('standardize',False):
print("standardizing dataset")
normalize_transform = torchvision.transforms.Normalize([0.4914, 0.4822, 0.4465],[0.2470, 0.2435, 0.2616])
transform = torchvision.transforms.Compose([
transform,
normalize_transform
])
train_data = torchvision.datasets.CIFAR10(dataset_path,train=True,download=True,transform=transform)
elif dataset == "cifar100":
if config.get("augment", False):
print("augmenting dataset")
transform = torchvision.transforms.Compose([
transform,
torchvision.transforms.RandomRotation(10),
torchvision.transforms.RandomHorizontalFlip(0.6)
])
if config.get('standardize',False):
print("standardizing")
normalize_transform = torchvision.transforms.Normalize([0.5071, 0.4867, 0.4408],[0.2675, 0.2565, 0.2761])
transform = torchvision.transforms.Compose([
transform,
normalize_transform
])
train_data = torchvision.datasets.CIFAR100(dataset_path,train=True,download=True,transform=transform)
split_data = len(train_data)
print(len(train_data))
client_distribution = None
if iid:
print("iid data loading")
each_client_data = split_data // n_clients
non_uniform = split_data % n_clients
clients_list = [each_client_data for i in range(n_clients)]
clients_list[-1] = clients_list[-1]+non_uniform
print(clients_list)
clients_list = torch.tensor(clients_list)
client_distribution = copy.copy(clients_list/torch.sum(clients_list))
each_client_data = torch.utils.data.random_split(train_data, clients_list)
else:
print("non iid data loading")
beta = config['beta']
client_list = torch.tensor(beta).repeat(n_clients)
non_iid_dirichlet = (torch.distributions.dirichlet.Dirichlet(client_list).sample()*split_data).type(torch.int64)
remaining_data = split_data - non_iid_dirichlet.sum()
non_iid_dirichlet[-1] += remaining_data
print(non_iid_dirichlet)
client_distribution = non_iid_dirichlet/torch.sum(non_iid_dirichlet)
each_client_data = torch.utils.data.random_split(train_data,non_iid_dirichlet)
for i in range(n_clients):
ci_dataloader = torch.utils.data.DataLoader(
each_client_data[i],
shuffle=True,
batch_size = batch_size,
pin_memory=pin_memory,
num_workers = n_workers
)
each_client_dataloader.append(ci_dataloader)
return each_client_dataloader, client_distribution
def load_dataset_test(config):
"""
dataset_name
pin_memory
n_workers
batch_size
img_size
"""
each_client_dataloader = []
dataset = config['test_dataset']
dataset_path = config['test_dataset_path']
pin_memory = config['pin_memory']
n_workers = config['n_workers']
img_size = config['img_size']
batch_size = config['batch_size']
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(img_size),
torchvision.transforms.ToTensor()
])
if dataset == 'cifar10':
if config.get('standardize',False):
normalize_transform = torchvision.transforms.Normalize([0.4914, 0.4822, 0.4465],[0.2470, 0.2435, 0.2616])
transform = torchvision.transforms.Compose([
transform,
normalize_transform
])
test_data = torchvision.datasets.CIFAR10(dataset_path,train=False,download=True,transform=transform)
elif dataset == "cifar100":
if config.get('standardize',False):
normalize_transform = torchvision.transforms.Normalize([0.5071, 0.4867, 0.4408],[0.2675, 0.2565, 0.2761])
transform = torchvision.transforms.Compose([
transform,
normalize_transform
])
test_data = torchvision.datasets.CIFAR100(dataset_path,train=False,download=True,transform=transform)
test_loader = torch.utils.data.DataLoader(
test_data,
shuffle=True,
batch_size = batch_size,
pin_memory=pin_memory,
num_workers = n_workers
)
return test_loader
def load_model(model_name, nclass, channel=3, pretrained=True):
if model_name == 'Resnet18':
global_model = torchvision.models.resnet18(pretrained=pretrained)
global_model.conv1 = nn.Conv2d(channel, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
global_model.fc = nn.Linear(in_features=512, out_features=nclass, bias=True)
elif model_name == "Resnet34":
global_model = torchvision.models.resnet34(pretrained=pretrained)
global_model.conv1 = nn.Conv2d(channel, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
global_model.fc = nn.Linear(in_features=512, out_features=nclass, bias=True)
elif model_name == 'Resnet50':
global_model = torchvision.models.resnet50(pretrained=pretrained)
global_model.conv1 = nn.Conv2d(channel, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
global_model.fc = nn.Linear(in_features=2048, out_features=nclass, bias=True)
elif model_name == "Mobilenetv2":
global_model = torchvision.models.mobilenet_v2(pretrained=pretrained)
global_model.features[0][0] = nn.Conv2d(channel, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
global_model.classifier[1] = nn.Linear(in_features = 1280, out_features = nclass, bias = True)
elif model_name == "Mobilenetv3":
global_model = torchvision.models.mobilenet_v3_small(pretrained=pretrained)
global_model.features[0][0] = nn.Conv2d(channel, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
global_model.classifier[3] = nn.Linear(in_features = 1024, out_features = nclass, bias = True)
elif model_name == "Shufflenet":
global_model = torchvision.models.shufflenet_v2_x1_0(pretrained = pretrained)
global_model.conv1[0] = nn.Conv2d(channel, 24, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
global_model.fc = nn.Linear(in_features = 1024, out_features = nclass, bias=True)
return global_model
if __name__ == "__main__":
pass