-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsynthesizer.py
47 lines (34 loc) · 1.31 KB
/
synthesizer.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
from tasks.batch import Batch
from tasks.task import Task
from utils.parameters import Params
class Synthesizer:
params: Params
task: Task
def __init__(self, task: Task):
self.task = task
self.params = task.params
def make_backdoor_batch(self, batch: Batch, test=False, attack=True) -> Batch:
# Don't attack if only normal loss task.
if (not attack) or (self.params.loss_tasks == ['normal'] and not test):
return batch
if test:
attack_portion = batch.batch_size
else:
attack_portion = round(
batch.batch_size * self.params.poisoning_proportion)
backdoored_batch = batch.clone()
self.apply_backdoor(backdoored_batch, attack_portion)
return backdoored_batch
def apply_backdoor(self, batch, attack_portion):
"""
Modifies only a portion of the batch (represents batch poisoning).
:param batch:
:return:
"""
self.synthesize_inputs(batch=batch, attack_portion=attack_portion)
self.synthesize_labels(batch=batch, attack_portion=attack_portion)
return
def synthesize_inputs(self, batch, attack_portion=None):
raise NotImplemented
def synthesize_labels(self, batch, attack_portion=None):
raise NotImplemented