示例#1
0
    def create_model(self,
                    images,
                    num_classes,
                    weight_decay=0.00004,
                    scope='Flowers',
                    reuse=None,
                    is_training=True):
        """Creates a base part of the Model (no gradients, no loss, no summaries).

        Args:
            images: A tensor of size [batch_size, height, width, channels].
            num_classes: The number of predicted classes.
            scope: Optional variable_scope.
            reuse: Whether or not the network or its variables should be reused. To
                be able to reuse 'scope' must be given.
            is_training: Whether is training or not.

        Returns:
            A named tuple OutputEndpoints.
        """
        with tf.variable_scope(scope, [images], reuse=reuse):
            with slim.arg_scope(inception_v3.inception_v3_arg_scope(weight_decay=weight_decay)):
                logits, endpoints = inception_v3.inception_v3(
                    inputs = images,
                    num_classes=num_classes,
                    is_training=is_training)
                return logits, endpoints
示例#2
0
def graph_small(x, target_class_input, i, x_max, x_min, grad):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    alpha = eps / 28
    momentum = FLAGS.momentum
    num_classes = 1001

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_v3, end_points_v3 = inception_v3.inception_v3(
            x, num_classes=num_classes, is_training=False)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2(
            x,
            num_classes=num_classes,
            is_training=False,
            scope='EnsAdvInceptionResnetV2')

    one_hot_target_class = tf.one_hot(target_class_input, num_classes)

    logits = (logits_v3 + 2 * logits_ensadv_res_v2) / 3
    auxlogits = (end_points_v3['AuxLogits'] +
                 2 * end_points_ensadv_res_v2['AuxLogits']) / 3
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                     auxlogits,
                                                     label_smoothing=0.0,
                                                     weights=0.4)
    noise = tf.gradients(cross_entropy, x)[0]
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]),
                                     axis=1), [FLAGS.batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]),
                                     axis=1), [FLAGS.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, target_class_input, i, x_max, x_min, noise
def modified_inception_v3(img, local, sex, age, reuse=None, trainable=True):

    with tf.variable_scope("Logits", reuse=reuse) as scope:
        with slim.arg_scope([slim.fully_connected],
                            scope="Logits",
                            reuse=reuse,
                            trainable=trainable):
            local_fc = slim.fully_connected(local, 2048, scope="Local_FC")
            local_logits = slim.fully_connected(local_fc,
                                                7,
                                                scope="Local_Logtis",
                                                activation_fn=None,
                                                normalizer_fn=None)
            sex_fc = slim.fully_connected(sex, 2048, scope="Sex_FC")
            sex_logits = slim.fully_connected(sex_fc,
                                              7,
                                              scope="Sex_Logtis",
                                              activation_fn=None,
                                              normalizer_fn=None)
            age_fc = slim.fully_connected(age, 2048, scope="Age_FC")
            age_logits = slim.fully_connected(age_fc,
                                              7,
                                              scope="Age_Logtis",
                                              activation_fn=None,
                                              normalizer_fn=None)

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits, end_points = inception_v3.inception_v3(img,
                                                       num_classes=7,
                                                       create_aux_logits=False,
                                                       is_training=trainable,
                                                       reuse=reuse)

    with tf.variable_scope(scope, auxiliary_name_scope=False) as scope1:
        with tf.name_scope(scope1.original_name_scope) as scope2:
            with slim.arg_scope([], scope=scope2, reuse=reuse):
                logits = tf.add(tf.add(tf.add(local_logits, sex_logits),
                                       age_logits),
                                logits,
                                name="Logits")
                pred = slim.softmax(logits, scope="Prediction")
    return logits, pred, end_points
示例#4
0
    def __init__(self, checkpoint_path):
        self.sess = tf.Session(config=tf.ConfigProto(
            allow_soft_placement=True))

        with tf.device("/gpu:0"):
            self.inputs = tf.placeholder(tf.float32,
                                         (None, image_size, image_size, 3))
            preprocessed_images = tf.map_fn(
                lambda input_img: inception_preprocessing.preprocess_image(
                    input_img, image_size, image_size, is_training=False),
                self.inputs)

            with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
                logits, _ = inception_v3.inception_v3(preprocessed_images,
                                                      num_classes=1001,
                                                      is_training=False)
                self.all_probabilities = tf.nn.softmax(logits)
                init_fn = slim.assign_from_checkpoint_fn(
                    checkpoint_path, slim.get_model_variables("InceptionV3"))

                init_fn(self.sess)
示例#5
0
def graph(x, y, i, x_max, x_min, accum_s, accum_g):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    # momentum = FLAGS.momentum
    num_classes = 1001
    beta_1 = FLAGS.beta_1
    beta_2 = FLAGS.beta_2

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_v3, end_points_v3 = inception_v3.inception_v3(
            x, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE)

    pred = tf.argmax(end_points_v3['Predictions'], 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_v3)
    grad = tf.gradients(cross_entropy, x)[0]

    grad = grad / tf.reduce_mean(tf.abs(grad), [1, 2, 3], keep_dims=True)

    accum_g = grad * (1 - beta_1) + accum_g * beta_1

    accum_s = tf.multiply(grad, grad) * (1 - beta_2) + accum_s * beta_2

    accum_g_hat = tf.divide(accum_g,
                            (1 - tf.pow(beta_1, tf.cast(i + 1, tf.float32))))

    accum_s_hat = tf.divide(accum_s,
                            (1 - tf.pow(beta_2, tf.cast(i + 1, tf.float32))))

    x = x + tf.multiply(tf.divide(alpha, tf.add(tf.sqrt(accum_s_hat), 1e-6)),
                        tf.sign(accum_g_hat))
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)

    return x, y, i, x_max, x_min, accum_s, accum_g
示例#6
0
def get_features_vector(url):
	try:
		with tf.Graph().as_default():
			image_string = urllib.urlopen(url).read()
			image = tf.image.decode_jpeg(image_string, channels=3)
			processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
			processed_images = tf.expand_dims(processed_image, 0)

			with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
				logits, _ = inception_v3.inception_v3(processed_images, num_classes=1001, is_training=False)

			init_fn = slim.assign_from_checkpoint_fn(
				os.path.join(checkpoints_dir, 'inception_v3.ckpt'),
				slim.get_model_variables('InceptionV3')
			)

			with tf.Session() as sess:
				init_fn(sess)
				np_logits = sess.run([logits])
				return np_logits
	except Exception as e:
		print(e)
