Beispiel #1
0
def GetGraySmoothGrad(net, img:torch.Tensor, label:torch.Tensor, DEVICE):
    grad_map = GetSmoothGrad(net, img, label, DEVICE, stdev_spread=0.10)
    # 3x224x224
    grad = GetSmoothGrad(net, img, label, DEVICE, stdev_spread = 0.0, num=1)

    gray_grad = clip_gradmap_to_gray(grad_map)
    grad = clip_gradmap_to_gray(grad)
    #print(gray_grad.shape)
    gray_grad = gray_grad[:, :, np.newaxis]
    grad = grad[:, :, np.newaxis]
    gray_grad = np.repeat(gray_grad, 3, axis=2) # (224, 224, 3)
    grad = np.repeat(grad, 3, axis=2)
    #print(gray_grad.shape)
    return gray_grad, grad
Beispiel #2
0
def l1_for_without_smooth(net, dl, DEVICE):
    net.eval()
    net.to(DEVICE)
    #criterion = nn.CrossEntropyLoss().to(DEVICE)
    l1s = []
    for i, (batch_img, batch_label) in enumerate(dl):
        #if i> 5:
        #    break
        for j in range(int(batch_img.size(0))):
            img = batch_img[j]
            label = batch_label[j]
            img = img.to(DEVICE)
            label = label.to(DEVICE)
            #print(img.size())
            grad_map = GetSmoothGrad(net,
                                     img,
                                     label,
                                     DEVICE,
                                     stdev_spread=0.05,
                                     num=32)
            #print(grad_maps.size(), batch_img.size())
            l1s.append(torch.norm(grad_map, 1).item())
    l1s = np.array(l1s)
    print("Min: {:.4f} -- Max: {:.2f} -- Mean:{:.2f}".format(
        l1s.min(), l1s.max(), l1s.mean()))
Beispiel #3
0
def GetSmoothRes(net, Data, DEVICE, save_path='./SmoothRes/Fashion_MNIST'):
    for i, (img, label) in enumerate(zip(Data.X, Data.Y)):
        #print(i)
        #print(img.shape, label.shape)
        img = img.astype(np.float32)
        #label = label.astype(np.float32)
        img = img[np.newaxis, :]
        img = torch.tensor(img)
        #print(img.type())
        label = torch.tensor(label).type(torch.LongTensor)
        grad_map = GetSmoothGrad(net, img, label, DEVICE=DEVICE)
        grad_map = grad_map.cpu().detach().numpy()
        grad_map = clip_gradmap(grad_map)
        #print(grad_map.shape, grad_map.mean())
        save_p = os.path.join(save_path, '{}.png'.format(i))
        #print(grad_map.shape)
        imwrite(save_p, grad_map)
    print('{} imgs saved in {}'.format(i + 1, save_path))
Beispiel #4
0
def get_result(net, dl, DEVICE, net_name = ''):
    save_bench = '../data/benchmark/'
    save_path = os.path.join('../SmoothRes/', net_name)
    labels = []
    net.eval()
    mean = torch.tensor(np.array([0.485, 0.456, 0.406]).astype(np.float32)[np.newaxis, :, np.newaxis, np.newaxis])
    std = torch.tensor(np.array([0.229, 0.224, 0.225]).astype(np.float32)[np.newaxis, :, np.newaxis, np.newaxis])
    mean = mean.to(DEVICE)
    std = std.to(DEVICE)
    for i, (batch_img, batch_label) in enumerate(dl):
        if i> 5:
            break
        for j in range(int(batch_img.size(0))):
            img = batch_img[j]
            label = batch_label[j]
            img = img.to(DEVICE)
            label = label.to(DEVICE)
            #print(img.size())
            grad_map = GetSmoothGrad(net, img, label, DEVICE, stdev_spread = 0.05)
            #print(grad_map.shape)
            clip_and_save_single_img(grad_map, i * batch_img.size(0) + j, save_dir=save_path)
            #print(grad.shape)
            #simg = (img + mean) * std
            simg = img * std + mean
            #print('rb', simg.max(), simg.min())
            simg = torch.clamp(simg, 0, 1)
            #print('r', simg.max(), simg.min())
            simg = simg.detach().cpu().numpy() * 255.0
            #print(simg.shape)
            #print(simg.shape)
            simg = simg[0]
            simg = np.transpose(simg, (1, 2, 0)).astype(np.uint8)
            #print('r', simg.max(), simg.min())
            #imwrite(os.path.join(save_bench, '{}.png'.format(i * batch_img.size(0) + j)), simg)
            io.imsave(os.path.join(save_bench, '{}.png'.format(i * batch_img.size(0) + j)), simg)
            print(i * batch_img.size(0) + j)

            #grad = imread(os.path.join(save_path, '{}-smooth.png'.format(i * batch_img.size(0) + j)))
            grad = io.imread(os.path.join(save_path, '{}-smooth.png'.format(i * batch_img.size(0) + j)),
                             as_gray = False)
            # if gray
            # grad = grad[:, :, np.newaxis]
            # grad = np.repeat(grad, 3, axis = 2)

            gray_grad = np.mean(grad, axis = -1, keepdims = True)
            gray_grad = gray_grad.astype(np.uint8)
            gray_grad = np.repeat(gray_grad, 3, axis = 2)
            pair_img = np.concatenate((gray_grad, grad, simg), axis=1)
            #imwrite(os.path.join(save_path, '{}-pair.png'.format(i * batch_img.size(0) + j)), pair_img)
            io.imsave(os.path.join(save_path, '{}-pair.png'.format(i * batch_img.size(0) + j)), pair_img)
            labels.append(batch_label.numpy())
    labels = np.array(labels)
    np.savetxt(os.path.join(save_bench, 'label.txt'), labels.reshape(-1))
