Ejemplo n.º 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):
      _ = inception.inception_v2(inputs, num_classes, depth_multiplier=-0.1)
    with self.assertRaises(ValueError):
      _ = inception.inception_v2(inputs, num_classes, depth_multiplier=0.0)
Ejemplo n.º 2
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))
    inception.inception_v2(train_inputs, num_classes)
    eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
    logits, _ = inception.inception_v2(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,))
Ejemplo n.º 3
0
  def testBuildEndPointsWithDepthMultiplierGreaterThanOne(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

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

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

    _, end_points_with_multiplier = inception.inception_v2(
        inputs, num_classes, scope='depth_multiplied_net',
        depth_multiplier=2.0)

    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(2.0 * original_depth, new_depth)
Ejemplo n.º 4
0
  def testLogitsNotSqueezed(self):
    num_classes = 25
    images = tf.random_uniform([1, 224, 224, 3])
    logits, _ = inception.inception_v2(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])
Ejemplo n.º 5
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 = inception.inception_v2(inputs, num_classes)
    self.assertTrue(net.op.name.startswith('InceptionV2/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)
Ejemplo n.º 6
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 = inception.inception_v2(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV2/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    pre_pool = end_points['Mixed_5c']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 4, 4, 1024])
Ejemplo n.º 7
0
Archivo: cnnt.py Proyecto: rhong3/BMIF
    def _buildGraph(self):
        x_in = tf.placeholder(
            tf.float32,
            shape=[
                None,  # enables variable batch size
                self.input_dim[0]
            ],
            name="x")
        x_in_reshape = tf.reshape(
            x_in, [-1, self.input_dim[1], self.input_dim[2], 3])

        dropout = tf.placeholder_with_default(1., shape=[], name="dropout")

        y_in = tf.placeholder(dtype=tf.int8, name="y")

        onehot_labels = tf.one_hot(indices=tf.cast(y_in, tf.int32), depth=2)

        is_train = tf.placeholder_with_default(True, shape=[], name="is_train")

        logits, nett, ww = inception_v2.inception_v2(
            x_in_reshape,
            num_classes=2,
            is_training=is_train,
            dropout_keep_prob=dropout,
            min_depth=16,
            depth_multiplier=1.0,
            prediction_fn=slim.softmax,
            spatial_squeeze=True,
            reuse=None,
            scope='InceptionV3',
            global_pool=False)

        pred = tf.nn.softmax(logits, name="prediction")

        global_step = tf.Variable(0, trainable=False)

        pred_cost = tf.losses.softmax_cross_entropy(
            onehot_labels=onehot_labels, logits=logits)

        tf.summary.scalar("InceptionV2_cost", pred_cost)

        train_op = tf.contrib.layers.optimize_loss(
            loss=pred_cost,
            learning_rate=self.learning_rate,
            global_step=global_step,
            optimizer="Adam")

        merged_summary = tf.summary.merge_all()

        return (x_in, dropout, is_train, y_in, logits, nett, ww, pred,
                pred_cost, global_step, train_op, merged_summary)
Ejemplo n.º 8
0
    def __call__(self, imgs, seqVec, seqNums, batchSize):
        arg_scope = inception_v2_arg_scope()
        with slim.arg_scope(arg_scope):
            _, feat = inception_v2(preprocess(imgs),
                                   num_classes=1001,
                                   is_training=False,
                                   reuse=tf.AUTO_REUSE)

        with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
            img_inputs = MLP("img_embedding", feat, 1024, self.embedSize)
            img_inputs = tf.expand_dims(img_inputs, axis=1)
            if self.rnnType == "lstm":
                cell = tf.nn.rnn_cell.BasicLSTMCell(self.hiddenSize)
            if self.rnnType == "rnn":
                cell = tf.nn.rnn_cell.BasicRNNCell(self.hiddenSize)
            if self.rnnType == "gru":
                cell = tf.nn.rnn_cell.GRUCell(self.hiddenSize)
            cells = tf.nn.rnn_cell.MultiRNNCell([cell] * self.layerSize)
            if batchSize > 1:
                cells = tf.nn.rnn_cell.DropoutWrapper(cells,
                                                      input_keep_prob=0.7,
                                                      output_keep_prob=0.7,
                                                      state_keep_prob=0.7)
            init_state = cells.zero_state(batchSize, tf.float32)
            _, img_states = tf.nn.dynamic_rnn(cells,
                                              img_inputs,
                                              initial_state=init_state)
            embeddingMat = tf.get_variable(
                "embeddingMat", [self.targetVocSize, self.embedSize],
                initializer=tf.truncated_normal_initializer(stddev=0.08))
            seqVec = tf.nn.embedding_lookup(embeddingMat, seqVec)
            if batchSize == 1:
                #Test phase
                outputs, states = tf.nn.dynamic_rnn(cells,
                                                    seqVec,
                                                    initial_state=init_state)
                outputs = tf.reshape(outputs, [-1, self.hiddenSize])
                logits = MLP("logits", outputs, self.hiddenSize,
                             self.targetVocSize)
                probs = tf.nn.softmax(logits)
                wordVal = tf.argmax(probs, axis=1)
                return probs, wordVal, states, img_states, init_state
            else:
                #Training phase
                outputs, _ = tf.nn.dynamic_rnn(cells, seqVec, seqNums,
                                               img_states)
                outputs = tf.reshape(outputs, [-1, self.hiddenSize])
                logits = MLP("logits", outputs, self.hiddenSize,
                             self.targetVocSize)
                probs = tf.nn.softmax(logits)
                return probs
Ejemplo n.º 9
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, _ = inception.inception_v2(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,))
Ejemplo n.º 10
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 = inception.inception_v2(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith(
        'InceptionV2/Logits/SpatialSqueeze'))
    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])
