def predict_slim(sample_images, print_func=print):
    """
    Code modified from here: [https://github.com/tensorflow/models/issues/429]
    """
    # Setup preprocessing
    input_tensor = tf.placeholder(tf.float32, shape=(None, 299, 299, 3), name='input_image')
    scaled_input_tensor = tf.scalar_mul((1.0 / 255), input_tensor)
    scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5)
    scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0)

    # Setup session
    sess = tf.Session()
    arg_scope = slim_irv2.inception_resnet_v2_arg_scope()
    with slim.arg_scope(arg_scope):
        _, end_points = slim_irv2.inception_resnet_v2(scaled_input_tensor, is_training=False)

    # Load the model
    print_func("Loading TF-slim checkpoint...")
    saver = tf.train.Saver()
    saver.restore(sess, SLIM_CKPT)

    # Make prediction
    predict_values = []
    for image in sample_images:
        im = Image.open(image).resize((299, 299))
        arr = np.expand_dims(np.array(im), axis=0)
        y_pred = sess.run([end_points['Predictions']], feed_dict={input_tensor: arr})
        y_pred = y_pred[0].ravel()

        y_pred = y_pred[1:] / y_pred[1:].sum()  # remove background class and renormalize
        print_func("{} class={} prob={}".format(image, np.argmax(y_pred), np.max(y_pred)))
        predict_values.append(y_pred)

    return predict_values
  def _extract_proposal_features(self, preprocessed_inputs, scope):
    """Extracts first stage RPN features.

    Extracts features using the first half of the Inception Resnet v2 network.
    We construct the network in `align_feature_maps=True` mode, which means
    that all VALID paddings in the network are changed to SAME padding so that
    the feature maps are aligned.

    Args:
      preprocessed_inputs: A [batch, height, width, channels] float32 tensor
        representing a batch of images.
      scope: A scope name.

    Returns:
      rpn_feature_map: A tensor with shape [batch, height, width, depth]
    Raises:
      InvalidArgumentError: If the spatial size of `preprocessed_inputs`
        (height or width) is less than 33.
      ValueError: If the created network is missing the required activation.
    """
    if len(preprocessed_inputs.get_shape().as_list()) != 4:
      raise ValueError('`preprocessed_inputs` must be 4 dimensional, got a '
                       'tensor of shape %s' % preprocessed_inputs.get_shape())

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope(
        weight_decay=self._weight_decay)):
      # Forces is_training to False to disable batch norm update.
      with slim.arg_scope([slim.batch_norm],
                          is_training=self._train_batch_norm):
        with tf.variable_scope('InceptionResnetV2',
                               reuse=self._reuse_weights) as scope:
          return inception_resnet_v2.inception_resnet_v2_base(
              preprocessed_inputs, final_endpoint='PreAuxLogits',
              scope=scope, output_stride=self._first_stage_features_stride,
              align_feature_maps=True)
  def _extract_box_classifier_features(self, proposal_feature_maps, scope):
    """Extracts second stage box classifier features.

    This function reconstructs the "second half" of the Inception ResNet v2
    network after the part defined in `_extract_proposal_features`.

    Args:
      proposal_feature_maps: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      scope: A scope name.

    Returns:
      proposal_classifier_features: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
    with tf.variable_scope('InceptionResnetV2', reuse=self._reuse_weights):
      with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope(
          weight_decay=self._weight_decay)):
        # Forces is_training to False to disable batch norm update.
        with slim.arg_scope([slim.batch_norm],
                            is_training=self._train_batch_norm):
          with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                              stride=1, padding='SAME'):
            with tf.variable_scope('Mixed_7a'):
              with tf.variable_scope('Branch_0'):
                tower_conv = slim.conv2d(proposal_feature_maps,
                                         256, 1, scope='Conv2d_0a_1x1')
                tower_conv_1 = slim.conv2d(
                    tower_conv, 384, 3, stride=2,
                    padding='VALID', scope='Conv2d_1a_3x3')
              with tf.variable_scope('Branch_1'):
                tower_conv1 = slim.conv2d(
                    proposal_feature_maps, 256, 1, scope='Conv2d_0a_1x1')
                tower_conv1_1 = slim.conv2d(
                    tower_conv1, 288, 3, stride=2,
                    padding='VALID', scope='Conv2d_1a_3x3')
              with tf.variable_scope('Branch_2'):
                tower_conv2 = slim.conv2d(
                    proposal_feature_maps, 256, 1, scope='Conv2d_0a_1x1')
                tower_conv2_1 = slim.conv2d(tower_conv2, 288, 3,
                                            scope='Conv2d_0b_3x3')
                tower_conv2_2 = slim.conv2d(
                    tower_conv2_1, 320, 3, stride=2,
                    padding='VALID', scope='Conv2d_1a_3x3')
              with tf.variable_scope('Branch_3'):
                tower_pool = slim.max_pool2d(
                    proposal_feature_maps, 3, stride=2, padding='VALID',
                    scope='MaxPool_1a_3x3')
              net = tf.concat(
                  [tower_conv_1, tower_conv1_1, tower_conv2_2, tower_pool], 3)
            net = slim.repeat(net, 9, inception_resnet_v2.block8, scale=0.20)
            net = inception_resnet_v2.block8(net, activation_fn=None)
            proposal_classifier_features = slim.conv2d(
                net, 1536, 1, scope='Conv2d_7b_1x1')
        return proposal_classifier_features
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

    # should keep original x here for output

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

    with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
        logits_v4, end_points_v4 = inception_v4.inception_v4(
            input_diversity(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(
            input_diversity(x),
            num_classes=num_classes,
            is_training=False,
            reuse=True)

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

    logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet) / 4
    auxlogits = (end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] +
                 end_points_res_v2['AuxLogits']) / 3
    cross_entropy = tf.losses.softmax_cross_entropy(y,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(y,
                                                     auxlogits,
                                                     label_smoothing=0.0,
                                                     weights=0.4)
    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.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 main(_):
    # 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
    num_classes = 1001
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]

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

    print(time.time() - start_time)

    with tf.Graph().as_default():
        # Prepare graph
        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)

        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            _, end_points = inception_resnet_v2.inception_resnet_v2(
                x_input, num_classes=num_classes, is_training=False)

        predicted_labels = tf.argmax(end_points['Predictions'], 1)
        y = tf.one_hot(predicted_labels, num_classes)

        i = tf.constant(0)
        grad = tf.zeros(shape=batch_shape)
        x_adv, _, _, _, _, _ = tf.while_loop(
            stop, graph, [x_input, y, i, x_max, x_min, grad])

        # Run computation
        s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
        s5 = tf.train.Saver(slim.get_model_variables(scope='InceptionV4'))
        s6 = tf.train.Saver(
            slim.get_model_variables(scope='InceptionResnetV2'))
        s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2'))

        with tf.Session() as sess:
            s1.restore(sess, FLAGS.checkpoint_path_inception_v3)
            s5.restore(sess, FLAGS.checkpoint_path_inception_v4)
            s6.restore(sess, FLAGS.checkpoint_path_inception_resnet_v2)
            s8.restore(sess, FLAGS.checkpoint_path_resnet)
            print(time.time() - start_time)

            for filenames, images in load_images(FLAGS.input_dir, batch_shape):
                adv_images = sess.run(x_adv, feed_dict={x_input: images})
                save_images(adv_images, filenames, FLAGS.output_dir)

        print(time.time() - start_time)
示例#6
0
 def __init__(self, num_classes):
     self.num_classes = num_classes
     with slim.arg_scope(
             inception_resnet_v2.inception_resnet_v2_arg_scope()):
         self.network_fn_incep_res = inception_resnet_v2.inception_resnet_v2
     with slim.arg_scope(vgg.vgg_arg_scope()):
         self.network_fn_vgg16 = vgg.vgg_16
     with slim.arg_scope(resnet_v2.resnet_arg_scope()):
         self.network_fn_res = resnet_v2.resnet_v2_152
     with slim.arg_scope(inception_utils.inception_arg_scope()):
         self.network_fn_incepv3 = inception_v3.inception_v3
         self.network_fn_incepv4 = inception_v4.inception_v4
     self.network_fn_alex = AlexNet()
     self.build = False
示例#7
0
def feature_extractor(image_path, options=None):
    """Runs a trained version of inception-resnet-v2 on an image and
       extracts the inputs from the final layer before the fully connected
       stages.

    Args:
        image_path: the path to the image
        options: in this case options is not used
    Return:
        an array of features which depending on the config options
    """

    # size of images inc-resnet is compatible with
    image_size = inception_resnet_v2.inception_resnet_v2.default_image_size

    checkpoint_path = os.path.join(
        dir, 'checkpoints/inception_resnet_v2_2016_08_30.ckpt')

    image_string = urllib2.urlopen(image_path).read()

    # JPEG format converted to unit8 tensor
    image = tf.image.decode_jpeg(image_string, channels=3)

    # inception specific pre processing
    processed_image = inception_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False)

    # the model accepts images in batches
    processed_images = tf.expand_dims(processed_image, 0)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits, end_points = inception_resnet_v2.inception_resnet_v2(
            processed_images, num_classes=1001, is_training=False)

    weights_from_file = slim.get_variables_to_restore(exclude=['logits'])

    init_fn = slim.assign_from_checkpoint_fn(checkpoint_path,
                                             weights_from_file)

    # last layer before fully connected layer
    features = end_points['PreLogitsFlatten']

    # run the image through the network
    with tf.Session() as sess:
        init_fn(sess)

        features = sess.run([features])

    return features[0][0]
 def _build(self, x_input=None):
     reuse = True if self.built else None
     if x_input is None:
         x_input = self.input
     with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
         logits, end_points = inception_resnet_v2.inception_resnet_v2(
                 x_input, num_classes=self.num_classes, is_training=False,
                 reuse=reuse)
         self.built = True
     self.end_points = end_points
     self.logits = logits
     if not self.ckpt_loaded:
         saver = tf.train.Saver(slim.get_model_variables())
         saver.restore(self.sess, ckpt_dir + 'inception_resnet_v2.ckpt')
         self.ckpt_loaded = True
    def _extract_proposal_features(self, preprocessed_inputs, scope):
        """Extracts first stage RPN features.

    Extracts features using the first half of the Inception Resnet v2 network.
    We construct the network in `align_feature_maps=True` mode, which means
    that all VALID paddings in the network are changed to SAME padding so that
    the feature maps are aligned.

    Args:
      preprocessed_inputs: A [batch, height, width, channels] float32 tensor
        representing a batch of images.
      scope: A scope name.

    Returns:
      rpn_feature_map: A tensor with shape [batch, height, width, depth]
    Raises:
      InvalidArgumentError: If the spatial size of `preprocessed_inputs`
        (height or width) is less than 33.
      ValueError: If the created network is missing the required activation.
    """
        if len(preprocessed_inputs.get_shape().as_list()) != 4:
            raise ValueError(
                '`preprocessed_inputs` must be 4 dimensional, got a '
                'tensor of shape %s' % preprocessed_inputs.get_shape())
        shape_assert = tf.Assert(
            tf.logical_and(
                tf.greater_equal(tf.shape(preprocessed_inputs)[1], 33),
                tf.greater_equal(tf.shape(preprocessed_inputs)[2], 33)),
            ['image size must at least be 33 in both height and width.'])

        with tf.control_dependencies([shape_assert]):
            with slim.arg_scope(
                    inception_resnet_v2.inception_resnet_v2_arg_scope(
                        weight_decay=self._weight_decay)):
                # Forces is_training to False to disable batch norm update.
                with slim.arg_scope([slim.batch_norm], is_training=False):
                    with tf.variable_scope('InceptionResnetV2',
                                           reuse=self._reuse_weights) as scope:
                        rpn_feature_map, _ = (
                            inception_resnet_v2.inception_resnet_v2_base(
                                preprocessed_inputs,
                                final_endpoint='PreAuxLogits',
                                scope=scope,
                                output_stride=self.
                                _first_stage_features_stride,
                                align_feature_maps=True))
        return rpn_feature_map
示例#10
0
def graph(x, y, i, x_max, x_min, grad, eg):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    num_classes = 1001
    ro = 0.9
    beta = 0.89
    v = 0.1
    eg = ro * eg + (1 - ro) * tf.square(grad)
    rms = tf.sqrt(eg + 0.000000001)
    x_n = x + (alpha / rms)*grad

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

    with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
        logits_v4, end_points_v4 = inception_v4.inception_v4(
            input_diversity(x_n), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE)

    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(
            input_diversity(x_n), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE)

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101(
            input_diversity(x_n), num_classes=num_classes, is_training=False, scope='resnet_v2_101', reuse=tf.AUTO_REUSE)
    logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet) / 4
    auxlogits = (end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] + end_points_res_v2['AuxLogits']) / 3
    cross_entropy = tf.losses.softmax_cross_entropy(y,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(y,
                                                     auxlogits,
                                                     label_smoothing=0.0,
                                                     weights=0.4)

    noise = tf.gradients(cross_entropy, x_n)[0]
    noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME')
    noise1 = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True)
    noise = beta * grad + (1-beta) * noise1
    noise2 = (1-v) * noise + v * noise1
    x = x + alpha * tf.sign(noise2)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    return x, y, i, x_max, x_min, noise, eg
示例#11
0
def graph(x, adv, y, t_y, i, x_max, x_min, grad, amplification):
    target_one_hot = tf.one_hot(t_y, 1001)
    true_one_hot = tf.one_hot(y, 1001)
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    num_iter = FLAGS.num_iter
    alpha = eps / num_iter
    alpha_beta = alpha
    gamma = alpha_beta
    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(
    #         adv, num_classes=num_classes, is_training=False, reuse = True)
    # auxlogit_v3 = end_points_v3['AuxLogits']

    with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
        logits_v4, end_points_v4 = inception_v4.inception_v4(
            adv, num_classes=num_classes, is_training=False, reuse=True)
    auxlogit_v4 = end_points_v4['AuxLogits']

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits_resnet_152, end_points_resnet = resnet_v2.resnet_v2_152(
            adv, num_classes=num_classes, is_training=False, reuse=True)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits_Incres, end_points_IR = inception_resnet_v2.inception_resnet_v2(
            adv, num_classes=num_classes, is_training=False, reuse=True)
    auxlogit_IR = end_points_IR['AuxLogits']

    logits = (logits_Incres + logits_resnet_152 + logits_v4) / 3.0
    auxlogit = (auxlogit_IR + auxlogit_v4) / 2.0

    target_cross_entropy = tf.losses.softmax_cross_entropy(target_one_hot,
                                                           logits,
                                                           label_smoothing=0.0,
                                                           weights=1.0)

    target_cross_entropy += tf.losses.softmax_cross_entropy(
        target_one_hot, auxlogit, label_smoothing=0.0, weights=1.0)

    noise = tf.gradients(target_cross_entropy, adv)[0]
    adv = adv - alpha * n_staircase_sign(noise, num_of_K)
    adv = tf.clip_by_value(adv, x_min, x_max)
    i = tf.add(i, 1)
    return x, adv, y, t_y, i, x_max, x_min, noise, amplification
示例#12
0
def graph(x, y, i, x_max, x_min, grad, eps_inside):
    num_iter = FLAGS.num_iter
    alpha = eps_inside / 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,
            scope='Ens3AdvInceptionV3')

    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(resnet_v2.resnet_arg_scope()):
        logits_resnet, end_points_resnet = resnet_v2.resnet_v2_50(
            x, num_classes=num_classes, is_training=False)

    pred = tf.argmax(
        end_points_v3['Predictions'] + end_points_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 + logits_res_v2 + logits_resnet) / 3
    auxlogits = (end_points_v3['AuxLogits'] +
                 end_points_res_v2['AuxLogits']) / 2
    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]
    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, eps_inside
示例#13
0
def graph(x, y, i, grad):
    num_classes = 1001
    x = x
    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(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_v4['Predictions'] + end_points_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

    return x, y, i, grad
示例#14
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 predict_slim(sample_images, print_func=print):
    """
    Code modified from here: [https://github.com/tensorflow/models/issues/429]
    """
    # Setup preprocessing
    input_tensor = tf.placeholder(tf.float32,
                                  shape=(None, 299, 299, 3),
                                  name='input_image')
    scaled_input_tensor = tf.scalar_mul((1.0 / 255), input_tensor)
    scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5)
    scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0)

    # Setup session
    sess = tf.Session()
    arg_scope = slim_irv2.inception_resnet_v2_arg_scope()
    with tf_slim.arg_scope(arg_scope):
        _, end_points = slim_irv2.inception_resnet_v2(scaled_input_tensor,
                                                      is_training=False)

    # Load the model
    print_func("Loading TF-slim checkpoint...")
    saver = tf.train.Saver()
    saver.restore(sess, SLIM_CKPT)

    # Make prediction
    predict_values = []
    for image in sample_images:
        im = Image.open(image).resize((299, 299))
        arr = np.expand_dims(np.array(im), axis=0)
        y_pred = sess.run([end_points['Predictions']],
                          feed_dict={input_tensor: arr})
        y_pred = y_pred[0].ravel()

        y_pred = y_pred[1:] / y_pred[1:].sum(
        )  # remove background class and renormalize
        print_func("{} class={} prob={}".format(image, np.argmax(y_pred),
                                                np.max(y_pred)))
        predict_values.append(y_pred)

    return predict_values
def model_fn(images, labels, num_classes, mode):
    with tf.contrib.slim.arg_scope(
            inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits, end_points = inception_resnet_v2.inception_resnet_v2(
            images,
            num_classes,
            is_training=(mode == tf.estimator.ModeKeys.TRAIN))
    predictions = {
        'classes': tf.argmax(input=logits, axis=1),
        'probabilities': tf.nn.softmax(logits, name='softmax_tensor')
    }

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    exclude = ['InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits']
    variables_to_restore = tf.contrib.slim.get_variables_to_restore(
        exclude=exclude)
    scopes = {os.path.dirname(v.name) for v in variables_to_restore}
    tf.train.init_from_checkpoint('inception_resnet_v2_2016_08_30.ckpt',
                                  {s + '/': s + '/'
                                   for s in scopes})

    tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits)
    total_loss = tf.losses.get_total_loss(
    )  #obtain the regularization losses as well

    # Configure the training op
    if mode == tf.estimator.ModeKeys.TRAIN:
        global_step = tf.train.get_or_create_global_step()
        optimizer = tf.train.AdamOptimizer(learning_rate=0.00002)
        train_op = optimizer.minimize(total_loss, global_step)
    else:
        train_op = None

    return tf.estimator.EstimatorSpec(mode=mode,
                                      predictions=predictions,
                                      loss=total_loss,
                                      train_op=train_op)
示例#17
0
    def __init__(self):
        self.sessIR = tf.Session()
        self.labels_names = self.imagenet_labels()
        self.image_sizeIR = inception_resnet_v2.inception_resnet_v2.default_image_size

        # Define Inception Resnet v2 neural network
        self.processed_imagesIR = tf.placeholder(tf.float32,
                                                 shape=(None,
                                                        self.image_sizeIR,
                                                        self.image_sizeIR, 3))
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logitsIR, _ = inception_resnet_v2.inception_resnet_v2(
                self.processed_imagesIR, num_classes=1001, is_training=False)
        self.probabilitiesIR = tf.nn.softmax(logitsIR)

        # Load saved checkpoint for Resnet v2 neural network
        checkpoints_dir = './checkpoints'
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir,
                         'inception_resnet_v2_2016_08_30.ckpt'),
            slim.get_model_variables('InceptionResnetV2'))
        init_fn(self.sessIR)
示例#18
0
文件: nima.py 项目: yuyu0927/nima-1
def net_inception(images, is_training=True, num_classes_finetune=10, finetune_dropout_keep=0.75):
  # from tensorflow.contrib import slim
  # from nets import inception_resnet_v2 as inception
  with slim.arg_scope([slim.conv2d, slim.fully_connected]):
    with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
      net, end_points = inception.inception_resnet_v2(images, 
                                    num_classes=None,
                                    is_training=is_training)
      end_points["baseline"] = net

      #
      # add finetune layer, with adjusted dropout_keep_prob
      #
      net = slim.flatten(net)
      net = slim.dropout(net, finetune_dropout_keep, 
                          is_training=is_training,
                          scope='Dropout')
      end_points['PreLogitsFlatten'] = net
      logits = slim.fully_connected(net, num_classes_finetune, 
                                    activation_fn=None,
                                    scope='Logits')
      end_points['finetune'] = end_points['Logits'] = logits
      end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions')
  return [net, end_points]
示例#19
0
def main(_):
    # eps = FLAGS.max_epsilon / 255.0
    eps = 2.0 / 255.0

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

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

    origin_preds = {}
    flip_preds = {}
    all_filenames = []

    # Origin
    with tf.Graph().as_default():
        # Prepare graph
        # x_input is a binary image (converted in the load_images phase)
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            _, end_points = inception_resnet_v2.inception_resnet_v2(
                x_input, num_classes=num_classes, is_training=False)

        predictions = end_points['Predictions']

        # Run computation
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    pred = sess.run(predictions, feed_dict={x_input: images})
                    for filename, p in zip(filenames, list(pred)):
                        origin_preds[filename] = p

                        # Keep the filename as well
                        all_filenames.append(filename)

    # Flip
    # tf.reset_default_graph()
    with tf.Graph().as_default():
        # Prepare graph
        # x_input is a binary image (converted in the load_images phase)
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

        x_input_flip = tf.image.flip_left_right(x_input)

        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            _, end_points = inception_resnet_v2.inception_resnet_v2(
                x_input_flip, num_classes=num_classes, is_training=False)

        predictions = end_points['Predictions']

        # Run computation
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    pred = sess.run(predictions, feed_dict={x_input: images})
                    for filename, p in zip(filenames, list(pred)):
                        flip_preds[filename] = p

    # Merge
    for filename in all_filenames:
        p = (origin_preds[filename] + flip_preds[filename]) / 2
        p_str = " ".join([str(i) for i in list(p)])
        out_file.write('{0},{1}\n'.format(filename, p_str))