示例#1
0
def read_and_decode(filename_queue, batch_size):
    reader = tf.TFRecordReader()

    _, serialized_example = reader.read(filename_queue)

    tffeatures = tf.parse_single_example(serialized_example,
                                         features={
                                             'name':
                                             tf.FixedLenFeature([], tf.string),
                                             'frame_len':
                                             tf.FixedLenFeature([], tf.int64),
                                             'features':
                                             tf.FixedLenFeature([], tf.string),
                                             'labels':
                                             tf.FixedLenFeature([], tf.string),
                                         })
    global FLAGS
    if FLAGS is None:
        FLAGS = Get_GlobalFLAG()
    FEATURE_SIZE = getattr(FLAGS, 'feature_size', 1024)

    frame_len = tffeatures['frame_len']
    features = tf.decode_raw(tffeatures['features'], tf.float32)
    features = tf.reshape(features, [600, FEATURE_SIZE])
    labels = tf.decode_raw(tffeatures['labels'], tf.int32)
    labels = tf.reshape(labels, [500])
    name = tffeatures['name']

    frame_len_batch, features_batch, labels_batch, name_batch = tf.train.shuffle_batch(
        [frame_len, features, labels, name],
        min_after_dequeue=32,
        batch_size=batch_size,
        num_threads=10,
        capacity=128)
    return frame_len_batch, features_batch, labels_batch, name_batch
示例#2
0
 def __init__(self):
     SetDefaultlValueToFLAG('moe_low_rank_gating', -1)
     SetDefaultlValueToFLAG('moe_prob_gating', False)
     SetDefaultlValueToFLAG('moe_prob_gating_input', 'prob')
     SetDefaultlValueToFLAG('moe_l2', 1e-8)
     SetDefaultlValueToFLAG('moe_num_mixtures', 2)
     global FLAGS
     FLAGS = Get_GlobalFLAG()
示例#3
0
            tf.reshape(gate_activations,
                       [-1, num_mixtures + 1
                        ]))  # (Batch * #Labels) x (num_mixtures + 1)
        expert_distribution = tf.nn.sigmoid(
            tf.reshape(expert_activations,
                       [-1, num_mixtures]))  # (Batch * #Labels) x num_mixtures

        final_probabilities_by_class_and_batch = tf.reduce_sum(
            gating_distribution[:, :num_mixtures] * expert_distribution, 1)
        final_probabilities = tf.reshape(
            final_probabilities_by_class_and_batch, [-1, vocab_size])
        return final_probabilities


if __name__ == '__main__':

    import TFFusions.Config.Config as Config
    from TFFusions.Train.load_yaml_to_FLAG import LOAD_YAML_TO_FLAG, Get_GlobalFLAG

    train_config = Config.TRAIN_SCRIPT + 'chaining-lstm-cnn.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    FLAGS.num_supports = 3

    inputs = tf.placeholder(dtype=tf.float32, shape=(3, 1024))
    num_frames = tf.placeholder(dtype=tf.int32, shape=(3))
    model = ChainMoeModel()

    model.create_model(inputs, 500)
示例#4
0
 def __init__(self):
     global FLAGS
     super(BiLstmModel, self).__init__()
     FLAGS = Get_GlobalFLAG()
示例#5
0
 def __init__(self):
     super(LstmModel, self).__init__()
     global FLAGS
     FLAGS = Get_GlobalFLAG()
示例#6
0
 def __init__(self):
     global FLAGS
     FLAGS = Get_GlobalFLAG()
