Exemplo n.º 1
0
    def clipping(self, imgs):

        imgs = reverse_preprocess(imgs, self.intensity_range)
        imgs = np.clip(imgs, 0, self.max_val)
        imgs = np.rint(imgs)

        imgs = preprocess(imgs, self.intensity_range)

        return imgs
def pubfig65_fingerprint_vggface():

    os.environ["CUDA_VISIBLE_DEVICES"] = DEVICE

    sess = utils_translearn.fix_gpu_memory()

    bottleneck_model, student_model = load_and_build_models()

    # form attacker class
    # setting mimic_img to False, since we are mimicking a specific vector
    print('loading attacker')
    attacker = MimicPenaltyDSSIM(sess,
                                 bottleneck_model,
                                 mimic_img=False,
                                 batch_size=BATCH_SIZE,
                                 intensity_range=INTENSITY_RANGE,
                                 initial_const=INITIAL_CONST,
                                 learning_rate=LR,
                                 max_iterations=MAX_ITER,
                                 l_threshold=DSSIM_THRESHOLD,
                                 verbose=1)

    print('building fingerprinting input')
    # initializing input with random noise
    X_source_raw = np.random.random((NB_IMGS, IMG_ROW, IMG_COL, IMG_COLOR))
    X_source_raw *= 255.0
    X_source = utils_translearn.preprocess(X_source_raw, INTENSITY_RANGE)

    # build target bottleneck neuron vector as all-zero vector
    bottleneck_shape = ([X_source.shape[0]] +
                        list(bottleneck_model.layers[-1].output_shape[1:]))
    X_target_bottleneck = np.zeros(bottleneck_shape)

    # build fingerprinting input
    X_adv = attacker.attack(X_source, X_target_bottleneck)

    print('testing fingerprint image on student')
    gini_list = []
    max_conf_list = []
    Y_pred = student_model.predict(X_adv)
    Y_conf = np.max(Y_pred, axis=1)
    for idx in xrange(NB_IMGS):
        gini_list.append(utils_translearn.gini(Y_pred[idx]))
        max_conf_list.append(Y_conf[idx])

    # Low gini index means fingerprinting is successful, the correct teacher
    # is found. You can also infer this from maximum confidence. If max_conf
    # is low (similar to 1 / NB_CLASSES), then fingerprinting is successful.
    avg_gini = np.mean(gini_list)
    avg_max_conf = np.mean(max_conf_list)
    print('INFO: avg_gini: %f, avg_max_conf: %f' % (avg_gini, avg_max_conf))

    pass
def load_dataset(data_file=DATA_FILE):

    dataset = utils_translearn.load_dataset(data_file,
                                            keys=['X_test', 'Y_test'])

    X = dataset['X_test']
    Y = dataset['Y_test']

    X = X.astype(np.float32)
    Y = Y.astype(np.float32)

    X = utils_translearn.preprocess(X, INTENSITY_RANGE)
    Y = np.argmax(Y, axis=1)

    return X, Y
def load_dataset(data_file=DATA_FILE):

    dataset = utils_translearn.load_dataset(
        data_file, keys=['X_train', 'Y_train', 'X_test', 'Y_test'])

    X_train = dataset['X_train']
    Y_train = dataset['Y_train']
    X_test = dataset['X_test']
    Y_test = dataset['Y_test']

    X_train = X_train.astype(np.float32)
    Y_train = Y_train.astype(np.float32)
    X_test = X_test.astype(np.float32)
    Y_test = Y_test.astype(np.float32)

    X_train = utils_translearn.preprocess(X_train, INTENSITY_RANGE)
    X_test = utils_translearn.preprocess(X_test, INTENSITY_RANGE)

    print('X_train shape: %s' % str(X_train.shape))
    print('Y_train shape: %s' % str(Y_train.shape))
    print('X_test shape: %s' % str(X_test.shape))
    print('Y_test shape: %s' % str(Y_test.shape))

    return X_train, Y_train, X_test, Y_test