def dry_run_load(checkpoint_file, scope):
    with tf.Graph().as_default():
        x_input = tf.placeholder(tf.float32, shape=BATCH_SHAPE)
        x_adv = x_input

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=NUM_CLASSES,
                                   is_training=False,
                                   scope=scope)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                x_adv,
                num_classes=NUM_CLASSES,
                is_training=False,
                reuse=True,
                scope=scope)

        saver = tf.train.Saver(slim.get_model_variables(scope=scope))
        with tf.Session().as_default() as sess:
            saver.restore(sess, checkpoint_file)

            tf_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                        scope=scope)
            print(len(tf_vars))
            print(tf_vars[0])
예제 #2
0
def main(_):
  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
  num_classes = 1001

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

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

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

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

    # 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):
          labels = sess.run(predicted_labels, feed_dict={x_input: images})
          for filename, label in zip(filenames, labels):
            out_file.write('{0},{1}\n'.format(filename, label))
def predict_inception(file_path):
    x = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name='x')
    with slim.arg_scope(inception.inception_v3_arg_scope()):

        # (logits, 層の情報の集合)
        logits, _ = inception.inception_v3(x,
                                           num_classes=1001,
                                           is_training=False)
        softmax = tf.nn.softmax(logits)
        top_labels = tf.nn.top_k(logits, 5)[1]

    # predictions = end_points['Predictions']
    saver = tf.train.Saver()
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        init.run()
        saver.restore(sess, "inception_v3_2016_08_28/inception_v3.ckpt")
        img = load_image(file_path).eval()[np.newaxis, :, :, :]
        print(img.shape)
        print(img.dtype)
        pred_i = top_labels.eval(feed_dict={x: img}).flatten()
        pred_logits = softmax.eval(feed_dict={x: img}).flatten()[pred_i]

        return [(CLASS_NAME_DICT[i], p) for i, p in zip(pred_i, pred_logits)]
예제 #4
0
def main(_):
    """Run the sample defense"""
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    nb_classes = 1001

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

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

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(
                x_input, num_classes=nb_classes, is_training=False
            )

        predicted_labels = tf.argmax(end_points["Predictions"], 1)

        # 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):
                    labels = sess.run(predicted_labels, feed_dict={x_input: images})
                    for filename, label in zip(filenames, labels):
                        out_file.write("{0},{1}\n".format(filename, label))
예제 #5
0
    def conv_tower_fn(self, images, is_training=True, reuse=None):
        """Computes convolutional features using the InceptionV3 model.

    Args:
      images: A tensor of shape [batch_size, height, width, channels].
      is_training: whether is training or not.
      reuse: whether or not the network and its variables should be reused. To
        be able to reuse 'scope' must be given.

    Returns:
      A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of
      output feature map and N is number of output features (depends on the
      network architecture).
    """
        mparams = self._mparams["conv_tower_fn"]
        logging.debug("Using final_endpoint=%s", mparams.final_endpoint)
        with tf.variable_scope("conv_tower_fn/INCE"):
            if reuse:
                tf.get_variable_scope().reuse_variables()
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                with slim.arg_scope([slim.batch_norm, slim.dropout],
                                    is_training=is_training):
                    net, _ = inception.inception_v3_base(
                        images, final_endpoint=mparams.final_endpoint)
            return net
예제 #6
0
def inception_v3(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 spatial_squeeze=True,
                 scope='InceptionV3'):
    with slim.arg_scope(inception.inception_v3_arg_scope(
            batch_norm_decay=BATCH_NORM_DECAY,
            batch_norm_epsilon=BATCH_NORM_EPSILON)):
        with slim.arg_scope(
                [slim.conv2d, slim.fully_connected, slim.batch_norm],trainable=True):
            with slim.arg_scope(
                    [slim.batch_norm, slim.dropout], is_training=is_training):
                net, _ = inception.inception_v3_base(
                    inputs,
                    scope=scope)
    with tf.variable_scope('Logits'):
        kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
        net = tf.contrib.layers.avg_pool2d(
            net,
            kernel_size,
            padding='VALID')
        if is_training:
            net =  tf.contrib.layers.dropout(
                net, keep_prob=dropout_keep_prob)
        logits = tf.contrib.layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None)
        if spatial_squeeze:
            logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze')
        return logits
    def load(self, **kwargs):
        session = kwargs["session"]
        assert isinstance(session, tf.Session)

        x_input = tf.placeholder(self.x_dtype, shape=(None, ) + self.x_shape)
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=self.n_class,
                                   is_training=False,
                                   reuse=tf.AUTO_REUSE)

        model_path = get_model_path('ens4_adv_inception_v3')
        if not os.path.exists(model_path):
            os.makedirs(model_path)
            urllib.request.urlretrieve(
                'http://download.tensorflow.org/models/ens4_adv_inception_v3_2017_08_18.tar.gz',
                os.path.join(model_path,
                             'ens4_adv_inception_v3_2017_08_18.tar.gz'),
                show_progress)

            tar = tarfile.open(
                os.path.join(model_path,
                             'ens4_adv_inception_v3_2017_08_18.tar.gz'))
            file_names = tar.getnames()
            for file_name in file_names:
                tar.extract(file_name, model_path)

        saver = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
        saver.restore(session,
                      os.path.join(model_path, 'ens4_adv_inception_v3.ckpt'))
예제 #8
0
def create_model(x, reuse=None):
    """Create model graph.

  Args:
    x: input images
    reuse: reuse parameter which will be passed to underlying variable scopes.
      Should be None first call and True every subsequent call.

  Returns:
    (logits, end_points) - tuple of model logits and enpoints

  Raises:
    ValueError: if model type specified by --model_name flag is invalid.
  """
    if FLAGS.model_name == 'inception_v3':
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            return inception.inception_v3(x,
                                          num_classes=NUM_CLASSES,
                                          is_training=False,
                                          reuse=reuse)
    elif FLAGS.model_name == 'inception_resnet_v2':
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            return inception_resnet_v2.inception_resnet_v2(
                x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
    else:
        raise ValueError('Invalid model name: %s' % (FLAGS.model_name))
예제 #9
0
def pred(full_image_path):
    images = np.zeros(importer.batch_shape, np.float32)
    image = imread(full_image_path, mode='RGB').astype(
        np.float32) * 2.0 / 255.0 - 1.0
    images[0, :, :, :] = image
    graph_eval = tf.Graph()
    with graph_eval.as_default():
        x_input = tf.placeholder(tf.float32, shape=importer.batch_shape)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(
                x_input, num_classes=importer.num_classes, is_training=False)

        predicted_labels = tf.argmax(end_points['Predictions'], 1)
        session_creator = tf.train.ChiefSessionCreator(
            checkpoint_filename_with_path=importer.checkpoint_path,
            master=importer.tensorflow_master)

        #TODO: I run out of memory when loading all images to the memory,
        #      Make one image prediction in each iteration
        #image_iterator = importer.load_images_generator(importer.input_dir_images, importer.batch_shape)
        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            filename = os.path.basename(full_image_path)
            true_classes = importer.filename_to_class([filename])
            predicted_classes = sess.run(predicted_labels,
                                         feed_dict={x_input: images})
            print("True class: {}, predicted: {}".format(
                true_classes, predicted_classes))
예제 #10
0
def inference(inputs, is_training=True):
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(inputs,
                                                    num_classes=1000,
                                                    is_training=is_training)
    logits = tf.nn.sigmoid(logits)
    return logits
def print_accuracy(images,
                   true_labels,
                   jpeg_reconstruct=False,
                   jpeg_quality=23):
    tf.reset_default_graph()
    with tf.Graph().as_default():  # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=[100, 299, 299, 3])

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

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

        # 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='./inception-v3/inception_v3.ckpt')

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            if jpeg_reconstruct:
                images = (images + 1.0) / 2.0
                images = jpeg(images, quality=jpeg_quality)
                images = images * 2.0 - 1.0
            labels = np.zeros(1000)
            for i in range(10):
                labels[i * 100:(i + 1) * 100] = sess.run(
                    predicted_labels,
                    feed_dict={x_input: images[i * 100:(i + 1) * 100, :]})

    accuracy = np.sum(labels == true_labels) / len(labels)
    return accuracy
예제 #12
0
def graph(x, y, i, x_max, x_min, grad):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    eps_iter = 2.0 / 255.0
    num_classes = 1001
    momentum = FLAGS.momentum

    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(input_diversity(x),
                                                    num_classes=num_classes,
                                                    is_training=False)
    pred = tf.argmax(end_points['Predictions'], 1)

    # here is the way to stable gt lables
    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)

    # compute the gradient info
    noise = tf.gradients(cross_entropy, x)[0]
    noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True)
    # accumulate the gradient
    noise = momentum * grad + noise

    x = x + eps_iter * 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
예제 #13
0
파일: model.py 프로젝트: 812864539/models
  def conv_tower_fn(self, images, is_training=True, reuse=None):
    """Computes convolutional features using the InceptionV3 model.

    Args:
      images: A tensor of shape [batch_size, height, width, channels].
      is_training: whether is training or not.
      reuse: whether or not the network and its variables should be reused. To
        be able to reuse 'scope' must be given.

    Returns:
      A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of
      output feature map and N is number of output features (depends on the
      network architecture).
    """
    mparams = self._mparams['conv_tower_fn']
    logging.debug('Using final_endpoint=%s', mparams.final_endpoint)
    with tf.variable_scope('conv_tower_fn/INCE'):
      if reuse:
        tf.get_variable_scope().reuse_variables()
      with slim.arg_scope(inception.inception_v3_arg_scope()):
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
          net, _ = inception.inception_v3_base(
            images, final_endpoint=mparams.final_endpoint)
      return net
예제 #14
0
def build_model(model_name, inputs, num_classes, is_training, dropout_keep_prob):
    use_fcn = False
    if model_name.find('fcn') >= 0:
        use_fcn = True
        model_base_name = model_name[0:-4]
    else:
        model_base_name = model_name

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

    with tf.variable_scope('not_pretrained'):
        if use_fcn:
            net = fully_convolutional_networks(net, num_classes, is_training, dropout_keep_prob)
        else:
            net = fully_connected_networks(net, num_classes, is_training, dropout_keep_prob)
    return net
예제 #15
0
  def __init__(self, sess, dim=299):
    self.batch_shape = [16, dim, dim, 3]
    self._num_classes = 1001
    self._scope = 'InceptionV3'
    self._weights_file = './Weights/inception_v3.ckpt'
    output_layer = 'Conv2d_1a_3x3'

    #
    # network inputs
    #
    self.x_tf = tf.placeholder(tf.float32, shape=self.batch_shape)

    #
    # network outputs
    #
    with slim.arg_scope(inception.inception_v3_arg_scope()): 
      with arg_scope([layers_lib.batch_norm, layers_lib.dropout], is_training=False): # we truncate before these layers, so this is not really necessary...
        net, endpoints = inception.inception_v3_base(self.x_tf, final_endpoint=output_layer, scope=self._scope)
        self.output = endpoints[output_layer]

    #
    # load weights
    #
    saver = tf.train.Saver(slim.get_model_variables(scope=self._scope))
    saver.restore(sess, self._weights_file)
예제 #16
0
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
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    nb_classes = 1001

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

    all_images_taget_class = load_target_class(FLAGS.input_dir)

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

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

        target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
        one_hot_target_class = tf.one_hot(target_class_input, nb_classes)
        cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                        logits,
                                                        label_smoothing=0.1,
                                                        weights=1.0)
        cross_entropy += tf.losses.softmax_cross_entropy(
            one_hot_target_class,
            end_points["AuxLogits"],
            label_smoothing=0.1,
            weights=0.4,
        )
        x_adv = x_input - eps * tf.sign(
            tf.gradients(cross_entropy, x_input)[0])
        x_adv = tf.clip_by_value(x_adv, -1.0, 1.0)

        # 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:
            for filenames, images in load_images(FLAGS.input_dir, batch_shape):
                target_class_for_batch = [
                    all_images_taget_class[n] for n in filenames
                ] + [0] * (FLAGS.batch_size - len(filenames))
                adv_images = sess.run(
                    x_adv,
                    feed_dict={
                        x_input: images,
                        target_class_input: target_class_for_batch,
                    },
                )
                save_images(adv_images, filenames, FLAGS.output_dir)
예제 #17
0
def build_single_inceptionv3(train_tfdata, is_train, dropout_keep_prob, reduce_dim = False):
    train_tfdata_resize = tf.image.resize_images(train_tfdata, (299, 299))
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        identity, end_points = inception.inception_v3(train_tfdata_resize, dropout_keep_prob = dropout_keep_prob, is_training=is_train)
        feature = slim.flatten(end_points['Mixed_7c'])
        if reduce_dim:
            feature = slim.fully_connected(feature, 256, scope='feat')
    return identity, feature
예제 #18
0
 def __init__(self, num_classes, x_input):
     self.num_classes = num_classes
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         _, end_points = inception.inception_v3(
             x_input,
             num_classes=importer.num_classes,
             is_training=False,
             reuse=False)
예제 #19
0
 def testModelHasExpectedNumberOfParameters(self):
     batch_size = 5
     height, width = 299, 299
     inputs = tf.random_uniform((batch_size, height, width, 3))
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         inception.inception_v3_base(inputs)
     total_params, _ = slim.model_analyzer.analyze_vars(slim.get_model_variables())
     self.assertAlmostEqual(21802784, total_params)
예제 #20
0
 def testModelHasExpectedNumberOfParameters(self):
   batch_size = 5
   height, width = 299, 299
   inputs = tf.random_uniform((batch_size, height, width, 3))
   with slim.arg_scope(inception.inception_v3_arg_scope()):
     inception.inception_v3_base(inputs)
   total_params, _ = slim.model_analyzer.analyze_vars(
       slim.get_model_variables())
   self.assertAlmostEqual(21802784, total_params)
예제 #21
0
파일: model.py 프로젝트: codybai/mycode
 def conv_tower_fn(self,images, is_training = True, reuse = None):
     mparams = self._mparams['conv_tower_fn']
     logging.debug('Using final_endpoint=%s',mparams.final_endpoint)
     if reuse:
         tf.get_variable_scope().reuse_variables()
     with slim.arg_scope(inception.inception_v3_arg_scope()):  #用slim.arg_scope设置默认参数,但是,设置参数需要用于被@add_arg_scope修饰过的参数,见源码
         with slim.arg_scope([slim.batch_norm,slim.dropout],is_training=is_training):
             net,_ = inception.inception_v3_base(images,final_endpoint=mparams.final_endpoint)
             return net
예제 #22
0
    def __call__(self, x_input):
        """Constructs model and return probabilities for given input."""
        reuse = True if self.built else None
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                x_input, num_classes=self.num_classes, is_training=False, reuse=tf.AUTO_REUSE, scope=self.scope)

        self.built = True
        return logits, end_points
예제 #23
0
 def __call__(self, inputs, training):
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         inputs = inputs / 255. * 2. - 1.
         self.inputs = inputs = tf.image.resize_images(inputs, (299, 299))
         _, end_points = inception.inception_v3(
             inputs, num_classes=self.num_classes, is_training=training)
         logits = end_points["Logits"]
         if self.mapping:
             logits = tf.gather(logits, self.label_map, axis=-1)
     return logits
예제 #24
0
 def init_model(self):
     if self.initialized:
         """ Model has already been built"""
         return
     input_node = tf.placeholder(tf.float32, shape=self.input_shape)
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         logits, end_points = inception.inception_v3(
             input_node, num_classes=self.num_classes, is_training=False, dropout_keep_prob=1.0)
     self.saver = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
     self.initialized = True
예제 #25
0
    def build(self,
              inputs,
              input_pixel_size,
              is_training,
              scope='img_inception'):
        """Inception for BEV feature extraction

        Args:
            inputs: a tensor of size [batch_size, height, width, channels].
            input_pixel_size: size of the input (H x W)
            is_training: True for training, False fo validation/testing.
            scope: Optional scope for the variables.

        Returns:
            The net, a rank-4 tensor of size [batch, height_out, width_out,
                channels_out] and end_points dict.
        """

        inception_config = self.config

        with tf.variable_scope(scope, 'img_inception', [inputs]) as scope:
            with slim.arg_scope([slim.batch_norm, slim.dropout],
                                is_training=is_training):

                if inception_config.inception_v == 'inception_v1':
                    with slim.arg_scope(inception.inception_v1_arg_scope()):
                        net, end_points = inception.inception_v1_base(
                            inputs, scope=scope)

                elif inception_config.inception_v == 'inception_v2':
                    with slim.arg_scope(inception.inception_v2_arg_scope()):
                        net, end_points = inception.inception_v2_base(
                            inputs, scope=scope)

                elif inception_config.inception_v == 'inception_v3':
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        net, end_points = inception.inception_v3_base(
                            inputs, scope=scope)
                else:
                    raise ValueError('Invalid Inception version {},'.format(
                        inception_config.inception_v))

                with tf.variable_scope('upsampling'):
                    # This feature extractor downsamples the input by a factor
                    # of 32
                    downsampling_factor = 32
                    downsampled_shape = input_pixel_size / downsampling_factor

                    upsampled_shape = downsampled_shape * \
                        inception_config.upsampling_multiplier

                    feature_maps_out = tf.image.resize_bilinear(
                        net, upsampled_shape)

        return feature_maps_out, end_points
예제 #26
0
 def get_logits_prob(self, batch_input):
     """
     Prediction from the model on a single batch.
     :param batch_input: the input batch. Must be from size [?, 299, 299, 3]
     :return: the logits and probabilities for the batch
     """
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         logits, _ = inception.inception_v3(batch_input, num_classes=1001, is_training=False)
         logits = logits[:, 1:]
         probs = tf.squeeze(tf.nn.softmax(logits))
     return logits, probs
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

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

    layer_names = FLAGS.feature_layer.split(',')
    output_databases = FLAGS.output_database.split(',')
    poolings = FLAGS.feature_pool.split(',')

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

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

        # feature_layer = tf.squeeze(end_points['PreLogits'])
        # feature_layer = slim.avg_pool2d(end_points[FLAGS.feature_layer], (8, 8))

        # Global Pool
        feature_layers = []
        if 'avg_pool' in poolings:
            feature_layers += [
                tf.reduce_mean(end_points[l], axis=(1, 2)) for l in layer_names
            ]

        if 'max_pool' in poolings:
            feature_layers += [
                tf.reduce_max(end_points[l], axis=(1, 2)) for l in layer_names
            ]

        # 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)

        envs = [
            lmdb.open(out_db, map_size=1e12) for out_db in output_databases
        ]
        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            for filenames, images in tqdm(
                    load_images(FLAGS.input_dir, batch_shape)):
                list_of_features = sess.run(feature_layers,
                                            feed_dict={x_input: images})
                for env, features in zip(envs, list_of_features):
                    with env.begin(write=True) as txn:
                        for filename, feature in zip(filenames, features):
                            txn.put(filename, feature)
예제 #28
0
 def __call__(self, x_input):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         _, end_points = inception.inception_v3(
                         x_input, num_classes=self.num_classes, is_training=False,
                         reuse=reuse)
     self.built = True
     output = end_points['Predictions']
     probs = output.op.inputs[0]
     return probs
 def fprop(self, x, **kwargs):
     reuse = True if self._built else None
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         logits, end_points = inception.inception_v3(x, num_classes=self.n_classes,
                                                     is_training=False, reuse=reuse)
     self._built = True
     output = end_points['Predictions']
     # Strip off the extra reshape op at the output
     probs = output.op.inputs[0]
     end_points['logits'] = logits
     end_points['probs'] = probs
     return end_points
예제 #30
0
def Eval(x_img, y):
    input_image = 2 * x_img / 255 - 1
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits_inception_v3, end_points_inception_v3 = inception.inception_v3(
            input_image,
            num_classes=110,
            is_training=False,
            scope='InceptionV3',
            reuse=tf.AUTO_REUSE)
        inc_label = tf.argmax(end_points_inception_v3['Predictions'][0], -1)
        y_inc = end_points_inception_v3['Predictions'][0][y[0]]
    return inc_label, y_inc
def setup_model():
    # Create graph
    X = tf.placeholder(tf.float32, shape=[None, height, width, channels])

    with slim.arg_scope(inception.inception_v3_arg_scope()):
        net, end_points = inception.inception_v3(X,
                                                 num_classes=1001,
                                                 is_training=False)

    saver = tf.train.Saver()

    return saver
예제 #32
0
def run(name, image_size, num_classes):
  with tf.Graph().as_default():
    image = tf.placeholder("float", [1, image_size, image_size, 3], name="input")
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, _ = inception.inception_v3(image, num_classes, is_training=False, spatial_squeeze=False)
    probabilities = tf.nn.softmax(logits)
    init_fn = slim.assign_from_checkpoint_fn('inception_v3.ckpt', slim.get_model_variables('InceptionV3'))

    with tf.Session() as sess:
        init_fn(sess)
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess, "output/"+name)
예제 #33
0
    def get_probs(self, x_input):
        """Constructs model and return probabilities for given input."""
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(
                x_input,
                num_classes=importer.num_classes,
                is_training=False,
                reuse=True)

        output = end_points['Predictions']
        probs = output.op.inputs[0]
        return probs
