text
stringlengths
1
93.6k
img: [0,1]."""
MSE = cal_MSE(img1, img2)
PSNR = 10.0 * tf.log(1.0 / MSE) / tf.log(10.0)
return PSNR
def main_train():
"""Train and evaluate model.
Output: model_QPxx, record_train_QPxx."""
### Defind a session
sess = tf.Session(config = config)
### Set placeholder
x1 = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, CHANNEL]) # pre
x2 = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, CHANNEL]) # cmp
x3 = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, CHANNEL]) # sub
x5 = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, CHANNEL]) # raw
is_training = tf.placeholder_with_default(False, shape=()) # for BN training/testing. default testing.
PSNR_0 = cal_PSNR(x2, x5) # PSNR before enhancement (cmp and raw)
### Motion compensation
x1to2 = warp_img(tf.shape(x2)[0], x2, x1, False)
x3to2 = warp_img(tf.shape(x2)[0], x2, x3, True)
### Flow loss
FlowLoss_1 = cal_MSE(x1to2, x2)
FlowLoss_2 = cal_MSE(x3to2, x2)
flow_loss = FlowLoss_1 + FlowLoss_2
### Enhance cmp frames
x2_enhanced = net_MFCNN.network(x1to2, x2, x3to2, is_training)
MSE = cal_MSE(x2_enhanced, x5)
PSNR = cal_PSNR(x2_enhanced, x5) # PSNR after enhancement (enhanced and raw)
delta_PSNR = PSNR - PSNR_0
### 2 kinds of loss for 2-step training
OptimizeLoss_1 = flow_loss + ratio_small * MSE # step1: the key is MC-subnet.
OptimizeLoss_2 = ratio_small * flow_loss + MSE # step2: the key is QE-subnet.
### Defind optimizer
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
Training_step1 = tf.train.AdamOptimizer(lr_ori).minimize(OptimizeLoss_1)
Training_step2 = tf.train.AdamOptimizer(lr_ori).minimize(OptimizeLoss_2)
### TensorBoard
tf.summary.scalar('MSE loss of motion compensation', flow_loss)
tf.summary.scalar('MSE loss of final quality enhancement', MSE)
tf.summary.scalar('MSE loss for training step1 (mainly MC-subnet)', OptimizeLoss_1)
tf.summary.scalar('MSE loss for training step2 (mainly QE-subnet)', OptimizeLoss_2)
tf.summary.scalar('PSNR before enhancement', PSNR_0)
tf.summary.scalar('PSNR after enhancement', PSNR)
tf.summary.scalar('PSNR improvement', delta_PSNR)
tf.summary.image('cmp', x2)
tf.summary.image('x1to2', x1to2)
tf.summary.image('x3to2', x3to2)
tf.summary.image('enhanced', x2_enhanced)
tf.summary.image('raw', x5)
summary_writer = tf.summary.FileWriter(dir_model, sess.graph)
summary_op = tf.summary.merge_all()
saver = tf.train.Saver(max_to_keep=None) # define a saver
sess.run(tf.global_variables_initializer()) # initialize network variables
### Calculate and present the num of parameters
num_params = 0
for variable in tf.trainable_variables():
shape = variable.get_shape()
num_params += reduce(mul, [dim.value for dim in shape], 1)
print("# num of parameters: %d #" % num_params)
file_object.write("# num of parameters: %d #\n" % num_params)
file_object.flush()
### Find all stacks then cal their number
stack_name = os.path.join(dir_stack, "stack_tra_pre_*")
num_TrainingStack = len(glob.glob(stack_name))
stack_name = os.path.join(dir_stack, "stack_val_pre_*")
num_ValidationStack = len(glob.glob(stack_name))
print("##### Start running! #####")
num_TrainingBatch_count = 0
### Step 1: converge MC-subnet; Step 2: converge QE-subnet
for ite_step in [1,2]:
if ite_step == 1:
num_epoch = epoch_step1
else:
num_epoch = epoch_step2