问题描述
我正在尝试使用在CLeverhans中实现的Elastic-Net算法在分类任务中生成对抗性样本。主要问题是我正在尝试以某种方式使用它,以便在分类时对目标类(与原始类有所不同)获得更高的置信度,但是我无法达到良好的效果。 我要愚弄的系统是DNN,其上有10个类的softmax输出。
例如:
- 给出3类样本,我想生成0类的对抗样本。
- 使用在cLeverhans的ElasticNetMethod中实现的默认超参数,我能够获得成功的攻击,因此分配给对抗性样本的类别变成了类别0,但可信度很低(大约30%)。尝试为超参数使用不同的值时也会发生这种情况。
- 我的目的是获得更高的置信度(至少90%)。
- 对于其他算法,例如“ FGSM”或“ MadryEtAl”,我可以达到此目的,从而创建一个循环,在该循环中应用该算法,直到将样本分类为目标类别且置信度大于90%,但无法将此迭代应用于EAD算法,因为在迭代的每个步骤都会生成第一步生成的对抗样本,而在随后的迭代中它将保持不变。 (我知道可能会发生这种情况,因为该算法与其他两种算法不同,但是我正在尝试找到一种解决方案来实现我的目的。)
ead_params = { 'binary_search_steps':9,'max_iterations':100,'learning_rate':0.001,'clip_min':0,'clip_max':1,'y_target':target}
adv_x = image
founded_adv = False
threshold = 0.9
wrap = KerasModelWrapper(model)
ead = ElasticNetMethod(wrap,sess=sess)
while (not founded_adv):
adv_x = ead.generate_np(adv_x,**ead_params)
prediction = model.predict(adv_x).tolist()
pred_class = np.argmax(prediction[0])
confidence = prediction[0][pred_class]
if (pred_class == 0 and confidence >= threshold):
founded_adv = True
while循环可能会生成一个样本,直到达到目标类别且置信度大于90%。该代码实际上可用于FGSM和Madry,但可使用EAD无限运行。
库版本:
Tensorflow :2.2.0 Keras :2.4.3 CLeverhans :2.0.0-451ccecad450067f99c333fc53592201
有人可以帮助我吗?
非常感谢。
解决方法
对于任何对此问题感兴趣的人,可以以这种方式修改以前的代码以使其正常工作:
第一个解决方案:
prediction = model.predict(image)
initial_predicted_class = np.argmax(prediction[0])
ead_params = { 'binary_search_steps':9,'max_iterations':100,'learning_rate':0.001,'confidence':1,'clip_min':0,'clip_max':1,'y_target':target}
adv_x = image
founded_adv = False
threshold = 0.9
wrap = KerasModelWrapper(model)
ead = ElasticNetMethod(wrap,sess=sess)
while (not founded_adv):
adv_x = ead.generate_np(adv_x,**ead_params)
prediction = model.predict(adv_x).tolist()
pred_class = np.argmax(prediction[0])
confidence = prediction[0][pred_class]
if (pred_class == initial_pred_class and confidence >= threshold):
founded_adv = True
else:
ead_params['confidence'] += 1
使用库中实现的置信度参数。实际上,如果目标类别的概率不增加,我们将置信度参数增加1。
第二个解决方案:
prediction = model.predict(image)
initial_predicted_class = np.argmax(prediction[0])
ead_params = {'beta':5e-3,'binary_search_steps':6,'max_iterations':10,'learning_rate':3e-2,'clip_max':1}
threshold = 0.96
adv_x = image
founded_adv = False
wrap = KerasModelWrapper(model)
ead = ElasticNetMethod(wrap,sess=sess)
while (not founded_adv):
eps_hyp = 0.5
new_adv_x = ead.generate_np(adv_x,**ead_params)
pert = new_adv_x-adv_x
new_adv_x = adv_x - eps_hyp*pert
new_adv_x = (new_adv_x - np.min(new_adv_x)) / (np.max(new_adv_x) - np.min(new_adv_x))
adv_x = new_adv_x
prediction = model.predict(new_adv_x).tolist()
pred_class = np.argmax(prediction[0])
confidence = prediction[0][pred_class]
print(pred_class)
print(confidence)
if (pred_class == initial_predicted_class and confidence >= threshold):
founded_adv = True
在第二个解决方案中,对原始代码进行了以下修改:
-Initial_predicted_class是模型在良性样本上预测的类(在我们的示例中为“ 0”)。
-在算法的参数(ead_params)中,我们没有插入目标类。
-然后,我们可以获得计算pert = new_adv_x-adv_x的算法给出的扰动,其中“ adv_x”是原始图像(在for循环的第一步中),而new_adv_x是该算法生成的扰动样本。
-先前的操作非常有用,因为EAD原始算法会计算扰动以使“ 0”类损失最大化,但在本例中,我们希望将其最小化。
-因此,我们可以将新的扰动图像计算为new_adv_x = adv_x-eps_hyp * pert(其中eps_hyp是为减少扰动而引入的epsilon超参数),然后我们将新的扰动图像归一化。 / p>
-我已经测试了用于大量图像的代码,并且置信度始终在提高,因此我认为这可以是实现此目的的一个很好的解决方案。
我认为第二种解决方案可以使干扰更小。