예제 #34
0
 def __call__(self, input_node):
     """
     input_node: should be a place holder passing the input image whose shape =[1,299,299,3] and values are
         in the [0,1] range.
     """
     normalized_input = input_node * 2.0 - 1.0
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         logits, end_points = inception.inception_v3(
             normalized_input, num_classes=self.num_classes, is_training=False, dropout_keep_prob=1.0, reuse=True)
     predictions_node = end_points['Predictions']
     output_node = tf.argmax(predictions_node, axis=1)
     return logits, output_node
예제 #35
0
    def __call__(self, x_input):
        """Constructs model and return probabilities for given input."""
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(
                x_input,
                num_classes=self.num_classes,
                is_training=False,
                reuse=True)

        output = end_points['Logits']
        logits = output.op.inputs[0]
        return logits
예제 #36
0
def run(name, image_size, num_classes):
  with tf.Graph().as_default():
    image = tf.placeholder("float", [1, image_size, image_size, 3], name="input")
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, _ = inception.inception_v3(image, num_classes, is_training=False, spatial_squeeze=False)
    probabilities = tf.nn.softmax(logits)
    init_fn = slim.assign_from_checkpoint_fn('inception_v3.ckpt', slim.get_model_variables('InceptionV3'))

    with tf.Session() as sess:
        init_fn(sess)
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess, "output/"+name)
    def predict(self,preprocessed_inputs):
        """
        Runs inference on model

        Args:
            preprocessed_inputs: processed batch to go to model
        Returns:
            pre_logits: layer right before the logits
        """
        with slim.arg_scope(inception.inception_v3_arg_scope()):
              _,end_points = inception.inception_v3(
                  preprocessed_inputs,num_classes=1001,is_training=self._is_training,reuse=self._reuse)
        return end_points['PreLogits']
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
  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
  num_classes = 1001

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

  all_images_taget_class = load_target_class(FLAGS.input_dir)

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

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

    target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    one_hot_target_class = tf.one_hot(target_class_input, num_classes)
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                    logits,
                                                    label_smoothing=0.1,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                     end_points['AuxLogits'],
                                                     label_smoothing=0.1,
                                                     weights=0.4)
    x_adv = x_input - eps * tf.sign(tf.gradients(cross_entropy, x_input)[0])
    x_adv = tf.clip_by_value(x_adv, -1.0, 1.0)

    # 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:
      for filenames, images in load_images(FLAGS.input_dir, batch_shape):
        target_class_for_batch = (
            [all_images_taget_class[n] for n in filenames]
            + [0] * (FLAGS.batch_size - len(filenames)))
        adv_images = sess.run(x_adv,
                              feed_dict={
                                  x_input: images,
                                  target_class_input: target_class_for_batch
                              })
        save_images(adv_images, filenames, FLAGS.output_dir)
 def __call__(self, x_input, return_logits=False):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         # Inception preprocessing uses [-1, 1]-scaled input.
         x_input = x_input * 2.0 - 1.0
         _, end_points = inception.inception_v3(
             x_input, num_classes=self.num_classes, is_training=False,
             reuse=reuse)
     self.built = True
     self.logits = end_points['Logits']
     # Strip off the extra reshape op at the output
     self.probs = end_points['Predictions'].op.inputs[0]
     if return_logits:
         return self.logits
     else:
         return self.probs
예제 #40
0
def create_model(x, reuse=None):
  """Create model graph.

  Args:
    x: input images
    reuse: reuse parameter which will be passed to underlying variable scopes.
      Should be None first call and True every subsequent call.

  Returns:
    (logits, end_points) - tuple of model logits and enpoints

  Raises:
    ValueError: if model type specified by --model_name flag is invalid.
  """
  if FLAGS.model_name == 'inception_v3':
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      return inception.inception_v3(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  elif FLAGS.model_name == 'inception_resnet_v2':
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
      return inception_resnet_v2.inception_resnet_v2(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  else:
    raise ValueError('Invalid model name: %s' % (FLAGS.model_name))