示例#7
0
    def __init__(self):
        batch_shape = [None, 299, 299, 3]
        self.x_input = tf.placeholder(tf.float32, shape=batch_shape)
        self.target_label = tf.placeholder(tf.int32, shape=[None])
        target_onehot = tf.one_hot(self.target_label, 1001)

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits, end_points = inception_v3.inception_v3(self.x_input,
                                                           num_classes=1001,
                                                           is_training=False)

        self.predicted_labels = tf.argmax(end_points['Predictions'], 1)
        #logits -= tf.reduce_min(logits)
        #real = tf.reduce_max(logits * target_onehot, 1)
        #other = tf.reduce_max(logits * (1 - target_onehot), 1)
        #self.loss = other - real
        self.loss = tf.nn.softmax_cross_entropy_with_logits(
            labels=target_onehot, logits=logits)
        self.grad = 2 * tf.gradients(self.loss, self.x_input)[0]

        saver = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
        self.sess = tf.get_default_session()
        saver.restore(self.sess, FLAGS.checkpoint_path)
示例#8
0
def Grad(x, y, j, grad):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    momentum = FLAGS.momentum
    num_classes = 1001
    std = FLAGS.std
    x = x + tf.random_normal(
        shape=x.get_shape(), mean=0.0, stddev=std, dtype=tf.float32)
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_v3, end_points_v3 = inception_v3.inception_v3(
            x, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE)

    pred = tf.argmax(end_points_v3['Predictions'], 1)

    one_hot = tf.one_hot(y, num_classes)

    cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits_v3)
    noise = tf.gradients(cross_entropy, x)[0]
    noise = grad + tf.sign(noise)
    j = tf.add(j, 1)

    return x, y, j, noise
示例#9
0
def model(inputs, tag, num_class, is_training, regular=0.0001):
    net = mean_image_subtraction(inputs)
    if tag == 0:
        return le_net(net, num_class, is_training, regular)
    if tag == 1:
        return vgg.vgg_16(net, num_classes=num_class,
                          is_training=is_training)[0]
    if tag == 2:
        with slim.arg_scope(resnet_v1.resnet_arg_scope()):
            logits, end_points = resnet_v1.resnet_v1_50(
                net,
                num_classes=num_class,
                is_training=is_training,
                scope='resnet_v1_50')
            return tf.squeeze(logits, [1, 2], name='SpatialSqueeze')
    if tag == 3:
        return inception_v3.inception_v3(net,
                                         num_classes=num_class,
                                         is_training=is_training,
                                         scope='inception_v3')[0]
    if tag == 4:
        return inception_resnet_v2.inception_resnet_v2(
            net, num_class, True, scope='incep_resnetv2')[0]
def get_features_vectors(urls):
    result_logits = []
    for i in range(len(urls)):
        with tf.Graph().as_default():
            try:
                url = urls[i]
                image_string = urllib.urlopen(url).read()
                image = tf.image.decode_jpeg(image_string, channels=3)
                processed_image = inception_preprocessing.preprocess_image(
                    image, image_size, image_size, is_training=False)
                processed_images = tf.expand_dims(processed_image, 0)

                with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
                    logits, _ = inception_v3.inception_v3(processed_images,
                                                          num_classes=1001,
                                                          is_training=False)

                probabilities = tf.nn.softmax(logits)

                init_fn = slim.assign_from_checkpoint_fn(
                    os.path.join(checkpoints_dir, 'inception_v3.ckpt'),
                    slim.get_model_variables('InceptionV3'))

                with tf.Session() as sess:
                    init_fn(sess)
                    np_image, np_logits, probabilities = sess.run(
                        [image, logits, probabilities])
                    probabilities = probabilities[0, 0:]
                    result_logits.append(np_logits)

                print(url)
                print(np_logits)

            except Exception as e:
                print(e)
                continue
    return result_logits
示例#11
0
def model_fn(features, labels, mode, params):

    logits, end_points = inception_v3(features, FLAGS.num_classes)

    if mode in (tf.estimator.ModeKeys.PREDICT, tf.estimator.ModeKeys.EVAL):
        predicted_indices = tf.argmax(input=logits, axis=1)
        probabilities = end_points['Predictions']

    if mode in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL):
        global_step = tf.train.get_or_create_global_step()
        label_indices = tf.argmax(input=labels, axis=1)
        loss = tf.losses.softmax_cross_entropy(onehot_labels=tf.one_hot(
            labels, FLAGS.num_classes),
                                               logits=logits)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'classes': predicted_indices,
            'probabilities': probabilities
        }
        export_outputs = {
            'predictions': tf.estimator.export.PredictOutput(predictions)
        }
        return tf.estimator.EstimatorSpec(mode,
                                          predictions=predictions,
                                          export_outputs=export_outputs)

    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.RMSPropOptimizer(0.001, 0.9)
        train_op = optimizer.minimize(loss, global_step=global_step)
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
    if mode == tf.estimator.ModeKeys.EVAL:
        eval_metric_ops = {
            'accuracy': tf.metrics.accuracy(label_indices, predicted_indices)
        }
        return tf.estimator.EstimatorSpec(mode,
                                          loss=loss,
                                          eval_metric_ops=eval_metric_ops)