示例#7
0
def main(config_yaml=None):
    train_config = config_yaml or Config.TRAIN_SCRIPT + 'lstm-memory-cell2048.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    if os.path.exists(FLAGS.train_dir) == False:
        print('mk train dir {}'.format(FLAGS.train_dir))
        os.mkdir(FLAGS.train_dir)

    # train_items = getTrainItems()
    # val_items = getValItems()
    batchsize = FLAGS.batchsize

    #
    if FLAGS.device_id != None:
        os.environ['CUDA_VISIBLE_DEVICES'] = str(FLAGS.device_id)[1:-1]

    inputs = tf.placeholder(dtype=tf.float32, shape=(batchsize, 600, 4096))
    num_frames = tf.placeholder(dtype=tf.int32, shape=(batchsize))
    target_labels = tf.placeholder(dtype=tf.int32,
                                   shape=(batchsize, FLAGS.vocab_size))

    model = GetFrameModel(FLAGS.frame_level_model)()
    lossfunc = SoftmaxLoss()

    predict_labels = model.create_model(model_input=inputs,
                                        vocab_size=FLAGS.vocab_size,
                                        num_frames=num_frames,
                                        num_mixtures=FLAGS.moe_num_mixtures)
    predict_labels = predict_labels['predictions']
    loss = lossfunc.calculate_loss(predict_labels, target_labels)

    global_step = tf.Variable(0, trainable=False)
    decayed_learning_rate = tf.train.exponential_decay(
        FLAGS.base_learning_rate,
        global_step,
        FLAGS.decay_at_epoch,
        FLAGS.learning_rate_decay,
        staircase=True)

    optimizer_class = find_class_by_name(FLAGS.optimize, [tf.train])
    train_op = optimizer_class(decayed_learning_rate).minimize(loss)

    # init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
    init_op = tf.global_variables_initializer()

    # Load from TFRecord
    val_file_list = glob.glob('/mnt/md0/LSVC/tfrecords/val_*')
    train_file_list = glob.glob('/mnt/md0/LSVC/tfrecords/train_*')
    train_file_queue = tf.train.string_input_producer(train_file_list)
    val_file_queue = tf.train.string_input_producer(val_file_list)
    train_frame_len_batch, train_feature_batch, train_label_batch, train_name_batch = read_and_decode(
        train_file_queue, batchsize)
    test_frame_len_batch, test_feature_batch, test_label_batch, test_name_batch = read_and_decode(
        val_file_queue, batchsize)

    # LOG
    log_prefix_name = '{}_{}'.format(FLAGS.name, FLAGS.EX_ID)
    # python's logging
    pylog = logging.getLogger(log_prefix_name)
    pylog.setLevel(logging.DEBUG)
    fh = logging.FileHandler(FLAGS.train_dir + '/' + log_prefix_name + '.log')
    ch = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s-%(name)s-%(levelname)s: %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    pylog.addHandler(fh)
    pylog.addHandler(ch)
    # tfboard's log
    logger = Logger(FLAGS.train_dir + log_prefix_name)

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    tf_config.allow_soft_placement = True
    tf_config.log_device_placement = True

    # init session
    sess = tf.Session(config=tf_config)
    sess.run(init_op)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # save ( after session )
    Saver = tf.train.Saver(max_to_keep=20, keep_checkpoint_every_n_hours=2)

    if FLAGS.model_checkpoint_path is not None:
        print('load model from {} ...'.format(FLAGS.model_checkpoint_path))
        Saver.restore(sess=sess, save_path=FLAGS.model_checkpoint_path)
        print('Success !!!')

    cnt = 0

    for epoch in range(FLAGS.num_epochs + 1):

        # loop = len(train_items) // batchsize
        loop = 2222
        pylog.info('epoch: {} ... '.format(epoch))

        for i in range(loop):

            features, target_label, video_frames, train_name = sess.run([
                train_feature_batch, train_label_batch, train_frame_len_batch,
                train_name_batch
            ])

            fd = {
                inputs: features,
                target_labels: target_label,
                num_frames: video_frames
            }

            loss_value, _ = sess.run([loss, train_op], feed_dict=fd)

            logger.scalar_summary(log_prefix_name + '/train_loss', loss_value,
                                  cnt)
            pylog.info('cnt: {} train_loss: {}'.format(cnt, loss_value))

            if cnt % 30 == 0:
                features, target_label, video_frames, train_name = sess.run([
                    train_feature_batch, train_label_batch,
                    train_frame_len_batch, train_name_batch
                ])
                fd = {
                    inputs: features,
                    target_labels: target_label,
                    num_frames: video_frames
                }
                predict = sess.run(predict_labels, feed_dict=fd)
                train_meanap = mean_ap(predict, target_label)
                acc = accuracy(predict, target_label, topk=(1, 5, 10))

                logger.scalar_summary(log_prefix_name + '/train_mAP',
                                      train_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@1', acc[0],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@5', acc[1],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@10',
                                      acc[2], cnt)

                pylog.info('cnt: {} train_mAP: {}'.format(cnt, train_meanap))
                pylog.info('cnt: {} train_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} train_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} train_acc@10: {}'.format(cnt, acc[2]))

                # items = random.choices(val_items,k=FLAGS.batchsize)
                # features, video_frames, target_label = gen_tf_input(items,'val')
                # features, video_frames, target_label = pq_test.Get()

                features, target_label, video_frames, test_name = sess.run([
                    test_feature_batch, test_label_batch, test_frame_len_batch,
                    test_name_batch
                ])

                fd = {
                    inputs: features,
                    target_labels: target_label,
                    num_frames: video_frames
                }
                predict, test_loss = sess.run([predict_labels, loss],
                                              feed_dict=fd)
                test_meanap = mean_ap(predict, target_label)
                acc = accuracy(predict, target_label, topk=(1, 5, 10))

                logger.scalar_summary(log_prefix_name + '/test_mAP',
                                      test_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@1', acc[0],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@5', acc[1],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@10', acc[2],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_loss',
                                      test_loss, cnt)

                pylog.info('cnt: {} test_mAP: {}'.format(cnt, test_meanap))
                pylog.info('cnt: {} test_loss: {}'.format(cnt, test_loss))
                pylog.info('cnt: {} test_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} test_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} test_acc@10: {}'.format(cnt, acc[2]))

            if cnt % 2000 == 0:
                savepath = FLAGS.train_dir + log_prefix_name + '_save{:03}.ckpt'.format(
                    cnt)
                Saver.save(sess, savepath, cnt)
                pylog.info('save model:{} at {}.'.format(FLAGS.name, savepath))

            cnt += 1

    coord.request_stop()
    coord.join(threads)
示例#8
0
 def __init__(self):
     super(GruPoolingModel,self).__init__()
     global FLAGS
     FLAGS =Get_GlobalFLAG()
示例#9
0
                                               time_major=False,
                                               swap_memory=FLAGS.rnn_swap_memory,
                                               dtype=tf.float32)
            num_frames_matrix = tf.maximum(tf.cast(tf.expand_dims(num_frames, axis=1), dtype=tf.float32), tf.ones([FLAGS.realbatchsize, 1]))
            pooling_output = tf.reduce_sum(outputs, axis = 1) / num_frames_matrix

        # aggregated_model = getattr(video_level_models,
        #                            FLAGS.video_level_classifier_model)

        aggregated_model = GetVideoModel(FLAGS.video_level_model)
        return aggregated_model().create_model(
            model_input=pooling_output,
            original_input=model_input,
            vocab_size=vocab_size,
            **unused_params)

if __name__=='__main__':

    from TFFusions.Train.load_yaml_to_FLAG import LOAD_YAML_TO_FLAG, Get_GlobalFLAG

    train_config ='/datacenter/1/LSVC/Code/VideoClassification/TFFusions/Train/train_config_yaml/gru_pooling_1.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    x = tf.placeholder(dtype=tf.float32,shape=(12,16,4096))
    y = tf.placeholder(dtype=tf.float32,shape=(12,))

    model = GruPoolingModel()

    z = model.create_model(x,500,y)
