-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
188 lines (151 loc) · 6.33 KB
/
main.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
import os
import json
from collections import defaultdict
import torch
import random
import numpy as np
from torchvision.models import resnet18
import datasets.ss_transforms as sstr
import datasets.np_transforms as nptr
from torch import nn
from client import Client
from datasets.femnist import Femnist
from server import Server
from utils.args import get_parser
from datasets.idda import IDDADataset
from models.deeplabv3 import deeplabv3_mobilenetv2
from utils.stream_metrics import StreamSegMetrics, StreamClsMetrics
def set_seed(random_seed):
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def get_dataset_num_classes(dataset):
if dataset == 'idda':
return 16
if dataset == 'femnist':
return 62
raise NotImplementedError
def model_init(args):
if args.model == 'deeplabv3_mobilenetv2':
return deeplabv3_mobilenetv2(num_classes=get_dataset_num_classes(args.dataset))
if args.model == 'resnet18':
model = resnet18()
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
model.fc = nn.Linear(in_features=512, out_features=get_dataset_num_classes(args.dataset))
return model
if args.model == 'cnn':
# TODO: missing code here!
raise NotImplementedError
raise NotImplementedError
def get_transforms(args):
# TODO: test your data augmentation by changing the transforms here!
if args.model == 'deeplabv3_mobilenetv2':
train_transforms = sstr.Compose([
sstr.RandomResizedCrop((512, 928), scale=(0.5, 2.0)),
sstr.ToTensor(),
sstr.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
test_transforms = sstr.Compose([
sstr.ToTensor(),
sstr.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
elif args.model == 'cnn' or args.model == 'resnet18':
train_transforms = nptr.Compose([
nptr.ToTensor(),
nptr.Normalize((0.5,), (0.5,)),
])
test_transforms = nptr.Compose([
nptr.ToTensor(),
nptr.Normalize((0.5,), (0.5,)),
])
else:
raise NotImplementedError
return train_transforms, test_transforms
def read_femnist_dir(data_dir):
data = defaultdict(lambda: {})
files = os.listdir(data_dir)
files = [f for f in files if f.endswith('.json')]
for f in files:
file_path = os.path.join(data_dir, f)
with open(file_path, 'r') as inf:
cdata = json.load(inf)
data.update(cdata['user_data'])
return data
def read_femnist_data(train_data_dir, test_data_dir):
return read_femnist_dir(train_data_dir), read_femnist_dir(test_data_dir)
def get_datasets(args):
train_datasets = []
train_transforms, test_transforms = get_transforms(args)
if args.dataset == 'idda':
root = 'data/idda'
with open(os.path.join(root, 'train.json'), 'r') as f:
all_data = json.load(f)
for client_id in all_data.keys():
train_datasets.append(IDDADataset(root=root, list_samples=all_data[client_id], transform=train_transforms,
client_name=client_id))
with open(os.path.join(root, 'test_same_dom.txt'), 'r') as f:
test_same_dom_data = f.read().splitlines()
test_same_dom_dataset = IDDADataset(root=root, list_samples=test_same_dom_data, transform=test_transforms,
client_name='test_same_dom')
with open(os.path.join(root, 'test_diff_dom.txt'), 'r') as f:
test_diff_dom_data = f.read().splitlines()
test_diff_dom_dataset = IDDADataset(root=root, list_samples=test_diff_dom_data, transform=test_transforms,
client_name='test_diff_dom')
test_datasets = [test_same_dom_dataset, test_diff_dom_dataset]
elif args.dataset == 'femnist':
niid = args.niid
train_data_dir = os.path.join('data', 'femnist', 'data', 'niid' if niid else 'iid', 'train')
test_data_dir = os.path.join('data', 'femnist', 'data', 'niid' if niid else 'iid', 'test')
train_data, test_data = read_femnist_data(train_data_dir, test_data_dir)
train_transforms, test_transforms = get_transforms(args)
train_datasets, test_datasets = [], []
for user, data in train_data.items():
train_datasets.append(Femnist(data, train_transforms, user))
for user, data in test_data.items():
test_datasets.append(Femnist(data, test_transforms, user))
else:
raise NotImplementedError
return train_datasets, test_datasets
def set_metrics(args):
num_classes = get_dataset_num_classes(args.dataset)
if args.model == 'deeplabv3_mobilenetv2':
metrics = {
'eval_train': StreamSegMetrics(num_classes, 'eval_train'),
'test_same_dom': StreamSegMetrics(num_classes, 'test_same_dom'),
'test_diff_dom': StreamSegMetrics(num_classes, 'test_diff_dom')
}
elif args.model == 'resnet18' or args.model == 'cnn':
metrics = {
'eval_train': StreamClsMetrics(num_classes, 'eval_train'),
'test': StreamClsMetrics(num_classes, 'test')
}
else:
raise NotImplementedError
return metrics
def gen_clients(args, train_datasets, test_datasets, model):
clients = [[], []]
for i, datasets in enumerate([train_datasets, test_datasets]):
for ds in datasets:
clients[i].append(Client(args, ds, model, test_client=i == 1))
return clients[0], clients[1]
def main():
parser = get_parser()
args = parser.parse_args()
set_seed(args.seed)
print(f'Initializing model...')
model = model_init(args)
model.cuda()
print('Done.')
print('Generate datasets...')
train_datasets, test_datasets = get_datasets(args)
print('Done.')
metrics = set_metrics(args)
train_clients, test_clients = gen_clients(args, train_datasets, test_datasets, model)
server = Server(args, train_clients, test_clients, model, metrics)
server.train()
if __name__ == '__main__':
main()