示例#1
0
 def testBatchNormScopeDoesHasIsTrainingWhenItsNotNone(self):
   global numbers
   numbers += 1
   print('testBatchNormScopeDoesHasIsTrainingWhenItsNotNone: ',numbers)
   sc = mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)
   self.assertIn('is_training', sc[slim.arg_scope_func_key(slim.batch_norm)])
   sc = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False)
   self.assertIn('is_training', sc[slim.arg_scope_func_key(slim.batch_norm)])
   sc = mobilenet_v1.mobilenet_v1_arg_scope()
   self.assertIn('is_training', sc[slim.arg_scope_func_key(slim.batch_norm)])
示例#2
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)
    inputs = np.random.randint(0, 255, size=(1,224,224,3))
    labels = np.random.randint(0, 10, size=(1,10))
    inputs = tf.Variable(inputs,dtype=tf.float32)
    labels = tf.Variable(labels,dtype=tf.float32)
    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
示例#3
0
def visual_modulator_lite(guide_image,
                          model_params,
                          scope='osmn',
                          is_training=False):
    """Defines the visual modulator
    Args:
    gudie_image: visual guide image
    model_params: parameters related to model structure
    scope: scope name for the network
    is_training: training or testing
    Returns:
    Tensor of the visual modulation parameters
    """
    mod_early_conv = model_params.mod_early_conv
    n_modulator_param = [128, 256, 512, 1024]  # 1024 + 512 + 256 + 128
    with tf.variable_scope(scope, [guide_image]) as sc, \
            slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=is_training)) as arg_sc:
        modulator_params = None

        # Collect outputs of all intermediate layers.
        modulator_params, end_points = mobilenet_v1.mobilenet_v1(
            guide_image,
            scope='modulator',
            num_classes=n_modulator_param,
            spatial_squeeze=False,
            is_training=is_training)
    return modulator_params
def run():
    image_size = 224
    num_classes = 200
    logdir = './log'

    checkpoint_file = tf.train.latest_checkpoint(logdir)

    with tf.Graph().as_default() as graph:
        
        images = tf.placeholder("float", [1, image_size, image_size, 3], name="input")
        #images = tf.placeholder(shape=[1, image_size, image_size, 3], dtype=tf.float32, name = 'Placeholder_only')

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

        variables_to_restore = slim.get_variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        #Setup graph def
        input_graph_def = graph.as_graph_def()
        output_node_names = "MobilenetV1/Predictions/Softmax"
        output_graph_name = "./frozen_model_mobilenet_v1.pb"

        with tf.Session() as sess:
            saver.restore(sess, checkpoint_file)

            #Exporting the graph
            print ("Exporting graph...")
            output_graph_def = graph_util.convert_variables_to_constants(
                sess,
                input_graph_def,
                output_node_names.split(","))

            with tf.gfile.GFile(output_graph_name, "wb") as f:
                f.write(output_graph_def.SerializeToString())
def test_mobilenet_v1():
    batch_size = 5
    height, width = 224, 224
    num_classes = 1001

    img = cv2.imread('laska.png')
    img_resize = cv2.resize(img, (height, width))
    img_rgb = cv2.cvtColor(img_resize, cv2.COLOR_BGR2RGB).astype(np.float32)
    # input_mat = np.random.rand(batch_size, height, width, 3).astype(np.float32)
    input_mat = np.zeros(shape=(batch_size, height, width, 3),
                         dtype=np.float32)
    # input_mat[0, :, :, :] = (img_rgb - 127) / 127.0
    input_mat[0, :, :, :] = img_rgb / 255.0
    print('input_mat[0]:', input_mat[0, :, :, :])
    print('input_mat[1]:', input_mat[1, :, :, :])

    # inputs = tf.random_uniform((batch_size, height, width, 3), name='input')
    inputs = tf.convert_to_tensor(input_mat, name='input', dtype=tf.float32)

    arg_scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False,
                                                    weight_decay=0.0)
    with slim.arg_scope(arg_scope):
        logits, end_points = mobilenet_v1.mobilenet_v1(inputs,
                                                       num_classes,
                                                       is_training=False)

    model_dir = '/Users/alexwang/data/mobilenet_v1_1.0_160'
    checkpoint_path = os.path.join(model_dir, 'mobilenet_v1_1.0_160.ckpt')
    print_tensors_in_checkpoint_file(checkpoint_path, None, False)
    saver = tf.train.Saver()

    # print all node in graph
    # for tensor in tf.get_default_graph().as_graph_def().node:
    #     print(tensor.name)

    input_get = tf.get_default_graph().get_tensor_by_name('input:0')
    print('shape of input_get:{}'.format(input_get.shape))

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        predict = sess.run([end_points['Predictions']])
        print(predict)
        classes = np.argmax(predict[0], axis=1)
        print(classes)

        saver.restore(sess, checkpoint_path)
        predict = sess.run([end_points['Predictions']])[0]
        print('predict:', predict)
        classes = np.argsort(predict, axis=1)
        for predict_result in classes:
            # print(predict_result[::-1][0:5])
            class_names = [(label_names[i], predict[0][i])
                           for i in predict_result[::-1][0:5]]
            print(class_names)
示例#6
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())

        corr = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
        accr = tf.reduce_mean(tf.cast(corr, "float"))

        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 = FLAGS.training_set_size
        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')
    slim.summaries.add_scalar_summary(accr, 'accuracy', 'training')
    return g, train_tensor
示例#7
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(mobilenet_v1.mobilenet_v1_arg_scope()):
     _, end_points = mobilenet_v1.mobilenet_v1(
         x_input, num_classes=self.num_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]
   return probs
