Exemplo n.º 1
0
def Create_X(pathTo_ILSVRC2012, mode):

    validate_arguments(mode)
    un_zip(os.path.join(pathTo_ILSVRC2012, 'ILSVRC2012_img_train'))
    datafile = os.path.join('data', 'imagenet_data_' + str(mode) + '_mode.npy')
    print('>> Creating pre-processed imagenet data...')
    X = create_imagenet_npy(Pos_unzip, mode=mode)
    print('>> Saving the pre-processed imagenet data')
    # Save the pre-processed images
    # Caution: This can take take a lot of space. Comment this part to discard saving.
    np.save(datafile, X)
    return X
Exemplo n.º 2
0
        print('>> Computing gradient function...')

        def grad_fs(image_inp, indices):
            return persisted_sess.run(dydx,
                                      feed_dict={
                                          persisted_input: image_inp,
                                          inds: indices
                                      }).squeeze(axis=1)

        if os.path.isfile(file_perturbation) == 0:

            # Load/Create data
            datafile = os.path.join('data', npy_data)
            if os.path.isfile(datafile) == 0:
                print('>> Creating pre-processed imagenet data...')
                X = create_imagenet_npy(path_train_imagenet)

                print('>> Saving the pre-processed imagenet data')
                if not os.path.exists('data'):
                    os.makedirs('data')

                # Save the pre-processed images
                # Caution: This can take take a lot of space. Comment this part to discard saving.
                np.save(os.path.join('data', npy_data), X)

            else:
                print('>> Pre-processed imagenet data detected')
                X = np.load(datafile)
            # Running universal perturbation
            for x in range(995):
                v = targeted_perturbation(X,

    def f(image_inp): return persisted_sess.run(persisted_output,
                                                feed_dict={persisted_input: np.reshape(image_inp, (-1, 224, 224, 3))})


    print('>> Compiling the gradient tensorflow functions. This might take some time...')
    y_flat = tf.reshape(persisted_output, (-1,))
    inds = tf.placeholder(tf.int32, shape=(2,))
    dydx = jacobian(y_flat, persisted_input, inds)

    print('>> Computing gradient function...')


    def grad_fs(image_inp, indices): return persisted_sess.run(dydx, feed_dict={persisted_input: image_inp,

                                                                                inds: indices}).squeeze(axis=1)

    for i in range(100):
        npy_img = 'data/npy_img/10classes1000imgs/' + str(i) +'.npy'
        npy_per = 'data/npy_per/10classes1000imgs/' + str(i) + '.npy'

        if os.path.isfile(npy_img) == 0:
            X = create_imagenet_npy(path_train_imagenet, len_batch=1100, num_class=10, p=0, q=0, r=i*10)
            np.save(npy_img, X)
        else:
            X = np.load(npy_img)
        if os.path.isfile(npy_per) == 0:
            v = targeted_perturbation(X, f, grad_fs, delta=0.25, max_iter_uni=10, target=target)
            np.save((npy_per), v)
Exemplo n.º 4
0
        print(">> Computing gradient function...")

        def grad_fs(image_inp, inds):
            return [
                persisted_sess.run(dydx[i],
                                   feed_dict={persisted_input: image_inp})
                for i in inds
            ]


        # Load/Create data
        datafile = os.path.join('data', 'imagenet_data.npy')
        if os.path.isfile(datafile) == 0:
            print(">> Creating pre-processed imagenet data...")
            #预处理一个batch的图片并返回
            X, dirs, sub_dirs = create_imagenet_npy(PATH_TRAIN_IMAGENET, 1000)
            print(X.shape)

            # print(">> Saving the pre-processed imagenet data")
            # if not os.path.exists('data'):
            #     os.makedirs('data')

            # Save the pre-processed images
            # Caution: This can take take a lot of space. Comment this part to discard saving.
            # np.save(os.path.join('data', 'imagenet_data.npy'), X)

        else:
            print(">> Pre-processed imagenet data detected")
            X = np.load(datafile)

        #核心部分!!计算通用干扰