Пример #1
0
def build_model(model_name, inputs, num_classes, is_training, dropout_keep_prob):
    use_fcn = False
    if model_name.find('fcn') >= 0:
        use_fcn = True
        model_base_name = model_name[0:-4]
    else:
        model_base_name = model_name

    if model_base_name == 'vgg16':
        net = vgg16_base(inputs)
    elif model_base_name == 'inception_v1':
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            net, _ = inception.inception_v1_base(inputs)
    elif model_base_name == 'inception_v2':
        with slim.arg_scope(inception.inception_v2_arg_scope()):
            net, _ = inception.inception_v2_base(inputs)
    elif model_base_name == 'inception_v3':
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            net, _ = inception.inception_v3_base(inputs)
    else:
        raise Exception('model {} is not existed'.format(model_name))

    with tf.variable_scope('not_pretrained'):
        if use_fcn:
            net = fully_convolutional_networks(net, num_classes, is_training, dropout_keep_prob)
        else:
            net = fully_connected_networks(net, num_classes, is_training, dropout_keep_prob)
    return net
Пример #2
0
def build_single_inceptionv1(train_tfdata, is_train, dropout_keep_prob):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        identity, end_points = inception.inception_v1(train_tfdata, dropout_keep_prob = dropout_keep_prob, is_training=is_train)
        net = slim.avg_pool2d(end_points['Mixed_5c'], [7, 7], stride=1, scope='MaxPool_0a_7x7')
        net = slim.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        feature = tf.squeeze(net, [1, 2])
    return identity, feature
Пример #3
0
def eval(x, num_classes=110):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')
    pred1 = tf.argmax(end_points_inc_v1['Predictions'], 1)
    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])
    pred2 = tf.argmax(end_points_res_v1_50['probs'], 1)
    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')
    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])
    pred3 = tf.argmax(end_points_vgg_16['probs'], 1)
    return [pred1, pred2, pred3]
Пример #4
0
 def _build_graph(self, inputs):
   orig_image = inputs[0]
   with tp.symbolic_functions.guided_relu():
     with slim.arg_scope(inception.inception_v1_arg_scope()):
       image = tf.expand_dims(((orig_image / 255) - 0.5) * 2, 0)
       logits, _ = inception.inception_v1(image, 1001, False)
     tp.symbolic_functions.saliency_map(logits, orig_image, name="saliency")
Пример #5
0
def non_target_graph(x, y, i, x_max, x_min, grad):

    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')

    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

        end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
        end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

        ########################
        # Using model predictions as ground truth to avoid label leaking
        pred = tf.argmax(
            end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] +
            end_points_vgg_16['probs'], 1)
        first_round = tf.cast(tf.equal(i, 0), tf.int64)
        y = first_round * pred + (1 - first_round) * y
        one_hot = tf.one_hot(y, num_classes)
        ########################
        logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits']
                  + end_points_vgg_16['logits']) / 3.0
        cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                        logits,
                                                        label_smoothing=0.0,
                                                        weights=1.0)
        noise = tf.gradients(cross_entropy, x)[0]
        noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3],
                                       keep_dims=True)
        noise = momentum * grad + noise
        x = x + alpha * tf.sign(noise)
        x = tf.clip_by_value(x, x_min, x_max)
        i = tf.add(i, 1)
        return x, y, i, x_max, x_min, noise