def build_model():
    print('build model')
    g = tf.Graph()
    with g.as_default(), tf.device(
            tf.train.replica_device_setter(FLAGS.ps_tasks)):
        # inputs, labels = imagenet_input(is_training=True)
        inputs, labels = cifar10_input.distorted_inputs(
            '../cifar10/cifar-10-batches-bin', 100)
        with slim.arg_scope(
                mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)):
            print('default layers')
            logits, _ = mobilenet_v1.mobilenet_v1(
                inputs,
                is_training=True,
                depth_multiplier=FLAGS.depth_multiplier,
                num_classes=FLAGS.num_classes,
            )

        print('start train')
        one_hot_labels = tf.one_hot(tf.cast(labels, dtype=tf.uint8),
                                    FLAGS.num_classes,
                                    dtype=tf.float32)
        slim.losses.softmax_cross_entropy(one_hot_labels, logits)
        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', 'traing')
    return g, train_tensor
示例#9
0
def ConvNet(input):
    curr_arg_scope = mobilenet_v1.mobilenet_v1_arg_scope()
    with slim.arg_scope(curr_arg_scope):
        nets = mobilenet(input, is_training=False)

    logits = slim.conv2d(nets,
                         1,
                         1,
                         activation_fn=tf.identity,
                         scope='outputR',
                         padding="VALID")

    with tf.name_scope('regress'):
        result = tf.identity(logits, name='result')

    return result
示例#10
0
def get_backbone_func_and_arg_scope(config):
    """

    :param network_name: string
    :return: network func
    """
    if config.Network.encoder_backbone is None:
        return None
    elif config.Network.encoder_backbone == 'MobilenetV1':
        from mobilenet_v1 import mobilenet_v1_base, mobilenet_v1_arg_scope
        return mobilenet_v1_base, mobilenet_v1_arg_scope(is_training=config.is_training)
    elif config.Network.encoder_backbone == 'Ori_Unet':
        from unet_base import unet_base, unet_base_arg_scope
        return unet_base, unet_base_arg_scope(is_training=config.is_training)
    else:
        raise Exception('invalid network name: %s' % config.Network.encoder_backbone)
示例#11
0
def build_model(image_datas):
    with tf.Graph().as_default():

        image = tf.placeholder(dtype=tf.float32, shape=[None, None, 3])

        def preprocessing_fn(image, output_height, output_width, **kwargs):
            return preprocessing_factory.preprocess_image(
                image, output_height, output_width, is_training=False, **kwargs)

        processed_image = preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
        processed_images = tf.expand_dims(processed_image, 0)

        scope = mobilenet_v1.mobilenet_v1_arg_scope(
            weight_decay=0.0)
        # Create the model, use the default arg scope to configure the batch norm parameters.

        with slim.arg_scope(scope):
            # 1000 classes instead of 1001.
            logits, _ = mobilenet_v1.mobilenet_v1(
                processed_images,
                is_training=False,
                depth_multiplier=FLAGS.depth_multiplier,
                num_classes=FLAGS.num_classes)
        probabilities = tf.nn.softmax(logits)
        variables_to_restore = slim.get_variables_to_restore()

        saver = tf.train.Saver(var_list=variables_to_restore)
        with tf.Session() as sess:

            saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_path))
            # print_tensor(sess)
            for image_data in image_datas:
                p_image, g_probabilities = sess.run([processed_image, probabilities], feed_dict={image: image_data})

                g_probabilities = g_probabilities[0].squeeze()
                sorted_inds = [i[0] for i in sorted(enumerate(-g_probabilities), key=lambda x: x[1])]

                for i in range(len(sorted_inds)):
                    index = sorted_inds[i]
                    # print the probabilities of each category.
                    # print('Probability %0.2f%% => [%s]' % (g_probabilities[index] * 100, names[index]))
                print('recognize character:', names[sorted_inds[0]])
                cv2.imshow('1', p_image)
                cv2.waitKey()
示例#12
0
def freeze():
    with tf.Graph().as_default() as graph:
        placeholder = tf.placeholder(
            name='input',
            dtype=tf.float32,
            shape=[FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 1])
        scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False,
                                                    weight_decay=0.0)
        with slim.arg_scope(scope):
            mobilenet_v1.mobilenet_v1(placeholder,
                                      is_training=False,
                                      depth_multiplier=FLAGS.depth_multiplier,
                                      num_classes=FLAGS.num_classes)

        if FLAGS.quantize:
            tf.contrib.quantize.create_eval_graph()
        graph_def = graph.as_graph_def()
        with tf.gfile.GFile(FLAGS.output_file, 'wb') as f:
            f.write(graph_def.SerializeToString())
示例#13
0
def mobilenetv1_fcn8_model(images,
                           num_classes,
                           is_training=False,
                           raw_image_shape=(520 - 170, 800),
                           data_aug_faster_mode=False,
                           decoder='fcn8'):
    train_image_shape = (224 * 2, 224 * 4)

    if decoder == 'fcn8':
        decoder_fn = mobilenet_v1_fcn_decoder
    elif decoder == 'fcn8_upsample':
        decoder_fn = mobilenet_v1_fcn8_upsample_decoder
    else:
        raise ValueError("the decoder should be either fcn8 or fcn8_upsample")
    # raw_image_shape=tf.constant((images.shape[2]),dtype=tf.int32)

    # images=tf.map_fn(lambda img: preprocess_image(img,224,224,is_training), images)

    tf.summary.image('image before resize', tf.expand_dims(images[0], 0))

    images = rescale_and_resize_images(images, train_image_shape)

    #if is_training:
    #    images = tf.map_fn(lambda x: inception_preprocessing.random_distort_images(x,fast_mode=data_aug_faster_mode),
    #                       images, dtype=tf.float32)

    with tf.contrib.slim.arg_scope(
            mobilenet_v1_arg_scope(is_training=is_training)):
        m_logits, end_points = mobilenet_v1(images,
                                            num_classes=1001,
                                            spatial_squeeze=False)

    layer4 = end_points['Conv2d_4_pointwise']
    layer6 = end_points['Conv2d_6_pointwise']
    layer13 = end_points['Conv2d_13_pointwise']

    last_layer = decoder_fn(layer13, layer4, layer6, num_classes)

    last_layer = post_process_logits(end_points, last_layer, raw_image_shape,
                                     train_image_shape)

    return last_layer, end_points