Ejemplo n.º 11
0
  def testUnknowBatchSize(self):
    batch_size = 1
    height, width = 224, 224
    num_classes = 1000

    inputs = tf.placeholder(tf.float32, (None, height, width, 3))
    logits, _ = inception.inception_v2(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV2/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))
Ejemplo n.º 12
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 = inception.inception_v2(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_5c']
     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])
Ejemplo n.º 13
0
                                           tf.FixedLenFeature([], tf.string)
                                       })
    img = tf.decode_raw(features["image"], tf.uint8)
    img = tf.reshape(img, [image_pixels, image_pixels, 3])
    img = tf.cast(img, tf.float32)
    label = tf.cast(features["label"], tf.int32)
    return img, label


images = tf.placeholder(tf.float32, [None, image_pixels, image_pixels, 3],
                        name="input/x_input")
labels = tf.placeholder(tf.int64, [None], name="input/y_input")

with slim.arg_scope(inception_v2_arg_scope()):
    logits, end_points = inception_v2(images,
                                      num_classes=classes,
                                      is_training=True)

exclude = ['InceptionV2/Logits']
variables_to_restore = slim.get_variables_to_restore(exclude=exclude)

one_hot_labels = slim.one_hot_encoding(labels, classes)
loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                       logits=logits)
total_loss = tf.losses.get_total_loss()
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
    train_step = tf.train.AdamOptimizer(0.00002).minimize(loss=total_loss)
correct_prediction = tf.equal(labels,
                              tf.argmax(end_points['Predictions'], 1),
                              name="correct_prediction")
