Ejemplo n.º 1
0
  def testRaiseValueErrorWithInvalidDepthMultiplier(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = random_ops.random_uniform((batch_size, height, width, 3))
    with self.assertRaises(ValueError):
      _ = inception_v3.inception_v3(inputs, num_classes, depth_multiplier=-0.1)
    with self.assertRaises(ValueError):
      _ = inception_v3.inception_v3(inputs, num_classes, depth_multiplier=0.0)
Ejemplo n.º 2
0
def predict(models_path, image_dir, labels_filename, labels_nums, data_format):
    """
    加载预测的图片与标签
    加载模型
    定义softmax
    定义预测最大值与下标
    回复模型参数W
    Session

    :param models_path:
    :param image_dir:
    :param labels_filename:
    :param labels_nums:
    :param data_format:
    :return:
    """
    [batch_size, resize_height, resize_width, depths] = data_format

    labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(
        dtype=tf.float32,
        shape=[None, resize_height, resize_width, depths],
        name='input')

    # 其他模型预测请修改这里
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        out, end_points = inception_v3.inception_v3(inputs=input_images,
                                                    num_classes=labels_nums,
                                                    dropout_keep_prob=1.0,
                                                    is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out, name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)  #恢复模型
    #glob模块是最简单的模块之一,内容非常少。用它可以查找符合特定规则的文件路径名
    ##获取指定目录下的所有图片
    # print glob.glob(r"E:/Picture/*/*.jpg")
    #获取上级目录的所有.py文件
    # print glob.glob(r'../*.py') #相对路径
    #意思就是image_dir路径下的所有。jpg文件名
    images_list = glob.glob(os.path.join(image_dir, '*.jpg'))
    for image_path in images_list:
        im = read_image(image_path,
                        resize_height,
                        resize_width,
                        normalization=True)
        im = im[np.newaxis, :]
        # pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score, pre_label = sess.run([score, class_id],
                                        feed_dict={input_images: im})
        #pre_score所有评分, pre_label最大分数的位置
        max_score = pre_score[0, pre_label]  #最大评分的值
        print("{} is: pre labels:{},name:{} score: {}".format(
            image_path, pre_label, labels[pre_label], max_score))
    sess.close()