Beispiel #5
0
def get_result(net, dl, DEVICE, net_name='', dl_name='raw'):

    PgdAttack = IPGD(eps=8 / 255.0,
                     sigma=2 / 255.0,
                     nb_iter=40,
                     norm=np.inf,
                     DEVICE=DEVICE)
    save_path = os.path.join('../Maps/', dl_name, net_name)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    labels = []
    net.eval()
    mean = torch.tensor(
        np.array([0.485, 0.456,
                  0.406]).astype(np.float32)[np.newaxis, :, np.newaxis,
                                             np.newaxis])
    std = torch.tensor(
        np.array([0.229, 0.224,
                  0.225]).astype(np.float32)[np.newaxis, :, np.newaxis,
                                             np.newaxis])
    mean = mean.to(DEVICE)
    std = std.to(DEVICE)
    for i, (batch_img, batch_label) in enumerate(dl):
        batch_img = batch_img.to(DEVICE)
        batch_label = batch_label.to(DEVICE)
        batch_img = PgdAttack.attack(net, batch_img, batch_label).detach()
        if i > 5:
            break
        for j in range(int(batch_img.size(0))):
            img = batch_img[j]
            label = batch_label[j]
            #img = img.to(DEVICE)
            #label = label.to(DEVICE)
            #print(img.size())
            grad_map = GetSmoothGrad(net,
                                     img,
                                     label,
                                     DEVICE,
                                     stdev_spread=0.10)
            #print(grad_map.shape)
            clip_and_save_single_img(grad_map,
                                     i * batch_img.size(0) + j,
                                     save_dir=save_path)
            #print(grad.shape)
            #simg = (img + mean) * std
            simg = img * std + mean
            #print('rb', simg.max(), simg.min())
            simg = torch.clamp(simg, 0, 1)
            #print('r', simg.max(), simg.min())
            simg = simg.detach().cpu().numpy() * 255.0
            #print(simg.shape)
            #print(simg.shape)
            simg = simg[0]
            simg = np.transpose(simg, (1, 2, 0)).astype(np.uint8)
            #print('r', simg.max(), simg.min())
            #imwrite(os.path.join(save_bench, '{}.png'.format(i * batch_img.size(0) + j)), simg)
            #io.imsave(os.path.join(save_bench, '{}.png'.format(i * batch_img.size(0) + j)), simg)
            #print(i * batch_img.size(0) + j)

            #grad = imread(os.path.join(save_path, '{}-smooth.png'.format(i * batch_img.size(0) + j)))
            grad = io.imread(os.path.join(
                save_path, '{}-smooth.png'.format(i * batch_img.size(0) + j)),
                             as_gray=False)
            # if gray
            # grad = grad[:, :, np.newaxis]
            # grad = np.repeat(grad, 3, axis = 2)

            gray_grad = np.mean(grad, axis=-1, keepdims=True)

            gray_grad = gray_grad.astype(np.uint8)
            gray_grad = np.repeat(gray_grad, 3, axis=2)
            pair_img = np.concatenate((gray_grad, grad, simg), axis=1)
            #imwrite(os.path.join(save_path, '{}-pair.png'.format(i * batch_img.size(0) + j)), pair_img)
            io.imsave(
                os.path.join(save_path,
                             '{}-pair.png'.format(i * batch_img.size(0) + j)),
                pair_img)