def main(): print("Loading chinese processor and substitute") chinese_processor = OpenAttack.text_processors.ChineseTextProcessor() chinese_substitute = OpenAttack.substitutes.ChineseCiLinSubstitute() print("New Attacker") attacker = OpenAttack.attackers.PWWSAttacker(processor=chinese_processor, substitute=chinese_substitute, threshold=None) print("Building model") clsf = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0") print("Loading dataset") dataset = datasets.load_dataset( "amazon_reviews_multi", 'zh', split="train[:20]").map(function=dataset_mapping) print("Start attack") options = { "success_rate": True, "fluency": True, "mistake": True, "semantic": True, "levenstein": True, "word_distance": True, "modification_rate": True, "running_time": True, } attack_eval = OpenAttack.attack_evals.ChineseAttackEval(attacker, clsf, **options, num_process=2) attack_eval.eval(dataset, visualize=True, progress_bar=True)
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = datasets.load_dataset( "sst", split="train[:100]").map(function=dataset_mapping) clsf = OpenAttack.loadVictim("BERT.SST").to("cuda:0") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) time_clsf = TimeCalcClsf(clsf) try: st = time.perf_counter() print( OpenAttack.attack_evals.DefaultAttackEval( attacker, time_clsf, num_process=2).eval(dataset, progress_bar=True), time_clsf.total_time, time.perf_counter() - st) except Exception as e: raise e print(e) print("\n")
def main(): victim = OpenAttack.loadVictim("BERT.SST") dataset = datasets.load_dataset( "sst", split="train[:10]").map(function=dataset_mapping) attacker = MyAttacker() attack_eval = OpenAttack.AttackEval(attacker, victim) attack_eval.eval(dataset, visualize=True)
def main(): victim = OpenAttack.loadVictim("BERT.SST") dataset = datasets.load_dataset( "sst", split="train[:20]").map(function=dataset_mapping) attacker = OpenAttack.attackers.PWWSAttacker() attack_eval = OpenAttack.AttackEval(attacker, victim, metrics=[SentenceLength()]) attack_eval.eval(dataset, visualize=True)
def main(): victim = OpenAttack.loadVictim("BERT.SST") # Victim.BiLSTM.SST is a pytorch model which is trained on Dataset.SST. It uses Glove vectors for word representation. # The load operation returns a PytorchClassifier that can be further used for Attacker and AttackEval. dataset = datasets.load_dataset("sst", split="train[:20]").map(function=dataset_mapping) # Dataset.SST.sample is a list of 1k sentences sampled from test dataset of Dataset.SST. attacker = OpenAttack.attackers.GeneticAttacker() # After this step, we’ve initialized a GeneticAttacker and uses the default configuration during attack process. attack_eval = OpenAttack.AttackEval(attacker, victim) # DefaultAttackEval is the default implementation for AttackEval which supports seven basic metrics. attack_eval.eval(dataset, visualize=True, num_workers=4)
def main(): dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: print( OpenAttack.attack_evals.DefaultAttackEval( attacker, clsf, progress_bar=False).eval(dataset)) except Exception as e: print(e) print("\n")
def main(): print("New Attacker") attacker = OpenAttack.attackers.PWWSAttacker(lang="chinese") print("Building model") victim = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0") print("Loading dataset") dataset = datasets.load_dataset( "amazon_reviews_multi", 'zh', split="train[:20]").map(function=dataset_mapping) print("Start attack") attack_eval = OpenAttack.AttackEval(attacker, victim) attack_eval.eval(dataset, visualize=True, progress_bar=True)
def main(): victim = OpenAttack.loadVictim("BERT.SST") # BERT.SST is a pytorch model which is fine-tuned on SST-2. It uses Glove vectors for word representation. # The load operation returns a PytorchClassifier that can be further used for Attacker and AttackEval. dataset = datasets.load_dataset( "sst", split="train[:20]").map(function=dataset_mapping) # We load the sst-2 dataset using `datasets` package, and map the fields. attacker = OpenAttack.attackers.PWWSAttacker() # After this step, we’ve initialized a PWWSAttacker and uses the default configuration during attack process. attack_eval = OpenAttack.AttackEval(attacker, victim) # Use the default implementation for AttackEval which supports seven basic metrics. attack_eval.eval(dataset, visualize=True)
def main(): print("New Attacker") attacker = OpenAttack.attackers.PWWSAttacker(lang="chinese") print("Building model") clsf = OpenAttack.loadVictim("BERT.AMAZON_ZH") print("Loading dataset") dataset = datasets.load_dataset("amazon_reviews_multi",'zh',split="train[:20]").map(function=dataset_mapping) print("Start attack") attack_eval = OpenAttack.AttackEval(attacker, clsf, metrics=[ OpenAttack.metric.Fluency(), OpenAttack.metric.GrammaticalErrors(), OpenAttack.metric.EditDistance(), OpenAttack.metric.ModificationRate() ]) attack_eval.eval(dataset, visualize=True, progress_bar=True)
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = datasets.load_dataset("sst", split="train[:100]").map(function=dataset_mapping) clsf = OpenAttack.loadVictim("BERT.SST") # .to("cuda:0") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: print( OpenAttack.AttackEval(attacker, clsf).eval(dataset, progress_bar=True), ) except Exception as e: raise e print(e) print("\n")
def main(): print("New Attacker") attacker = OpenAttack.attackers.PWWSAttacker() print("Build model") clsf = OpenAttack.loadVictim("BERT.SST") dataset = datasets.load_dataset("sst", split="train[:100]").map(function=dataset_mapping) print("Start attack") attack_eval = OpenAttack.AttackEval( attacker, clsf, metrics=[ OpenAttack.metric.Fluency(), OpenAttack.metric.GrammaticalErrors(), OpenAttack.metric.SemanticSimilarity(), OpenAttack.metric.EditDistance(), OpenAttack.metric.ModificationRate() ] ) attack_eval.eval(dataset, visualize=True, progress_bar=True)
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST").to("cuda:0") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: print( OpenAttack.attack_evals.MultiProcessAttackEval( attacker, clsf, num_process=2, progress_bar=False).eval(dataset)) except Exception as e: raise e print(e) print("\n")
def main(): print("Loading chinese processor and substitute") chinese_processor = OpenAttack.text_processors.ChineseTextProcessor() chinese_substitute = OpenAttack.substitutes.ChineseCiLinSubstitute() print("New Attacker") attacker = OpenAttack.attackers.PWWSAttacker(processor=chinese_processor, substitute=chinese_substitute, threshold=None) print("Building model") clsf = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0") print("Loading dataset") dataset = datasets.load_dataset( "amazon_reviews_multi", 'zh', split="train[:20]").map(function=dataset_mapping) print("Start attack") attack_eval = OpenAttack.attack_evals.ChineseAttackEval(attacker, clsf) attack_eval.eval(dataset, visualize=True, progress_bar=True)
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = datasets.load_dataset("amazon_reviews_multi",'zh',split="train[:5]").map(dataset_mapping) clsf = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0") attackers = get_attackers_on_chinese(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: st = time.perf_counter() print( OpenAttack.AttackEval(attacker, clsf, language="chinese").eval(dataset, progress_bar=True), time.perf_counter() - st ) except Exception as e: raise e print(e) print("\n")
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = datasets.load_dataset( 'amazon_reviews_multi', 'zh', split="train[:100]").map(function=dataset_mapping) clsf = OpenAttack.loadVictim("BERT.AMAZON_ZH").to("cuda:0") attackers = get_attackers_on_chinese(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) time_clsf = TimeCalcClsf(clsf) try: st = time.perf_counter() print( OpenAttack.attack_evals.MultiProcessAttackEval( attacker, time_clsf, num_process=2, progress_bar=False).eval(dataset), time_clsf.total_time, time.perf_counter() - st) except Exception as e: raise e print(e) print("\n")
import OpenAttack dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST") rules = OpenAttack.attackers.SEAAttacker.get_rules(clsf, dataset) triggers = OpenAttack.attackers.UATAttacker.get_triggers( clsf, dataset, word2id=clsf.config["word2id"], embedding=clsf.config["embedding"]) attackers = [ OpenAttack.attackers.FDAttacker(word2id=clsf.config["word2id"], embedding=clsf.config["embedding"]), OpenAttack.attackers.SEAAttacker(rules=rules), OpenAttack.attackers.UATAttacker(triggers=triggers), OpenAttack.attackers.TextBuggerAttacker(), OpenAttack.attackers.TextFoolerAttacker(), OpenAttack.attackers.VIPERAttacker(), OpenAttack.attackers.DeepWordBugAttacker(), OpenAttack.attackers.GANAttacker(), OpenAttack.attackers.GeneticAttacker(), OpenAttack.attackers.HotFlipAttacker(), OpenAttack.attackers.PWWSAttacker(), OpenAttack.attackers.SCPNAttacker(), OpenAttack.attackers.PSOAttacker(), ] for attacker in attackers: print(attacker.__class__.__name__) try: