예제 #1
0
 def testEndPoints(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     for is_training in [True, False]:
         with ops.Graph().as_default():
             inputs = random_ops.random_uniform(
                 (batch_size, height, width, 3))
             _, end_points = vgg.vgg_16(inputs,
                                        num_classes,
                                        is_training=is_training)
             expected_names = [
                 'vgg_16/conv1/conv1_1', 'vgg_16/conv1/conv1_2',
                 'vgg_16/pool1', 'vgg_16/conv2/conv2_1',
                 'vgg_16/conv2/conv2_2', 'vgg_16/pool2',
                 'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2',
                 'vgg_16/conv3/conv3_3', 'vgg_16/pool3',
                 'vgg_16/conv4/conv4_1', 'vgg_16/conv4/conv4_2',
                 'vgg_16/conv4/conv4_3', 'vgg_16/pool4',
                 'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2',
                 'vgg_16/conv5/conv5_3', 'vgg_16/pool5', 'vgg_16/fc6',
                 'vgg_16/fc7', 'vgg_16/fc8'
             ]
             self.assertSetEqual(set(end_points.keys()),
                                 set(expected_names))
def predict(models_path, image_dir, labels_filename, labels_nums, data_format):
    [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(vgg.vgg_arg_scope()):
        out, end_points = vgg.vgg_16(inputs=input_images,
                                     num_classes=labels_nums,
                                     dropout_keep_prob=1.0,
                                     is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别

    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, '*.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 = sess.run([out], feed_dict={input_images: im})
        print "image_path:{},pre_score:{}".format(image_path, pre_score)
    sess.close()
예제 #3
0
    def extract_features(self, inputs):
        with tf.variable_scope('fcn_16s'):
            with slim.arg_scope(
                    vgg.vgg_arg_scope(weight_decay=self._weight_decay)):
                fc8_logits, end_points = vgg.vgg_16(
                    inputs=inputs,
                    num_classes=self._num_classes,
                    is_training=self._is_training,
                    spatial_squeeze=self._spatial_squeeze,
                    fc_conv_padding='SAME',
                    global_pool=self._global_pool)

                upsampled_fc8_logits = slim.conv2d_transpose(
                    fc8_logits, self._num_classes, kernel_size=[4, 4],
                    stride=[2, 2], padding='SAME', activation_fn=None,
                    normalizer_fn=None, scope='deconv1')
                pool4 = end_points['fcn_16s/vgg_16/pool4']
                pool4_logits = slim.conv2d(
                    pool4, self._num_classes, [1, 1], activation_fn=None,
                    normalizer_fn=None, scope='pool4_conv')
                fused_logits = tf.add(pool4_logits, upsampled_fc8_logits,
                                      name='fused_logits')
                logits = tf.image.resize_bilinear(
                    fused_logits, tf.shape(inputs)[1:3], align_corners=True)

        return logits
예제 #4
0
def retrieve(image_dir, gallery_dir, model_path, gallery_encode, feature_code):
    """
    :param image_dir: 图片根目录,内有两个子文件夹,query和gallery,都保存有图片
    :param gallery_dir: build_gallery将数据保存在此目录,single_query从此目录读取数据
    :param model_path: 加载指定模型
    :param gallery_encode: True 则进行特征编码,否则加载已经保存的文件
    :param feature_code: 1-scda ,2-scda_flip,3-scda_plus,4-scda_flip_plus.input_batch and output_layer also different
    :return:
    """
    # check dir
    assert os.path.isdir(gallery_dir), 'no directory name {}'.format(
        gallery_dir)  # 保存gallery的文件夹
    assert os.path.isdir(image_dir), 'no directory name {}'.format(
        image_dir)  # 数据集文件夹
    assert os.path.isfile(model_path), 'model path not given!'

    # build model
    input_shape = (None, None, None, 3)
    images = tf.placeholder(shape=input_shape, dtype=tf.float32)
    final_output, feature_dict = vgg.vgg_16(inputs=images,
                                            num_classes=None,
                                            is_training=False)
    # print(feature_dict)
    feature_1 = feature_dict['vgg_16/pool5']
    feature_2 = feature_dict['vgg_16/conv5/conv5_2']
    # final output node depend on feature code
    if feature_code == 1 or feature_code == 2:
        feature = feature_1
    else:
        feature = [feature_1, feature_2]

    # restore 过滤掉一些不需要加载参数 返回dict可以将保存的变量对应到模型中新的变量,返回list直接加载
    include_vars_map = None
    saver = tf.train.Saver(include_vars_map)

    # define session
    with tf.Session() as sess:
        # load param
        sess.run(tf.global_variables_initializer())
        print(model_path)
        saver.restore(sess, model_path)

        # data_set
        query_im_paths, query_labels, gallery_im_paths, gallery_labels = data_utils.split_dataset(
            image_dir)

        # gallery特征提取或加载
        if gallery_encode:
            gallery_features = build_gallery(sess, images, feature,
                                             feature_code, gallery_im_paths,
                                             gallery_dir)
        else:
            gallery_features = np.load(
                os.path.join(gallery_dir, 'gallery_features.npy'))

        # 开始检索
        query(sess, images, feature, feature_code, query_im_paths,
              gallery_features, query_labels, gallery_labels)
예제 #5
0
 def testModelVariables(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         inputs = random_ops.random_uniform((batch_size, height, width, 3))
         vgg.vgg_16(inputs, num_classes)
         expected_names = [
             'vgg_16/conv1/conv1_1/weights',
             'vgg_16/conv1/conv1_1/biases',
             'vgg_16/conv1/conv1_2/weights',
             'vgg_16/conv1/conv1_2/biases',
             'vgg_16/conv2/conv2_1/weights',
             'vgg_16/conv2/conv2_1/biases',
             'vgg_16/conv2/conv2_2/weights',
             'vgg_16/conv2/conv2_2/biases',
             'vgg_16/conv3/conv3_1/weights',
             'vgg_16/conv3/conv3_1/biases',
             'vgg_16/conv3/conv3_2/weights',
             'vgg_16/conv3/conv3_2/biases',
             'vgg_16/conv3/conv3_3/weights',
             'vgg_16/conv3/conv3_3/biases',
             'vgg_16/conv4/conv4_1/weights',
             'vgg_16/conv4/conv4_1/biases',
             'vgg_16/conv4/conv4_2/weights',
             'vgg_16/conv4/conv4_2/biases',
             'vgg_16/conv4/conv4_3/weights',
             'vgg_16/conv4/conv4_3/biases',
             'vgg_16/conv5/conv5_1/weights',
             'vgg_16/conv5/conv5_1/biases',
             'vgg_16/conv5/conv5_2/weights',
             'vgg_16/conv5/conv5_2/biases',
             'vgg_16/conv5/conv5_3/weights',
             'vgg_16/conv5/conv5_3/biases',
             'vgg_16/fc6/weights',
             'vgg_16/fc6/biases',
             'vgg_16/fc7/weights',
             'vgg_16/fc7/biases',
             'vgg_16/fc8/weights',
             'vgg_16/fc8/biases',
         ]
         model_variables = [
             v.op.name for v in variables_lib.get_model_variables()
         ]
         self.assertSetEqual(set(model_variables), set(expected_names))
예제 #6
0
 def testForward(self):
     batch_size = 1
     height, width = 224, 224
     with self.test_session() as sess:
         inputs = random_ops.random_uniform((batch_size, height, width, 3))
         logits, _ = vgg.vgg_16(inputs)
         sess.run(variables.global_variables_initializer())
         output = sess.run(logits)
         self.assertTrue(output.any())
예제 #7
0
 def testFullyConvolutional(self):
     batch_size = 1
     height, width = 256, 256
     num_classes = 1000
     with self.test_session():
         inputs = random_ops.random_uniform((batch_size, height, width, 3))
         logits, _ = vgg.vgg_16(inputs, num_classes, spatial_squeeze=False)
         self.assertEquals(logits.op.name, 'vgg_16/fc8/BiasAdd')
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, 2, 2, num_classes])
예제 #8
0
 def testBuild(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         inputs = random_ops.random_uniform((batch_size, height, width, 3))
         logits, _ = vgg.vgg_16(inputs, num_classes)
         self.assertEquals(logits.op.name, 'vgg_16/fc8/squeezed')
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
예제 #9
0
 def testEvaluation(self):
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         eval_inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = vgg.vgg_16(eval_inputs, is_training=False)
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
         predictions = tf.argmax(logits, 1)
         self.assertListEqual(predictions.get_shape().as_list(),
                              [batch_size])
예제 #10
0
def vgg_model_fn(features, labels, mode, params):
    """Model function for tf.estimator

    Args:
        features: input batch of images
        labels: labels of the images
        mode: can be one of tf.estimator.ModeKeys.{TRAIN, EVAL, PREDICT}
        params: contains hyperparameters of the model (ex: `params.learning_rate`)

    Returns:
        model_spec: tf.estimator.EstimatorSpec object
    """
    images = features
    labels = tf.cast(labels, tf.int64)
    # images = tf.reshape(images, [-1, params.image_size, params.image_size, 1])
    assert images.shape[1:] == [params.image_size, params.image_size,
                                3], "{}".format(images.shape)

    # MODEL: define the layers of the model
    is_training = (mode == tf.estimator.ModeKeys.TRAIN)
    embeddings = vgg.vgg_16(inputs=images,
                            num_classes=params.embedding_size,
                            is_training=is_training,
                            dropout_keep_prob=params.dropout_keep_prob)
    # 增加一个l2 norm
    embedding_mean_norm = tf.reduce_mean(tf.norm(embeddings, axis=1))

    # Define triplet loss
    if params.triplet_strategy == "batch_all":
        loss, fraction = batch_all_triplet_loss(labels,
                                                embeddings,
                                                margin=params.margin,
                                                squared=params.squared)
    elif params.triplet_strategy == "batch_hard":
        loss = batch_hard_triplet_loss(labels,
                                       embeddings,
                                       margin=params.margin,
                                       squared=params.squared)
    else:
        raise ValueError("Triplet strategy not recognized: {}".format(
            params.triplet_strategy))

    # Define training step that minimizes the loss with the Adam optimizer
    optimizer = tf.train.AdamOptimizer(params.learning_rate)
    global_step = tf.train.get_global_step()

    # Add a dependency to update the moving mean and variance for batch normalization
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        train_op = optimizer.minimize(loss, global_step=global_step)

    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
예제 #11
0
    def extract_features(self, inputs):
        with slim.arg_scope(
                vgg.vgg_arg_scope(weight_decay=self._weight_decay)):
            with tf.variable_scope('fcn_32s'):
                fc8_logits, end_points = vgg.vgg_16(
                    inputs=inputs,
                    num_classes=self._num_classes,
                    is_training=self._is_training,
                    spatial_squeeze=self._spatial_squeeze,
                    fc_conv_padding='SAME',
                    global_pool=self._global_pool)
                logits = tf.image.resize_bilinear(
                    fc8_logits, tf.shape(inputs)[1:3], align_corners=True)

        return logits
예제 #12
0
 def testTrainEvalWithReuse(self):
     train_batch_size = 2
     eval_batch_size = 1
     train_height, train_width = 224, 224
     eval_height, eval_width = 256, 256
     num_classes = 1000
     with self.test_session():
         train_inputs = random_ops.random_uniform(
             (train_batch_size, train_height, train_width, 3))
         logits, _ = vgg.vgg_16(train_inputs)
         self.assertListEqual(logits.get_shape().as_list(),
                              [train_batch_size, num_classes])
         variable_scope.get_variable_scope().reuse_variables()
         eval_inputs = random_ops.random_uniform(
             (eval_batch_size, eval_height, eval_width, 3))
         logits, _ = vgg.vgg_16(eval_inputs,
                                is_training=False,
                                spatial_squeeze=False)
         self.assertListEqual(logits.get_shape().as_list(),
                              [eval_batch_size, 2, 2, num_classes])
         logits = math_ops.reduce_mean(logits, [1, 2])
         predictions = math_ops.argmax(logits, 1)
         self.assertEquals(predictions.get_shape().as_list(),
                           [eval_batch_size])
예제 #13
0
 def testEndPoints(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         _, end_points = vgg.vgg_16(inputs, num_classes)
         expected_names = [
             'vgg_16/conv1/conv1_1', 'vgg_16/conv1/conv1_2', 'vgg_16/pool1',
             'vgg_16/conv2/conv2_1', 'vgg_16/conv2/conv2_2', 'vgg_16/pool2',
             'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2',
             'vgg_16/conv3/conv3_3', 'vgg_16/pool3', 'vgg_16/conv4/conv4_1',
             'vgg_16/conv4/conv4_2', 'vgg_16/conv4/conv4_3', 'vgg_16/pool4',
             'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2',
             'vgg_16/conv5/conv5_3', 'vgg_16/pool5', 'vgg_16/fc6',
             'vgg_16/fc7', 'vgg_16/fc8'
         ]
         self.assertSetEqual(set(end_points.keys()), set(expected_names))
예제 #14
0
def se_vgg_model_fn(features, labels, mode, params):
    """Model function for tf.estimator

    Args:
        features: input batch of images
        labels: labels of the images
        mode: can be one of tf.estimator.ModeKeys.{TRAIN, EVAL, PREDICT}
        params: contains hyperparameters of the model (ex: `params.learning_rate`)

    Returns:
        model_spec: tf.estimator.EstimatorSpec object
    """
    images = features
    labels = tf.cast(labels, tf.int64)
    # images = tf.reshape(images, [-1, params.image_size, params.image_size, 1])
    assert images.shape[1:] == [params.image_size, params.image_size,
                                3], "{}".format(images.shape)

    # MODEL: define the layers of the model
    is_training = (mode == tf.estimator.ModeKeys.TRAIN)
    _, end_points = vgg.vgg_16(images,
                               num_classes=1000,
                               is_training=is_training,
                               dropout_keep_prob=0.5,
                               spatial_squeeze=True,
                               scope='vgg_16',
                               fc_conv_padding='VALID',
                               global_pool=False)
    # SE module 2 loc feature vector
    net = end_points['pool5']
    loc_feature1 = se_moduel(net, 16)
    loc_feature2 = se_moduel(net, 16)

    # Define training step that minimizes the loss with the Adam optimizer
    optimizer = tf.train.AdamOptimizer(params.learning_rate)
    global_step = tf.train.get_global_step()

    # Add a dependency to update the moving mean and variance for batch normalization
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        train_op = optimizer.minimize(loss, global_step=global_step)

    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
예제 #15
0
def predict(models_path, image_dir, labels_filename, labels_nums, data_format):
    [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(vgg.vgg_arg_scope()):
        out, end_points = vgg.vgg_16(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)
    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})
        max_score = pre_score[0, pre_label]
        print("{} is: pre labels:{},name:{} score: {}".format(
            image_path, pre_label,
            list(labels_filename.keys())[list(
                labels_filename.values()).index(pre_label)], max_score))
    sess.close()
예제 #16
0
def extract_feature(imgList, args):
    tf.reset_default_graph()

    queue = tf.train.string_input_producer(imgList,
                                           num_epochs=None,
                                           shuffle=False)
    reader = tf.WholeFileReader()

    img_path, img_data = reader.read(queue)
    img = vgg_preprocessing.preprocess_image(
        tf.image.decode_jpeg(contents=img_data, channels=3), args.imgSize,
        args.imgSize)
    img = tf.expand_dims(img, 0)

    _, _, features = vgg.vgg_16(img, is_training=False)
    pool5 = features['pool5']
    fc7 = features['fc7']

    saver = tf.train.Saver()
    init_op = tf.global_variables_initializer()

    pool5s = []
    fc7s = []

    with tf.Session() as sess:
        sess.run(init_op)
        saver.restore(sess, args.cnnModel)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        feats = []
        for i in range(len(imgList)):
            p, f = sess.run([pool5, fc7])  # (1, 7, 7, 512) (1, 1, 1, 4096)
            pool5s.append(p[0])
            fc7s.append(f[0][0][0])

        coord.request_stop()
        coord.join(threads)
    return pool5s, fc7s
예제 #17
0
####################   改这里  ########################################
#导入模型
from slim.nets.vgg import vgg_16
from slim.nets.vgg import vgg_arg_scope

#要测试的如数尺寸
input_size = [224, 224, 3]

# 给模型的输入口
inputs = tf.placeholder(tf.float32,
                        shape=(1, input_size[0], input_size[1], input_size[2]))
num_class = 32

#建造模型
with slim.arg_scope(vgg_arg_scope()):
    net, end_points = vgg_16(inputs, num_class)

#只要有logits就可以评估了,中间过程layers_end_points有的话就显示,没有就不显示
logits = net
layers_end_points = end_points

####################   end  ###########################################


def show_parament_numbers():
    from functools import reduce
    from operator import mul

    def get_num_params():
        num_params = 0
        for variable in tf.trainable_variables():
예제 #18
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(vgg.vgg_arg_scope()):
        out, end_points = vgg.vgg_16(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)
    # 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 = 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_op = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)

    saver = tf.train.Saver()
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_input_images, batch_input_labels = sess.run(
                [train_images_batch, train_labels_batch])
            _, train_loss = sess.run(
                [train_op, loss],
                feed_dict={
                    input_images: batch_input_images,
                    input_labels: batch_input_labels,
                    keep_prob: 0.5,
                    is_training: True
                })
            # train测试(这里仅测试训练集的一个batch)
            if i % train_log_step == 0:
                train_acc = sess.run(accuracy,
                                     feed_dict={
                                         input_images: batch_input_images,
                                         input_labels: batch_input_labels,
                                         keep_prob: 1.0,
                                         is_training: False
                                     })
                print(
                    "%s: Step [%d]  train Loss : %f, training accuracy :  %g" %
                    (datetime.now(), i, train_loss, train_acc))

            # val测试(测试全部val数据)
            if i % val_log_step == 0:
                mean_loss, mean_acc = net_evaluation(sess, loss, accuracy,
                                                     val_images_batch,
                                                     val_labels_batch,
                                                     val_nums)
                print("%s: Step [%d]  val Loss : %f, val accuracy :  %g" %
                      (datetime.now(), i, mean_loss, mean_acc))

            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # 保存val准确率最高的模型
            if mean_acc > max_acc and mean_acc > 0.5:
                max_acc = mean_acc
                path = os.path.dirname(snapshot_prefix)
                best_models = os.path.join(
                    path, 'best_models_{}_{:.4f}.ckpt'.format(i, max_acc))
                print('------save:{}'.format(best_models))
                saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
예제 #19
0
                                                   padding_size[1],
                                                   target_input_size[0],
                                                   target_input_size[1])

processed_images = tf.expand_dims(mean_centered_image, 0)

upsample_filter_np = biliner_upsample_weights(upsample_factor,
                                              number_of_classes)

upsample_factor_tensor = tf.Variable(upsample_filter_np,
                                     name='vgg_16/fc8/t_conv')

with slim.arg_scope(vgg.vgg_arg_scope()):
    logits, end_points = vgg.vgg_16(processed_images,
                                    num_classes=2,
                                    is_training=is_training_placeholder,
                                    spatial_squeeze=False,
                                    fc_conv_padding='SAME')

downsampled_logits_shape = tf.shape(logits)

upsampled_logits_shape = tf.stack([
    downsampled_logits_shape[0], original_shape[0], original_shape[1],
    downsampled_logits_shape[3]
])

upsample_logits = tf.nn.conv2d_transpose(
    logits,
    upsample_factor_tensor,
    output_shape=upsampled_logits_shape,
    strides=[1, upsample_factor, upsample_factor, 1],
################################################
## Defining the generators for training and validation batches
################################################
train_gen = data_gen_small(train_images, batch_size, width, height)
val_gen = data_gen_small(validation_images, batch_size, width, height)

with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [None, width, height, 3])
    y = tf.placeholder(tf.float32, [None, 1])

    ################################################
    ## Load the VGG16 model from slim extract the fully connected layer
    ## before the final output layer
    ################################################
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits, end_points = vgg.vgg_16(x, num_classes=1000, is_training=False)
        fc_7 = end_points['vgg_16/fc7']
## Define the only set of weights that we will learn W1 and b1
################################################
    W1 = tf.Variable(tf.random_normal([4096, 1], mean=0.0, stddev=0.02),
                     name='W1')
    b = tf.Variable(tf.random_normal([1], mean=0.0, stddev=0.02), name='b')

    ################################################
    ## Reshape the fully connected layer fc_7 and define
    ## the logits and probability
    ################################################
    fc_7 = tf.reshape(fc_7, [-1, W1.get_shape().as_list()[0]])
    logitx = tf.nn.bias_add(tf.matmul(fc_7, W1), b)
    probx = tf.nn.sigmoid(logitx)
예제 #21
0
def train(data_csv_path_train, train_log_step, train_param, data_csv_path_val,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param data_csv_path_train: 训练的csv文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param data_csv_path_val: 验证的val文件
    :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

    # 获得训练和测试的样本数
    with open(dataset_csv_path_train, 'r') as f:
        train_nums = len(f.readlines())
    with open(dataset_csv_path_val, 'r') as v:
        val_nums = len(v.readlines())
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    train_batch = data_loader.load_data(data_csv_path_train,
                                        image_type=args.image_type,
                                        image_size_before_crop=resize_height,
                                        labels_nums=labels_nums)
    train_images_batch = train_batch['image']
    train_labels_batch = train_batch['label']
    #     print('......................................................')
    #     print(train_images_batch)
    #     print(train_labels_batch)

    # val数据,验证数据可以不需要打乱数据
    val_batch = data_loader.load_data(data_csv_path_val,
                                      image_type=args.image_type,
                                      image_size_before_crop=resize_height,
                                      labels_nums=labels_nums,
                                      do_shuffle=False)
    val_images_batch = val_batch['image']
    val_labels_batch = val_batch['label']

    # Define the model:
    with slim.arg_scope(vgg.vgg_arg_scope()):
        out, end_points = vgg.vgg_16(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=False)  #添加正则化损失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=base_lr,momentum= 0.9)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)
    # # 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)
예제 #22
0
model_dir = "../model/vgg_16/"



x_shape = np.append([None], (IMAGE_SIZE, IMAGE_SIZE, 3), axis=0)
x = tf.placeholder(tf.float32, x_shape, "x")

y_shape = np.append([None], [NUM_CLASSES], axis=0)
y = tf.placeholder(tf.float32, y_shape, "y") 
y_test = tf.placeholder(tf.float32, y_shape, "y_test")

is_training = tf.placeholder(dtype=tf.bool, name="is_training")


with slim.arg_scope(vgg.vgg_arg_scope()):
    logits, _ = vgg.vgg_16(x, num_classes=NUM_CLASSES, is_training=is_training)
        
# Name logits Tensor, so that is can be loaded from disk after training
logits = tf.identity(logits, name='logits')

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)

correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

saver = tf.train.Saver()        

epochs = 1000
batch_size = 128
log_every_n_iter = 50
def train(train_filename, train_images_dir, train_log_step, train_param,
          val_filename, val_images_dir, 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

    # # 从record中读取图片和labels数据
    tf_image, tf_labels = read_images(train_filename,
                                      train_images_dir,
                                      data_shape,
                                      shuffle=True,
                                      type='normalization')
    train_images_batch, train_labels_batch = get_batch_images(
        tf_image,
        tf_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=False,
        shuffle=True)

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

    loss = tf.reduce_sum(tf.squared_difference(x=out, y=input_labels))
    # loss1=tf.squared_difference(x=out,y=input_labels)

    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    train_op = tf.train.AdamOptimizer(learning_rate=base_lr).minimize(loss)

    # tf.losses.add_loss(loss1)
    # # 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)
    # # 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)

    saver = tf.train.Saver(max_to_keep=4)
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_input_images, batch_input_labels = sess.run(
                [train_images_batch, train_labels_batch])
            _, train_loss = sess.run(
                [train_op, loss],
                feed_dict={
                    input_images: batch_input_images,
                    input_labels: batch_input_labels,
                    keep_prob: 0.5,
                    is_training: True
                })
            if i % train_log_step == 0:
                print("%s: Step [%d]  train Loss : %f" %
                      (datetime.now(), i, train_loss))
            # # train测试(这里仅测试训练集的一个batch)
            # if i%train_log_step == 0:
            #     train_acc = sess.run(accuracy, feed_dict={input_images:batch_input_images,
            #                                               input_labels: batch_input_labels,
            #                                               keep_prob:1.0, is_training: False})
            #     print "%s: Step [%d]  train Loss : %f, training accuracy :  %g" % (datetime.now(), i, train_loss, train_acc)
            #
            # # val测试(测试全部val数据)
            # if i%val_log_step == 0:
            #     _, train_loss = sess.run([train_step, loss], feed_dict={input_images: batch_input_images,
            #                                                             input_labels: batch_input_labels,
            #                                                             keep_prob: 1.0, is_training: False})
            #     print "%s: Step [%d]  val Loss : %f, val accuracy :  %g" % (datetime.now(), i, mean_loss, mean_acc)
            #
            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # # 保存val准确率最高的模型
            # if mean_acc>max_acc and mean_acc>0.5:
            #     max_acc=mean_acc
            #     path = os.path.dirname(snapshot_prefix)
            #     best_models=os.path.join(path,'best_models_{}_{:.4f}.ckpt'.format(i,max_acc))
            #     print('------save:{}'.format(best_models))
            #     saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
예제 #24
0
    #     # Subtract the mean pixel value from each pixel
    processed_image = _mean_image_subtraction(image_float,
                                              [_R_MEAN, _G_MEAN, _B_MEAN])

    print "processed_image: ", processed_image

    input_image = tf.expand_dims(processed_image, 0)

    print "input_image: ", input_image

    with slim.arg_scope(vgg.vgg_arg_scope()):

        # spatial_squeeze option enables to use network in a fully
        # convolutional manner
        logits, _ = vgg.vgg_16(input_image,
                               num_classes=1000,
                               is_training=False,
                               spatial_squeeze=False)

        print "logits: ", logits

    # For each pixel we get predictions for each class
    # out of 1000. We need to pick the one with the highest
    # probability. To be more precise, these are not probabilities,
    # because we didn't apply softmax. But if we pick a class
    # with the highest value it will be equivalent to picking
    # the highest value after applying softmax
    pred = tf.argmax(logits, dimension=3)

    var_list = slim.get_model_variables('vgg_16')

    init_fn = slim.assign_from_checkpoint_fn(