Ejemplo n.º 14
0
    def _buildGraph(self, model):
        # image input
        x_in = tf.placeholder(tf.float32, name="x")
        x_in_reshape = tf.reshape(
            x_in, [-1, self.input_dim[1], self.input_dim[2], 3])
        # dropout
        dropout = tf.placeholder_with_default(1., shape=[], name="dropout")
        # label input
        y_in = tf.placeholder(dtype=tf.float32, name="y")
        # train or test
        is_train = tf.placeholder_with_default(True, shape=[], name="is_train")

        if model == 'IG':
            import GoogleNet
            logits, nett, ww = GoogleNet.googlenet(x_in_reshape,
                                                   num_classes=4,
                                                   is_training=is_train,
                                                   dropout_keep_prob=dropout,
                                                   scope='GoogleNet')
            print('Using Inception-V1')
        elif model == 'I2':
            import inception_v2
            logits, nett, ww = inception_v2.inception_v2(
                x_in_reshape,
                num_classes=4,
                is_training=is_train,
                dropout_keep_prob=dropout,
                min_depth=16,
                depth_multiplier=1.0,
                prediction_fn=slim.softmax,
                spatial_squeeze=True,
                reuse=None,
                scope='InceptionV2',
                global_pool=False)
            print('Using Inception-V2')
        elif model == 'I3':
            import inception_v3
            logits, nett, ww = inception_v3.inception_v3(
                x_in_reshape,
                num_classes=4,
                is_training=is_train,
                dropout_keep_prob=dropout,
                min_depth=16,
                depth_multiplier=1.0,
                prediction_fn=slim.softmax,
                spatial_squeeze=True,
                reuse=None,
                create_aux_logits=True,
                scope='InceptionV3',
                global_pool=False)
            print('Using Inception-V3')
        elif model == 'I4':
            import inception_v4
            logits, nett, ww = inception_v4.inception_v4(
                x_in_reshape,
                num_classes=4,
                is_training=is_train,
                dropout_keep_prob=dropout,
                reuse=None,
                create_aux_logits=True,
                scope='InceptionV4')
            print('Using Inception-V4')
        elif model == 'IR1':
            import inception_resnet_v1
            logits, nett, ww = inception_resnet_v1.inception_resnet_v1(
                x_in_reshape,
                num_classes=4,
                is_training=is_train,
                dropout_keep_prob=dropout,
                reuse=None,
                scope='InceptionRes1')
            print('Using Inception-Resnet-V1')
        elif model == 'IR2':
            import inception_resnet_v2
            logits, nett, ww = inception_resnet_v2.inception_resnet_v2(
                x_in_reshape,
                num_classes=4,
                is_training=is_train,
                dropout_keep_prob=dropout,
                reuse=None,
                create_aux_logits=True,
                scope='InceptionRes2')
            print('Using Inception-Resnet-V2')
        else:
            import GoogleNet
            logits, nett, ww = GoogleNet.googlenet(x_in_reshape,
                                                   num_classes=4,
                                                   is_training=is_train,
                                                   dropout_keep_prob=dropout,
                                                   scope='GoogleNet')
            print('Using Default: Inception-V1')

        pred = tf.nn.softmax(logits, name="prediction")

        global_step = tf.Variable(0, trainable=False)

        pred_cost = tf.losses.softmax_cross_entropy(onehot_labels=y_in,
                                                    logits=logits)

        tf.summary.scalar("{}_cost".format(model), pred_cost)

        tf.summary.tensor_summary("{}_pred".format(model), pred)

        train_op = tf.contrib.layers.optimize_loss(
            loss=pred_cost,
            learning_rate=self.learning_rate,
            global_step=global_step,
            optimizer="Adam")

        merged_summary = tf.summary.merge_all()

        return (x_in, dropout, is_train, y_in, logits, nett, ww, pred,
                pred_cost, global_step, train_op, merged_summary)
def train(train_record_file,
          train_log_step,
          train_param,
          val_record_file,
          val_log_step,
          labels_nums,
          data_shape,
          snapshot,
          snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr,max_steps]=train_param
    [batch_size,resize_height,resize_width,depths]=data_shape

    # 获得训练和测试的样本数
    train_nums=get_example_nums(train_record_file)
    val_nums=get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d'%(train_nums,val_nums))

    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    train_images, train_labels = read_records(train_record_file, resize_height, resize_width, type='normalization')
    train_images_batch, train_labels_batch = get_batch_images(train_images, train_labels,
                                                              batch_size=batch_size, labels_nums=labels_nums,
                                                              one_hot=True, shuffle=True)
    # val数据,验证数据可以不需要打乱数据
    val_images, val_labels = read_records(val_record_file, resize_height, resize_width, type='normalization')
    val_images_batch, val_labels_batch = get_batch_images(val_images, val_labels,
                                                          batch_size=batch_size, labels_nums=labels_nums,
                                                          one_hot=True, shuffle=False)

    # Define the model:
    with slim.arg_scope(inception_v2.inception_v2_arg_scope()):
        out, end_points = inception_v2.inception_v2(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=keep_prob, is_training=is_training)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels, logits=out)#添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(add_regularization_losses=True)#添加正则化损失loss=2.2

    # Specify the optimization scheme:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)


    # global_step = tf.Variable(0, trainable=False)
    # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9)
    #
    # optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
    # # train_tensor = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)


    # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数,
    # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新
    # 通过`tf.get_collection`获得所有需要更新的`op`
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练
    with tf.control_dependencies(update_ops):
        # create_train_op that ensures that when we evaluate it to get the loss,
        # the update_ops are done and the gradient updates are computed.
        # train_op = slim.learning.create_train_op(total_loss=loss,optimizer=optimizer)
        train_op = slim.learning.create_train_op(total_loss=loss, optimizer=optimizer)

    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)), tf.float32))
    # 循环迭代过程
    step_train(train_op, loss, accuracy,
               train_images_batch, train_labels_batch, train_nums, train_log_step,
               val_images_batch, val_labels_batch, val_nums, val_log_step,
               snapshot_prefix, snapshot)