Esempio n. 1
0
    generated_img_adv = tf.reverse(generated_img_adv,
                                   axis=[-1])  # switch RGB to BGR
    adv_img_bgr = generated_img_adv
    generated_img_adv = stn.encoder.preprocess(
        generated_img_adv)  # preprocess image
    enc_gen_adv, enc_gen_layers_adv = stn.encoder.encode(generated_img_adv)

    generated_img = tf.reverse(generated_img, axis=[-1])  # switch RGB to BGR
    img_bgr = generated_img
    generated_img = stn.encoder.preprocess(generated_img)  # preprocess image
    enc_gen, enc_gen_layers = stn.encoder.encode(generated_img)

    if data_set == "cifar10":
        if model_name in ["cifar10_nat", "cifar10_adv"]:
            classifier = Model("eval", raw_cifar.train_images)
            classifier._build_model(adv_img, label, reuse=False, conf=0.1)
            adv_loss = -classifier.target_loss
            adv_acc = classifier.accuracy
            adv_acc_y = tf.cast(classifier.correct_prediction, tf.float32)
            classifier._build_model(content, label, reuse=True, conf=0.1)
            normal_loss = -classifier.target_loss
            norm_acc = classifier.accuracy
            acc_y = tf.cast(classifier.correct_prediction, tf.float32)
            classifier._build_model(img, label, reuse=True)
            decode_acc_y = tf.cast(classifier.correct_prediction, tf.float32)
        elif model_name in ["cifar10_trades"]:
            classifier = build_model_trades(adv_img, label, conf=0.1)
            #classifier._build_model(adv_img, label, reuse=False, conf=0.1)
            adv_loss = -classifier.target_loss
            adv_acc = classifier.accuracy
            adv_acc_y = tf.cast(classifier.correct_prediction, tf.float32)
    # pass the generated_img to the encoder, and use the output compute loss
    generated_img_adv = tf.reverse(generated_img_adv,
                                   axis=[-1])  # switch RGB to BGR
    adv_img_bgr = generated_img_adv
    generated_img_adv = stn.encoder.preprocess(
        generated_img_adv)  # preprocess image
    enc_gen_adv, enc_gen_layers_adv = stn.encoder.encode(generated_img_adv)

    generated_img = tf.reverse(generated_img, axis=[-1])  # switch RGB to BGR
    img_bgr = generated_img
    generated_img = stn.encoder.preprocess(generated_img)  # preprocess image
    enc_gen, enc_gen_layers = stn.encoder.encode(generated_img)

    if data_set == "cifar10":
        classifier = Model("eval", raw_cifar.train_images)
        classifier._build_model(adv_img, label, reuse=False)
        adv_loss = -classifier.relaxed_y_xent
        adv_acc = classifier.accuracy
        classifier._build_model(img, label, reuse=True)
        normal_loss = -classifier.relaxed_y_xent
        norm_acc = classifier.accuracy
    elif data_set == "imagenet":
        classifier = build_imagenet_model(adv_img_bgr, label)
        losses = tf.stack([
            classifier.rev_xent, classifier.poss_loss, classifier.xent,
            classifier.xent_filter
        ])
        adv_loss = -tf.reduce_sum(losses[loss_choice])
        adv_acc = classifier.accuracy
        acc_y = classifier.acc_y
        classifier = build_imagenet_model(img_bgr, label, reuse=True)
                _d["stn_vars"] = get_scope_var(aname1 + "/" + "transform")
                # get the target feature maps which is the output of AdaI

                # pass the generated_img to the encoder, and use the output compute loss
                _d["generated_img_adv"] = tf.reverse(
                    _d["generated_img_adv"], axis=[-1])  # switch RGB to BGR
                adv_img_bgr = _d["generated_img_adv"]
                _d["generated_img_adv"] = stn.encoder.preprocess(
                    _d["generated_img_adv"])  # preprocess image
                _d["enc_gen_adv"], _d[
                    "enc_gen_layers_adv"] = stn.encoder.encode(
                        _d["generated_img_adv"])

            if data_set == "cifar10":
                classifier._build_model(_d["adv_img"],
                                        label,
                                        reuse=not first_use,
                                        conf=5)
                _d["adv_acc_y"] = tf.cast(classifier.correct_prediction,
                                          tf.float32)
            else:
                classifier = build_imagenet_model(adv_img_bgr,
                                                  label,
                                                  conf=5,
                                                  reuse=not first_use)
                _d["adv_acc_y"] = classifier.acc_y
            first_use = False
            _d["adv_loss"] = -classifier.target_loss
            _d["adv_acc"] = classifier.accuracy

            if data_set == "cifar10":
                classifier._build_model(content, label, reuse=True, conf=5)