示例#12
0
def main(args):
    # load the dataset
    dataset = flowers.get_split('train', FLAGS.data_dir)
    # dataset = cifar10.get_split('train', FLAGS.data_dir)
    # load batch of dataset
    images, image_raw, labels = load_flower_batch(dataset,
                                                  FLAGS.batch_size,
                                                  is_training=True)
    #images, labels = load_mnist_batch(
    #    dataset,
    #    FLAGS.batch_size,
    #    is_training=True)
    # run the image through the model
    # predictions = lenet(images)
    logits, end_points = inception_v3(images, dataset.num_classes)

    # get the cross-entropy loss
    one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
    slim.losses.softmax_cross_entropy(logits, one_hot_labels)
    total_loss = slim.losses.get_total_loss()
    tf.summary.scalar('loss', total_loss)

    # use RMSProp to optimize
    optimizer = tf.train.RMSPropOptimizer(0.001, 0.9)

    # create train op
    train_op = slim.learning.create_train_op(total_loss,
                                             optimizer,
                                             summarize_gradients=True)

    # run training
    slim.learning.train(train_op,
                        FLAGS.train_log,
                        save_summaries_secs=30,
                        number_of_steps=5000,
                        save_interval_secs=60)
示例#13
0
    label_holder = tf.placeholder(shape=[input_size,7], dtype=tf.float32, name="Label_Holder")
    img = tf.Variable(img_holder, name="Img_Var", trainable=False)
    label = tf.Variable(label_holder, name="Label_Var", trainable=False)
    img_assign = img.assign(img_holder, name="Img_Assign")
    label_assign = label.assign(label_holder, name="Label_Assign")
    # define validation holders/vars/assign operation
    img_holder_val = tf.placeholder(shape=[input_size,299,299,3], dtype=tf.float32, name="Img_Holder_val")
    label_holder_val = tf.placeholder(shape=[input_size,7], dtype=tf.float32, name="Label_Holder_val")
    img_val = tf.Variable(img_holder_val, name="Img_Var_val", trainable=False)
    label_val = tf.Variable(label_holder_val, name="Label_Var_val", trainable=False)
    img_assign_val = img_val.assign(img_holder_val, name="Img_Assign_val")
    label_assign_val = label_val.assign(label_holder_val, name="Label_Assign_val")

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        # This defines the network we need to train
        logits, end_points = inception_v3.inception_v3(img, num_classes=7, create_aux_logits=False, is_training=True)
        # This one just create an alis of the network above for validation
        logits_val, _ = inception_v3.inception_v3(img_val, num_classes=7, create_aux_logits=False, is_training=False, reuse=tf.AUTO_REUSE)
    # set up loss
    loss = tf.losses.softmax_cross_entropy(label, logits)
    # Use the following line to seperate validation loss from traning process
    total_loss = tf.losses.get_total_loss()
    # loss for validation just for summary purposes
    loss_val = tf.losses.softmax_cross_entropy(label_val, logits_val, loss_collection="validation")
    # set decay learning rate
    learning_rate = tf.train.exponential_decay(START_LR, tf.train.get_or_create_global_step(), DECAY_STEP, DECAY_RATE)
    # creat train op
    opt = tf.train.AdamOptimizer(learning_rate)
    # creat train fn that will be fed into a slim.train wrapper later
    train_tensor = slim.learning.create_train_op(total_loss, optimizer=opt)
    # Creat Summary
示例#14
0
    os.path.join(checkpoint_path, 'resnet_v2_101.ckpt')
}

if __name__ == '__main__':
    f2l = load_labels('./dev_data/val_rs.csv')
    input_dir = './outputs'

    batch_shape = [50, 299, 299, 3]
    num_classes = 1001
    tf.logging.set_verbosity(tf.logging.ERROR)

    with tf.Graph().as_default():
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_v3, end_points_v3 = inception_v3.inception_v3(
                x_input, num_classes=num_classes, is_training=False)

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3(
                x_input,
                num_classes=num_classes,
                is_training=False,
                scope='AdvInceptionV3')

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3(
                x_input,
                num_classes=num_classes,
                is_training=False,
                scope='Ens3AdvInceptionV3')
