-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiments_CIFAR10_IID.py
160 lines (138 loc) · 8.23 KB
/
Experiments_CIFAR10_IID.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
#================================= Start of importing required packages and libraries =========================================#
from __future__ import print_function
%matplotlib inline
import numpy as np
import torch
from experiment_federated import *
import random
#================================== End of importing required packages and libraries ==========================================#
#=============================== Defining global variables ========================#
DATASET_NAME = "CIFAR10"
MODEL_NAME = "ResNet18"
DD_TYPE = 'IID'
ALPHA = 1000000
NUM_PEERS = 20 # "number of peers: K"
FRAC_PEERS = 1 #'the fraction of peers: C to bel selected in each round'
SEED = 7 #fixed seed
random.seed(SEED)
CRITERION = nn.CrossEntropyLoss()
GLOBAL_ROUNDS = 100 #"number of rounds of federated model training"
LOCAL_EPOCHS = 3 #"the number of local epochs: E for each peer"
TEST_BATCH_SIZE = 1000
LOCAL_BS = 32 #"local batch size: B for each peer"
LOCAL_LR = 0.01#local learning rate: lr for each peer
LOCAL_MOMENTUM = 0.9 #local momentum for each peer
NUM_CLASSES = 10 # number of classes in an experiment
LABELS_DICT = {'Plane':0,
'Car':1,
'Bird':2,
'Cat':3,
'Deer':4,
'Dog':5,
'Frog':6,
'Horse':7,
'Ship':8,
'Truck':9}
#select the device to work with cpu or gpu
if torch.cuda.is_available():
DEVICE = "cuda"
else:
DEVICE = "cpu"
DEVICE = torch.device(DEVICE)
SOURCE_CLASS = 5 # the source class
TARGET_CLASS = 3 # the target class
CLASS_PER_PEER = 1
SAMPLES_PER_CLASS = 582
RATE_UNBALANCE = 1
# Baseline|: FedAvg-no attacks (FL)
RULE = 'fedavg'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'median'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'rmedian'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'tmean'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'mkrum'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'foolsgold'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)
RULE = 'lfa_defender'
ATTACK_TYPE='label_flipping'
MALICIOUS_BEHAVIOR_RATE = 1
for atr in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]:
run_exp(dataset_name = DATASET_NAME, model_name = MODEL_NAME, dd_type = DD_TYPE, num_peers = NUM_PEERS,
frac_peers = FRAC_PEERS, seed = SEED, test_batch_size = TEST_BATCH_SIZE,
criterion = CRITERION, global_rounds = GLOBAL_ROUNDS, local_epochs = LOCAL_EPOCHS, local_bs = LOCAL_BS,
local_lr = LOCAL_LR, local_momentum = LOCAL_MOMENTUM, labels_dict = LABELS_DICT, device = DEVICE,
attackers_ratio = atr, attack_type=ATTACK_TYPE,
malicious_behavior_rate = MALICIOUS_BEHAVIOR_RATE, rule = RULE,
source_class = SOURCE_CLASS, target_class = TARGET_CLASS,
class_per_peer = CLASS_PER_PEER, samples_per_class = SAMPLES_PER_CLASS,
rate_unbalance = RATE_UNBALANCE, alpha = ALPHA, resume = False)