def target_graph(x, y, i, x_max, x_min, grad):

    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110
    #input image size[224,224,3]

    images3 = tf.image.resize_bilinear(input_diversity(x), [224, 224], align_corners=False)

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            images3, num_classes=num_classes, is_training=False, scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image1 = (((input_diversity(x) + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image1, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50')

    end_points_res_v1_50['logits'] = tf.squeeze(end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    image2 = (((input_diversity(x) + 1.0) * 0.5) * 255.0)
    processed_imgs_vgg_16 = preprocess_for_model(image2, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(
            processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])


    one_hot = tf.one_hot(y, num_classes)

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]

    noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME')
    noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1),
                               [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1),
                               [batch_size, 1, 1, 1])
    noise1 = tf.image.resize_images(noise, [140, 140], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    print("noise shape:", noise.shape)
    noise1 = alpha * tf.clip_by_value(tf.round(noise1), -2, 2)
    noise_paded = tf.pad(noise1,[[0, 0], [42, 42], [42, 42], [0, 0]], constant_values=0.)
    x = x - noise_paded
    x = tf.clip_by_value(x, x_min, x_max)
    print("x.shape:", x.shape)
    i = tf.add(i, 1)
    return x, y, i, x_max, x_min, noise
Пример #7
0
 def testModelHasExpectedNumberOfParameters(self):
   batch_size = 5
   height, width = 224, 224
   inputs = tf.random_uniform((batch_size, height, width, 3))
   with slim.arg_scope(inception.inception_v1_arg_scope()):
     inception.inception_v1_base(inputs)
   total_params, _ = slim.model_analyzer.analyze_vars(
       slim.get_model_variables())
   self.assertAlmostEqual(5607184, total_params)
Пример #8
0
def target_graph(x, y, i, x_max, x_min, grad):
    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')

    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    ########################
    one_hot = tf.one_hot(y, num_classes)
    ########################

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] +
              end_points_vgg_16['logits']) / 3.0
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    return x, y, i, x_max, x_min, noise
Пример #9
0
def build_single_inceptionv1(train_tfdata, is_train, dropout_keep_prob):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        identity, end_points = inception.inception_v1(
            train_tfdata,
            dropout_keep_prob=dropout_keep_prob,
            is_training=is_train)
        net = slim.avg_pool2d(end_points['Mixed_5c'], [7, 7],
                              stride=1,
                              scope='MaxPool_0a_7x7')
        net = slim.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        feature = tf.squeeze(net, [1, 2])
    return identity, feature
Пример #10
0
def run(name, image_size, num_classes):
  with tf.Graph().as_default():
    image = tf.placeholder("float", [1, image_size, image_size, 3], name="input")
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(image, num_classes, is_training=False, spatial_squeeze=False)
    probabilities = tf.nn.softmax(logits)
    init_fn = slim.assign_from_checkpoint_fn('inception_v1.ckpt', slim.get_model_variables('InceptionV1'))

    with tf.Session() as sess:
        init_fn(sess)
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess, "output/"+name)
Пример #11
0
    def _forward_pass(self, input_layer, trainable=True):
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            input_shape = input_layer.get_shape().as_list()
            batch_size = input_shape[0]
            logits, _ = inception.inception_v1(input_layer,
                                               self._hyparams.num_classes,
                                               is_training=False,
                                               spatial_squeeze=False)
            logits = tf.reshape(logits,
                                (batch_size, self._hyparams.num_classes))

        probabilities = tf.nn.softmax(logits)

        return probabilities
Пример #12
0
def ens_model(x):

    #input remains in[0,1]
    image = (x * 255.0)
    num_classes = 110
    processed_incv1 = preprocess_for_model(image, 'inception_v1')
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            processed_incv1,
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')

    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    ########################
    #one_hot = tf.one_hot(y, num_classes)
    ########################

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] +
              end_points_vgg_16['logits']) / 3.0
    print('logits.shape:', logits.shape)

    return logits
Пример #13
0
    def restore_model(self):
        with tf.Graph().as_default():
            # Prepare graph
            self.x_input = tf.placeholder(tf.float32, shape=self.input_shape)

            with tf.contrib.slim.arg_scope(inception.inception_v1_arg_scope()):
                _, end_points = inception.inception_v1(self.x_input, num_classes=self.nb_classes, is_training=False)
                self.pre_labels = tf.argmax(end_points['Predictions'], 1)
                self.features = end_points['Mixed_5c']

            # Restore Model
            saver = tf.train.Saver(tf.contrib.slim.get_model_variables())
            session_creator = tf.train.ChiefSessionCreator(
                scaffold=tf.train.Scaffold(saver=saver),
                checkpoint_filename_with_path=FLAGS.checkpoint_path)
            
            self.sess = tf.train.MonitoredSession(session_creator=session_creator)