def main(_):
    start = time.clock()
    # Images for inception classifier are normalized to be in [-1, 1] interval,
    # eps is a difference between pixels so it should be in [0, 2] interval.
    # Renormalizing epsilon from [0, 255] to [0, 2].
    # eps = 2.0 * FLAGS.max_epsilon / 255.0
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    tf.logging.set_verbosity(tf.logging.INFO)

    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    momentum = FLAGS.momentum
    num_classes = 1001

    with tf.Session() as sess:
        x = tf.placeholder(dtype=tf.float32, shape=batch_shape)

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_v3, end_points_v3 = inception_v3.inception_v3(
                x, num_classes=num_classes, is_training=False)
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3(
                x,
                num_classes=num_classes,
                is_training=False,
                scope='AdvInceptionV3')
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3(
                x,
                num_classes=num_classes,
                is_training=False,
                scope='Ens3AdvInceptionV3')
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3(
                x,
                num_classes=num_classes,
                is_training=False,
                scope='Ens4AdvInceptionV3')
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            logits_v4, end_points_v4 = inception_v4.inception_v4(
                x, num_classes=num_classes, is_training=False)
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2(
                x, num_classes=num_classes, is_training=False)
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2(
                x,
                num_classes=num_classes,
                is_training=False,
                scope='EnsAdvInceptionResnetV2')
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101(
                x, num_classes=num_classes, is_training=False)

        pred = tf.argmax(
            end_points_v3['Predictions'] + end_points_adv_v3['Predictions'] + end_points_ens3_adv_v3['Predictions'] + \
            end_points_ens4_adv_v3['Predictions'] + end_points_v4['Predictions'] + \
            end_points_res_v2['Predictions'] + end_points_ensadv_res_v2['Predictions'] + end_points_resnet['predictions'], 1)

        y = tf.placeholder(tf.int32, shape=[
            FLAGS.batch_size,
        ])
        one_hot = tf.one_hot(y, num_classes)

        logits_dict = [
            logits_v3, logits_adv_v3, logits_ens3_adv_v3, logits_ens4_adv_v3,
            logits_v4, logits_res_v2, logits_ensadv_res_v2, logits_resnet
        ]

        logits_gravity = tf.placeholder(dtype=tf.float32,
                                        shape=[FLAGS.batch_size, 8])

        logits0 = (logits_dict[0][0] * logits_gravity[0][0] + logits_dict[1][0] * logits_gravity[0][1] + logits_dict[2][0] * logits_gravity[0][2]\
                  + logits_dict[3][0] * logits_gravity[0][3] + logits_dict[4][0] * logits_gravity[0][4] + logits_dict[5][0] * logits_gravity[0][5]\
                  + logits_dict[6][0] * logits_gravity[0][6] + logits_dict[7][0] * logits_gravity[0][7]) / 36

        logits1 = (logits_dict[0][1] * logits_gravity[1][0] + logits_dict[1][1] * logits_gravity[1][1] + logits_dict[2][1] * logits_gravity[1][2]\
                  + logits_dict[3][1] * logits_gravity[1][3] + logits_dict[4][1] * logits_gravity[1][4] + logits_dict[5][1] * logits_gravity[1][5]\
                  + logits_dict[6][1] * logits_gravity[1][6] + logits_dict[7][1] * logits_gravity[1][7]) / 36

        logits0 = tf.reshape(logits0, [1, 1001])
        logits1 = tf.reshape(logits1, [1, 1001])
        logits = tf.concat([logits0, logits1], 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 = noise / tf.reduce_sum(tf.abs(noise), [1, 2, 3], keep_dims=True)
        grad = tf.placeholder(tf.float32, shape=batch_shape)
        noise = momentum * grad + noise
        adv = x + alpha * tf.sign(noise)
        x_max = tf.placeholder(tf.float32, shape=batch_shape)
        x_min = tf.placeholder(tf.float32, shape=batch_shape)
        adv = tf.clip_by_value(adv, x_min, x_max)

        # Run computation
        s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
        s2 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3'))
        s3 = tf.train.Saver(
            slim.get_model_variables(scope='Ens3AdvInceptionV3'))
        s4 = tf.train.Saver(
            slim.get_model_variables(scope='Ens4AdvInceptionV3'))
        s5 = tf.train.Saver(slim.get_model_variables(scope='InceptionV4'))
        s6 = tf.train.Saver(
            slim.get_model_variables(scope='InceptionResnetV2'))
        s7 = tf.train.Saver(
            slim.get_model_variables(scope='EnsAdvInceptionResnetV2'))
        s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2'))

        s1.restore(sess, FLAGS.checkpoint_path_inception_v3)
        s2.restore(sess, FLAGS.checkpoint_path_adv_inception_v3)
        s3.restore(sess, FLAGS.checkpoint_path_ens3_adv_inception_v3)
        s4.restore(sess, FLAGS.checkpoint_path_ens4_adv_inception_v3)
        s5.restore(sess, FLAGS.checkpoint_path_inception_v4)
        s6.restore(sess, FLAGS.checkpoint_path_inception_resnet_v2)
        s7.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2)
        s8.restore(sess, FLAGS.checkpoint_path_resnet)

        sum = 0
        failure_num = 0
        l2_distance = 0
        label_distance = 0
        images = []

        for filenames, images in load_images(FLAGS.input_dir, batch_shape):
            images = images.astype(np.float32)  #不知道需不需要!!!!!!!!!!!!!!!!!!!!!!!
            images_flatten_initial = images.reshape((2, 268203))
            sum += len(filenames)
            # 对于每个图片在迭代生成对抗样本的过程中,x_max和x_min 是不变的!!!!!!!
            x_Max = np.clip(images + eps, -1.0, 1.0)
            x_Min = np.clip(images - eps, -1.0, 1.0)

            prediction = []
            Noise = []

            for i in range(FLAGS.num_iter):
                if i == 0:
                    prediction = sess.run(pred, feed_dict={x: images})
                    print('true_label::::::::', prediction)

                # x可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                End_points_v3, End_points_adv_v3, End_points_ens3_adv_v3, End_points_ens4_adv_v3, End_points_v4, End_points_res_v2, End_points_ensadv_res_v2, End_points_resnet = \
                sess.run([end_points_v3, end_points_adv_v3, end_points_ens3_adv_v3, end_points_ens4_adv_v3, end_points_v4, end_points_res_v2, end_points_ensadv_res_v2, end_points_resnet], feed_dict={x: images, y: prediction})

                logits_proportion = []
                for j in range(FLAGS.batch_size):
                    end_points_v3_Pred = End_points_v3['Predictions'][j][
                        prediction[j]]
                    end_points_adv_v3_Pred = End_points_adv_v3['Predictions'][
                        j][prediction[j]]
                    end_points_ens3_adv_v3_Pred = End_points_ens3_adv_v3[
                        'Predictions'][j][prediction[j]]
                    end_points_ens4_adv_v3_Pred = End_points_ens4_adv_v3[
                        'Predictions'][j][prediction[j]]
                    end_points_v4_Pred = End_points_v4['Predictions'][j][
                        prediction[j]]
                    end_points_res_v2_Pred = End_points_res_v2['Predictions'][
                        j][prediction[j]]
                    end_points_ensadv_res_v2_Pred = End_points_ensadv_res_v2[
                        'Predictions'][j][prediction[j]]
                    end_points_resnet_Pred = End_points_resnet['predictions'][
                        j][prediction[j]]

                    print('end_points_v3_Pred::::::', end_points_v3_Pred)
                    print('end_points_adv_v3_Pred::::::',
                          end_points_adv_v3_Pred)
                    print('end_points_ens3_adv_v3_Pred::::::',
                          end_points_ens3_adv_v3_Pred)
                    print('end_points_ens4_adv_v3_Pred::::::',
                          end_points_ens4_adv_v3_Pred)
                    print('end_points_v4_Pred::::::', end_points_v4_Pred)
                    print('end_points_res_v2_Pred::::::',
                          end_points_res_v2_Pred)
                    print('end_points_ensadv_res_v2_Pred::::::',
                          end_points_ensadv_res_v2_Pred)
                    print('end_points_resnet_Pred::::::',
                          end_points_resnet_Pred)

                    ens_Pred_Value = np.array([
                        end_points_v3_Pred, end_points_adv_v3_Pred,
                        end_points_ens3_adv_v3_Pred,
                        end_points_ens4_adv_v3_Pred, end_points_v4_Pred,
                        end_points_res_v2_Pred, end_points_ensadv_res_v2_Pred,
                        end_points_resnet_Pred
                    ])
                    print('ens_Pred_Value:::::', ens_Pred_Value)
                    TopKFitIndx = np.argsort(ens_Pred_Value)

                    a = [0.0] * 8

                    for m in range(8):
                        a[TopKFitIndx[m]] = 8 - m
                        # a[m] = 1
                    logits_proportion.append(a)

                if i == 0:
                    Grad = np.zeros(shape=[FLAGS.batch_size, 299, 299, 3],
                                    dtype=np.float32)
                    Noise, images = sess.run(
                        [noise, adv],
                        feed_dict={
                            x: images,
                            y: prediction,
                            logits_gravity: logits_proportion,
                            grad: Grad,
                            x_max: x_Max,
                            x_min: x_Min
                        })  # x可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                else:
                    Noise, images = sess.run(
                        [noise, adv],
                        feed_dict={
                            x: images,
                            y: prediction,
                            logits_gravity: logits_proportion,
                            grad: Noise,
                            x_max: x_Max,
                            x_min: x_Min
                        })  # Noise可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            print('images::::::', images)
            adv_prediction = sess.run(pred, feed_dict={x: images})
            images_flatten_adv = images.reshape((2, 268203))
            save_images(images, filenames, FLAGS.output_dir)

            l2_diatance_list = np.linalg.norm(
                (images_flatten_initial - images_flatten_adv),
                axis=1,
                keepdims=True)
            for n in range(len(filenames)):
                l2_distance += l2_diatance_list[n]
            for j in range(len(filenames)):
                label_distance += abs(prediction[j] - adv_prediction[j])
                if int(prediction[j]) == adv_prediction[j]:
                    failure_num += 1
                    print('failure_num:::::', failure_num)
            print('Prediction:::::::', adv_prediction)

        print('sum::::', sum)
        print('failure_num::::', failure_num)
        rate_wrong = failure_num / sum
        print('rate_wrong:::::::', rate_wrong)
        print('l2_distance:::::::', l2_distance)
        print('label_distance::::::', label_distance)
        end = time.clock()
        print('run time::::::::', end - start)