示例#14
0
    def predict(self, preprocessed_inputs, is_training):
        """Predict prediction tensors from inputs tensor.

        Outputs of this function can be passed to loss or postprocess functions.

        Args:
            preprocessed_inputs: A float32 tensor with shape [batch_size,
                height, width, num_channels] representing a batch of images.

        Returns:
            prediction_dict: A dictionary holding prediction tensors to be
                passed to the Loss or Postprocess functions.
        """
        with slim.arg_scope(mobilenet_v1_arg_scope(is_training=is_training)):
            logits, end_points = mobilenet_v1(preprocessed_inputs, is_training=is_training, depth_multiplier=1.0, num_classes=70)


        prob = tf.nn.softmax(logits, name='prob')
        prediction_dict = {'prob': prob}
        return prediction_dict
示例#15
0
def _tower_fn(is_training, dp_keep_prob, weight_decay, feature, label,
              data_format, num_layers, batch_norm_decay, batch_norm_epsilon,
              params):

    if params.model_name == 'mobilenet_v2':
        with slim.arg_scope(
                mobilenet_v2.training_scope(is_training=True,
                                            dropout_keep_prob=dp_keep_prob)):
            logits, end_points = mobilenet_v2.mobilenet(
                feature, num_classes=num_classes, prediction_fn=None)
    if params.model_name == 'mobilenet_v1':
        with slim.arg_scope(
                mobilenet_v1.mobilenet_v1_arg_scope(is_training=False)):
            logits, end_points = mobilenet_v1.mobilenet_v1(
                feature, num_classes=num_classes)

    tower_pred = {
        'classes': tf.argmax(input=logits, axis=1),
        'probabilities': tf.nn.softmax(logits)
    }
    #tower_loss = tf.losses.softmax_cross_entropy(
    #              logits=logits, onehot_labels=tf.one_hot(label, depth=num_classes))
    tower_loss = tf.losses.sparse_softmax_cross_entropy(logits=logits,
                                                        labels=label)
    tower_loss = tf.reduce_mean(tower_loss)
    model_params = tf.trainable_variables()
    depthwise_params = [v for v in model_params if 'depthwise' in v.op.name]

    if params.weight_decay_not_used_on_depthwise:
        model_params_for_weight_decay = [
            v for v in model_params if v not in depthwise_params
        ]
    else:
        model_params_for_weight_decay = model_params

    tower_loss += weight_decay * tf.add_n(
        [tf.nn.l2_loss(v) for v in model_params_for_weight_decay])
    tower_grad = tf.gradients(tower_loss, model_params)
    return tower_loss, zip(tower_grad, model_params), tower_pred
示例#16
0
def visual_modulator_lite(guide_image,
                          model_params,
                          scope='osmn',
                          is_training=False):
    """Defines the visual modulator
    Args:
    gudie_image: visual guide image
    model_params: parameters related to model structure
    scope: scope name for the network
    is_training: training or testing
    Returns:
    Tensor of the visual modulation parameters
    """
    mod_early_conv = model_params.mod_early_conv
    n_modulator_param = [128, 256, 512, 1024]  # 1024 + 512 + 256 + 128
    with tf.variable_scope(scope, [guide_image]) as sc, \
            slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=is_training)) as arg_sc:
        modulator_params = None

        # Collect outputs of all intermediate layers.
        net, end_points = mobilenet_v1.mobilenet_v1(guide_image,
                                                    scope='modulator',
                                                    num_classes=2048,
                                                    spatial_squeeze=False,
                                                    is_training=is_training)
        net = tf.nn.relu(net)
        modulator_params = []
        for i, n in enumerate(n_modulator_param):
            logits = slim.conv2d(net,
                                 n, [1, 1],
                                 activation_fn=None,
                                 weights_initializer=tf.zeros_initializer(),
                                 biases_initializer=tf.ones_initializer(),
                                 normalizer_fn=None,
                                 scope='Conv2d_out_%d' % (i + 1))
            modulator_params.append(logits)
    return modulator_params
示例#17
0
def build_model():
    """Build the mobilenet_v1 model for freeze.
  Returns:
    g: graph with rewrites after insertion of quantization ops and batch norm
    folding.
  """
    g = tf.Graph()
    with g.as_default():
        inputs = tf.placeholder(
            dtype=tf.float32,
            shape=[None, FLAGS.image_size, FLAGS.image_size, 3],
            name="input_node")
        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)
        logits = tf.identity(logits, name='output_node')
        if FLAGS.quantize:
            tf.contrib.quantize.create_eval_graph()
    return g
示例#18
0
def test_tf(inp):
    import tensorflow.contrib.slim as slim
    import mobilenet_v1
    input = tf.placeholder(tf.float32, [None, args.image_size, args.image_size, 3])
    with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
        net = mobilenet_v1.mobilenet_v1(input, num_classes=1001, depth_multiplier=args.depth_multiplier, is_training=False)
    # Add ops to restore all the variables.
    restorer = tf.train.Saver()

    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
      # Restore variables from disk.
      restorer.restore(sess, args.tensorflow_model)

      # with tf.variable_scope('',reuse=True):
      #   tmp = tf.get_variable('MobilenetV1/Conv2d_0/weights').eval()
      #   sess.run(tf.assign(tf.get_variable('MobilenetV1/Conv2d_0/weights'), var_dict['features.Conv2d_0.0.weight'].numpy().transpose([2,3,1,0])))#tmp * 0+1))
      print("Model restored.")
      # Do some work with the model
      out = sess.run(net, feed_dict={input: inp})
      # out = sess.run(net[1]['Conv2d_0'], feed_dict={input: inp})

    return out
示例#19
0
文件: model.py 项目: JC1DA/EAST
def model(images, weight_decay=1e-5, is_training=True, reuse=False):
    '''
    define the model, we use slim's implemention of resnet
    '''
    #images = mean_image_subtraction(images)
    images = (images - 127.5) / 128.0

    #with slim.arg_scope(resnet_v1.resnet_arg_scope(weight_decay=weight_decay)):
    #    logits, end_points = resnet_v1.resnet_v1_50(images, is_training=is_training, scope='resnet_v1_50')

    with slim.arg_scope(
            mobilenet.mobilenet_v1_arg_scope(
                is_training=is_training,
                weight_decay=weight_decay,
                regularize_depthwise=True if weight_decay > 0 else False)):
        logits, end_points = mobilenet.mobilenet_v1(
            images,
            num_classes=None,
            is_training=is_training,
            is_finetuning=is_training,
            final_endpoint='Conv2d_12_pointwise',
            reuse=reuse)

    with tf.variable_scope('feature_fusion', values=[end_points.values]):
        batch_norm_params = {
            'decay': 0.997,
            'epsilon': 1e-5,
            'scale': True,
            'is_training': is_training
        }
        with slim.arg_scope(
            [slim.conv2d],
                activation_fn=tf.nn.relu,
                normalizer_fn=slim.batch_norm,
                normalizer_params=batch_norm_params,
                weights_regularizer=slim.l2_regularizer(weight_decay)):
            # f = [end_points['pool5'], end_points['pool4'],
            #      end_points['pool3'], end_points['pool2']]
            f = [
                end_points['Conv2d_12_pointwise'],
                end_points['Conv2d_6_pointwise'],
                end_points['Conv2d_4_pointwise'],
                end_points['Conv2d_2_pointwise']
            ]
            for i in range(4):
                print('Shape of f_{} {}'.format(i, f[i].shape))
            g = [None, None, None, None]
            h = [None, None, None, None]
            num_outputs = [None, 128, 64, 32]
            for i in range(4):
                if i == 0:
                    h[i] = f[i]
                else:
                    c1_1 = slim.conv2d(tf.concat([g[i - 1], f[i]], axis=-1),
                                       num_outputs[i], 1)
                    h[i] = slim.conv2d(c1_1, num_outputs[i], 3)
                if i <= 2:
                    g[i] = unpool(h[i])
                else:
                    g[i] = slim.conv2d(h[i], num_outputs[i], 3)
                print('Shape of h_{} {}, g_{} {}'.format(
                    i, h[i].shape, i, g[i].shape))

            # here we use a slightly different way for regression part,
            # we first use a sigmoid to limit the regression range, and also
            # this is do with the angle map
            F_score = slim.conv2d(g[3],
                                  1,
                                  1,
                                  activation_fn=tf.nn.sigmoid,
                                  normalizer_fn=None)
            # 4 channel of axis aligned bbox and 1 channel rotation angle
            geo_map = slim.conv2d(
                g[3], 4, 1, activation_fn=tf.nn.sigmoid,
                normalizer_fn=None) * FLAGS.text_scale
            angle_map = (slim.conv2d(
                g[3], 1, 1, activation_fn=tf.nn.sigmoid, normalizer_fn=None) -
                         0.5) * np.pi / 2  # angle is between [-45, 45]
            F_geometry = tf.concat([geo_map, angle_map], axis=-1)

    return F_score, F_geometry
def debug():
    g = tf.Graph()
    inputs = tf.placeholder(shape=[None, 28, 28, 3], dtype=tf.float32)
    labels = tf.placeholder(shape=[
        None,
    ], dtype=tf.float32)
    with g.as_default(), tf.device(
            tf.train.replica_device_setter(FLAGS.ps_tasks)):
        # inputs, labels = imagenet_input(is_training=True)
        tf_inputs, tf_labels = cifar10_input.distorted_inputs(
            '../cifar10/cifar-10-batches-bin', 100)
        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,
            )

        one_hot_labels = tf.one_hot(tf.cast(labels, dtype=tf.uint8),
                                    FLAGS.num_classes,
                                    dtype=tf.float32)
        slim.losses.softmax_cross_entropy(one_hot_labels, logits)
        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', 'traing')

    init_op = [
        tf.global_variables_initializer(),
        tf.local_variables_initializer()
    ]

    # accuracy
    test_inputs, test_labels = cifar10_input.distorted_inputs(
        '../cifar10/cifar-10-batches-bin', 1000, is_validdata=True)
    accuracy = tf.metrics.accuracy(tf.argmax(logits, axis=1), test_labels)

    with tf.Session(graph=g) as session:
        session.run(init_op)

        for step in range(10000):
            _loss, _ = session.run([total_loss, train_tensor],
                                   feed_dict={
                                       inputs: tf_inputs,
                                       labels: tf_labels,
                                   })
            if step % 100 == 0:
                _accuracy = session.run(accuracy,
                                        feed_dict={
                                            inputs: test_inputs,
                                            labels: test_labels
                                        })
                print('step: %d, loss: %s, accuracy: %s' %
                      (step, _loss, _accuracy))
示例#21
0
dataset = Dataset(**vars(args))

# 1. Loads data set.
mnist = input_data.read_data_sets('MNIST_data')

# 2. Defines network.
# 2.1 Input feature dim = Nx784, N is sample number
# x = tf.placeholder(tf.float32, [None, 784], name='x-input')
x = tf.placeholder(tf.float32, [10, 224, 224, 3], name='x-input')

# 2.2 Reshapes the feature to Nx28x28 images
# x_image = tf.reshape(x, [-1, 28, 28, 1])
x_image = tf.reshape(x, [10, 224, 224, 3])

# 2.3 Defines the network.
with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)):
    y, _ = mobilenet_v1.mobilenet_v1(
        x_image,
        # num_classes=10,
        num_classes=2,
        is_training=True,
    )
# 2.4 Input ground truth labels in on-hot.
# y_ = tf.placeholder(tf.int32, [None, 10], name='y-input')
y_ = tf.placeholder(tf.int32, [None, 2], name='y-input')

# 2.5 Defines Loss function.
loss = tf.losses.softmax_cross_entropy(logits=y, onehot_labels=y_)

# 2.6 Defines accuracy.
accuracy = tf.metrics.accuracy(labels=tf.argmax(y_, axis=1),
def run():
    #Create log_dir for evaluation information
    if not os.path.exists(log_eval):
        os.mkdir(log_eval)

    #Just construct the graph from scratch again
    with tf.Graph().as_default() as graph:
        tf.logging.set_verbosity(tf.logging.INFO)
        #Get the dataset first and load one batch of validation images and labels tensors. Set is_training as False so as to use the evaluation preprocessing
        dataset = get_split('validation', dataset_dir)
        images, raw_images, labels = load_batch(dataset,
                                                batch_size=batch_size,
                                                is_training=False)

        #Create some information about the training steps
        num_batches_per_epoch = dataset.num_samples / batch_size
        num_steps_per_epoch = num_batches_per_epoch

        #Now create the inference model but set is_training=False
        with slim.arg_scope(mobilenet_v1_arg_scope()):
            logits, end_points = mobilenet_v1(images,
                                              num_classes=dataset.num_classes,
                                              is_training=False)

        # #get all the variables to restore from the checkpoint file and create the saver function to restore
        variables_to_restore = slim.get_variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        def restore_fn(sess):
            return saver.restore(sess, checkpoint_file)

        #Just define the metrics to track without the loss or whatsoever
        probabilities = end_points['Predictions']
        predictions = tf.argmax(probabilities, 1)

        accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy(
            predictions, labels)
        metrics_op = tf.group(accuracy_update)

        #Create the global step and an increment op for monitoring
        global_step = get_or_create_global_step()
        global_step_op = tf.assign(
            global_step, global_step + 1
        )  #no apply_gradient method so manually increasing the global_step

        #Create a evaluation step function
        def eval_step(sess, metrics_op, global_step):
            '''
            Simply takes in a session, runs the metrics op and some logging information.
            '''
            start_time = time.time()
            _, global_step_count, accuracy_value = sess.run(
                [metrics_op, global_step_op, accuracy])
            time_elapsed = time.time() - start_time

            #Log some information
            logging.info(
                'Global Step %s: Streaming Accuracy: %.4f (%.2f sec/step)',
                global_step_count, accuracy_value, time_elapsed)

            return accuracy_value

        #Define some scalar quantities to monitor
        tf.summary.scalar('Validation_Accuracy', accuracy)
        my_summary_op = tf.summary.merge_all()

        #Get your supervisor
        sv = tf.train.Supervisor(logdir=log_eval,
                                 summary_op=None,
                                 init_fn=restore_fn)

        #Now we are ready to run in one session
        with sv.managed_session() as sess:
            for step in xrange(int(num_batches_per_epoch * num_epochs)):
                #print vital information every start of the epoch as always
                if step % num_batches_per_epoch == 0:
                    logging.info('Epoch: %s/%s',
                                 step / num_batches_per_epoch + 1, num_epochs)
                    logging.info('Current Streaming Accuracy: %.4f',
                                 sess.run(accuracy))

                #Compute summaries every 10 steps and continue evaluating
                if step % 10 == 0:
                    eval_step(sess,
                              metrics_op=metrics_op,
                              global_step=sv.global_step)
                    summaries = sess.run(my_summary_op)
                    sv.summary_computed(sess, summaries)

                #Otherwise just run as per normal
                else:
                    eval_step(sess,
                              metrics_op=metrics_op,
                              global_step=sv.global_step)

            #At the end of all the evaluation, show the final accuracy
            logging.info('Final Streaming Accuracy: %.4f', sess.run(accuracy))

            #Now we want to visualize the last batch's images just to see what our model has predicted
            raw_images, labels, predictions, probabilities = sess.run(
                [raw_images, labels, predictions, probabilities])
            for i in range(10):
                image, label, prediction, probability = raw_images[i], labels[
                    i], predictions[i], probabilities[i]
                prediction_name, label_name = dataset.labels_to_name[
                    prediction], dataset.labels_to_name[label]
                text = 'Prediction: %s \n Ground Truth: %s \n Probability: %s' % (
                    prediction_name, label_name, probability[prediction])
                img_plot = plt.imshow(image)

                #Set up the plot and hide axes
                plt.title(text)
                img_plot.axes.get_yaxis().set_ticks([])
                img_plot.axes.get_xaxis().set_ticks([])
                plt.show()

            logging.info(
                'Model evaluation has completed! Visit TensorBoard for more information regarding your evaluation.'
            )
            sv.saver.save(sess, sv.save_path, global_step=sv.global_step)
示例#23
0
from tensorflow.contrib import slim

SOURCE_PATH = ''
TARGET_PATH = os.path.join('.', 'npy')
MULTIPLIER = os.getenv('MOBILENET_MULTIPLIER')
RESOLUTION = os.getenv('MOBILENET_RESOLUTION')

if os.path.isdir(TARGET_PATH):
    shutil.rmtree(TARGET_PATH)
os.mkdir(TARGET_PATH)

with tf.Session() as sess:
    input_shape = (None, int(RESOLUTION), int(RESOLUTION), 3)
    input_node = tf.placeholder(tf.float32, shape=input_shape, name="input")
    with slim.arg_scope(
            mobilenet_v1.mobilenet_v1_arg_scope(is_training=False)):
        mobilenet_v1.mobilenet_v1(input_node,
                                  num_classes=1001,
                                  is_training=False,
                                  depth_multiplier=float(MULTIPLIER))

    saver = tf.train.Saver()
    ckpt_file_prefix = 'mobilenet_v1_{}_{}.ckpt'.format(MULTIPLIER, RESOLUTION)
    saver.restore(sess, os.path.join(SOURCE_PATH, ckpt_file_prefix))

    for t in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
        varname = t.name
        if os.path.sep in t.name:
            varname = varname.replace(os.path.sep, '_')
        if varname.startswith('MobilenetV1_'):
            varname = varname[12:]
示例#24
0
def osmn_lite(inputs,
              model_params,
              visual_modulator_params=None,
              scope='osmn',
              is_training=False):
    """Defines the OSMN
    Args:
    inputs: Tensorflow placeholder that contains the input image, visual guide, and spatial guide
    model_params: paramters related to the model structure
    visual_modulator_params: if None it will generate new visual modulation parameters using guide image, otherwise
            it will reuse the current paramters.
    scope: Scope name for the network
    is_training: training or testing 
    Returns:
    net: Output Tensor of the network
    end_points: Dictionary with all Tensors of the network
    """
    guide_im_size = tf.shape(inputs[0])
    im_size = model_params.im_size
    batch_size = inputs[1].get_shape().as_list()[0]
    use_visual_modulator = model_params.use_visual_modulator
    use_spatial_modulator = model_params.use_spatial_modulator
    train_seg = model_params.train_seg
    n_modulator_param = 1024 + 512 + 256 + 128
    mod_layer_ids = [3, 5, 11, 13]
    output_stride = 32
    batch_norm_params = {
        'decay': 0.99,
        'scale': True,
        'epsilon': 0.001,
        'updates_collections': None,
        'is_training': not model_params.fix_bn and is_training
    }
    if use_visual_modulator and visual_modulator_params == None:
        visual_modulator_params = visual_modulator_lite(
            inputs[0], model_params, scope=scope, is_training=is_training)
    with tf.variable_scope(scope, [inputs]) as sc, slim.arg_scope(
            mobilenet_v1.mobilenet_v1_arg_scope(
                is_training=is_training)) as arg_sc:
        end_points_collection = sc.name + '_end_points'

        # index to mark the current position of the modulation params
        visual_mod_id = 0
        with tf.variable_scope('modulator_sp'):
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    normalizer_fn=slim.batch_norm,
                    normalizer_params=batch_norm_params,
                    padding='SAME',
                    outputs_collections=end_points_collection) as bn_arg_sc:
                if not use_spatial_modulator:
                    sp_mod_params = None
                else:
                    ds_mask = slim.avg_pool2d(inputs[1], [4, 4],
                                              stride=4,
                                              scope='pool4')
                    conv3_att = slim.conv2d(ds_mask,
                                            128, [1, 1],
                                            scope='conv3')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool8')
                    conv5_att = slim.conv2d(ds_mask,
                                            256, [1, 1],
                                            scope='conv5')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool16')
                    conv11_att = slim.conv2d(ds_mask,
                                             512, [1, 1],
                                             scope='conv11')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool32')
                    conv13_att = slim.conv2d(ds_mask,
                                             1024, [1, 1],
                                             scope='conv13')
                    sp_mod_params = [
                        conv3_att, conv5_att, conv11_att, conv13_att
                    ]

        # Collect outputs of all intermediate layers.
        net, end_points = mobilenet_v1.mobilenet_v1_base(
            inputs[2],
            output_stride=output_stride,
            vis_mod_params=visual_modulator_params,
            sp_mod_params=sp_mod_params,
            mod_layer_ids=mod_layer_ids,
            scope='seg')

        with slim.arg_scope([slim.conv2d],
                            activation_fn=None,
                            normalizer_fn=None):
            net_2 = end_points['Conv2d_3_pointwise']
            net_3 = end_points['Conv2d_5_pointwise']
            net_4 = end_points['Conv2d_11_pointwise']
            net_5 = end_points['Conv2d_13_pointwise']
            side_2 = slim.conv2d(net_2, 16, [3, 3], scope='conv3_16')
            side_3 = slim.conv2d(net_3, 16, [3, 3], scope='conv5_16')
            side_4 = slim.conv2d(net_4, 16, [3, 3], scope='conv11_16')
            side_5 = slim.conv2d(net_5, 16, [3, 3], scope='conv13_16')
            up_size = [im_size[1] / 2, im_size[0] / 2]
            side_2_f = tf.image.resize_bilinear(side_2, up_size)
            side_3_f = tf.image.resize_bilinear(side_3, up_size)
            side_4_f = tf.image.resize_bilinear(side_4, up_size)
            side_5_f = tf.image.resize_bilinear(side_5, up_size)

            net = tf.concat([side_2_f, side_3_f, side_4_f, side_5_f], axis=3)
            net = slim.conv2d(net, 1, [1, 1], scope='score')
            net = tf.image.resize_bilinear(net, [im_size[1], im_size[0]])
        #net = slim.conv2d_transpose(net, 1, output_stride * 2, output_stride, normalizer_fn=None, padding="SAME", scope='score-up')
        end_points = slim.utils.convert_collection_to_dict(
            end_points_collection)
        return net, end_points
示例#25
0
def run():
    #Create the log directory here. Must be done here otherwise import will activate this unneededly.
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    #======================= TRAINING PROCESS =========================
    #Now we start to construct the graph and build our model
    with tf.Graph().as_default() as graph:
        tf.logging.set_verbosity(
            tf.logging.INFO)  #Set the verbosity to INFO level

        #First create the dataset and load one batch
        dataset = get_split('train', dataset_dir, file_pattern=file_pattern)
        images, _, labels = load_batch(dataset, batch_size=batch_size)

        #Know the number steps to take before decaying the learning rate and batches per epoch
        num_batches_per_epoch = dataset.num_samples // batch_size
        num_steps_per_epoch = num_batches_per_epoch  #Because one step is one batch processed
        decay_steps = int(num_epochs_before_decay * num_steps_per_epoch)

        #Create the model inference
        with slim.arg_scope(mobilenet_v1_arg_scope()):
            logits, end_points = mobilenet_v1(images,
                                              num_classes=dataset.num_classes,
                                              is_training=True)

        #Perform one-hot-encoding of the labels (Try one-hot-encoding within the load_batch function!)
        one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)

        #Performs the equivalent to tf.nn.sparse_softmax_cross_entropy_with_logits but enhanced with checks
        loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                               logits=logits)
        total_loss = tf.losses.get_total_loss(
        )  #obtain the regularization losses as well

        #Create the global step for monitoring the learning_rate and training.
        global_step = get_or_create_global_step()

        #Define your exponentially decaying learning rate
        lr = tf.train.exponential_decay(learning_rate=initial_learning_rate,
                                        global_step=global_step,
                                        decay_steps=decay_steps,
                                        decay_rate=learning_rate_decay_factor,
                                        staircase=True)

        #Now we can define the optimizer that takes on the learning rate
        optimizer = tf.train.AdamOptimizer(learning_rate=lr)
        # optimizer = tf.train.RMSPropOptimizer(learning_rate = lr, momentum=0.9)

        #Create the train_op.
        train_op = slim.learning.create_train_op(total_loss, optimizer)

        #State the metrics that you want to predict. We get a predictions that is not one_hot_encoded.
        predictions = tf.argmax(end_points['Predictions'], 1)
        probabilities = end_points['Predictions']
        accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy(
            predictions, labels)
        metrics_op = tf.group(accuracy_update, probabilities)

        #Now finally create all the summaries you need to monitor and group them into one summary op.
        tf.summary.scalar('losses/Total_Loss', total_loss)
        tf.summary.scalar('accuracy', accuracy)
        tf.summary.scalar('learning_rate', lr)
        my_summary_op = tf.summary.merge_all()

        #Now we need to create a training step function that runs both the train_op, metrics_op and updates the global_step concurrently.
        def train_step(sess, train_op, global_step):
            '''
            Simply runs a session for the three arguments provided and gives a logging on the time elapsed for each global step
            '''
            #Check the time for each sess run
            start_time = time.time()
            total_loss, global_step_count, _ = sess.run(
                [train_op, global_step, metrics_op])
            time_elapsed = time.time() - start_time

            #Run the logging to print some results
            logging.info('global step %s: loss: %.4f (%.2f sec/step)',
                         global_step_count, total_loss, time_elapsed)

            return total_loss, global_step_count

        #Define your supervisor for running a managed session. Do not run the summary_op automatically or else it will consume too much memory
        sv = tf.train.Supervisor(logdir=log_dir, summary_op=None)

        #Run the managed session
        with sv.managed_session() as sess:
            for step in range(num_steps_per_epoch * num_epochs):
                #At the start of every epoch, show the vital information:
                if step % num_batches_per_epoch == 0:
                    logging.info('Epoch %s/%s',
                                 step / num_batches_per_epoch + 1, num_epochs)
                    learning_rate_value, accuracy_value = sess.run(
                        [lr, accuracy])
                    logging.info('Current Learning Rate: %s',
                                 learning_rate_value)
                    logging.info('Current Streaming Accuracy: %s',
                                 accuracy_value)

                    # optionally, print your logits and predictions for a sanity check that things are going fine.
                    logits_value, probabilities_value, predictions_value, labels_value = sess.run(
                        [logits, probabilities, predictions, labels])
                    print('logits: \n', logits_value[:100])
                    print('Probabilities: \n', probabilities_value[:100])
                    print('predictions: \n', predictions_value[:100])
                    print('Labels:\n:', labels_value[:100])

                #Log the summaries every 10 step.
                if step % 10 == 0:
                    loss, _ = train_step(sess, train_op, sv.global_step)
                    summaries = sess.run(my_summary_op)
                    sv.summary_computed(sess, summaries)

                #If not, simply run the training step
                else:
                    loss, _ = train_step(sess, train_op, sv.global_step)

            #We log the final training loss and accuracy
            logging.info('Final Loss: %s', loss)
            logging.info('Final Accuracy: %s', sess.run(accuracy))

            #Once all the training has been done, save the log files and checkpoint model
            logging.info('Finished training! Saving model to disk now.')
def ssd_net(inputs,
            num_classes=SSDNet.default_params.num_classes,
            feat_layers=SSDNet.default_params.feat_layers,
            anchor_sizes=SSDNet.default_params.anchor_sizes,
            anchor_ratios=SSDNet.default_params.anchor_ratios,
            normalizations=SSDNet.default_params.normalizations,
            is_training=True,
            dropout_keep_prob=0.5,
            prediction_fn=slim.softmax,
            reuse=None,
            scope='ssd_300_mobilenetv1'):
    """SSD net definition.
    """
    # if data_format == 'NCHW':
    #     inputs = tf.transpose(inputs, perm=(0, 3, 1, 2))

    # End_points collect relevant activations for external use.
    end_points = {}

    min_depth = 32 
    depth_multiplier = 1.0
    with tf.variable_scope(scope, 'ssd_300_mobilenetv1', [inputs], reuse=reuse):
        input_shape = inputs.get_shape().as_list()
        if len(input_shape) != 4:
          raise ValueError('Invalid input tensor rank, expected 4, was: %d' %
                     len(input_shape))

        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=is_training)):	
          with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training):
            net, end_points = mobilenet_v1.mobilenet_v1_base(inputs, scope='MobilenetV1',
                                          min_depth=min_depth,
                                          depth_multiplier=depth_multiplier,
                                          conv_defs=None)
        '''
        # Additional SSD blocks.
        # Block 6: let's dilate the hell out of it!
        end_point = 'block13'
        with tf.variable_scope(end_point)
          net = slim.conv2d(net, 1024, [3, 3], rate=6, scope='atrous_conv')
          end_points['block13_atrous'] = net
          net = tf.layers.dropout(net, rate=dropout_keep_prob, training=is_training)
          # Block 7: 1x1 conv. Because the f**k.
          net = slim.conv2d(net, 1024, [1, 1], scope='conv1x1')
          end_points['block13'] = net
          net = tf.layers.dropout(net, rate=dropout_keep_prob, training=is_training)
        '''

        # Block 14/15/16/17: 1x1 and 3x3 convolutions stride 2 (except lasts).
        end_point = 'block14'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 256, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = custom_layers.pad2d(net, pad=(1, 1))
            net = slim.conv2d(net, 512, [3, 3], biases_initializer=None, trainable=is_training, stride=2, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        end_point = 'block15'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 128, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = custom_layers.pad2d(net, pad=(1, 1))
            net = slim.conv2d(net, 256, [3, 3], biases_initializer=None, trainable=is_training, stride=2, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        end_point = 'block16'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 128, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = slim.conv2d(net, 256, [3, 3], biases_initializer=None, trainable=is_training, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        '''
        end_point = 'block17'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 64, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = slim.conv2d(net, 128, [3, 3], biases_initializer=None, trainable=is_training, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        '''

        # Prediction and localisations layers.
        predictions = []
        logits = []
        localisations = []
        

        for i, layer in enumerate(feat_layers):
            with tf.variable_scope(layer + '_box'):
                p, l = ssd_multibox_layer(end_points[layer],
                                          num_classes,
                                          anchor_sizes[i],
                                          anchor_ratios[i],
                                          normalizations[i],
										  is_training=is_training)
            predictions.append(prediction_fn(p))
            logits.append(p)
            localisations.append(l)
#        end_points['logits'] = logits
#        end_points['predictions'] = predictions
#        end_points['localisations'] = localisations

        return predictions, localisations, logits, end_points
示例#27
0
 def network_fn(images):
   arg_scope = mobilenet_v1.mobilenet_v1_arg_scope(is_training=False)
   with slim.arg_scope(arg_scope):
     return networks_map[name](images, num_classes, is_training=False)
def main():
    # 加载预处理好的数据。
    train_data, train_labels, valid_data, valid_labels, label_keys = train_test(
        IMAGE_PATH)
    print("%d training examples, %d validation examples." %
          (len(train_data), len(valid_data)))

    # 定义inception-v1的输入,images为输入图片,labels为每一张图片对应的标签。
    images = tf.placeholder(tf.float32, [None, IMAGE_SIZE, IMAGE_SIZE, 3],
                            name='input_images')
    labels = tf.placeholder(tf.int64, [None, N_CLASSES], name='labels')

    with slim.arg_scope(mobilenet.mobilenet_v1_arg_scope()):
        logits, _ = mobilenet.mobilenet_v1(images,
                                           num_classes=N_CLASSES,
                                           is_training=True)
    predictions = tf.nn.softmax(logits, name='output/prob')

    # 定义损失函数和训练过程。
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
    loss_summary = tf.summary.scalar('loss', cost)

    train_step = tf.train.AdamOptimizer(
        learning_rate=LEARNING_RATE).minimize(cost)

    # 计算正确率。
    with tf.name_scope('evaluation'):
        correct_prediction = tf.equal(tf.argmax(logits, 1),
                                      tf.argmax(labels, 1))
        evaluation_step = tf.reduce_mean(
            tf.cast(correct_prediction, tf.float32))

    saver = tf.train.Saver()
    tf.add_to_collection("predict", predictions)

    with tf.Session() as sess:
        merged_summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter('logs', sess.graph)

        # 初始化没有加载进来的变量。
        init = tf.global_variables_initializer()
        sess.run(init)

        for i in range(STEPS):
            print('step:', i)
            for batch_xs, batch_ys in minibatches(train_data,
                                                  train_labels,
                                                  BATCH_SIZE,
                                                  shuffle=True):
                # 训练数据
                _, summary_str = sess.run([train_step, merged_summary_op],
                                          feed_dict={
                                              images: batch_xs,
                                              labels: batch_ys
                                          })
                summary_writer.add_summary(summary_str, i)

            # 在验证集上测试正确率
            if (i + 1) % 5 == 0 or i + 1 == STEPS:
                valid_accuracy = sess.run(evaluation_step,
                                          feed_dict={
                                              images: valid_data,
                                              labels: valid_labels
                                          })
                print('Step %d: Validation accuracy = %.1f%%' %
                      (i, valid_accuracy * 100.0))

            # 保存ckpt
            #if (i+1) % 20 == 0 or i + 1 == STEPS:
            #    path = saver.save(sess, TRAIN_FILE, global_step=i)
            #    print('Saved model to {}\n'.format(path))

        # 保存标签
        output_labels = os.path.join('./', 'labels.txt')
        with tf.gfile.FastGFile(output_labels, 'w') as f:
            k = {}
            for i in range(N_CLASSES):
                k[str(i)] = label_keys[i]
            json.dump(k, f)

        saver.save(sess, 'model/mobilenet.ckpt')
        tf.train.write_graph(sess.graph_def,
                             'model/',
                             'trash_mobilenet.pb',
                             as_text=False)