您好,我在使用ebook_imagenet_jsma_tf.ipynb时,出现了一点问题
Eva-Deng opened this issue · comments
我在使用JSMA的双像素点算法时:
attack = JSMA(m)
attack_config = {
"max_iter": 2000,
"theta": 0.3,
"max_perturbations_per_pixel": 7,
"fast":True,
"two_pix":True
}
报了下面的错:
ValueError: axes don't match array
检查后,发现应该是saliency.py中有错误出现:
def _saliency_map(self, image, target, labels, mask, fast=False, two_pix=True):
"""
Get pixel location with highest influence on class.
Args:
image(numpy.ndarray): Image with shape (height, width, channels).
target(int): The target label.
labels(int): The number of classes of the output label.
mask(list): Each modified pixel with border value is set to zero in mask.
fast(bool): Whether evaluate the pixel influence on sum of residual classes.
two_pix(bool): Whether to select two pixels from salience map which
is used in original paper.
Return:
idx: The index of optimal pixel (idx = idx1 * input_dimension +
idx2 in two pix setting)
pix_sign: The direction of perturbation
"""
# pixel influence on target class
alphas = self.model.gradient(image, target) * mask
# pixel influence on sum of residual classes(don't evaluate if fast == True)
if fast:
betas = -np.ones_like(alphas)
else:
betas = np.sum([
self.model.gradient(image, label) * mask - alphas
for label in labels
], 0)
if two_pix:
# construct two pix matrix to treat it as one pix
alphas_col = np.expand_dims(alphas, axis=0)
#下面这句出现了问题
alphas_mat = alphas_col.transpose(1, 0) + alphas_col
输入image是一个形状[h,w,c]的矩阵,alphas模型关于image的偏导中属于类别target的部分,所以alphas的形状也为[h,w,c],alphas_col扩维后形状为[1,h,w,c],alphas_col.transpose(1, 0)这句维度不匹配。我不确定是否是这个问题,如果您有空的话,希望能得到您的回复,不胜感激
双像素的算法实现有问题 我修复后push上去
好的,非常感谢
修改了接口 目前仅支持单像素算法