示例#16
0
def target_graph(x, y, i, x_max, x_min, grad, label, x_ori):
    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = FLAGS.num_classes

    # find adversarial example in perlin noise direction
    x = x + tf.random_uniform(tf.shape(x), minval=-1e-2, maxval=1e-2) * \
        create_perlin_noise(seed=None, color=True, batch_size=FLAGS.batch_size, image_size=FLAGS.image_height, normalize=True, precalc_fade=None)

    # dropout
    x = generate_jitter_sample(x_ori, x, fade_eps=0, width=np.random.randint(low=1, high=12))

    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_in_v1 = 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(
            structure(processed_imgs_in_v1), 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
    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(
            structure(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['while/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(
            structure(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'])

    processed_imgs_in_v3 = preprocess_for_model(image, 'inception_v3')
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_in_v3, end_points_in_v3 = inception_v3.inception_v3(
            structure_res(processed_imgs_in_v3), num_classes=num_classes, is_training=False, scope='InceptionV3')

    processed_res_v2 = preprocess_for_model(image, 'resnet_v2_152')
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits_res_v2, end_points_res_v2 = resnet_v2.resnet_v2_152(
            structure_res(processed_res_v2), num_classes=num_classes, is_training=False, scope='resnet_v2_152')
    end_points_res_v2['logits'] = tf.squeeze(end_points_res_v2['resnet_v2_152/logits'], [1, 2])
    end_points_res_v2['probs'] = tf.nn.softmax(end_points_res_v2['logits'])

    one_hot = tf.one_hot(y, num_classes)

    # separate the loss
    cross_entropy_v1 = tf.losses.softmax_cross_entropy(one_hot,
                                                    end_points_inc_v1['Logits'],
                                                    label_smoothing=0.0,
                                                    weights=1.0)

    cross_entropy_re = tf.losses.softmax_cross_entropy(one_hot,
                                                    end_points_res_v1_50['logits'],
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy_vgg = tf.losses.softmax_cross_entropy(one_hot,
                                                    end_points_vgg_16['logits'],
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy_re2 = tf.losses.softmax_cross_entropy(one_hot,
                                                    end_points_res_v2['logits'],
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy_v3 = tf.losses.softmax_cross_entropy(one_hot,
                                                     end_points_in_v3['Logits'],
                                                     label_smoothing=0.0,
                                                     weights=1.0)


    pred = tf.argmax(end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] + end_points_vgg_16['probs']+end_points_res_v2['probs'], 1)


    first_round = tf.cast(tf.equal(i, 0), tf.int64)
    label = first_round * pred + (1 - first_round) * label

    noise_re = tf.gradients(cross_entropy_re,x)[0]
    noise_re2 = tf.gradients(cross_entropy_re2,x)[0]
    noise_v1 = tf.gradients(cross_entropy_v1,x)[0]
    noise_vgg = tf.gradients(cross_entropy_vgg,x)[0]
    noise_v3 = tf.gradients(cross_entropy_v3,x)[0]

    noise_re = tf.Print(noise_re, [i, cross_entropy_re, cross_entropy_re2, cross_entropy_v1, cross_entropy_vgg, cross_entropy_v3])

    noise_re = noise_re / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise_re, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.batch_size, 1, 1, 1])
    noise_re2 = noise_re2 / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise_re2, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.batch_size, 1, 1, 1])
    noise_v1 = noise_v1 / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise_v1, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.batch_size, 1, 1, 1])
    noise_vgg = noise_vgg / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise_vgg, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.batch_size, 1, 1, 1])
    noise_v3 = noise_v3 / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise_v3, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.batch_size, 1, 1, 1])

    noise = momentum * grad + noise_re + noise_re2 + noise_v1 + noise_vgg + noise_v3

    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1),
        [FLAGS.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, label, x_ori
def graph(x, y, i, x_max, x_min, grad):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    momentum = FLAGS.momentum
    num_classes = 1001

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_v3, end_points_v3 = inception_v3.inception_v3(
            x, num_classes=num_classes, is_training=False)

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3(
            x,
            num_classes=num_classes,
            is_training=False,
            scope='AdvInceptionV3')

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3(
            x,
            num_classes=num_classes,
            is_training=False,
            scope='Ens3AdvInceptionV3')

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3(
            x,
            num_classes=num_classes,
            is_training=False,
            scope='Ens4AdvInceptionV3')

    with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
        logits_v4, end_points_v4 = inception_v4.inception_v4(
            x, num_classes=num_classes, is_training=False)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2(
            x, num_classes=num_classes, is_training=False)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2(
            x,
            num_classes=num_classes,
            is_training=False,
            scope='EnsAdvInceptionResnetV2')

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101(
            x, num_classes=num_classes, is_training=False)

    pred = tf.argmax(end_points_v3['Predictions'] + end_points_adv_v3['Predictions'] + end_points_ens3_adv_v3['Predictions'] + \
                    end_points_ens4_adv_v3['Predictions'] + end_points_v4['Predictions'] + \
                    end_points_res_v2['Predictions'] + end_points_ensadv_res_v2['Predictions'] + end_points_resnet['predictions'], 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 = (logits_v3 + 0.25 * logits_adv_v3 + logits_ens3_adv_v3 + \
             logits_ens4_adv_v3 + logits_v4 + \
             logits_res_v2 + logits_ensadv_res_v2 + logits_resnet) / 7.25
    auxlogits = (end_points_v3['AuxLogits'] + 0.25 * end_points_adv_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + \
                end_points_ens4_adv_v3['AuxLogits'] + end_points_v4['AuxLogits'] + \
                end_points_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits']) / 6.25
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(one_hot,
                                                     auxlogits,
                                                     label_smoothing=0.0,
                                                     weights=0.4)
    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
示例#18
0
def graph(x, target_class_input, i, x_max, x_min, grad):
  eps = 2.0 * FLAGS.max_epsilon / 255.0
  alpha = eps / FLAGS.iterations
  momentum = FLAGS.momentum
  num_classes = 1001

  x_div = input_diversity(x)
  #x_div = x

  with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
    logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3(
        x_div, num_classes=num_classes, is_training=False, scope='AdvInceptionV3')

  with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
    logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3(
        x_div, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3')

  with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
    logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3(
        x_div, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3')

  with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
    logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2(
        x_div, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2')

  with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
    logits_adv_res_v2, end_points_adv_res_v2 = inception_resnet_v2.inception_resnet_v2(
        x_div, num_classes=num_classes, is_training=False, scope='AdvInceptionResnetV2')
            
  one_hot_target_class = tf.one_hot(target_class_input, num_classes)

  logits = (logits_adv_v3 + logits_ens3_adv_v3 + 
    logits_ens4_adv_v3 + logits_ensadv_res_v2 + logits_adv_res_v2) / 5

  auxlogits = (end_points_adv_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + 
    end_points_ens4_adv_v3['AuxLogits'] + end_points_adv_res_v2['AuxLogits'] + 
    end_points_ensadv_res_v2['AuxLogits']) / 5
  
  cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                  logits,
                                                  label_smoothing=0.0,
                                                  weights=1.0)
  cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                   auxlogits,
                                                   label_smoothing=0.0,
                                                   weights=0.4)
  noise = tf.gradients(cross_entropy, x)[0]

  kernel = gkern(7, FLAGS.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, [FLAGS.batch_size, -1]), axis=1), 
    [FLAGS.batch_size, 1, 1, 1])
  noise = momentum * grad + noise
  noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), 
    [FLAGS.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, target_class_input, i, x_max, x_min, noise
    processed_image = inception_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False)

    # 可以批量导入图像
    # 第一个维度指定每批图片的张数
    # 我们每次只导入一张图片
    processed_images = tf.expand_dims(processed_image, 0)

    # 创建模型,使用默认的arg scope参数
    # arg_scope是slim library的一个常用参数
    # 可以设置它指定网络层的参数,比如stride, padding 等等。

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        ## num_classes=1001是对于imagenet而言的 至于为什么不是1000它的原网页有解释,如果是你的3类 写3就好了
        logits, _ = inception_v3.inception_v3(processed_images,
                                              num_classes=1001,
                                              is_training=False)

    # 我们在输出层使用softmax函数,使输出项是概率值
    probabilities = tf.nn.softmax(logits)

    # 创建一个函数,从checkpoint读入网络权值
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_v3.ckpt'),
        slim.get_variables_to_restore())

    with tf.Session() as sess:
        # 加载权值
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
示例#20
0
			if gray[row][col] < 0.1:
				color[row][col][0] = 0.0
				color[row][col][1] = 0.0
				color[row][col][2] = 0.0

	gray = EliminateIsolatedPixels(color)
	row_b, row_e, col_b, col_e = get_box_coord(gray)
	box = color_orig[row_b:row_e, col_b:col_e]
	return box


with graph.as_default():
	images = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))

	with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
		_, end_points = inception_v3.inception_v3(images, is_training=False, num_classes=1001)

		# Restore the checkpoint
		sess = tf.Session(graph=graph)
		saver = tf.train.Saver()
		saver.restore(sess, ckpt_file)

	# Construct the scalar neuron tensor.
	logits = graph.get_tensor_by_name('InceptionV3/Logits/SpatialSqueeze:0')
	neuron_selector = tf.placeholder(tf.int32)
	y = logits[0][neuron_selector]

	# Construct tensor for predictions.
	prediction = tf.argmax(logits, 1)

def GetSalientImage(imagePath):
def run_test(read_csv, logger):
    img_folder = '/home/zgwu/HandImages/long_video/test_frames/'
    save_folder = '/home/zgwu/HandImages/long_video/double_frames/'
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)
    label_dict = read_test_csv_from(read_csv)
    input_images = tf.placeholder(dtype=tf.float32,
                                  shape=[None, 224, 224, 3],
                                  name='input')
    # placeholder holds an input tensor for classification
    if ModelType == 'mobilenet':
        out, end_points = mobilenet_v2.mobilenet(input_tensor=input_images,
                                                 num_classes=num_classes,
                                                 depth_multiplier=1.0,
                                                 is_training=False)
    else:
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            out, end_points = inception_v3.inception_v3(
                inputs=input_images,
                num_classes=num_classes,
                is_training=False)
    detection_graph, sessd = detector_utils.load_inference_graph(
    )  # ssd to detect hands
    saver = tf.train.Saver()
    sess = tf.Session()
    saver.restore(sess, CKPT)
    CM = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0]]
    D = {
        'num_pictures': 0,
        'acc': 0,
        'num_hands': 0,
        'detect_t': 0.0,
        'classify_t': 0.0,
        'o': [0, 0, 0, 0, 0, 0, 0],
        'd': [0, 0, 0, 0, 0, 0, 0],
        'r': [0, 0, 0, 0, 0, 0, 0],
        'tp': [0, 0, 0, 0, 0, 0, 0]
    }
    tot_count = 0
    y_true, y_pred = [], []

    label_matrix = np.empty((0, num_classes), dtype=int)
    score_matrix = np.empty((0, num_classes), dtype=int)
    for num_img, (img_name, frame_label) in enumerate(label_dict.items()):
        tot_count += 1
        if tot_count % 100 == 1:
            print('Process {} --- {} / {}'.format(img_name, tot_count,
                                                  len(label_dict)))
        l, t, r, b, clazz = frame_label
        acc_index = labelsDict[clazz]
        D['o'][acc_index] += 1  # confusion matrix

        # filename = os.path.basename(img_name)
        name, ext = os.path.splitext(img_name)
        # print("Processing the image : " + name + " ... {}/{}".format(num_img+1, len(label_dict)))
        key = cv2.waitKey(5) & 0xff  ## Use Esc key to close the program
        if key == 27:
            break
        if key == ord('p'):
            cv2.waitKey(0)
        image_raw = cv2.imread(os.path.join(img_folder, img_name))
        image_np = cv2.resize(image_raw, (im_width, im_height))
        try:
            image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
        except:
            print("Error converting to RGB")
        start = time.time()  # set time
        boxes, scores = detector_utils.detect_objects(image_np,
                                                      detection_graph, sessd)
        hands = from_image_crop_boxes(num_hands_detect, score_thresh, scores,
                                      boxes, im_width, im_height, 0.5)
        endh = time.time()  # detect hand
        detect_t = endh - start
        D['detect_t'] += detect_t
        if not hands:
            continue
        else:
            D['num_pictures'] += 1
        D['d'][acc_index] += 1
        for rank, rect in enumerate(hands):
            D['num_hands'] += 1
            left, top, right, bottom = rect
            region = image_np[top:bottom, left:right]
            region = cv2.resize(region, (224, 224), cv2.INTER_AREA)
            # feed = np.expand_dims(region, axis=0)   # maybe it's a wrong format to feed
            img_data = tf.image.convert_image_dtype(np.array(region)[:, :,
                                                                     0:3],
                                                    dtype=tf.float32)  # RGB
            # elements are in [0,1)
            resized_img = tf.image.resize_images(img_data,
                                                 size=[224, 224],
                                                 method=0)
            # decode an image
            img = resized_img.eval(session=sess)
            img.resize([1, 224, 224, 3])
            # input an image array and inference to get predictions and set normal format
            predictions = end_points['Predictions'].eval(
                session=sess, feed_dict={input_images: img})

            label = np.zeros((1, num_classes), dtype=int)
            label[0, acc_index] = 1
            label_matrix = np.append(label_matrix, label, axis=0)
            score_matrix = np.append(score_matrix,
                                     predictions.reshape([1, num_classes]),
                                     axis=0)
            #print(label, predictions.reshape([1, num_classes]))

            predictions.resize([num_classes])
            np.set_printoptions(precision=4, suppress=True)
            index = int(np.argmax(predictions))
            y_true.append(acc_index)
            y_pred.append(index)
            D['r'][index] += 1
            msg = img_name + ' ' + clazz + ' ' + labelsStr[index]
            CM[acc_index][index] += 1

            if index == acc_index:
                D['acc'] += 1
                D['tp'][index] += 1

            logger.info(msg)
            if key == ord('s'):
                region = cv2.cvtColor(region, cv2.COLOR_RGB2BGR)
                cv2.imwrite(
                    frame_path + name + '_' + str(D['num_frames']) + '_' +
                    str(rank) + '.jpg', region)
                cv2.waitKey(0)
        endr = time.time()
        classify_t = endr - endh
        D['classify_t'] += classify_t
    print(
        "From {} pictures, we detect {} hands with {} accurate prediction ({:.2f})"
        .format(tot_count, D['num_hands'], D['acc'],
                D['acc'] / D['num_hands']))
    result_log = '\n@@images_count: {} and detect_count: {}'.format(tot_count, D['num_pictures']) + \
                 '\n@@image_size: (width : {}, height: {})'.format(im_width, im_height) + \
                 '\n@@num_hand_detect: {} - {}%'.format(D['num_hands'], int(100 * D['num_hands'] / tot_count)) + \
                 '\n@@each_elapsed_time: (detect_hands: {:.4f}s, classify_hand: {:.4f}s)'.format(
                     D['detect_t'] / tot_count, D['classify_t'] / D['num_hands']) + \
                 '\n@@classify_result: Fist  Admire  Victory  Okay  None  Palm  Six' + \
                 '\n                   {: <6d}{: <8d}{: <9d}{: <6d}{: <6d}{: <6d}{} -- origin classes' \
                 '\n                   {: <6d}{: <8d}{: <9d}{: <6d}{: <6d}{: <6d}{} -- detect classes' \
                 '\n                   {: <6d}{: <8d}{: <9d}{: <6d}{: <6d}{: <6d}{} -- recognize count' \
                 '\n                   {: <6d}{: <8d}{: <9d}{: <6d}{: <6d}{: <6d}{} -- true positive' \
                     .format(D['o'][0], D['o'][1], D['o'][2], D['o'][3],D['o'][4], D['o'][5], D['o'][6],
                     D['d'][0], D['d'][1], D['d'][2], D['d'][3],D['d'][4], D['d'][5], D['d'][6],
                     D['r'][0], D['r'][1], D['r'][2], D['r'][3], D['r'][4], D['r'][5], D['r'][6],
                     D['tp'][0], D['tp'][1], D['tp'][2], D['tp'][3], D['tp'][4], D['tp'][5], D['tp'][6]) + \
                 '\n@@accuracy: {}/{} - {}%'.format(D['acc'], D['num_hands'], int(100 * D['acc'] / D['num_hands'])) + \
                 '\n' + '-' * 100 + \
                 '\n' + str(CM)
    #print(result_log)
    logger.info(result_log)
    #print(classification_report(y_true, y_pred, target_names=labelsStr, digits=3))
    logger.info(
        str(
            classification_report(y_true,
                                  y_pred,
                                  target_names=labelsStr,
                                  digits=3)))

    print(label_matrix.shape, score_matrix.shape)
    # 计算每一类的ROC
    fpr = dict()
    tpr = dict()
    roc_auc = dict()

    # Compute micro-average ROC curve and ROC area(方法二)
    fpr["micro"], tpr["micro"], _ = roc_curve(label_matrix.ravel(),
                                              score_matrix.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

    # FPR就是横坐标,TPR就是纵坐标
    plt.plot(fpr["micro"],
             tpr["micro"],
             c='r',
             lw=2,
             alpha=0.7,
             label=u'AUC=%.3f' % roc_auc["micro"])
    plt.plot((0, 1), (0, 1), c='#808080', lw=1, ls='--', alpha=0.7)
    plt.xlim((-0.01, 1.02))
    plt.ylim((-0.01, 1.02))
    plt.xticks(np.arange(0, 1.1, 0.1))
    plt.yticks(np.arange(0, 1.1, 0.1))
    plt.xlabel('False Positive Rate', fontsize=13)
    plt.ylabel('True Positive Rate', fontsize=13)
    plt.grid(b=True, ls=':')
    plt.legend(loc='lower right', fancybox=True, framealpha=0.8, fontsize=12)
    plt.title(u'The ROC and AUC of MobileNet Classifier.', fontsize=17)
    plt.show()
    """
示例#22
0
def main(_):
    start = time.clock()
    # Images for inception classifier are normalized to be in [-1, 1] interval,
    # eps is a difference between pixels so it should be in [0, 2] interval.
    # Renormalizing epsilon from [0, 255] to [0, 2].
    eps = 2.0 * FLAGS.max_epsilon / 255.0

    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]

    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        x_adv_v3 = 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([FLAGS.batch_size]), tf.int64)
        i = tf.constant(0)
        grad = tf.zeros(shape=batch_shape)
        pred = tf.zeros(shape=[
            2,
        ], dtype=tf.int64)
        Prediction_adv_v3 = tf.zeros(shape=[
            2,
        ], dtype=tf.int64)
        x_adv, _, label, _, _, _, _, Pred = tf.while_loop(
            stop, graph, [x_input, x_adv_v3, y, i, x_max, x_min, grad, pred])

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits_adv_v3_one, end_points_adv_v3_one = inception_v3.inception_v3(
                x_adv_v3,
                num_classes=1001,
                is_training=False,
                reuse=True,
                scope='AdvInceptionV3')
        pred_adv_v3 = tf.argmax(end_points_adv_v3_one['Predictions'], axis=1)

        # Run computation
        s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
        s2 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3'))
        s3 = tf.train.Saver(
            slim.get_model_variables(scope='Ens3AdvInceptionV3'))
        s4 = tf.train.Saver(
            slim.get_model_variables(scope='Ens4AdvInceptionV3'))
        s5 = tf.train.Saver(slim.get_model_variables(scope='InceptionV4'))
        s6 = tf.train.Saver(
            slim.get_model_variables(scope='InceptionResnetV2'))
        s7 = tf.train.Saver(
            slim.get_model_variables(scope='EnsAdvInceptionResnetV2'))
        s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2'))

        with tf.Session() as sess:
            s1.restore(sess, FLAGS.checkpoint_path_inception_v3)
            s2.restore(sess, FLAGS.checkpoint_path_adv_inception_v3)
            s3.restore(sess, FLAGS.checkpoint_path_ens3_adv_inception_v3)
            s4.restore(sess, FLAGS.checkpoint_path_ens4_adv_inception_v3)
            s5.restore(sess, FLAGS.checkpoint_path_inception_v4)
            s6.restore(sess, FLAGS.checkpoint_path_inception_resnet_v2)
            s7.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2)
            s8.restore(sess, FLAGS.checkpoint_path_resnet)

            sum = 0
            loss_num = 0
            l2_distance = 0
            label_distance = 0

            for filenames, images in load_images(FLAGS.input_dir, batch_shape):
                images_flatten_initial = images.reshape((2, 268203))
                sum += len(filenames)

                # for i in range(len(filenames)):
                #     true_label.append(filenames[i].split('-')[2][:-4])

                adv_images, true_label = sess.run([x_adv, label],
                                                  feed_dict={x_input: images})
                print('adv_images::::', type(adv_images[0][0][0][0]),
                      adv_images.shape)
                Prediction = sess.run(pred_adv_v3,
                                      feed_dict={x_adv_v3: adv_images})
                print('true_label::::::::', true_label)

                images_flatten_adv = adv_images.reshape((2, 268203))
                l2_diatance_list = np.linalg.norm(
                    (images_flatten_initial - images_flatten_adv),
                    axis=1,
                    keepdims=True)
                for n in range(len(filenames)):
                    l2_distance += l2_diatance_list[n]

                for j in range(len(filenames)):
                    label_distance += abs(true_label[j] - Prediction[j])
                    if int(true_label[j]) == Prediction[
                            j]:  # The test tag is always 1 more than the real tag, so I subtract 1 here
                        loss_num += 1
                        print('sucess_num:::::', loss_num)

                print('Prediction:::::::', Prediction)
                save_images(adv_images, filenames, FLAGS.output_dir)

            print('sum::::', sum)
            print('loss_num::::', loss_num)
            rate_wrong = loss_num / sum
            print('rate_wrong:::::::', rate_wrong)
            print('l2_distance:::::::', l2_distance)
            print('label_distance::::::', label_distance)
            end = time.clock()
            print('run time::::::::', end - start)