示例#1
0
  def testRaiseValueErrorWithInvalidDepthMultiplier(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    with self.assertRaises(ValueError):
      _ = mobilenet_v1.mobilenet_v1(
          inputs, num_classes, depth_multiplier=-0.1)
    with self.assertRaises(ValueError):
      _ = mobilenet_v1.mobilenet_v1(
          inputs, num_classes, depth_multiplier=0.0)
示例#2
0
  def testRaiseValueErrorWithInvalidDepthMultiplier(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    with self.assertRaises(ValueError):
      _ = mobilenet_v1.mobilenet_v1(
          inputs, num_classes, depth_multiplier=-0.1)
    with self.assertRaises(ValueError):
      _ = mobilenet_v1.mobilenet_v1(
          inputs, num_classes, depth_multiplier=0.0)
示例#3
0
    def build(self, cost, model, train):
        if model == "MobilenetV1":
            with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
                self.logits, self.end_points = mobilenet_v1.mobilenet_v1(
                    self.input,
                    num_classes=self.numb_logits,
                    dropout_keep_prob=self.kp,
                    is_training=train)
        elif model == "vgg_16":
            with slim.arg_scope(vgg.vgg_arg_scope()):
                self.logits, self.end_points = vgg.vgg_16(
                    self.input,
                    num_classes=self.numb_logits,
                    dropout_keep_prob=self.kp,
                    is_training=True)

        self.prob = tf.nn.softmax(self.logits, name="prob")
        self.loss = tf.reduce_mean(
            tf.reduce_sum(tf.pow(self.prob - self.target, 2), axis=1))
        tf.summary.scalar('loss', self.loss)
        if cost == "mse":
            self.cost = self.loss
        else:
            self.xtarget = self.target * (1 - 1e-11) + 1e-12
            assert self.xtarget.get_shape().as_list()[1] == self.numb_logits
            self.xprob = self.prob * (1 - 1e-11) + 1e-12
            assert self.xprob.get_shape().as_list()[1] == self.numb_logits
            self.cost = tf.reduce_mean(
                tf.reduce_sum(self.xtarget * tf.log(self.xtarget / self.prob),
                              axis=1))
            tf.summary.scalar('cost_kl', self.cost)
示例#4
0
def build_model():
  """Build the mobilenet_v1 model for evaluation.

  Returns:
    g: graph with rewrites after insertion of quantization ops and batch norm
    folding.
    eval_ops: eval ops for inference.
    variables_to_restore: List of variables to restore from checkpoint.
  """
  g = tf.Graph()
  with g.as_default():
    inputs, labels = imagenet_input(is_training=False)

    scope = mobilenet_v1.mobilenet_v1_arg_scope(
        is_training=False, weight_decay=0.0)
    with slim.arg_scope(scope):
      logits, _ = mobilenet_v1.mobilenet_v1(
          inputs,
          is_training=False,
          depth_multiplier=FLAGS.depth_multiplier,
          num_classes=FLAGS.num_classes)

    if FLAGS.quantize:
      tf.contrib.quantize.create_eval_graph()

    eval_ops = metrics(logits, labels)

  return g, eval_ops
示例#5
0
def generator(images, n_filter=4,train=True, reuse=False):
    """define generator model
    Args:
        images: input images for generator
        n_filter: number of filter to learn from each image
        train: boolean value to specify if the network is in training mode or inference mode
        reuse: whether to reuse network variables or not    return: 
        output_2: enhanced version of input value 
    """
    with tf.variable_scope("generator",reuse=reuse):
        # first generator network
        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
            logits, end_points = mobilenet_v1.mobilenet_v1(images, activation_fn=leakyRely, dropout_keep_prob=1, is_training=train)
        net = end_points['Conv2d_13_pointwise']

        filters_1 = slim.conv2d(net,256, [3, 3],stride=2, activation_fn=tf.nn.relu, padding='VALID',
                             normalizer_fn=None, scope='filters_1')
        filters_2_1 = slim.conv2d(filters_1,n_filter, [1, 1],stride=1, activation_fn=None, padding='SAME',
                             normalizer_fn=None, scope='filters_2_1')

        filters_2_2 = tf.expand_dims(filters_2_1,axis=4, name='filters_2_2')

        output_1 = adapt_filter(images[:,:,:,1:2], filters_2_2[:,:,:,0:4,:], name="sat_adapt", train=train)
        print("output_1", output_1.get_shape().as_list())
        output_2 = tf.concat([images[:,:,:,0:1], output_1,images[:,:,:,2:3] ], axis=3)
        print("output_2", output_2.get_shape().as_list())
    return output_2
示例#6
0
def freeze_graph(checkpoint_path, output_node_name, outfile):
    input_layer = tf.placeholder(tf.uint8, shape=[None, None, 3], name='input')
    with tf.variable_scope('input_scaling'):
        image = tf.expand_dims(input_layer, axis=0)
        image = tf.image.resize_bilinear(image, [224, 224])
        image = tf.cast(image, tf.float32)
        image = image / 127.5
        image = image - 1

    logits, _ = mobilenet_v1.mobilenet_v1(image,
                                          num_classes=2,
                                          is_training=False)
    preds = tf.squeeze(tf.nn.softmax(logits), name='preds')

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(checkpoint_path)
        saver = tf.train.Saver()
        saver.restore(sess, ckpt.model_checkpoint_path)

        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess,
            tf.get_default_graph().as_graph_def(), [output_node_name])

        with tf.gfile.GFile(outfile, 'wb') as f:
            f.write(output_graph_def.SerializeToString())

        # print a list of ops
        for op in output_graph_def.node:
            print(op.name)

        print('Saved frozen model to {}'.format(outfile))
        print('{:d} ops in the final graph.'.format(len(
            output_graph_def.node)))
示例#7
0
 def create(self, images, num_classes, is_training):
     """See baseclass."""
     with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
         _, endpoints = mobilenet_v1.mobilenet_v1(inputs=images,
                                                  num_classes=num_classes,
                                                  is_training=is_training)
         return endpoints
示例#8
0
def build_model():
    """Build the mobilenet_v1 model for evaluation.

  Returns:
    g: graph with rewrites after insertion of quantization ops and batch norm
    folding.
    eval_ops: eval ops for inference.
    variables_to_restore: List of variables to restore from checkpoint.
  """
    g = tf.Graph()
    with g.as_default():
        inputs, labels = imagenet_input(is_training=False)

        scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False,
                                                    weight_decay=0.0)
        with slim.arg_scope(scope):
            logits, _ = mobilenet_v1.mobilenet_v1(
                inputs,
                is_training=False,
                depth_multiplier=FLAGS.depth_multiplier,
                num_classes=FLAGS.num_classes,
                final_endpoint=FLAGS.final_endpoint)

        if FLAGS.quantize:
            contrib_quantize.create_eval_graph()

        eval_ops = metrics(logits, labels)

    return g, eval_ops
示例#9
0
文件: model.py 项目: ziippy/vai-101
def create_mnist_mobilenet_model(input, label_count, is_training):
    dropout_prob = tf.placeholder(tf.float32, name='dropout_prob')

    X_img = tf.reshape(input, [-1, 28, 28, 1])
    resizing_img = tf.image.resize_bilinear(X_img, [224, 224])

    logits, end_points = mobilenet_v1.mobilenet_v1(resizing_img, label_count)
    return logits, dropout_prob
    def testTrainEvalWithReuse(self):
        train_batch_size = 5
        eval_batch_size = 2
        height, width = 150, 150
        num_classes = 1000

        train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
        mobilenet_v1.mobilenet_v1(train_inputs, num_classes)
        eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
        logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
                                              reuse=True)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (eval_batch_size,))
示例#11
0
  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 150, 150
    num_classes = 1000

    train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
    mobilenet_v1.mobilenet_v1(train_inputs, num_classes)
    eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
    logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
                                          reuse=True)
    predictions = tf.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
示例#12
0
  def testBuildEndPointsWithDepthMultiplierLessThanOne(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    _, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)

    endpoint_keys = [key for key in end_points.keys() if key.startswith('Conv')]

    _, end_points_with_multiplier = mobilenet_v1.mobilenet_v1(
        inputs, num_classes, scope='depth_multiplied_net',
        depth_multiplier=0.5)

    for key in endpoint_keys:
      original_depth = end_points[key].get_shape().as_list()[3]
      new_depth = end_points_with_multiplier[key].get_shape().as_list()[3]
      self.assertEqual(0.5 * original_depth, new_depth)
    def testBuildEndPointsWithDepthMultiplierLessThanOne(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        _, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)

        endpoint_keys = [key for key in end_points.keys() if key.startswith('Conv')]

        _, end_points_with_multiplier = mobilenet_v1.mobilenet_v1(
            inputs, num_classes, scope='depth_multiplied_net',
            depth_multiplier=0.5)

        for key in endpoint_keys:
            original_depth = end_points[key].get_shape().as_list()[3]
            new_depth = end_points_with_multiplier[key].get_shape().as_list()[3]
            self.assertEqual(0.5 * original_depth, new_depth)
示例#14
0
  def testLogitsNotSqueezed(self):
    num_classes = 25
    images = tf.random_uniform([1, 224, 224, 3])
    logits, _ = mobilenet_v1.mobilenet_v1(images,
                                          num_classes=num_classes,
                                          spatial_squeeze=False)

    with self.test_session() as sess:
      tf.global_variables_initializer().run()
      logits_out = sess.run(logits)
      self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
    def testLogitsNotSqueezed(self):
        num_classes = 25
        images = tf.random_uniform([1, 224, 224, 3])
        logits, _ = mobilenet_v1.mobilenet_v1(images,
                                              num_classes=num_classes,
                                              spatial_squeeze=False)

        with self.test_session() as sess:
            tf.global_variables_initializer().run()
            logits_out = sess.run(logits)
            self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
示例#16
0
  def testBuildPreLogitsNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = None

    inputs = tf.random_uniform((batch_size, height, width, 3))
    net, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
    self.assertTrue(net.op.name.startswith('MobilenetV1/Logits/AvgPool'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1024])
    self.assertFalse('Logits' in end_points)
    self.assertFalse('Predictions' in end_points)
    def testBuildPreLogitsNetwork(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = None

        inputs = tf.random_uniform((batch_size, height, width, 3))
        net, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
        self.assertTrue(net.op.name.startswith('MobilenetV1/Logits/AvgPool'))
        self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1024])
        self.assertFalse('Logits' in end_points)
        self.assertFalse('Predictions' in end_points)
示例#18
0
def build_model():
  """Builds graph for model to train with rewrites for quantization.

  Returns:
    g: Graph with fake quantization ops and batch norm folding suitable for
    training quantized weights.
    train_tensor: Train op for execution during training.
  """
  g = tf.Graph()
  with g.as_default(), tf.device(
      tf.train.replica_device_setter(FLAGS.ps_tasks)):

    #Reads in data and/or performs pre-processing on the images.
    inputs, labels, _ = tf_input.input(is_random=True, is_training=True)
    labels = labels - 1
    labels = slim.one_hot_encoding(labels, FLAGS.num_classes)

    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)):
      logits, _ = mobilenet_v1.mobilenet_v1(
          inputs,
          is_training=True,
          depth_multiplier=FLAGS.depth_multiplier,
          num_classes=FLAGS.num_classes)

    tf.losses.softmax_cross_entropy(labels, logits)

    # Call rewriter to produce graph with fake quant ops and folded batch norms
    # quant_delay delays start of quantization till quant_delay steps, allowing
    # for better model accuracy.
    if FLAGS.quantize:
      tf.contrib.quantize.create_training_graph(quant_delay=get_quant_delay())

    total_loss = tf.losses.get_total_loss(name='total_loss')
    # Configure the learning rate using an exponential decay.
    num_epochs_per_decay = 2.5
    data_size = tf_input.TRAINING_SET_SIZE
    decay_steps = int(data_size / FLAGS.batch_size * num_epochs_per_decay)

    learning_rate = tf.train.exponential_decay(
        get_learning_rate(),
        tf.train.get_or_create_global_step(),
        decay_steps,
        _LEARNING_RATE_DECAY_FACTOR,
        staircase=True)
    opt = tf.train.GradientDescentOptimizer(learning_rate)

    train_tensor = slim.learning.create_train_op(
        total_loss,
        optimizer=opt)

  slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses')
  slim.summaries.add_scalar_summary(learning_rate, 'learning_rate', 'training')
  return g, train_tensor
示例#19
0
def fcn_mobv1(images, num_classes, is_training=True):

    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
        _, end_points = mobilenet_v1.mobilenet_v1(images,
                                                  num_classes,
                                                  is_training=False,
                                                  spatial_squeeze=False)

        #        for v,k in end_points.items():
        #                print('{v}:{k}'.format(v = v, k = k))

        #        pool4=end_points['resnet_v1_101/pool4']
        #
        #        dconv1_out=pool4.get_shape().as_list()
        #
        #
        #        deconv1=slim.conv2d_transpose(net,dconv1_out[3],[4,4], stride=2,scope='deconv1')
        #
        #        fu1=tf.add(deconv1,pool4)
        #
        #
        #        pool3=end_points['resnet_v1_101/pool3']
        #        dconv2_out=pool3.get_shape().as_list()
        #        deconv2=slim.conv2d_transpose(fu1,dconv2_out[3],[4,4], stride=2,scope='deconv2')
        #
        #        fu2=tf.add(deconv2,pool3)
        net_7 = end_points['Conv2d_13_pointwise']
        net_14 = end_points['Conv2d_11_pointwise']
        net_28 = end_points['Conv2d_5_pointwise']

        up1 = slim.conv2d_transpose(net_7,
                                    512, [4, 4],
                                    stride=2,
                                    scope='deconv32')
        fu1 = tf.add(up1, net_14, name='fu1')

        up2 = slim.conv2d_transpose(fu1,
                                    256, [4, 4],
                                    stride=2,
                                    scope='deconv16')
        fu2 = tf.add(up2, net_28, name='fu2')

        logit = slim.conv2d_transpose(fu2,
                                      num_classes, [16, 16],
                                      stride=8,
                                      scope='deconv8')

        prediction = tf.argmax(logit, dimension=3)  #, name="prediction")

        print('logit', logit)

        return logit, tf.expand_dims(prediction, axis=3)
示例#20
0
  def testBuildClassificationNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue('Predictions' in end_points)
    self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
                         [batch_size, num_classes])
示例#21
0
  def testHalfSizeImages(self):
    batch_size = 5
    height, width = 112, 112
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    pre_pool = end_points['Conv2d_13_pointwise']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 4, 4, 1024])
示例#22
0
  def testBuildClassificationNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue('Predictions' in end_points)
    self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
                         [batch_size, num_classes])
def _build_mobilenet_model(is_training, images, params):
    with slim.arg_scope(
            mobilenet_v1.mobilenet_v1_arg_scope(is_training=is_training)):
        out, _ = mobilenet_v1.mobilenet_v1(
            images,
            is_training=is_training,
            depth_multiplier=params.depth_multiplier,
            num_classes=None)
        tf.logging.info("mobilenet preembedding shape{}".format(
            out.get_shape().as_list()))
        out = tf.reshape(out, [-1, 256])
        out = tf.layers.dense(out, params.embedding_size, name="embeddings")
    return out
    def testHalfSizeImages(self):
        batch_size = 5
        height, width = 112, 112
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(),
                             [batch_size, num_classes])
        pre_pool = end_points['Conv2d_13_pointwise']
        self.assertListEqual(pre_pool.get_shape().as_list(),
                             [batch_size, 4, 4, 1024])
def endpoints(image, is_training):
    if image.get_shape().ndims != 4:
        raise ValueError('Input must be of size [batch, height, width, 3]')

    image = tf.divide(image, 255.0)

    with tf.contrib.slim.arg_scope(mobilenet_v1_arg_scope(batch_norm_decay=0.9, weight_decay=0.0)):
        _, endpoints = mobilenet_v1(image, num_classes=1001, is_training=is_training)

    endpoints['model_output'] = endpoints['global_pool'] = tf.reduce_mean(
        endpoints['Conv2d_13_pointwise'], [1, 2], name='global_pool', keep_dims=False)

    return endpoints, 'MobilenetV1'
示例#26
0
def perceptual_params(images, reuse=False):
    """get semntics params of images
    Args:
        images: input images for generator
        reuse: whether to reuse network variables or not
    return: 
        sementics params of images
    """
    with tf.variable_scope("semantic",reuse=reuse):
        # first generator network
        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
            logits, end_points = mobilenet_v1.mobilenet_v1(images, num_classes=1001, dropout_keep_prob=1, is_training=False)
    return tf.squeeze(end_points['AvgPool_1a'],[1,2])
    def testEvaluation(self):
        batch_size = 2
        height, width = 224, 224
        num_classes = 1000

        eval_inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
                                              is_training=False)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (batch_size,))
示例#28
0
  def testEvaluation(self):
    batch_size = 2
    height, width = 224, 224
    num_classes = 1000

    eval_inputs = tf.random_uniform((batch_size, height, width, 3))
    logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
                                          is_training=False)
    predictions = tf.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.placeholder(tf.float32, (None, height, width, 3))
        logits, _ = mobilenet_v1.mobilenet_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
        images = tf.random_uniform((batch_size, height, width, 3))

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(logits, {inputs: images.eval()})
            self.assertEquals(output.shape, (batch_size, num_classes))
示例#30
0
def build_model():
  """Builds graph for model to train with rewrites for quantization.

  Returns:
    g: Graph with fake quantization ops and batch norm folding suitable for
    training quantized weights.
    train_tensor: Train op for execution during training.
  """
  g = tf.Graph()
  with g.as_default(), tf.device(
      tf.train.replica_device_setter(FLAGS.ps_tasks)):
    inputs, labels = imagenet_input(is_training=True)
    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)):
      logits, _ = mobilenet_v1.mobilenet_v1(
          inputs,
          is_training=True,
          depth_multiplier=FLAGS.depth_multiplier,
          num_classes=FLAGS.num_classes)

    tf.losses.softmax_cross_entropy(labels, logits)

    # Call rewriter to produce graph with fake quant ops and folded batch norms
    # quant_delay delays start of quantization till quant_delay steps, allowing
    # for better model accuracy.
    if FLAGS.quantize:
      tf.contrib.quantize.create_training_graph(quant_delay=get_quant_delay())

    total_loss = tf.losses.get_total_loss(name='total_loss')
    # Configure the learning rate using an exponential decay.
    num_epochs_per_decay = 2.5
    imagenet_size = 1271167
    decay_steps = int(imagenet_size / FLAGS.batch_size * num_epochs_per_decay)

    learning_rate = tf.train.exponential_decay(
        get_learning_rate(),
        tf.train.get_or_create_global_step(),
        decay_steps,
        _LEARNING_RATE_DECAY_FACTOR,
        staircase=True)
    opt = tf.train.GradientDescentOptimizer(learning_rate)

    train_tensor = slim.learning.create_train_op(
        total_loss,
        optimizer=opt)

  slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses')
  slim.summaries.add_scalar_summary(learning_rate, 'learning_rate', 'training')
  return g, train_tensor
示例#31
0
  def testUnknowBatchSize(self):
    batch_size = 1
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.placeholder(tf.float32, (None, height, width, 3))
    logits, _ = mobilenet_v1.mobilenet_v1(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [None, num_classes])
    images = tf.random_uniform((batch_size, height, width, 3))

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))
    def create_model(self, inputs, num_classes, is_training):
        with slim.arg_scope(
                mobilenet_v1_arg_scope(
                    is_training,
                    FLAGS.weight_decay,
                    regularize_depthwise=FLAGS.regularize_depthwise)):
            self.logits, self.end_points = mobilenet_v1(
                inputs,
                num_classes,
                FLAGS.dropout_keep_prob,
                is_training,
                depth_multiplier=FLAGS.depth_multiplier)

        for var in tf.model_variables():
            if 'weights' in var.op.name:
                tf.add_to_collection(tf.GraphKeys.WEIGHTS, var)
示例#33
0
def load_model(model='Mobile'):
    global x, y, sess
    # 占位符
    x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 1])

    # 模型保存路径,前向传播
    if model == 'Alex':
        log_path = 'weight/Alex'
        y, _ = alexnet.alexnet_v2(
            x,
            num_classes=CLASSES,  # 分类的类别
            is_training=False,  # 是否在训练
            dropout_keep_prob=1.0,  # 保留比率
            spatial_squeeze=True,  # 压缩掉1维的维度
            global_pool=GLOBAL_POOL)  # 输入不是规定的尺寸时,需要global_pool
    elif model == 'Mobile':
        log_path = 'weight/Mobile'
        y, _ = mobilenet_v1.mobilenet_v1(x,
                                         num_classes=CLASSES,
                                         dropout_keep_prob=1.0,
                                         is_training=False,
                                         min_depth=8,
                                         depth_multiplier=1.0,
                                         conv_defs=None,
                                         prediction_fn=None,
                                         spatial_squeeze=True,
                                         reuse=None,
                                         scope='MobilenetV1',
                                         global_pool=GLOBAL_POOL)
    else:
        print('Error: model name not exist')
        return
    y = tf.nn.softmax(y)

    saver = tf.train.Saver()

    sess = tf.Session()
    # 恢复模型权重
    # print('Reading checkpoints from: ', model)
    ckpt = tf.train.get_checkpoint_state(log_path)
    if ckpt and ckpt.model_checkpoint_path:
        global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
        saver.restore(sess, ckpt.model_checkpoint_path)
        # print('Loading success, global_step is %s' % global_step)
    else:
        print('Error: no checkpoint file found')
        return -1
 def testUnknownImageShape(self):
     tf.reset_default_graph()
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
         logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
         pre_pool = end_points['Conv2d_13_pointwise']
         feed_dict = {inputs: input_np}
         tf.global_variables_initializer().run()
         pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
         self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
示例#35
0
 def testUnknownImageShape(self):
   tf.reset_default_graph()
   batch_size = 2
   height, width = 224, 224
   num_classes = 1000
   input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
     logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Conv2d_13_pointwise']
     feed_dict = {inputs: input_np}
     tf.global_variables_initializer().run()
     pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
     self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
def freeze_mobilenet(meta_file, img_size=224, factor=1.0, num_classes=1001):

    tf.reset_default_graph()

    inp = tf.placeholder(tf.float32,
                         shape=(None, img_size, img_size, 3),
                         name="input")

    is_training = False
    weight_decay = 0.0
    arg_scope = mobilenet_v1.mobilenet_v1_arg_scope(weight_decay=weight_decay)
    with slim.arg_scope(arg_scope):
        logits, _ = mobilenet_v1.mobilenet_v1(inp,
                                              num_classes=num_classes,
                                              is_training=is_training,
                                              depth_multiplier=factor)

    predictions = tf.contrib.layers.softmax(logits)
    output = tf.identity(predictions, name='output')

    ckpt_file = meta_file.replace('.meta', '')
    output_graph_fn = ckpt_file.replace('.ckpt', '.pb')
    output_node_names = "output"

    rest_var = slim.get_variables_to_restore()

    with tf.Session() as sess:
        graph = tf.get_default_graph()
        input_graph_def = graph.as_graph_def()

        saver = tf.train.Saver(rest_var)
        saver.restore(sess, ckpt_file)

        # We use a built-in TF helper to export variables to constant
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,  # The session is used to retrieve the weights
            input_graph_def,  # The graph_def is used to retrieve the nodes
            output_node_names.split(
                ","
            )  # The output node names are used to select the usefull nodes
        )

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph_fn, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("{} ops in the final graph.".format(len(output_graph_def.node)))
def freeze_mobilenet(meta_file, img_size=224, factor=1.0, num_classes=1001):

  tf.reset_default_graph()

  inp = tf.placeholder(tf.float32,
                      shape=(None, img_size, img_size, 3),
                      name="input")

  is_training=False
  weight_decay = 0.0
  arg_scope = mobilenet_v1.mobilenet_v1_arg_scope(weight_decay=weight_decay)
  with slim.arg_scope(arg_scope):
    logits, _ = mobilenet_v1.mobilenet_v1(inp,
                                          num_classes=num_classes,
                                          is_training=is_training,
                                          depth_multiplier=factor)

  predictions = tf.contrib.layers.softmax(logits)
  output = tf.identity(predictions, name='output')

  ckpt_file = meta_file.replace('.meta', '')
  output_graph_fn = ckpt_file.replace('.ckpt', '.pb')
  output_node_names = "output"

  rest_var = slim.get_variables_to_restore()

  with tf.Session() as sess:
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    saver = tf.train.Saver(rest_var)
    saver.restore(sess, ckpt_file)

    # We use a built-in TF helper to export variables to constant
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, # The session is used to retrieve the weights
        input_graph_def, # The graph_def is used to retrieve the nodes
        # The output node names are used to select the useful nodes
        output_node_names.split(",")
    )

    # Finally we serialize and dump the output graph to the filesystem
    with tf.gfile.GFile(output_graph_fn, "wb") as f:
        f.write(output_graph_def.SerializeToString())
    print("{} ops in the final graph.".format(len(output_graph_def.node)))
示例#38
0
def mobilenet_v1_100(inputs, is_training, opts):
    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(
            is_training=is_training,
            weight_decay=opts.weight_decay,
            stddev=0.09,
            regularize_depthwise=False,
            batch_norm_decay=opts.batch_norm_decay,
            batch_norm_epsilon=opts.batch_norm_epsilon)):
        return mobilenet_v1.mobilenet_v1(
            inputs,
            num_classes=opts.num_classes,
            dropout_keep_prob=opts.dropout_keep_prob,
            is_training=is_training,
            min_depth=8,
            depth_multiplier=1.0,
            global_pool=opts.global_pool,
            spatial_squeeze=opts.spatial_squeeze,
            reuse=None)
示例#39
0
def discriminator(images, kp, n_output=10, reuse=False, train=True):
    """define discriminator model 
    Args:
        images: input images for discriminator
        kp: keeping probality for droping layer of discriminator
        n_output: discriminator output size
        reuse: whether reuse variable or not 
        train: training mode or inference mode
    return: 
        preds: discriminator output containing image aesthetic rating and other variables
    """
    with tf.variable_scope("discriminator", reuse=reuse):
        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
            logits, end_points = mobilenet_v1.mobilenet_v1(images, num_classes=n_output,
                                                       dropout_keep_prob=kp, is_training=train)
    preds = tf.nn.softmax(logits)
    print("preds: ",preds.get_shape().as_list())
    return preds
示例#40
0
def export_eval_pbtxt():
    """Export eval.pbtxt."""
    g = tf.Graph()
    with g.as_default():
        inputs = tf.placeholder(dtype=tf.float32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, CHANNEL])
        scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False,
                                                    weight_decay=0.0)
        with slim.arg_scope(scope):
            _, _ = mobilenet_v1.mobilenet_v1(
                inputs,
                is_training=False,
                depth_multiplier=FLAGS.depth_multiplier,
                num_classes=FLAGS.num_classes)
        if FLAGS.quantize:
            tf.contrib.quantize.create_eval_graph()
        with tf.Session() as sess:
            with open(FLAGS.eval_graph_file, 'w') as f:
                f.write(str(g.as_graph_def()))
示例#41
0
  def __call__(self, x_input, batch_size=None, is_training=False):
    """Constructs model and return probabilities for given input."""
    reuse = True if self.built else None

    preproc = tf.map_fn(
      lambda img: inception_preprocess(img,
                                       mobilenet_v1.mobilenet_v1.default_image_size,
                                       mobilenet_v1.mobilenet_v1.default_image_size), x_input)

    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
      with tf.variable_scope(self.ckpt):
        logits, end_points = mobilenet_v1.mobilenet_v1(
            preproc, num_classes=self.num_classes, is_training=is_training,
            reuse=reuse)

      preds = tf.argmax(logits, axis=1)
    self.built = True
    self.logits = logits
    self.preds = preds
    return logits
示例#42
0
def model_fn(features,labels,mode,params):
    global_step = tf.train.get_global_step()
    inputs = features['MRI']
    logits, end_points= mobilenet_v1.mobilenet_v1(inputs=inputs,
                                                  num_classes=30)

    # predict mode
    predicted_classes = tf.argmax(logits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class':predicted_classes,
            'prob':tf.nn.softmax(logits)
        }
        return tf.estimator.EstimatorSpec(mode,predictions=predictions)
    with tf.name_scope('accuracy'):
        accuracy = tf.metrics.accuracy(labels=labels,predictions=predicted_classes)
        my_acc = tf.reduce_mean(tf.cast(tf.equal(labels, predicted_classes), tf.float32))
        tf.summary.scalar('accuracy',my_acc)


    # compute loss
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels,logits=logits)
    # hook
    train_hook_list = []
    train_tensors_log = {'accuracy':accuracy[1],
                         'my_acc': my_acc,
                         'loss':loss,
                         'global_step':global_step}
    train_hook_list.append(tf.train.LoggingTensorHook(tensors=train_tensors_log,every_n_iter=200))
    # training op
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.MomentumOptimizer(learning_rate=0.001,momentum=0.9)
        train_op = optimizer.minimize(loss,global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode,loss=loss,train_op=train_op,training_hooks=train_hook_list)
    # compute evaluation metrics
    eval_metric_ops = {
        'accuracy':tf.metrics.accuracy(labels=labels,predictions=predicted_classes)
    }
    return tf.estimator.EstimatorSpec(mode=mode,loss=loss,eval_metric_ops=eval_metric_ops)
示例#43
0
def build_model():
    """Build the mobilenet_v1 model for evaluation.

  Returns:
    g: graph with rewrites after insertion of quantization ops and batch norm
    folding.
    eval_ops: eval ops for inference.
    variables_to_restore: List of variables to restore from checkpoint.
  """
    g = tf.Graph()
    with g.as_default():
        inputs, labels = dataset_input(is_training=False)
        # inputs, labels = merge_dataset(is_training=False)

        scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False,
                                                    weight_decay=0.0)
        with slim.arg_scope(scope):
            dataset = dataset_factory.get_dataset(FLAGS.dataset_name,
                                                  FLAGS.dataset_split_name,
                                                  FLAGS.dataset_dir)
            logits, _ = mobilenet_v1.mobilenet_v1(
                inputs,
                is_training=False,
                depth_multiplier=FLAGS.depth_multiplier,
                num_classes=dataset.num_classes,
                model_scope=FLAGS.model_scope,
                logits_scope=FLAGS.logits_scope,
                conv2d_0_scope=FLAGS.conv2d_0_scope,
                depthwise_scope=FLAGS.depthwise_scope,
                pointwise_scope=FLAGS.pointwise_scope,
                pointwise_merged_mask=FLAGS.pointwise_merged_mask)

        if FLAGS.quantize:
            tf.contrib.quantize.create_eval_graph()

        eval_ops = metrics(logits, labels)

    tf.logging.info('Evaluating %s' % FLAGS.checkpoint_path)
    return g, eval_ops
示例#44
0
 def create(self, images, num_classes, is_training):
   """See baseclass."""
   with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
     _, endpoints = mobilenet_v1.mobilenet_v1(
         inputs=images, num_classes=num_classes, is_training=is_training)
     return endpoints