Пример #14
0
def compute_embedding_inception_v1(inputs,
                                   embedding_dim=64,
                                   is_training=True,
                                   dropout_keep_prob=0.8,
                                   scope='InceptionV1',
                                   l2_normalize=True):
    """Compute embedding with inception v1."""
    with tf.variable_scope(scope, 'InceptionV1',
                           [inputs, embedding_dim]) as scope:
        with slim.arg_scope([layers.batch_norm, layers.dropout],
                            is_training=is_training):

            with slim.arg_scope(inception.inception_v1_arg_scope()):
                net, end_points = inception.inception_v1_base(inputs,
                                                              scope=scope)
                net = layers.avg_pool2d(net, [7, 7],
                                        stride=1,
                                        scope='AvgPool_0a_7x7')
                net = layers.dropout(net,
                                     dropout_keep_prob,
                                     scope='Dropout_0b')

            base_variables = slim.get_variables_to_restore(
                exclude=['global_step'])

            # Embedding bottleneck.
            net = layers.conv2d(net,
                                embedding_dim, [1, 1],
                                activation_fn=None,
                                normalizer_fn=None,
                                scope='Bottleneck')
            embedding = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
            if l2_normalize:
                embedding = tf.nn.l2_normalize(embedding, dim=1)
            end_points['embeddings'] = embedding

            bottleneck_variables = tf.contrib.framework.get_variables(
                scope='InceptionV1/Bottleneck')

    return embedding, end_points, base_variables, bottleneck_variables