Ejemplo n.º 3
0
  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 150, 150
    num_classes = 1000

    train_inputs = random_ops.random_uniform(
        (train_batch_size, height, width, 3))
    inception_v3.inception_v3(train_inputs, num_classes)
    eval_inputs = random_ops.random_uniform((eval_batch_size, height, width, 3))
    logits, _ = inception_v3.inception_v3(
        eval_inputs, num_classes, is_training=False, reuse=True)
    predictions = math_ops.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(variables.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
Ejemplo n.º 4
0
  def testLogitsNotSqueezed(self):
    num_classes = 25
    images = random_ops.random_uniform([1, 299, 299, 3])
    logits, _ = inception_v3.inception_v3(
        images, num_classes=num_classes, spatial_squeeze=False)

    with self.test_session() as sess:
      variables.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 testBuildEndPointsWithDepthMultiplierGreaterThanOne(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = random_ops.random_uniform((batch_size, height, width, 3))
    _, end_points = inception_v3.inception_v3(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_v3.inception_v3(
        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.º 6
0
  def testBuildClassificationNetwork(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = random_ops.random_uniform((batch_size, height, width, 3))
    logits, end_points = inception_v3.inception_v3(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV3/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])
Ejemplo n.º 7
0
  def testHalfSizeImages(self):
    batch_size = 5
    height, width = 150, 150
    num_classes = 1000

    inputs = random_ops.random_uniform((batch_size, height, width, 3))
    logits, end_points = inception_v3.inception_v3(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV3/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    pre_pool = end_points['Mixed_7c']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 3, 3, 2048])
Ejemplo n.º 8
0
  def testEvaluation(self):
    batch_size = 2
    height, width = 299, 299
    num_classes = 1000

    eval_inputs = random_ops.random_uniform((batch_size, height, width, 3))
    logits, _ = inception_v3.inception_v3(
        eval_inputs, num_classes, is_training=False)
    predictions = math_ops.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(variables.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
Ejemplo n.º 9
0
def predict_single_image(models_path, image_path, labels_filename,
                         label_id_filename, data_format):
    [batch_size, resize_height, resize_width, depths] = data_format

    labels = np.loadtxt(labels_filename, str, delimiter='\t', encoding='utf-8')
    labels_id = np.loadtxt(label_id_filename,
                           str,
                           delimiter='\t',
                           encoding='utf-8')
    labels_nums = labels_id.size
    input_images = tf.placeholder(
        dtype=tf.float32,
        shape=[None, resize_height, resize_width, depths],
        name='input')

    #其他模型预测请修改这里
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        out, end_points = inception_v3.inception_v3(inputs=input_images,
                                                    num_classes=labels_nums,
                                                    dropout_keep_prob=1.0,
                                                    is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out, name='pre')

    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    im = read_image(image_path,
                    resize_height,
                    resize_width,
                    normalization=True)
    im = im[np.newaxis, :]
    #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
    pre_score, pre_label = sess.run([score, class_id],
                                    feed_dict={input_images: im})
    # print("pre_score:{}".format(pre_score))
    # print("pre_label:{}".format(pre_label))
    # pre_sort = np.argsort(-pre_score)
    # print("pre_sort:{}".format(pre_sort))
    # print(labels[pre_sort[0][0]])
    # max_score=pre_score[0,pre_label]
    # print("{} is: pre labels:{},name:{} score: {}".format(image_path,pre_label,labels[pre_label], max_score))
    result = get_top_result(pre_score, labels, labels_id)
    print("{} is result : {}".format(image_path, result))
    sess.close()
Ejemplo n.º 10
0
  def testUnknownBatchSize(self):
    batch_size = 1
    height, width = 299, 299
    num_classes = 1000

    inputs = array_ops.placeholder(dtypes.float32, (None, height, width, 3))
    logits, _ = inception_v3.inception_v3(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV3/Logits'))
    self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
    images = random_ops.random_uniform((batch_size, height, width, 3))

    with self.test_session() as sess:
      sess.run(variables.global_variables_initializer())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))
Ejemplo n.º 11
0
 def testUnknownImageShape(self):
   ops.reset_default_graph()
   batch_size = 2
   height, width = 299, 299
   num_classes = 1000
   input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
   with self.test_session() as sess:
     inputs = array_ops.placeholder(
         dtypes.float32, shape=(batch_size, None, None, 3))
     logits, end_points = inception_v3.inception_v3(inputs, num_classes)
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_7c']
     feed_dict = {inputs: input_np}
     variables.global_variables_initializer().run()
     pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
     self.assertListEqual(list(pre_pool_out.shape), [batch_size, 8, 8, 2048])
Ejemplo n.º 12
0
def predict(models_path, image_dir, labels_nums, data_format, modelselect=0):
    [batch_size, resize_height, resize_width, depths] = data_format

    #labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(
        dtype=tf.float32,
        shape=[None, resize_height, resize_width, depths],
        name='input')
    if modelselect == 0:
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            out, end_points = inception_v3.inception_v3(
                inputs=input_images,
                num_classes=labels_nums,
                dropout_keep_prob=1.0,
                is_training=False)
    elif modelselect == 1:
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            out, end_points = resnet_v2.resnet_v2_101(inputs=input_images,
                                                      num_classes=labels_nums,
                                                      is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out, name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    images_list = glob.glob(os.path.join(image_dir, '*.bmp'))
    for image_path in images_list:
        im = read_image(image_path,
                        resize_height,
                        resize_width,
                        normalization=True)
        im = im[np.newaxis, :]
        #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score, pre_label = sess.run([score, class_id],
                                        feed_dict={input_images: im})
        max_score = pre_score[0, pre_label]
        print("{} is: pre labels:{},name:{} score: {}".format(
            image_path, pre_label, chineseMJ[pre_label[0]], max_score))

    sess.close()
Ejemplo n.º 13
0
  def testBuildEndPoints(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = random_ops.random_uniform((batch_size, height, width, 3))
    _, end_points = inception_v3.inception_v3(inputs, num_classes)
    self.assertTrue('Logits' in end_points)
    logits = end_points['Logits']
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue('AuxLogits' in end_points)
    aux_logits = end_points['AuxLogits']
    self.assertListEqual(aux_logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue('Mixed_7c' in end_points)
    pre_pool = end_points['Mixed_7c']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 8, 8, 2048])
    self.assertTrue('PreLogits' in end_points)
    pre_logits = end_points['PreLogits']
    self.assertListEqual(pre_logits.get_shape().as_list(),
                         [batch_size, 1, 1, 2048])
Ejemplo n.º 14
0
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_v3.inception_v3_arg_scope()):
        out, end_points = inception_v3.inception_v3(
            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
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))

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

    # 循环迭代过程
    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)
def train(
    train_record_file,
    train_log_step,
    train_param,
    val_record_file,
    val_log_step,
    labels_nums,
    data_shape,
    snapshot,
    snapshot_prefix,
    RESTORE_FROM_CHECKPOINT,
    restore_steps,
):
    '''
    :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

    # 获得训练和测试的样本数
    # 运行过一遍可以直接拿数据跑 不需要再从tfrecord读
    train_nums = get_example_nums(train_record_file)
    val_nums = get_example_nums(val_record_file)
    # train nums:77268,val nums:32973
    # train_nums = 77268
    # val_nums = 32973
    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_v3.inception_v3_arg_scope()):
        out, end_points = inception_v3.inception_v3(
            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)

    LEARNING_RATE_BASE = base_lr  # 设置初始学习率为0.1
    LEARNING_RATE_DECAY = 0.99  # 设置学习衰减率为0.99
    LEARNING_RATE_STEP = 100  # 设置喂入多少轮BATCH_SIZE之后更新一次学习率,一般设置为 总样本数/BATCH_SIZE

    global_steps = None
    if RESTORE_FROM_CHECKPOINT:
        global_steps = tf.Variable(restore_steps, trainable=False)
    else:
        global_steps = tf.Variable(0, trainable=False)
    learing_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,
                                              global_steps,
                                              LEARNING_RATE_STEP,
                                              LEARNING_RATE_DECAY,
                                              staircase=True)

    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)
    optimizer = tf.train.AdamOptimizer(learing_rate, 0.9)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_steps)

    # 在定义训练的时候, 注意到我们使用了`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,
                                                 global_step=global_steps)

    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, max_steps, 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, RESTORE_FROM_CHECKPOINT,
               restore_steps)