示例#10
0
    :param x: a ndarray (batch)
    :param vocab: classes num
    :return: a one_hot array (batch x vocab)
    '''
    batchsize = x.shape[0]
    ret = np.zeros(shape=(batchsize, vocab))
    for i in range(batchsize):
        ret[i, x[i]] = 1
    return ret


#############################################################################

train_config = '/datacenter/1/LSVC/Code/VideoClassification/TrainScript/Server209/LstmAttentionLstmModel_EX19_2.yaml'
LOAD_YAML_TO_FLAG(train_config)
FLAGS = Get_GlobalFLAG()

FLAGS.train_dir = '/tmp/test/'
if os.path.exists(FLAGS.train_dir) == False:
    print('mk train dir {}'.format(FLAGS.train_dir))
    os.mkdir(FLAGS.train_dir)
batchsize = FLAGS.batchsize

one_hot = getattr(FLAGS, 'one_hot', False)

# model
if FLAGS.device_id != None:
    os.environ['CUDA_VISIBLE_DEVICES'] = str(FLAGS.device_id)[1:-1]

inputs = tf.placeholder(dtype=tf.float32,
                        shape=(batchsize * FLAGS.scale, FLAGS.fix_length,
示例#11
0
def main(config_yaml=None):
    global FLAGS
    # train_config = config_yaml or Config.TRAIN_SCRIPT + 'att-lstm.yaml'
    train_config = config_yaml or Config.TRAIN_SCRIPT + 'lstm-memory-cell1024_ex14.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    if os.path.exists(FLAGS.train_dir) == False:
        print('mk train dir {}'.format(FLAGS.train_dir))
        os.mkdir(FLAGS.train_dir)
    batchsize = FLAGS.batchsize

    one_hot = getattr(FLAGS, 'one_hot', False)

    # model
    if FLAGS.device_id != None:
        os.environ['CUDA_VISIBLE_DEVICES'] = str(FLAGS.device_id)[1:-1]

    FEATURE_SIZE = getattr(FLAGS, 'real_feature_size',
                           getattr(FLAGS, 'feature_size', 1024))
    inputs = tf.placeholder(dtype=tf.float32,
                            shape=(batchsize * FLAGS.scale, FLAGS.fix_length,
                                   FEATURE_SIZE))
    num_frames = tf.placeholder(dtype=tf.int32,
                                shape=(batchsize * FLAGS.scale))

    if one_hot == True:
        target_labels = tf.placeholder(dtype=tf.int32,
                                       shape=(batchsize * FLAGS.scale,
                                              FLAGS.vocab_size))
    else:
        target_labels = tf.placeholder(dtype=tf.int32,
                                       shape=(batchsize * FLAGS.scale))

    model = GetFrameModel(FLAGS.frame_level_model)()

    predict_labels = model.create_model(model_input=inputs,
                                        vocab_size=FLAGS.vocab_size,
                                        num_frames=num_frames,
                                        num_mixtures=FLAGS.moe_num_mixtures)
    predict_labels = predict_labels['predictions']
    enable_softmax = getattr(FLAGS, 'enable_softmax', False)
    if enable_softmax:
        predict_labels = tf.nn.softmax(predict_labels)

    vars = tf.trainable_variables()
    lossL2 = tf.add_n([tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name
                       ]) * FLAGS.regularization_penalty

    # loss
    if one_hot == True:
        # lossfunc = SoftmaxLoss()
        lossfunc = CrossEntropyLoss()
        loss = lossfunc.calculate_loss(predict_labels, target_labels) + lossL2
    else:
        # WARNNING!!!
        # this loss function can't work well
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=predict_labels, labels=target_labels)
        loss = tf.reduce_mean(loss) + lossL2

    # optimize
    global_step = tf.Variable(0, trainable=False)
    decayed_learning_rate = tf.train.exponential_decay(
        FLAGS.base_learning_rate,
        global_step,
        FLAGS.decay_at_step,
        FLAGS.learning_rate_decay,
        staircase=True)
    optimizer_class = find_class_by_name(FLAGS.optimize, [tf.train])
    # train_op = tf.train.GradientDescentOptimizer(learning_rate=decayed_learning_rate).minimize(loss,global_step=global_step)
    train_op = optimizer_class(learning_rate=decayed_learning_rate).minimize(
        loss, global_step=global_step)

    # init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
    init_op = tf.global_variables_initializer()

    # Load from TFRecord
    data_kind = getattr(FLAGS, 'train_data', 'inc')
    if data_kind == 'inc':
        val_file_list = glob.glob('/mnt/md0/LSVC/inc_tfrecords/val_*')
        train_file_list = glob.glob('/mnt/md0/LSVC/inc_tfrecords/train_*')
    elif data_kind == 'vgg':
        val_file_list = glob.glob('/mnt/md0/LSVC/tfrecords/val_*')
        train_file_list = glob.glob('/mnt/md0/LSVC/tfrecords/train_*')
    elif data_kind == 'sen':
        val_file_list = glob.glob('/mnt/md0/LSVC/sen_tfrecords/val_*')
        train_file_list = glob.glob('/mnt/md0/LSVC/sen_tfrecords/train_*')

    train_file_queue = tf.train.string_input_producer(train_file_list)
    val_file_queue = tf.train.string_input_producer(val_file_list)
    train_frame_len_batch, train_feature_batch, train_label_batch, train_name_batch = read_and_decode(
        train_file_queue, FLAGS.batchsize)
    test_frame_len_batch, test_feature_batch, test_label_batch, test_name_batch = read_and_decode(
        val_file_queue, FLAGS.batchsize)

    # LOG
    log_prefix_name = '{}_{}'.format(FLAGS.name, FLAGS.EX_ID)
    # python's logging
    pylog = logging.getLogger(log_prefix_name)
    pylog.setLevel(logging.DEBUG)
    fh = logging.FileHandler(FLAGS.train_dir + '/' + log_prefix_name + '.log')
    ch = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s-%(name)s-%(levelname)s: %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    pylog.addHandler(fh)
    pylog.addHandler(ch)
    # tfboard's log
    logger = Logger(FLAGS.train_dir + log_prefix_name)

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    tf_config.allow_soft_placement = True
    tf_config.log_device_placement = True

    # init session
    sess = tf.Session(config=tf_config)
    sess.run(init_op)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # save ( after session )
    Saver = tf.train.Saver(max_to_keep=20, keep_checkpoint_every_n_hours=2)

    if FLAGS.model_checkpoint_path is not None:
        print('load model from {} ...'.format(FLAGS.model_checkpoint_path))
        Saver.restore(sess=sess, save_path=FLAGS.model_checkpoint_path)
        print('Success !!!')

    pylog.info('train_config: {}'.format(FLAGS.YAML))

    cnt = 0
    for epoch in range(FLAGS.num_epochs + 1):

        # loop = len(train_items) // batchsize
        loop = 2222
        pylog.info('epoch: {} ... '.format(epoch))

        for i in range(loop):

            input_features, input_target_labels, input_video_frames, train_name = sess.run(
                [
                    train_feature_batch, train_label_batch,
                    train_frame_len_batch, train_name_batch
                ])
            input_features, input_target_labels, input_video_frames = split_into_small_peice(
                input_features, input_target_labels, input_video_frames)
            fd = {
                inputs: input_features,
                target_labels: input_target_labels,
                num_frames: input_video_frames
            }
            loss_value, _ = sess.run([loss, train_op], feed_dict=fd)
            logger.scalar_summary(log_prefix_name + '/train_loss', loss_value,
                                  cnt)
            pylog.info('cnt: {} train_loss: {}'.format(cnt, loss_value))

            if cnt % 30 == 0:
                input_features, input_target_labels, input_video_frames, train_name = sess.run(
                    [
                        train_feature_batch, train_label_batch,
                        train_frame_len_batch, train_name_batch
                    ])
                input_features, input_target_labels, input_video_frames = split_into_small_peice(
                    input_features, input_target_labels, input_video_frames)
                fd = {
                    inputs: input_features,
                    target_labels: input_target_labels,
                    num_frames: input_video_frames
                }

                predict = sess.run(predict_labels, feed_dict=fd)

                if one_hot == False:
                    input_target_labels = toOneHot(input_target_labels,
                                                   FLAGS.vocab_size)

                train_meanap = mean_ap(predict, input_target_labels)
                acc = accuracy(predict, input_target_labels, topk=(1, 5, 10))

                # logger.scalar_summary(log_prefix_name + '/train_mAP', train_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@1', acc[0],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@5', acc[1],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@10',
                                      acc[2], cnt)

                pylog.info('cnt: {} train_mAP: {}'.format(cnt, train_meanap))
                pylog.info('cnt: {} train_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} train_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} train_acc@10: {}'.format(cnt, acc[2]))

                input_features, input_target_labels, input_video_frames, test_name = sess.run(
                    [
                        test_feature_batch, test_label_batch,
                        test_frame_len_batch, test_name_batch
                    ])
                input_features, input_target_labels, input_video_frames = split_into_small_peice(
                    input_features, input_target_labels, input_video_frames)
                fd = {
                    inputs: input_features,
                    target_labels: input_target_labels,
                    num_frames: input_video_frames
                }
                predict, test_loss = sess.run([predict_labels, loss],
                                              feed_dict=fd)

                if one_hot == False:
                    input_target_labels = toOneHot(input_target_labels,
                                                   FLAGS.vocab_size)

                test_meanap = mean_ap(predict, input_target_labels)
                acc = accuracy(predict, input_target_labels, topk=(1, 5, 10))

                # logger.scalar_summary(log_prefix_name + '/test_mAP', test_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@1', acc[0],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@5', acc[1],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@10', acc[2],
                                      cnt)
                logger.scalar_summary(log_prefix_name + '/test_loss',
                                      test_loss, cnt)

                pylog.info('cnt: {} test_mAP: {}'.format(cnt, test_meanap))
                pylog.info('cnt: {} test_loss: {}'.format(cnt, test_loss))
                pylog.info('cnt: {} test_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} test_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} test_acc@10: {}'.format(cnt, acc[2]))

            if cnt % 2000 == 0:
                savepath = FLAGS.train_dir + log_prefix_name + '_save{:03}.ckpt'.format(
                    cnt)
                Saver.save(sess, savepath, cnt)
                pylog.info('save model:{} at {}'.format(FLAGS.name, savepath))

            cnt += 1

    coord.request_stop()
    coord.join(threads)
示例#12
0
 def __init__(self):
     super(LstmCnnDeepCombineChainModel, self).__init__()
     global FLAGS
     FLAGS = Get_GlobalFLAG()
示例#13
0
 def __init__(self):
     super(LstmAttentionLstmModel2, self).__init__()
     global FLAGS
     FLAGS = Get_GlobalFLAG()
示例#14
0
def main(config_yaml=None):
    train_config = config_yaml or Config.TRAIN_SCRIPT + 'lstm-memory-cell1024.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    if os.path.exists(FLAGS.train_dir) == False:
        print('mk train dir {}'.format(FLAGS.train_dir))
        os.mkdir(FLAGS.train_dir)

    train_items = getTrainItems()
    val_items = getValItems()
    batchsize = FLAGS.batchsize

    inputs = tf.placeholder(dtype=tf.float32, shape=(None, 600, 4096))
    num_frames = tf.placeholder(dtype=tf.int32, shape=(None))
    target_labels = tf.placeholder(dtype=tf.int32, shape=(None, FLAGS.vocab_size))

    model = GetFrameModel(FLAGS.frame_level_model)()
    lossfunc = SoftmaxLoss()

    predict_labels = model.create_model(model_input=inputs, vocab_size=FLAGS.vocab_size, num_frames=num_frames)
    predict_labels = predict_labels['predictions']
    loss = lossfunc.calculate_loss(predict_labels, target_labels)

    global_step = tf.Variable(0, trainable=False)
    decayed_learning_rate = tf.train.exponential_decay(FLAGS.base_learning_rate,
                                                       global_step,
                                                       FLAGS.decay_at_epoch,
                                                       FLAGS.learning_rate_decay,
                                                       staircase=True)

    optimizer_class = find_class_by_name(FLAGS.optimize, [tf.train])
    train_op = optimizer_class(decayed_learning_rate).minimize(loss)

    # LOG
    log_prefix_name = '{}_{}'.format(FLAGS.name, FLAGS.EX_ID)
    # python's logging
    pylog = logging.getLogger(log_prefix_name)
    pylog.setLevel(logging.DEBUG)
    fh = logging.FileHandler(FLAGS.train_dir + '/' + log_prefix_name + '.log')
    ch = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s: %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    pylog.addHandler(fh)
    pylog.addHandler(ch)
    # tfboard's log
    logger = Logger(FLAGS.train_dir + log_prefix_name)

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    tf_config.allow_soft_placement = True
    tf_config.log_device_placement = True
    sess = tf.Session(config=tf_config)
    sess.run(tf.global_variables_initializer())

    # Load Queue
    pq_train = PictureQueue(kind='train', batchsize=batchsize, worker=5)
    pq_test = PictureQueue(kind='val', batchsize=batchsize, worker=3)

    # save ( after session )
    Saver = tf.train.Saver(max_to_keep=20, keep_checkpoint_every_n_hours=2)

    if FLAGS.model_checkpoint_path is not None:
        print('load model from {} ...'.format(FLAGS.model_checkpoint_path))
        Saver.restore(sess=sess, save_path=FLAGS.model_checkpoint_path)
        print('Success !!!')

    cnt = 0

    for epoch in range(FLAGS.num_epochs):
        loop = len(train_items) // batchsize
        for i in range(loop):

            # l = i*batchsize
            # r = l+batchsize
            # items = train_items[l:r]
            # features, video_frames, target_label = gen_tf_input(items,'train')

            features, video_frames, target_label = pq_train.Get()

            video_frames = np.array(video_frames)

            fd = {inputs: features, target_labels: target_label, num_frames: video_frames}
            loss_value, _ = sess.run([loss, train_op], feed_dict=fd)

            logger.scalar_summary(log_prefix_name + '/train_loss', loss_value, cnt)
            pylog.info('cnt: {} train_loss: {}'.format(cnt, loss_value))

            if cnt % 50 == 0:
                fd = {inputs: features, target_labels: target_label, num_frames: video_frames}
                predict = sess.run(predict_labels, feed_dict=fd)
                train_meanap = mean_ap(predict, target_label)
                acc = accuracy(predict, target_labels, topk=(1, 5, 10))

                logger.scalar_summary(log_prefix_name + '/train_mAP', train_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@1', acc[0], cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@5', acc[1], cnt)
                logger.scalar_summary(log_prefix_name + '/train_acc@10', acc[2], cnt)

                pylog.info('cnt: {} train_mAP: {}'.format(cnt, train_meanap))
                pylog.info('cnt: {} train_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} train_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} train_acc@10: {}'.format(cnt, acc[2]))

                # items = random.choices(val_items,k=FLAGS.batchsize)
                # features, video_frames, target_label = gen_tf_input(items,'val')
                features, video_frames, target_label = pq_test.Get()

                fd = {inputs: features, target_labels: target_label, num_frames: video_frames}
                predict, test_loss = sess.run([predict_labels, loss], feed_dict=fd)
                test_meanap = mean_ap(predict, target_label)
                acc = accuracy(predict, target_label, topk=(1, 5, 10))

                logger.scalar_summary(log_prefix_name + '/test_mAP', test_meanap, cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@1', acc[0], cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@5', acc[1], cnt)
                logger.scalar_summary(log_prefix_name + '/test_acc@10', acc[2], cnt)
                logger.scalar_summary(log_prefix_name + '/test_loss', test_loss, cnt)

                pylog.info('cnt: {} test_mAP: {}'.format(cnt, test_meanap))
                pylog.info('cnt: {} test_loss: {}'.format(cnt, test_loss))
                pylog.info('cnt: {} test_acc@1: {}'.format(cnt, acc[0]))
                pylog.info('cnt: {} test_acc@5: {}'.format(cnt, acc[1]))
                pylog.info('cnt: {} test_acc@10: {}'.format(cnt, acc[2]))

            if cnt % 2000 == 0:
                savepath = FLAGS.train_dir + log_prefix_name + '_save{:03}.ckpt'.format(cnt)
                Saver.save(sess, savepath, cnt)
                pylog.info('save model:{} at {}.'.format(FLAGS.name, savepath))

            cnt += 1
示例#15
0
                       [-1, num_mixtures + 1
                        ]))  # (Batch * #Labels) x (num_mixtures + 1)
        expert_distribution = tf.nn.sigmoid(
            tf.reshape(expert_activations,
                       [-1, num_mixtures]))  # (Batch * #Labels) x num_mixtures

        final_probabilities_by_class_and_batch = tf.reduce_sum(
            gating_distribution[:, :num_mixtures] * expert_distribution, 1)
        final_probabilities = tf.reshape(
            final_probabilities_by_class_and_batch, [-1, vocab_size])
        return final_probabilities


if __name__ == '__main__':

    import TFFusions.Config.Config as Config
    from TFFusions.Train.load_yaml_to_FLAG import LOAD_YAML_TO_FLAG, Get_GlobalFLAG

    train_config = Config.TRAIN_SCRIPT + 'chaining-lstm-cnn.yaml'
    LOAD_YAML_TO_FLAG(train_config)
    FLAGS = Get_GlobalFLAG()

    FLAGS.num_supports = 3
    FLAGS.deep_chain_relu_type = tf.nn.relu

    inputs = tf.placeholder(dtype=tf.float32, shape=(3, 1024))
    num_frames = tf.placeholder(dtype=tf.int32, shape=(3))
    model = DeepCombineChainModel()

    model.create_model(inputs, 500)