示例#1
0
def train_CAE(path_boxes_np, args):
    epoch_len = len(np.load(path_boxes_np))
    f_imgs, g_imgs, b_imgs = util.CAE_dataset_feed_dict(
        prefix, path_boxes_np, dataset_name=args.dataset)
    #former_batch,gray_batch,back_batch=util.CAE_dataset(path_boxes_np,args.dataset,epochs,batch_size)
    former_batch = tf.placeholder(dtype=tf.float32,
                                  shape=[batch_size, 64, 64, 1],
                                  name='former_batch')
    gray_batch = tf.placeholder(dtype=tf.float32,
                                shape=[batch_size, 64, 64, 1],
                                name='gray_batch')
    back_batch = tf.placeholder(dtype=tf.float32,
                                shape=[batch_size, 64, 64, 1],
                                name='back_batch')

    grad1_x, grad1_y = tf.image.image_gradients(former_batch)
    # grad2_x,grad2_y=tf.image.image_gradients(gray_batch)
    grad3_x, grad3_y = tf.image.image_gradients(back_batch)

    grad_dis_1 = tf.sqrt(tf.square(grad1_x) + tf.square(grad1_y))
    grad_dis_2 = tf.sqrt(tf.square(grad3_x) + tf.square(grad3_y))

    former_outputs = CAE.CAE(grad_dis_1, 'former', bn=args.bn, training=True)
    gray_outputs = CAE.CAE(gray_batch, 'gray', bn=args.bn, training=True)
    back_outputs = CAE.CAE(grad_dis_2, 'back', bn=args.bn, training=True)

    former_loss = CAE.pixel_wise_L2_loss(former_outputs, grad_dis_1)
    gray_loss = CAE.pixel_wise_L2_loss(gray_outputs, gray_batch)
    back_loss = CAE.pixel_wise_L2_loss(back_outputs, grad_dis_2)

    global_step = tf.Variable(0, dtype=tf.int32, trainable=False)
    global_step_a = tf.Variable(0, dtype=tf.int32, trainable=False)
    global_step_b = tf.Variable(0, dtype=tf.int32, trainable=False)

    lr_decay_epochs[0] = int(epoch_len // batch_size) * lr_decay_epochs[0]

    lr = tf.train.piecewise_constant(global_step,
                                     boundaries=lr_decay_epochs,
                                     values=learning_rate)

    former_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                    scope='former_')
    gray_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                  scope='gray_')
    back_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                  scope='back_')
    # print(former_vars)
    if args.weight_reg != 0:
        former_loss = former_loss + args.weight_reg * weiht_regualized_loss(
            former_vars)
        gray_loss = gray_loss + args.weight_reg * weiht_regualized_loss(
            gray_vars)
        back_loss = back_loss + args.weight_reg * weiht_regualized_loss(
            back_vars)

    former_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        former_loss, var_list=former_vars, global_step=global_step)
    gray_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        gray_loss, var_list=gray_vars, global_step=global_step_a)
    back_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        back_loss, var_list=back_vars, global_step=global_step_b)

    step = 0
    if not args.bn:
        writer = tf.summary.FileWriter(logdir=summary_save_path_pre +
                                       args.dataset)
    else:
        writer = tf.summary.FileWriter(logdir=summary_save_path_pre +
                                       args.dataset + '_bn')

    tf.summary.scalar('loss/former_loss', former_loss)
    tf.summary.scalar('loss/gray_loss', gray_loss)
    tf.summary.scalar('loss/back_loss', back_loss)
    tf.summary.image('inputs/former', grad_dis_1)
    tf.summary.image('inputs/gray', gray_batch)
    tf.summary.image('inputs/back', grad_dis_2)
    tf.summary.image('outputs/former', former_outputs)
    tf.summary.image('outputs/gray', gray_outputs)
    tf.summary.image('outputs/back', back_outputs)
    summary_op = tf.summary.merge_all()

    saver = tf.train.Saver(var_list=tf.global_variables())
    indices = list(range(epoch_len))

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(epochs):
            random.shuffle(indices)
            for i in range(epoch_len // batch_size):
                feed_dict = {
                    former_batch: [
                        f_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ],
                    gray_batch: [
                        g_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ],
                    back_batch: [
                        b_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ]
                }
                step, _lr, _, _, _, _former_loss, _gray_loss, _back_loss = sess.run(
                    [
                        global_step, lr, former_op, gray_op, back_op,
                        former_loss, gray_loss, back_loss
                    ],
                    feed_dict=feed_dict)
                if step % 10 == 0:
                    print('At step {}'.format(step))
                    print('\tLearning Rate {:.4f}'.format(_lr))
                    print('\tFormer Loss {:.4f}'.format(_former_loss))
                    print('\tGray Loss {:.4f}'.format(_gray_loss))
                    print('\tBack Loss {:.4f}'.format(_back_loss))

                if step % 50 == 0:
                    _summary = sess.run(summary_op, feed_dict=feed_dict)
                    writer.add_summary(_summary, global_step=step)
        if not args.bn:
            saver.save(sess, model_save_path_pre + args.dataset)
        else:
            saver.save(sess, model_save_path_pre + args.dataset + '_bn')

        print('train finished!')
        sess.close()
示例#2
0
def train_CAE(path_boxes_np, args):
    epoch_len = len(np.load(path_boxes_np))
    f_imgs, g_imgs, b_imgs, class_indexs = util.CAE_dataset_feed_dict(
        prefix, path_boxes_np, dataset_name=args.dataset)
    #former_batch,gray_batch,back_batch=util.CAE_dataset(path_boxes_np,args.dataset,epochs,batch_size)
    former_batch = tf.placeholder(dtype=tf.float32,
                                  shape=[batch_size, 64, 64, 1],
                                  name='former_batch')
    gray_batch = tf.placeholder(dtype=tf.float32,
                                shape=[batch_size, 64, 64, 1],
                                name='gray_batch')
    back_batch = tf.placeholder(dtype=tf.float32,
                                shape=[batch_size, 64, 64, 1],
                                name='back_batch')

    # * tf.image.image_gradients() 计算单张图片的x和y方向的梯度,与论文意思不一致
    # * 应修改为计算frame_{t} 和 frame_{t-3}及 frame_{t+3}的 帧差(absdiff)

    # grad1_x, grad1_y = tf.image.image_gradients(former_batch)
    # grad1=tf.concat([grad1_x,grad1_y],axis=-1)
    grad1 = tf.math.abs(tf.math.subtract(former_batch, gray_batch))
    # grad2_x,grad2_y=tf.image.image_gradients(gray_batch)
    # grad3_x, grad3_y = tf.image.image_gradients(back_batch)
    # grad3=tf.concat([grad3_x,grad3_y],axis=-1)
    grad3 = tf.math.abs(tf.math.subtract(back_batch, gray_batch))

    #grad_dis_1 = tf.sqrt(tf.square(grad1_x) + tf.square(grad1_y))
    #grad_dis_2 = tf.sqrt(tf.square(grad3_x) + tf.square(grad3_y))

    former_outputs = CAE.CAE(grad1, 'former', bn=args.bn, training=True)
    gray_outputs = CAE.CAE(gray_batch, 'gray', bn=args.bn, training=True)
    back_outputs = CAE.CAE(grad3, 'back', bn=args.bn, training=True)

    former_loss = CAE.pixel_wise_L2_loss(former_outputs, grad1)
    gray_loss = CAE.pixel_wise_L2_loss(gray_outputs, gray_batch)
    back_loss = CAE.pixel_wise_L2_loss(back_outputs, grad3)

    global_step = tf.Variable(0, dtype=tf.int32, trainable=False)
    global_step_a = tf.Variable(0, dtype=tf.int32, trainable=False)
    global_step_b = tf.Variable(0, dtype=tf.int32, trainable=False)

    lr_decay_epochs[0] = int(epoch_len // batch_size) * lr_decay_epochs[0]

    lr = tf.train.piecewise_constant(global_step,
                                     boundaries=lr_decay_epochs,
                                     values=learning_rate)

    former_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                    scope='former_')
    gray_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                  scope='gray_')
    back_vars = tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES,
                                  scope='back_')
    # print(former_vars)
    if args.weight_reg != 0:
        former_loss = former_loss + args.weight_reg * weiht_regualized_loss(
            former_vars)
        gray_loss = gray_loss + args.weight_reg * weiht_regualized_loss(
            gray_vars)
        back_loss = back_loss + args.weight_reg * weiht_regualized_loss(
            back_vars)

    former_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        former_loss, var_list=former_vars, global_step=global_step)
    gray_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        gray_loss, var_list=gray_vars, global_step=global_step_a)
    back_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
        back_loss, var_list=back_vars, global_step=global_step_b)

    step = 0
    if not args.bn:
        logdir = f'{summary_save_path_pre}/{args.dataset}'
    else:
        logdir = f'{summary_save_path_pre}/{args.dataset}_bn'
    writer = tf.summary.FileWriter(logdir=logdir)

    tf.summary.scalar('loss/former_loss', former_loss)
    tf.summary.scalar('loss/gray_loss', gray_loss)
    tf.summary.scalar('loss/back_loss', back_loss)
    #tf.summary.image('inputs/former',grad_dis_1)
    tf.summary.image('inputs/gray', gray_batch)
    #tf.summary.image('inputs/back',grad_dis_2)
    #tf.summary.image('outputs/former',former_outputs)
    tf.summary.image('outputs/gray', gray_outputs)
    #tf.summary.image('outputs/back',back_outputs)
    summary_op = tf.summary.merge_all()

    saver = tf.train.Saver(var_list=tf.global_variables())
    indices = list(range(epoch_len))

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(epochs):
            random.shuffle(indices)
            for i in range(epoch_len // batch_size):
                feed_dict = {
                    former_batch: [
                        f_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ],
                    gray_batch: [
                        g_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ],
                    back_batch: [
                        b_imgs[d]
                        for d in indices[i * batch_size:(i + 1) * batch_size]
                    ]
                }
                step, _lr, _, _, _, _former_loss, _gray_loss, _back_loss = sess.run(
                    [
                        global_step, lr, former_op, gray_op, back_op,
                        former_loss, gray_loss, back_loss
                    ],
                    feed_dict=feed_dict)
                step_result = f'step{step}: lr={_lr:.4f}, fl={_former_loss:.4f}, gl={_gray_loss:.4f}, bl={_back_loss:.4f}'
                if step % 10 == 0:
                    print(step_result)

                if step % 50 == 0:
                    _summary = sess.run(summary_op, feed_dict=feed_dict)
                    writer.add_summary(_summary, global_step=step)
        if not args.bn:
            ckpt_path = f'{model_save_path_pre}{args.dataset}/{args.dataset}.ckpt'
        else:
            ckpt_path = f'{model_save_path_pre}{args.dataset}_bn/{args.dataset}.ckpt'
        saver.save(sess, ckpt_path)

        print('train finished!')
        sess.close()