Пример #15
0
def non_target_mi_fgsm_attack(input_dir, output_dir):

    # some parameter
    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    batch_shape = [batch_size, 224, 224, 3]

    #_check_or_create_dir(output_dir)

    with tf.Graph().as_default():
        # Prepare graph
        raw_inputs = tf.placeholder(tf.uint8, shape=[None, 224, 224, 3])

        # preprocessing for model input,
        # note that images for all classifier will be normalized to be in [-1, 1]
        #processed_imgs = preprocess_for_model(raw_inputs, 'inception_v1')

        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0)
        x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0)

        # y = tf.constant(np.zeros([batch_size]), tf.int64)
        #y = tf.placeholder(tf.int32, shape=[batch_size])
        y = tf.constant(np.zeros([batch_size]), tf.int64)
        i = tf.constant(0)
        #grad = tf.zeros(shape=batch_shape)
        if rand_init:
            grad = tf.random_uniform(tf.shape(x_input),
                                     tf.cast(-0.05, x_input.dtype),
                                     tf.cast(0.05, x_input.dtype),
                                     dtype=x_input.dtype)
        else:
            grad = tf.zeros(tf.shape(x_input))
        adv_x = x_input + grad
        adv_x = tf.clip_by_value(adv_x, x_min, x_max)
        #ori_x,x_adv, _, _, _, _, _ = tf.while_loop(stop, non_target_graph, [x_input, adv_x, y, i, x_max, x_min, grad])

        #training setting
        train_img = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])
        train_label = tf.placeholder(tf.float32, shape=[None, 110])
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
                train_img,
                num_classes=num_classes,
                is_training=True,
                scope='InceptionV1')

        predict = tf.argmax(tf.nn.softmax(end_points_inc_v1['Logits']), 1)
        logits = end_points_inc_v1['Logits']
        cost = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=train_label,
                                                    logits=logits))
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=learning_rate)
        train = optimizer.minimize(cost)
        accuracy = tf.reduce_mean(
            tf.cast(tf.equal(predict, tf.argmax(train_label, 1)), tf.float32))

        ori_x, x_adv, _, _, _, _, _ = tf.while_loop(
            stop, non_target_graph, [x_input, adv_x, y, i, x_max, x_min, grad])
        # Run computation
        s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV1'))
        s2 = tf.train.Saver(slim.get_model_variables(scope='resnet_v1_50'))
        s3 = tf.train.Saver(slim.get_model_variables(scope='vgg_16'))

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            s1.restore(sess, model_checkpoint_map['inception_v1'])
            s2.restore(sess, model_checkpoint_map['resnet_v1_50'])
            s3.restore(sess, model_checkpoint_map['vgg_16'])

            for time in range(epoch_num):
                train_acc = []
                count = 0
                train_loss = []
                val_acc = []
                val_loss = []
                epoch_learning_rate = init_learning_rate
                for filenames, raw_images, true_labels in load_images_with_true_label(
                        input_dir):
                    raw_images = list(raw_images)
                    adv_images = sess.run(
                        x_adv, feed_dict={x_input: np.array(raw_images[:16])})
                    raw_images[:16] = list(adv_images)
                    labels = one_hot(true_labels, 110)
                    img = (np.array(raw_images) + 1.0) * 0.5 * 255.0
                    img = preprocess_for_model(img, model_type, train=True)
                    train_feed_dict = {
                        train_img: img,
                        train_label: labels,
                        learning_rate: epoch_learning_rate
                    }
                    a, batch_loss = sess.run([train, cost],
                                             feed_dict=train_feed_dict)
                    batch_acc = accuracy.eval(feed_dict=train_feed_dict)
                    train_acc.append(batch_acc)
                    train_loss.append(batch_loss)
                    count += 1
                    if count % 100 == 0:
                        print('acc: ', np.mean(np.array(train_acc)), ' loss: ',
                              np.mean(np.array(train_loss)))
                        #break
                for filenames, raw_images, true_labels in load_images_with_true_label(
                        output_dir, train=False):
                    raw_images = list(raw_images)
                    adv_images = sess.run(
                        x_adv, feed_dict={x_input: np.array(raw_images[:16])})
                    raw_images[:16] = list(adv_images)
                    labels = one_hot(true_labels, 110)
                    img = (np.array(raw_images) + 1.0) * 0.5 * 255.0
                    img = preprocess_for_model(img, model_type, train=True)
                    train_feed_dict = {
                        train_img: img,
                        train_label: labels,
                        learning_rate: epoch_learning_rate
                    }
                    pre, batch_acc = sess.run([predict, accuracy],
                                              feed_dict=train_feed_dict)
                    val_acc.append(batch_acc)
                    val_loss.append(batch_loss)
                    count += 1
                    #break
                    #print(pre,'\n',true_labels,batch_acc)
                print('val_acc: ', np.mean(np.array(val_acc)), ' loss: ',
                      np.mean(np.array(val_loss)))
                s1.save(sess=sess,
                        save_path='./train_model/%s_%s.ckpt' %
                        (model_type, time))
    checkpoint_dir = os.path.join(CHECKPOINT_DIR, model_name, str(l_rate))

    # VAL_SUMMARY_DIR = "/data/summary/flowers/val"
    # log_dir = os.path.join(VAL_SUMMARY_DIR, model_name, str(l_rate), datetime.datetime.now().strftime("%Y%m%d-%H%M"))
    flowers_data_dir = "/data/flowers"
    batch_size = 10

    image_size = inception.inception_v1.default_image_size
    val_dataset = flowers.get_split('validation', flowers_data_dir)

    # Load the data
    images, labels = dataset.load_batch(
        val_dataset, batch_size, height=image_size, width=image_size, is_training=False)

    # Create the model:
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(images, num_classes=5, is_training=False)
        predictions = tf.argmax(logits, 1)

    checkpoint_path = tf.train.latest_checkpoint(checkpoint_dir)
    init_fn = slim.assign_from_checkpoint_fn(
      checkpoint_path,
      slim.get_variables_to_restore())

    # Choose the metrics to compute:
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
        "accuracy": slim.metrics.streaming_accuracy(predictions, labels),
        'precision': slim.metrics.streaming_precision(predictions, labels),
    })

    # Evaluate the model using 1000 batches of data:
Пример #17
0
def create_model(images, labels):
    """
    This methods initialize the inception v1 model with weights generated
    from training on the ImageNet dataset for all layers expect the last.
    The last layer is adjusted to output only 9 classes (instead of the
    1000 required for ImageNet). Note also that the methods set the model
    for fine-tuning meaning that during training only the last layer's
    weights can change.

    :param images: A tensor containing the images.
    :param labels: A tensor representing the correct labels for the images.

    :return restore_op: The operation used to restore the weights of the model.
    :return feed_dict: The feed_dict used for restoring the model.
    :return train_op: The train_op used to train the model.
    :return metrics_to_values: The metrics collected when training.
    :return metrics_to_updates: The metrics update op used when training.
    """
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        #  Load the deep learning model.
        logits, end_points = inception.inception_v1(images,
                                                    num_classes=NUM_CLASSES,
                                                    is_training=False)

        # We are going to train only the last layer of the model.
        trainable_layer = 'InceptionV1/Logits/Conv2d_0c_1x1'

        variables_to_restore = slim.get_variables_to_restore(
            exclude=[trainable_layer])
        variables_to_train = slim.get_variables_by_suffix('', trainable_layer)

        # Transform the labels into one hot encoding.
        one_hot_labels = tf.one_hot(
            labels,
            NUM_CLASSES,
        )

        # Define the loss function.
        loss = tf.losses.softmax_cross_entropy(
            one_hot_labels,
            end_points['Logits'],
        )

        # Select the optimizer.
        optimizer = tf.train.AdamOptimizer(1e-4)

        # Create a train op.
        train_op = tf.contrib.training.create_train_op(
            loss,
            optimizer,
            variables_to_train=variables_to_train,
        )

        predictions = tf.argmax(end_points['Predictions'],
                                1,
                                name="predictions")
        metrics_to_values, metrics_to_updates = \
            slim.metrics.aggregate_metric_map({
                'accuracy': tf.metrics.accuracy(predictions, labels),
                'mean_loss': tf.metrics.mean(loss),
            })

        # Define load predefined model operation.
        restore_op, feed_dict = slim.assign_from_checkpoint(
            'inception_v1.ckpt', variables_to_restore)

        return (
            restore_op,
            feed_dict,
            train_op,
            metrics_to_values,
            metrics_to_updates,
        )
Пример #18
0
def non_target_graph(ori_x, x, y, i, x_max, x_min,
                     grad):  #[x_input, adv_x, y, i, x_max, x_min, grad]
    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    alpha = eps_iter
    num_classes = 110
    #image = robust_resize(x)
    image = input_diversity(x)
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            image, num_classes=num_classes, is_training=False)
    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    #image = (((image + 1.0) * 0.5) * 255.0)
    image = (image + 1.0) * 0.5 * 255.0
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    #image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    image = (image / 255.0) * 2.0 - 1.0
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_adv, end_points_inc_adv = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='inception_adv')

    image = (image + 1.0) * 0.5 * 255.0
    #processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    #with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    #  logits_res_adv, end_points_res_adv = resnet_v1.resnet_v1_50(
    #    processed_imgs_res_v1_50, num_classes=num_classes,is_training=False,scope='adv_resnet')
    #end_points_res_adv['logits'] = tf.squeeze(end_points_res_adv['adv_resnet/logits'], [1, 2])
    #end_points_res_adv['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])

    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_adv, end_points_vgg_adv = vgg.vgg_16(
            processed_imgs_vgg_16,
            num_classes=num_classes,
            is_training=False,
            scope='adv_vgg')
    end_points_vgg_adv['logits'] = end_points_vgg_adv['adv_vgg/fc8']
    end_points_vgg_adv['probs'] = tf.nn.softmax(end_points_vgg_adv['logits'])

    ########################
    #logits = tf.cond(tf.less(i,cond_number),lambda: end_points_vgg_16['logits'],lambda:end_points_vgg_16['logits'])

    logits = (0.8*end_points_inc_v1['Logits'] + 1.1*end_points_res_v1_50['logits'] \
        + 0.6*end_points_vgg_16['logits'] + end_points_inc_adv['Logits'] \
        + end_points_vgg_adv['logits']) \
       /6.5
    # logits = tf.cond(tf.less(i,cond_number),lambda: logits,lambda:end_points_vgg_16['logits'])

    prediction = (end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] \
        + end_points_vgg_16['probs'] + end_points_inc_adv['Predictions'] \
        + end_points_vgg_adv['probs']) \
       /5.0
    pred = tf.argmax(prediction, 1)
    first_round = tf.cast(tf.equal(i, 0), tf.int64)
    y = first_round * pred + (1 - first_round) * y
    one_hot = tf.one_hot(y, num_classes)

    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    #another loss function
    #loss = tf.reshape(tf.nn.top_k(prediction,110)[0][:,0],[10,1])-tf.reshape(\
    #       tf.nn.top_k(prediction,110)[0][:,109],[10,1])
    #item1 = tf.reshape(tf.cast(tf.equal(pred,y),tf.float32),[batch_size,1])
    #item2 = tf.reshape(tf.cast(tf.equal(pred,y),tf.float32)-1.0,[batch_size,1])
    #loss = item1 * loss + item2 * loss
    loss = 1.0 * cross_entropy  # +0.2* loss

    noise = tf.gradients(loss, x)[0]
    kernel = gkern(7, sig).astype(np.float32)
    stack_kernel = np.stack([kernel, kernel, kernel]).swapaxes(2, 0)
    stack_kernel = np.expand_dims(stack_kernel, 3)
    noise = tf.nn.depthwise_conv2d(noise,
                                   stack_kernel,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME')

    #noise = noise / tf.reduce_mean(tf.abs(noise), [1,2,3], keep_dims=True)
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])

    #noise = clip_eta(noise, ord = 2, eps=eps)
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])

    perturb = tf.clip_by_value(tf.round(noise), -10, 10)
    x = x + alpha * perturb
    # x = x + alpha * tf.sign(noise)
    x = tf.clip_by_value(x, -1.0, 1.0)

    #part_x,x = change_size(ori_x,x,size = crop_size)
    #x = x+part_x
    ############################PGD################
    #eta = x - ori_x
    #eta = clip_eta(eta, ord = 2, eps=eps)
    #x = ori_x + eta
    #x = tf.clip_by_value(x, x_min, x_max)
    ##############################################
    i = tf.add(i, 1)
    #random_noise = tf.random_normal(x.shape,6/255,6/255,dtype=x.dtype)
    #x = tf.cond(tf.less(i,num_iter),lambda: x,lambda:random_noise+x)
    #x = tf.clip_by_value(x, -1, 1)
    return ori_x, x, y, i, x_max, x_min, noise
