Exemple #1
0
def run(model_path, image_path):
    predict_func = tp.OfflinePredictor(tp.PredictConfig(
        model=Model(),
        session_init=tp.get_model_loader(model_path),
        input_names=['image'],
        output_names=['saliency']))
    im = cv2.imread(image_path)
    assert im is not None and im.ndim == 3, image_path

    # resnet expect RGB inputs of 224x224x3
    im = cv2.resize(im, (IMAGE_SIZE, IMAGE_SIZE))
    im = im.astype(np.float32)[:, :, ::-1]

    saliency_images = predict_func([im])[0]

    abs_saliency = np.abs(saliency_images).max(axis=-1)
    pos_saliency = np.maximum(0, saliency_images)
    neg_saliency = np.maximum(0, -saliency_images)

    pos_saliency -= pos_saliency.min()
    pos_saliency /= pos_saliency.max()
    cv2.imwrite('pos.jpg', pos_saliency * 255)

    neg_saliency -= neg_saliency.min()
    neg_saliency /= neg_saliency.max()
    cv2.imwrite('neg.jpg', neg_saliency * 255)

    abs_saliency = viz.intensity_to_rgb(abs_saliency, normalize=True)[:, :, ::-1]  # bgr
    cv2.imwrite("abs-saliency.jpg", abs_saliency)

    rsl = im * 0.2 + abs_saliency * 0.8
    cv2.imwrite("blended.jpg", rsl)
def main():
    # tf.logging.set_verbosity(tf.logging.INFO)
    # instantiate blackbox and substitute model
    # instantiate blackbox and substitute model
    forward_model = load_model()
    # backward_model1 = create_fmodel_18()
    backward_model2 = create_fmodel_ALP()
    backward_model3 = create_fmodel_ALP1000()
    # print(backward_model1[0])
    # instantiate differntiable composite model
    # (predictions from blackbox, gradients from substitute)
    model = CompositeModel(
        forward_model=forward_model,
        backward_models=[backward_model2, backward_model3],
        weights = [0.5, 0.5])
    predictor = tp.OfflinePredictor(tp.PredictConfig(
        model=SaliencyModel(),
        session_init=tp.get_model_loader("resnet_v1_50.ckpt"),
        input_names=['image'],
        output_names=['saliency']))
    for (file_name, image, label) in read_images():
        pos_salience = find_salience(predictor, image)
        adversarial = run_attack(model, image, label, pos_salience)
        store_adversarial(file_name, adversarial)
    attack_complete()
Exemple #3
0
def save_model(model_paths, model, target="", compact=False):
    """Save a model to given dir"""
    from os import path
    from os import makedirs

    import tensorpack as tp

    from tensorpack.tfutils.varmanip import get_checkpoint_path
    from tensorpack.tfutils.export import ModelExporter

    import misc.logger as logger
    _L = logger.getLogger("Saver")

    save_to_modeldir = target is ""

    for model_path in model_paths:
        # get model path
        real_path = get_checkpoint_path(model_path)
        abs_p = path.realpath(model_path)
        if (not path.isfile(abs_p)):
            _L.error("{} is not a model file".format(model_path))
            continue

        # save to same folder as model
        if (save_to_modeldir):
            target = path.dirname(abs_p)

        # make sure the folder exists
        if not path.exists(target):
            makedirs(target)

        conf = tp.PredictConfig(session_init=tp.get_model_loader(model_path),
                                model=model,
                                input_names=["input"],
                                output_names=["emb"])

        exporter = ModelExporter(conf)
        if (compact):
            out = path.join(target, "{}.pb".format(path.basename(real_path)))
            _L.info("saving {} to {}".format(path.basename(real_path), out))
            exporter.export_compact(out)
        else:
            _L.info("compact saving {} to {}".format(path.basename(real_path),
                                                     target))
            exporter.export_serving(target)
Exemple #4
0
def get_predictor(model_path, model=None):
    import tensorpack as tp
    import tensorflow as tf

    if model:
        sess_conf = tp.tfutils.get_default_sess_config()
        sess_conf.log_device_placement = config.LOG_DEVICES
        session_creator = tf.compat.v1.train.ChiefSessionCreator(
            config=sess_conf)

        pred = tp.OfflinePredictor(
            tp.PredictConfig(session_creator=session_creator,
                             session_init=tp.get_model_loader(model_path),
                             model=model,
                             input_names=["input", "heights", "wavelets"],
                             output_names=["emb"]))

        def prediction(*inp):
            return pred(*inp)[0], pred.sess

    else:
        sess_conf = tf.ConfigProto(allow_soft_placement=True)
        model_file = "{}.pb".format(model_path)

        def prediction(*inp):
            with tf.Session(config=sess_conf) as sess:
                with tf.gfile.GFile(model_file, "rb") as f:
                    graph_def = tf.GraphDef()
                    graph_def.ParseFromString(f.read())
                    tf.import_graph_def(graph_def)
                inp_key = sess.graph.get_tensor_by_name("import/input:0")
                heights_key = sess.graph.get_tensor_by_name("import/heights:0")
                wavelets_key = sess.graph.get_tensor_by_name(
                    "import/wavelets:0")
                emb = sess.graph.get_tensor_by_name("import/emb:0")

                pred = sess.run(emb, {
                    inp_key: inp[0],
                    heights_key: inp[1],
                    wavelets_key: inp[2]
                })
                return pred, sess

    return prediction
