-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
215 lines (170 loc) · 8.18 KB
/
test.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
205
206
207
208
209
210
211
212
213
214
215
# import PyTorch
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.tensorboard import SummaryWriter
# import python library
import os
import random
import numpy as np
import argparse
import zlib
import copy
import sys
import yaml
import time
from random import shuffle
from tqdm import tqdm
# import local library
import models
from fl_utils import (adjust_learning_rate, set_model, update_model, compute_client_gradients,
VirtualWorker, loss_prox, _zero_weights, adjust_gradient_by_scaffold, update_client_state, update_server_state)
from utils import AverageMeter, Statistics, accuracy, Parser, LearningScheduler, UpdateScheduler, Cifar100_FL_Dataset, EMNIST_FL_Dataset
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-cfg', '--cfg', default=None, type=str, required=True)
parser.add_argument('-seed', '--seed', default=None)
parser.add_argument('-data-path', '--data-path', default='/p/home/jusers/wang34/juwels/hai_tfda_wp_2_2/huipo/datasets', type=str)
parser.add_argument('-download', '--download', action='store_true')
parser.add_argument('-save_path', '--save_path', default='./saves', type=str)
# if start-epoch != 1, load the pretrained model
parser.add_argument('-start-epoch', '--start-epoch', default=1, type=int)
parser.add_argument('-start-model', '--start-model', default='./saves/model_last.tar', type=str)
args = parser.parse_args()
with open(args.cfg, 'r') as stream:
settings = yaml.safe_load(stream)
args = Parser(args, settings)
args.name = os.path.basename(args.cfg).split('.')[0]
# used for keeping all model weights and the configuration file, etc.
args.train_dir = os.path.join(args.save_path, args.name)
if not os.path.exists(args.train_dir):
os.makedirs(args.train_dir)
print(args)
return args
def prepare_data(args, use_cuda):
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
split_in = False
if args.dataset == 'cifar10':
transform_train = transforms.Compose([
transforms.RandomCrop(
size=32,
padding=4),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(
brightness=0.4,
contrast=0.4,
saturation=0.4),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010) ),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = datasets.CIFAR10(args.data_path, train=True, transform=transform_train, download=args.download)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR10(args.data_path, train=False, transform=transform_test, download=args.download),
batch_size=args.test_batch_size, shuffle=False, **kwargs)
elif args.dataset == 'cifar100':
split_in = True
transform_train = transforms.Compose([
transforms.RandomCrop(
size=24,
padding=0),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(
brightness=0.4,
contrast=0.4,
saturation=0.4),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010) ),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = []
for i in range(args.n_client):
dset_tmp = Cifar100_FL_Dataset(args.data_path, i, transform=transform_train)
trainset.append(dset_tmp)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR100(args.data_path, train=False, transform=transform_test, download=args.download),
batch_size=args.test_batch_size, shuffle=False, **kwargs)
elif args.dataset == 'mnist':
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
])
trainset = datasets.MNIST(args.data_path, train=True, transform=transform_train, download=args.download)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(args.data_path, train=False, transform=transform_test, download=args.download),
batch_size=args.test_batch_size, shuffle=False, **kwargs)
elif args.dataset == 'emnist':
split_in = True
data_num = np.load(f"{args.data_path}/EMNIST/num.npy").astype(np.uint)
data_start = np.array([0] + list(np.load(f"{args.data_path}/EMNIST/num.npy"))).astype(np.uint)
for i in range(1,len(data_start)):
data_start[i] = data_start[i] + data_start[i-1]
train_data_ubyte = idx2numpy.convert_from_file(f"{args.data_path}/EMNIST/emnist-byclass-train-images-idx3-ubyte")
train_label_ubyte = idx2numpy.convert_from_file(f"{args.data_path}/EMNIST/emnist-byclass-train-labels-idx1-ubyte")
test_data_ubyte = idx2numpy.convert_from_file(f"{args.data_path}/EMNIST/emnist-byclass-test-images-idx3-ubyte")
test_label_ubyte = idx2numpy.convert_from_file(f"{args.data_path}/EMNIST/emnist-byclass-test-labels-idx1-ubyte")
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
test_loader = torch.utils.data.DataLoader(
EMNIST_FL_Dataset(test_data_ubyte[:77483], test_label_ubyte[:77483], transform=transform_test ),
batch_size=args.test_batch_size, shuffle=True, **kwargs)
trainset = []
for i in range(args.n_client):
dset_tmp = EMNIST_FL_Dataset(train_data_ubyte[data_start[i]:data_start[i]+data_num[i]], train_label_ubyte[data_start[i]:data_start[i]+data_num[i]], transform=transform_train )
trainset.append(dset_tmp)
else:
raise NotImplementedError()
return trainset, test_loader, split_in
def test(args, model, device, test_loader, result):
model.eval()
correct = [0 for _ in range(args.num_stages)]
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model.dense_forward(data)
for i in range(args.num_stages):
pred = output[i].argmax(1, keepdim=True) # get the index of the max log-probability
correct[i] += pred.eq(target.view_as(pred)).sum().item()
for i in range(args.num_stages):
print('Stage {i} Test set: Accuracy: {}/{} ({:.2f}%)\n'.format(
correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def main(args):
use_cuda = True if torch.cuda.is_available() else False
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
torch.manual_seed(args.seed)
random.seed(args.seed)
# data
trainset, test_loader, split_in = prepare_data(args, use_cuda)
Network = getattr(models, args.arch)
model_server = Network(args).to(device)
n_param_model = 0
for parameter in model_server.parameters(): n_param_model += parameter.nelement()
print("# of model parameters: %d"%n_param_model)
if args.start_epoch != 1:
model_load_tmp = torch.load(args.start_model)
model.load_state_dict(model_load_tmp["state_dict"])
model_server.load_state_dict(model_load_tmp["state_dict"])
result = list(model_load_tmp["result"].numpy()[:-1])
test(args, model_server, device, test_loader, result)
if __name__ == '__main__':
args = parse_args()
main(args)