Пример #19
0
def non_target_graph(ori_x, x, y, i, x_max, x_min, grad):

    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    alpha = eps_iter
    num_classes = 110
    #image = preprocess_for_model(x, 'inception_v1')
    #image = x * 2.0 -1.0
    image = input_diversity(x)
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = ((image + 1.0) * 0.5) * 255.0
    #image = (image + 1.0)*0.5
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    #image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')
    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    image = (image / 255.0) * 2.0 - 1.0
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_adv, end_points_inc_adv = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='inception_adv')
    image = (image + 1.0) * 0.5 * 255.0
    #processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    #with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    #  logits_res_adv, end_points_res_adv = resnet_v1.resnet_v1_50(
    #    processed_imgs_res_v1_50, num_classes=num_classes,is_training=False,scope='adv_resnet')
    #end_points_res_adv['logits'] = tf.squeeze(end_points_res_adv['adv_resnet/logits'], [1, 2])
    #end_points_res_adv['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])

    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_adv, end_points_vgg_adv = vgg.vgg_16(
            processed_imgs_vgg_16,
            num_classes=num_classes,
            is_training=False,
            scope='adv_vgg')
    end_points_vgg_adv['logits'] = end_points_vgg_adv['adv_vgg/fc8']
    end_points_vgg_adv['probs'] = tf.nn.softmax(end_points_vgg_adv['logits'])

    ########################
    # Using model predictions as ground truth to avoid label leaking
    one_hot = tf.one_hot(y, num_classes)
    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] \
        + end_points_vgg_16['logits'] + end_points_inc_adv['Logits'] \
        + end_points_vgg_adv['logits']) \
       /5.0
    logits = tf.cond(tf.less(i, cond_number), lambda: logits,
                     lambda: end_points_vgg_16['logits'])
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]

    kernel = gkern(7, sig).astype(np.float32)
    stack_kernel = np.stack([kernel, kernel, kernel]).swapaxes(2, 0)
    stack_kernel = np.expand_dims(stack_kernel, 3)
    noise = tf.nn.depthwise_conv2d(noise,
                                   stack_kernel,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME')

    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    x = x - alpha * tf.clip_by_value(tf.round(noise), -10, 10)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    ############################PGD################
    #eta = x - ori_x
    #eta = clip_eta(eta, ord = np.inf, eps=eps)
    #x = ori_x + eta
    #x = tf.clip_by_value(x, x_min, x_max)
    ##############################################
    return ori_x, x, y, i, x_max, x_min, noise