Exemple #5
0
def main():
    # instantiate blackbox and substitute model
    forward_model = load_model()
    backward_model = create_fmodel()

    # instantiate differntiable composite model
    # (predictions from blackbox, gradients from substitute)
    model = CompositeModel(
        forward_model=forward_model,
        backward_model=backward_model)
    predictor = tp.OfflinePredictor(tp.PredictConfig(
        model=SaliencyModel(),
        session_init=tp.get_model_loader("resnet_v1_50.ckpt"),
        input_names=['image'],
        output_names=['saliency']))
    for (file_name, image, label) in read_images():
        pos_salience = find_salience(predictor, image)
        adversarial = run_attack(model, image, label, pos_salience)
        store_adversarial(file_name, adversarial)
    attack_complete()
Exemple #6
0
def main():
    # instantiate blackbox and substitute model
    forward_model = load_model()
    backward_model = create_fmodel()

    # instantiate differntiable composite model
    # (predictions from blackbox, gradients from substitute)
    model = CompositeModel(forward_model=forward_model,
                           backward_model=backward_model)
    predictor = tp.OfflinePredictor(
        tp.PredictConfig(model=SaliencyModel(),
                         session_init=tp.get_model_loader("resnet_v1_50.ckpt"),
                         input_names=['image'],
                         output_names=['saliency']))
    for (file_name, image, label) in read_images():
        pos_salience = find_salience(predictor, image)
        adversarial = run_attack(model, image, label, pos_salience)
        store_adversarial(file_name, adversarial)
    # Announce that the attack is complete
    # NOTE: In the absence of this call, your submission will timeout
    # while being graded.
    print("Attack is complete")
    attack_complete()
def main():
    loader = TinyImageNetLoader()
    forward_model = load_model()
    backward_model1 = create_fmodel_ALP()
    backward_model2 = create_fmodel_ALP1000()
    model = CompositeModel(forward_model=forward_model,
                           backward_models=[backward_model1, backward_model2],
                           weights=[0.5, 0.5])
    predictor = tp.OfflinePredictor(
        tp.PredictConfig(model=SaliencyModel(),
                         session_init=tp.get_model_loader("resnet_v1_50.ckpt"),
                         input_names=['image'],
                         output_names=['saliency']))
    for (file_name, image, label) in read_images():
        adversarial = run_attack(loader, forward_model, image, label)
        if adversarial is None:
            pos_salience = find_salience(predictor, image)
            adversarial = run_attack2(model, image, label, pos_salience)
        store_adversarial(file_name, adversarial)

    # Announce that the attack is complete
    # NOTE: In the absence of this call, your submission will timeout
    # while being graded.
    attack_complete()
Exemple #8
0
def visualize(model_path, model, iimgs):
    import matplotlib.pyplot as plt
    from os import path
    from matplotlib.image import imread
    import numpy as np
    import tensorpack as tp

    import misc.logger as logger
    _L = logger.getLogger("visualize")

    _L.debug(iimgs)
    images = [imread(img) for img in iimgs]
    imgs = [np.array([img]) for img in images]

    # imgs = [np.expand_dims(img, 0) for img in imgs]
    # print(imgs[0].shape)
    # print(np.expand_dims(imgs[0], 0).shape)
    # print(np.expand_dims(imgs[0], 3).shape)
    # exit()

    config.c_width = imgs[0].shape[2]
    config.c_height = imgs[0].shape[1]

    _L.debug("{} x {} cutouts".format(config.C_WIDTH, config.C_HEIGHT))

    pred = tp.OfflinePredictor(
        tp.PredictConfig(session_init=tp.get_model_loader(model_path),
                         model=model(config.depth, config.mode),
                         input_names=["input"],
                         output_names=["emb"]))

    preds = [pred(el)[0] for el in imgs]

    for i, p in enumerate(preds):
        print("pred{}: ".format(i), end="")
        print(p)

    dists = [
        np.sum((preds[0] - preds[i])**2, 1)[0] for i in range(1, len(preds))
    ]

    for i, d in enumerate(dists):
        print("dist{}: ".format(i), end="")
        print(d)

    file_name = path.basename(iimgs[0])
    name_parts = file_name.split(".")
    class_id, rel_id = name_parts[0].split("_")
    ax = plt.subplot(1, len(images), 1)
    ax.set_yticks([])
    ax.set_xticks([])
    plt.imshow(images[0])
    plt.title("Cls {}/{} - #{}".format(class_id, rel_id, 0))

    indices = sorted(list(range(len(dists))), key=lambda el: dists[el])

    # for i, img in enumerate(images):
    for j, i in enumerate(indices):
        img = images[i + 1]
        file_name = path.basename(iimgs[i + 1])
        name_parts = file_name.split(".")
        class_id, rel_id = name_parts[0].split("_")

        ax = plt.subplot(1, len(images), j + 2)
        ax.set_yticks([])
        ax.set_xticks([])

        plt.imshow(img)
        plt.title("Cls {}/{} - #{}".format(class_id, rel_id, i))

        plt.ylabel("{}".format(preds[i]))
        plt.xlabel("{:E}".format(dists[i]))
    plt.show()