提交 542c8839 编写于 作者: L lijianshe02

support variable length input

上级 cac2fbdf
......@@ -19,6 +19,7 @@ INFER:
number_frames: 5
batch_size: 1
file_root: "/workspace/color/input_frames"
#file_root: "/workspace/video_test/video/data/dataset/edvr/REDS4/sharp_bicubic"
#gt_root: "/workspace/video_test/video/data/dataset/edvr/REDS4/GT"
use_flip: False
use_rot: False
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import os
import sys
import time
import logging
import argparse
import ast
import numpy as np
try:
import cPickle as pickle
except:
import pickle
import paddle.fluid as fluid
from utils.config_utils import *
import models
from reader import get_reader
#from metrics import get_metrics
from utils.utility import check_cuda
logging.root.handlers = []
FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT, stream=sys.stdout)
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_name',
type=str,
default='AttentionCluster',
help='name of model to train.')
parser.add_argument(
'--config',
type=str,
default='configs/attention_cluster.txt',
help='path to config file of model')
parser.add_argument(
'--use_gpu',
type=ast.literal_eval,
default=True,
help='default use gpu.')
parser.add_argument(
'--weights',
type=str,
default=None,
help='weight path, None to automatically download weights provided by Paddle.'
)
parser.add_argument(
'--batch_size',
type=int,
default=1,
help='sample number in a batch for inference.')
parser.add_argument(
'--save_dir',
type=str,
default='./',
help='directory to store model and params file')
args = parser.parse_args()
return args
def save_inference_model(args):
# parse config
config = parse_config(args.config)
infer_config = merge_configs(config, 'infer', vars(args))
print_configs(infer_config, "Infer")
infer_model = models.get_model(args.model_name, infer_config, mode='infer')
infer_model.build_input(use_dataloader=False)
infer_model.build_model()
infer_feeds = infer_model.feeds()
infer_outputs = infer_model.outputs()
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
if args.weights:
assert os.path.exists(
args.weights), "Given weight dir {} not exist.".format(args.weights)
# if no weight files specified, download weights from paddle
weights = args.weights or infer_model.get_weights()
infer_model.load_test_weights(exe, weights,
fluid.default_main_program(), place)
if not os.path.isdir(args.save_dir):
os.makedirs(args.save_dir)
# saving inference model
fluid.io.save_inference_model(
args.save_dir,
feeded_var_names=[item.name for item in infer_feeds],
target_vars=infer_outputs,
executor=exe,
main_program=fluid.default_main_program(),
model_filename=args.model_name + "_model.pdmodel",
params_filename=args.model_name + "_params.pdparams")
print("save inference model at %s" % (args.save_dir))
if __name__ == "__main__":
args = parse_args()
# check whether the installed paddle is compiled with GPU
check_cuda(args.use_gpu)
logger.info(args)
save_inference_model(args)
from .model import regist_model, get_model
from .edvr import EDVR
regist_model("EDVR", EDVR)
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import math
import paddle.fluid as fluid
from paddle.fluid import ParamAttr
from ..model import ModelBase
from .edvr_model import EDVRModel
import logging
logger = logging.getLogger(__name__)
__all__ = ["EDVR"]
class EDVR(ModelBase):
def __init__(self, name, cfg, mode='train'):
super(EDVR, self).__init__(name, cfg, mode=mode)
self.get_config()
def get_config(self):
self.num_filters = self.get_config_from_sec('model', 'num_filters')
self.num_frames = self.get_config_from_sec('model', 'num_frames')
self.dcn_groups = self.get_config_from_sec('model', 'deform_conv_groups')
self.front_RBs = self.get_config_from_sec('model', 'front_RBs')
self.back_RBs = self.get_config_from_sec('model', 'back_RBs')
self.center = self.get_config_from_sec('model', 'center', 2)
self.predeblur = self.get_config_from_sec('model', 'predeblur', False)
self.HR_in = self.get_config_from_sec('model', 'HR_in', False)
self.w_TSA = self.get_config_from_sec('model', 'w_TSA', True)
self.crop_size = self.get_config_from_sec(self.mode, 'crop_size')
self.scale = self.get_config_from_sec(self.mode, 'scale', 1)
self.num_gpus = self.get_config_from_sec(self.mode, 'num_gpus', 8)
self.batch_size = self.get_config_from_sec(self.mode, 'batch_size', 256)
# get optimizer related parameters
self.base_learning_rate = self.get_config_from_sec('train', 'base_learning_rate')
self.l2_weight_decay = self.get_config_from_sec('train', 'l2_weight_decay')
self.T_periods = self.get_config_from_sec('train', 'T_periods')
self.restarts = self.get_config_from_sec('train', 'restarts')
self.weights = self.get_config_from_sec('train', 'weights')
self.eta_min = self.get_config_from_sec('train', 'eta_min')
self.TSA_only = self.get_config_from_sec('train', 'TSA_only', False)
def build_input(self, use_dataloader=True):
if self.mode != 'test':
gt_shape = [None, 3, self.crop_size, self.crop_size]
else:
gt_shape = [None, 3, 720, 1280]
if self.HR_in:
img_shape = [-1, self.num_frames, 3, self.crop_size, self.crop_size]
else:
if (self.mode != 'test') and (self.mode != 'infer') :
img_shape = [None, self.num_frames, 3, \
int(self.crop_size / self.scale), int(self.crop_size / self.scale)]
else:
img_shape = [None, self.num_frames, 3, 360, 472] #180, 320]
self.use_dataloader = use_dataloader
image = fluid.data(name='LQ_IMGs', shape=img_shape, dtype='float32')
if self.mode != 'infer':
label = fluid.data(name='GT_IMG', shape=gt_shape, dtype='float32')
else:
label = None
if use_dataloader:
assert self.mode != 'infer', \
'dataloader is not recommendated when infer, please set use_dataloader to be false.'
self.dataloader = fluid.io.DataLoader.from_generator(
feed_list=[image, label], capacity=4, iterable=True)
self.feature_input = [image]
self.label_input = label
def create_model_args(self):
cfg = {}
cfg['nf'] = self.num_filters
cfg['nframes'] = self.num_frames
cfg['groups'] = self.dcn_groups
cfg['front_RBs'] = self.front_RBs
cfg['back_RBs'] = self.back_RBs
cfg['center'] = self.center
cfg['predeblur'] = self.predeblur
cfg['HR_in'] = self.HR_in
cfg['w_TSA'] = self.w_TSA
cfg['mode'] = self.mode
cfg['TSA_only'] = self.TSA_only
return cfg
def build_model(self):
cfg = self.create_model_args()
videomodel = EDVRModel(**cfg)
out = videomodel.net(self.feature_input[0])
self.network_outputs = [out]
def optimizer(self):
assert self.mode == 'train', "optimizer only can be get in train mode"
learning_rate = get_lr(base_lr = self.base_learning_rate,
T_periods=self.T_periods,
restarts=self.restarts,
weights=self.weights,
eta_min=self.eta_min)
l2_weight_decay = self.l2_weight_decay
optimizer = fluid.optimizer.Adam(
learning_rate = learning_rate,
beta1 = 0.9,
beta2 = 0.99,
regularization=fluid.regularizer.L2Decay(l2_weight_decay))
return optimizer
def loss(self):
assert self.mode != 'infer', "invalid loss calculationg in infer mode"
pred = self.network_outputs[0]
label = self.label_input
epsilon = 1e-6
diff = pred - label
diff = diff * diff + epsilon
diff = fluid.layers.sqrt(diff)
self.loss_ = fluid.layers.reduce_sum(diff)
return self.loss_
def outputs(self):
return self.network_outputs
def feeds(self):
return self.feature_input if self.mode == 'infer' else self.feature_input + [
self.label_input
]
def fetches(self):
if self.mode == 'train' or self.mode == 'valid':
losses = self.loss()
fetch_list = [losses, self.network_outputs[0], self.label_input]
elif self.mode == 'test':
losses = self.loss()
fetch_list = [losses, self.network_outputs[0], self.label_input]
elif self.mode == 'infer':
fetch_list = self.network_outputs
else:
raise NotImplementedError('mode {} not implemented'.format(
self.mode))
return fetch_list
def pretrain_info(self):
return (
None,
None
)
def weights_info(self):
return (
None,
None
)
def load_pretrain_params0(self, exe, pretrain, prog, place):
"""load pretrain form .npz which is created by torch"""
def is_parameter(var):
return isinstance(var, fluid.framework.Parameter)
params_list = list(filter(is_parameter, prog.list_vars()))
import numpy as np
state_dict = np.load(pretrain)
for p in params_list:
if p.name in state_dict.keys():
print('########### load param {} from file'.format(p.name))
else:
print('----------- param {} not in file'.format(p.name))
fluid.set_program_state(prog, state_dict)
print('load pretrain from ', pretrain)
def load_test_weights(self, exe, weights, prog, place):
"""load weights from .npz which is created by torch"""
def is_parameter(var):
return isinstance(var, fluid.framework.Parameter)
params_list = list(filter(is_parameter, prog.list_vars()))
import numpy as np
state_dict = np.load(weights)
for p in params_list:
if p.name in state_dict.keys():
print('########### load param {} from file'.format(p.name))
else:
print('----------- param {} not in file'.format(p.name))
fluid.set_program_state(prog, state_dict)
print('load weights from ', weights)
# This is for learning rate cosine annealing restart
Dtype='float32'
def decay_step_counter(begin=0):
# the first global step is zero in learning rate decay
global_step = fluid.layers.autoincreased_step_counter(
counter_name='@LR_DECAY_COUNTER@', begin=begin, step=1)
return global_step
def get_lr(base_lr = 0.001,
T_periods = [250000, 250000, 250000, 250000],
restarts = [250000, 500000, 750000],
weights=[1, 1, 1],
eta_min=0):
with fluid.default_main_program()._lr_schedule_guard():
global_step = decay_step_counter()
lr = fluid.layers.create_global_var(shape=[1], value=base_lr, dtype=Dtype, persistable=True, name="learning_rate")
num_segs = len(restarts)
restart_point = 0
with fluid.layers.Switch() as switch:
with switch.case(global_step == 0):
pass
for i in range(num_segs):
T_max = T_periods[i]
weight = weights[i]
with switch.case(global_step < restarts[i]):
with fluid.layers.Switch() as switch_second:
value_2Tmax = fluid.layers.fill_constant(shape=[1], dtype='int64', value=2*T_max)
step_checker = global_step-restart_point-1-T_max
with switch_second.case(fluid.layers.elementwise_mod(step_checker, value_2Tmax)==0):
var_value = lr + (base_lr - eta_min) * (1 - math.cos(math.pi / float(T_max))) / 2
fluid.layers.assign(var_value, lr)
with switch_second.default():
double_step = fluid.layers.cast(global_step, dtype='float64') - float(restart_point)
double_scale = (1 + fluid.layers.cos(math.pi * double_step / float(T_max))) / \
(1 + fluid.layers.cos(math.pi * (double_step - 1) / float(T_max)))
float_scale = fluid.layers.cast(double_scale, dtype=Dtype)
var_value = float_scale * (lr - eta_min) + eta_min
fluid.layers.assign(var_value, lr)
with switch.case(global_step == restarts[i]):
var_value = fluid.layers.fill_constant(
shape=[1], dtype=Dtype, value=float(base_lr*weight))
fluid.layers.assign(var_value, lr)
restart_point = restarts[i]
T_max = T_periods[num_segs]
with switch.default():
with fluid.layers.Switch() as switch_second:
value_2Tmax = fluid.layers.fill_constant(shape=[1], dtype='int64', value=2*T_max)
step_checker = global_step-restart_point-1-T_max
with switch_second.case(fluid.layers.elementwise_mod(step_checker, value_2Tmax)==0):
var_value = lr + (base_lr - eta_min) * (1 - math.cos(math.pi / float(T_max))) / 2
fluid.layers.assign(var_value, lr)
with switch_second.default():
double_step = fluid.layers.cast(global_step, dtype='float64') - float(restart_point)
double_scale = (1 + fluid.layers.cos(math.pi * double_step / float(T_max))) / \
(1 + fluid.layers.cos(math.pi * (double_step - 1) / float(T_max)))
float_scale = fluid.layers.cast(double_scale, dtype=Dtype)
var_value = float_scale * (lr - eta_min) + eta_min
fluid.layers.assign(var_value, lr)
return lr
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import os
import time
import sys
import paddle.fluid as fluid
import math
def DCNPack(fea_and_offset, num_filters, kernel_size, stride=1, padding=0,
dilation=1, deformable_groups=1, extra_offset_mask=True, name=None, local_lr=1.0):
# create offset and mask similar to EDVR
# To be added
if isinstance(kernel_size, int):
kernel_size = [kernel_size, kernel_size]
if name is None:
conv_offset_mask_name = None
dcn_name = None
else:
conv_offset_mask_name = name + '_conv_offset_mask'
dcn_name = name + '_dcn'
if extra_offset_mask:
out = fluid.layers.conv2d(fea_and_offset[1], deformable_groups * 3 * kernel_size[0] * kernel_size[1],
kernel_size, stride, padding, name = conv_offset_mask_name,
param_attr=fluid.ParamAttr(name=conv_offset_mask_name+'.w_0',
initializer=fluid.initializer.ConstantInitializer(value=0.0),
learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name=conv_offset_mask_name+'.b_0',
initializer=fluid.initializer.ConstantInitializer(value=0.0),
learning_rate=local_lr))
x = fea_and_offset[0]
else:
x = fea_and_offset
out = fluid.layers.conv2d(x, deformable_groups * 3 * kernel_size[0] * kernel_size[1],
kernel_size, stride, padding, name = conv_offset_mask_name,
param_attr=fluid.ParamAttr(name=conv_offset_mask_name+'.w_0',
initializer=fluid.initializer.Constant(value=0.0),
learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name=conv_offset_mask_name+'.b_0',
initializer=fluid.initializer.Constant(value=0.0),
learning_rate=local_lr))
total_channels = deformable_groups * 3 * kernel_size[0] * kernel_size[1]
split_channels = total_channels // 3
o1 = out[:, 0:split_channels, :, :]
o2 = out[:, split_channels:split_channels*2, :, :]
mask = out[:, split_channels*2:, :, :]
#o1 = out[:, 0::3, :, :]
#o2 = out[:, 1::3, :, :]
#mask = out[:, 2::3, :, :]
offset = fluid.layers.concat([o1, o2], axis = 1)
mask = fluid.layers.sigmoid(mask)
#x = fluid.layers.Print(x, message='dcn_x')
#offset = fluid.layers.Print(offset, message='dcn_offset')
#mask = fluid.layers.Print(mask, message='dcn_mask')
y = fluid.layers.deformable_conv(x, offset, mask, num_filters, kernel_size, stride=stride, padding=padding,
deformable_groups=deformable_groups, modulated=True, name=dcn_name, im2col_step=1,
param_attr=fluid.ParamAttr(name=dcn_name+'.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name=dcn_name+'.b_0', learning_rate=local_lr))
#y = fluid.layers.Print(y, message='dcn_y')
return y
def PCD_Align(nbr_fea_l, ref_fea_l, nf=64, groups=8, local_lr=1.0):
# L3
L3_offset = fluid.layers.concat([nbr_fea_l[2], ref_fea_l[2]], axis = 1)
#L3_offset = fluid.layers.Print(L3_offset, message='L3_offset1')
L3_offset = fluid.layers.conv2d(L3_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L3_offset_conv1', \
param_attr=fluid.ParamAttr(name='PCD_Align_L3_offset_conv1.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L3_offset_conv1.b_0', learning_rate=local_lr))
L3_offset = fluid.layers.leaky_relu(L3_offset, alpha=0.1)
L3_offset = fluid.layers.conv2d(L3_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L3_offset_conv2', \
param_attr=fluid.ParamAttr(name='PCD_Align_L3_offset_conv2.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L3_offset_conv2.b_0', learning_rate=local_lr))
L3_offset = fluid.layers.leaky_relu(L3_offset, alpha=0.1)
#L3_offset = fluid.layers.Print(L3_offset, message="PCD_Align L3_offset", summarize=20)
L3_fea = DCNPack([nbr_fea_l[2], L3_offset], nf, 3, stride=1, \
padding=1, deformable_groups = groups, name='PCD_Align_L3_dcn', local_lr=local_lr)
#L3_offset = fluid.layers.Print(L3_offset, message='L3_offset2')
L3_fea = fluid.layers.leaky_relu(L3_fea, alpha=0.1)
#L3_fea = fluid.layers.Print(L3_fea, message="PCD_Align L3_fea", summarize=20)
# L2
L2_offset = fluid.layers.concat([nbr_fea_l[1], ref_fea_l[1]], axis = 1)
L2_offset = fluid.layers.conv2d(L2_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L2_offset_conv1',
param_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv1.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv1.b_0', learning_rate=local_lr))
L2_offset = fluid.layers.leaky_relu(L2_offset, alpha=0.1)
L3_offset = fluid.layers.resize_bilinear(L3_offset, scale=2, align_corners=False, align_mode=0)
#L3_offset = fluid.layers.Print(L3_offset, message='L3_offset3')
L2_offset = fluid.layers.concat([L2_offset, L3_offset * 2], axis=1)
L2_offset = fluid.layers.conv2d(L2_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L2_offset_conv2',
param_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv2.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv2.b_0', learning_rate=local_lr))
L2_offset = fluid.layers.leaky_relu(L2_offset, alpha=0.1)
L2_offset = fluid.layers.conv2d(L2_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L2_offset_conv3',
param_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv3.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L2_offset_conv3.b_0', learning_rate=local_lr))
L2_offset = fluid.layers.leaky_relu(L2_offset, alpha=0.1)
L2_fea = DCNPack([nbr_fea_l[1], L2_offset], nf, 3, stride=1, \
padding=1, deformable_groups = groups, name='PCD_Align_L2_dcn', local_lr=local_lr)
#L2_fea = fluid.layers.Print(L2_fea, message="L2_fea_after_dcn", summarize=20)
L3_fea = fluid.layers.resize_bilinear(L3_fea, scale=2, align_corners=False, align_mode=0)
#L3_fea = fluid.layers.Print(L3_fea, message="L3_fea_after_resize", summarize=20)
L2_fea = fluid.layers.concat([L2_fea, L3_fea], axis=1)
L2_fea = fluid.layers.conv2d(L2_fea, nf, 3, stride=1, padding=1, name='PCD_Align_L2_fea_conv',
param_attr=fluid.ParamAttr(name='PCD_Align_L2_fea_conv.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L2_fea_conv.b_0', learning_rate=local_lr))
L2_fea = fluid.layers.leaky_relu(L2_fea, alpha=0.1)
# L1
L1_offset = fluid.layers.concat([nbr_fea_l[0], ref_fea_l[0]], axis=1)
L1_offset = fluid.layers.conv2d(L1_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L1_offset_conv1',
param_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv1.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv1.b_0', learning_rate=local_lr))
L1_offset = fluid.layers.leaky_relu(L1_offset, alpha=0.1)
L2_offset = fluid.layers.resize_bilinear(L2_offset, scale=2, align_corners=False, align_mode=0)
L1_offset = fluid.layers.concat([L1_offset, L2_offset * 2], axis=1)
L1_offset = fluid.layers.conv2d(L1_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L1_offset_conv2',
param_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv2.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv2.b_0', learning_rate=local_lr))
L1_offset = fluid.layers.leaky_relu(L1_offset, alpha=0.1)
L1_offset = fluid.layers.conv2d(L1_offset, nf, 3, stride=1, padding=1, name='PCD_Align_L1_offset_conv3',
param_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv3.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L1_offset_conv3.b_0', learning_rate=local_lr))
L1_offset = fluid.layers.leaky_relu(L1_offset, alpha=0.1)
L1_fea = DCNPack([nbr_fea_l[0], L1_offset], nf, 3, stride=1, padding=1, \
deformable_groups = groups, name='PCD_Align_L1_dcn', local_lr=local_lr) # this output is consistent
#L1_fea = fluid.layers.Print(L1_fea, message="PCD_Align_L1_dcn", summarize=20)
#L2_fea = fluid.layers.Print(L2_fea, message="L2_fea_before_resize", summarize=20)
L2_fea = fluid.layers.resize_bilinear(L2_fea, scale=2, align_corners=False, align_mode=0)
#L2_fea = fluid.layers.Print(L2_fea, message="L2_fea_after_resize", summarize=20)
L1_fea = fluid.layers.concat([L1_fea, L2_fea], axis=1)
L1_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=1, padding=1, name='PCD_Align_L1_fea_conv',
param_attr=fluid.ParamAttr(name='PCD_Align_L1_fea_conv.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_L1_fea_conv.b_0', learning_rate=local_lr))
#L1_fea = fluid.layers.Print(L1_fea, message="PCD_Align_L1_fea_conv", summarize=20)
# cascade
offset = fluid.layers.concat([L1_fea, ref_fea_l[0]], axis=1)
offset = fluid.layers.conv2d(offset, nf, 3, stride=1, padding=1, name='PCD_Align_cas_offset_conv1',
param_attr=fluid.ParamAttr(name='PCD_Align_cas_offset_conv1.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_cas_offset_conv1.b_0', learning_rate=local_lr))
offset = fluid.layers.leaky_relu(offset, alpha=0.1)
offset = fluid.layers.conv2d(offset, nf, 3, stride=1, padding=1, name='PCD_Align_cas_offset_conv2',
param_attr=fluid.ParamAttr(name='PCD_Align_cas_offset_conv2.w_0', learning_rate=local_lr),
bias_attr=fluid.ParamAttr(name='PCD_Align_cas_offset_conv2.b_0', learning_rate=local_lr))
offset = fluid.layers.leaky_relu(offset, alpha=0.1)
L1_fea = DCNPack([L1_fea, offset], nf, 3, stride=1, padding=1, \
deformable_groups = groups, name='PCD_Align_cascade_dcn', local_lr=local_lr) #this L1_fea is different
L1_fea = fluid.layers.leaky_relu(L1_fea, alpha=0.1)
#L1_fea = fluid.layers.Print(L1_fea, message="PCD_Align L1_fea output", summarize=20)
return L1_fea
def TSA_Fusion(aligned_fea, nf=64, nframes=5, center=2):
# In actual fact, nf == C should be required
B, N, C, H, W = aligned_fea.shape
# temporal fusion
x_center = aligned_fea[:, center, :, :, :]
emb_rf = fluid.layers.conv2d(x_center, nf, 3, stride=1, padding=1, name='tAtt_2')
emb = fluid.layers.reshape(aligned_fea, [-1, C, H, W])
emb = fluid.layers.conv2d(emb, nf, 3, stride=1, padding=1, name='tAtt_1')
emb = fluid.layers.reshape(emb, [-1, N, nf, H, W])
cor_l = []
for i in range(N):
emb_nbr = emb[:, i, :, :, :]
cor_tmp = fluid.layers.reduce_sum(emb_nbr * emb_rf, dim=1, keep_dim = True)
cor_l.append(cor_tmp)
cor_prob = fluid.layers.concat(cor_l, axis=1)
cor_prob = fluid.layers.sigmoid(cor_prob)
cor_prob = fluid.layers.unsqueeze(cor_prob, axes=2)
#cor_prob = fluid.layers.expand(cor_prob, [1, 1, C, 1, 1])
cor_prob = fluid.layers.expand(cor_prob, [1, 1, nf, 1, 1])
#cor_prob = fluid.layers.reshape(cor_prob, [-1, N*C, H, W])
cor_prob = fluid.layers.reshape(cor_prob, [-1, N*nf, H, W])
aligned_fea = fluid.layers.reshape(aligned_fea, [-1, N*C, H, W])
aligned_fea = aligned_fea * cor_prob
#aligned_fea = fluid.layers.Print(aligned_fea, message="aligned_fea temporal0", summarize=20)
fea = fluid.layers.conv2d(aligned_fea, nf, 1, stride=1, padding=0, name='fea_fusion')
fea = fluid.layers.leaky_relu(fea, alpha=0.1)
#fea = fluid.layers.Print(fea, message="aligned_fea temporal", summarize=20)
# spatial fusion
att = fluid.layers.conv2d(aligned_fea, nf, 1, stride=1, padding=0, name='sAtt_1')
att = fluid.layers.leaky_relu(att, alpha=0.1)
#att = fluid.layers.Print(att, message="sAtt_1", summarize=20)
att_max = fluid.layers.pool2d(att, pool_size=3, pool_stride=2, pool_padding=1, pool_type='max')
att_avg = fluid.layers.pool2d(att, pool_size=3, pool_stride=2, pool_padding=1, pool_type='avg')
att_pool = fluid.layers.concat([att_max, att_avg], axis=1)
#att_pool = fluid.layers.Print(att_pool, message="att cat", summarize=20)
att = fluid.layers.conv2d(att_pool, nf, 1, stride=1, padding=0, name='sAtt_2')
#att = fluid.layers.Print(att, message="att sAtt2", summarize=20)
att = fluid.layers.leaky_relu(att, alpha=0.1)
#att = fluid.layers.Print(att, message="att spatial fusion", summarize=20)
# pyramid
att_L = fluid.layers.conv2d(att, nf, 1, stride=1, padding=0, name='sAtt_L1')
att_L = fluid.layers.leaky_relu(att_L, alpha=0.1)
#att_L = fluid.layers.Print(att_L, message="sAtt_L1", summarize=20)
att_max = fluid.layers.pool2d(att_L, pool_size=3, pool_stride=2, pool_padding=1, pool_type='max')
att_avg = fluid.layers.pool2d(att_L, pool_size=3, pool_stride=2, pool_padding=1, pool_type='avg')
att_pool = fluid.layers.concat([att_max, att_avg], axis=1)
att_L = fluid.layers.conv2d(att_pool, nf, 3, stride=1, padding=1, name='sAtt_L2')
att_L = fluid.layers.leaky_relu(att_L, alpha=0.1)
att_L = fluid.layers.conv2d(att_L, nf, 3, stride=1, padding=1, name='sAtt_L3')
att_L = fluid.layers.leaky_relu(att_L, alpha=0.1)
#att_L = fluid.layers.Print(att_L, message="att_L before resize", summarize=20)
att_L = fluid.layers.resize_bilinear(att_L, scale=2, align_corners=False, align_mode=0)
#att_L = fluid.layers.Print(att_L, message="att_L", summarize=20)
att = fluid.layers.conv2d(att, nf, 3, stride=1, padding=1, name='sAtt_3')
att = fluid.layers.leaky_relu(att, alpha=0.1)
att = att + att_L
att = fluid.layers.conv2d(att, nf, 1, stride=1, padding=0, name='sAtt_4')
att = fluid.layers.leaky_relu(att, alpha=0.1)
att = fluid.layers.resize_bilinear(att, scale=2, align_corners=False, align_mode=0)
att = fluid.layers.conv2d(att, nf, 3, stride=1, padding=1, name='sAtt_5')
att_add = fluid.layers.conv2d(att, nf, 1, stride=1, padding=0, name='sAtt_add_1')
att_add = fluid.layers.leaky_relu(att_add, alpha=0.1)
att_add = fluid.layers.conv2d(att_add, nf, 1, stride=1, padding=0, name='sAtt_add_2')
att = fluid.layers.sigmoid(att)
#att = fluid.layers.Print(att, message="att", summarize=20)
fea = fea * att * 2 + att_add
return fea
def get_initializer(fan_in, scale=0.1):
std = math.sqrt(2.0/fan_in) * scale
return fluid.initializer.NormalInitializer(loc=0.0, scale=std)
def ResidualBlock_noBN(x, nf=64, name='', local_lr=1.0):
# in the pytorch code, conv1 and conv2 are initialized with scale factor 0.1 than MSRA,
# thi will be added later.
fan_in = x.shape[1] * 3 * 3
out = fluid.layers.conv2d(x, nf, 3, stride=1, padding=1, name=name+'_conv1', act='relu',
param_attr = fluid.ParamAttr(initializer=get_initializer(fan_in, scale=0.1),
learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
fan_in = out.shape[1] * 3 * 3
out = fluid.layers.conv2d(out, nf, 3, stride=1, padding=1, name=name+'_conv2',
param_attr = fluid.ParamAttr(initializer=get_initializer(fan_in, scale=0.1),
learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
return out + x
def MakeMultiBlocks(x, func, num_layers, nf=64, name='', local_lr=1.0):
for i in range(num_layers):
x = func(x, nf=nf, name=name+"_block%d"%i, local_lr=local_lr)
#x = fluid.layers.Print(x, message=name+"_block%d"%i, summarize=20)
return x
def EDVRArch(x, nf=64, nframes=5, groups=8, front_RBs=5, back_RBs=10, center=None,
predeblur=False, HR_in=False, w_TSA=True, TSA_only=False):
B, N, C, H, W = x.shape
center = nframes//2 if center is None else center
x_center = x[:, center, :, :, :]
local_lr = 1.0 - float(TSA_only)
if predeblur:
# not implemented yet
pass
else:
if HR_in:
L1_fea = fluid.layers.reshape(x, [-1, C, H, W])
L1_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=1, padding=1, name='conv_first_1',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L1_fea = fluid.layers.leaky_relu(L1_fea, alpha=0.1)
L1_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=2, padding=1, name='conv_first_2',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L1_fea = fluid.layers.leaky_relu(L1_fea, alpha=0.1)
L1_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=2, padding=1, name='conv_first_3',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L1_fea = fluid.layers.leaky_relu(L1_fea, alpha=0.1)
H = H // 4
W = W // 4
else:
L1_fea = fluid.layers.reshape(x, [-1, C, H, W])
L1_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=1, padding=1, name='conv_first',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L1_fea = fluid.layers.leaky_relu(L1_fea, alpha=0.1)
#L1_fea = fluid.layers.Print(L1_fea, message="L1_fea", summarize=20)
# feature extraction
L1_fea = MakeMultiBlocks(L1_fea, ResidualBlock_noBN, front_RBs, nf=nf, name='feature_extractor', local_lr = local_lr)
# L2
L2_fea = fluid.layers.conv2d(L1_fea, nf, 3, stride=2, padding=1, name='fea_L2_conv1',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L2_fea = fluid.layers.leaky_relu(L2_fea, alpha=0.1)
L2_fea = fluid.layers.conv2d(L2_fea, nf, 3, stride=1, padding=1, name='fea_L2_conv2',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L2_fea = fluid.layers.leaky_relu(L2_fea, alpha=0.1)
# L3
L3_fea = fluid.layers.conv2d(L2_fea, nf, 3, stride=2, padding=1, name='fea_L3_conv1',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L3_fea = fluid.layers.leaky_relu(L3_fea, alpha=0.1)
L3_fea = fluid.layers.conv2d(L3_fea, nf, 3, stride=1, padding=1, name='fea_L3_conv2',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
L3_fea = fluid.layers.leaky_relu(L3_fea, alpha=0.1)
L1_fea = fluid.layers.reshape(L1_fea, [-1, N, nf, H, W])
L2_fea = fluid.layers.reshape(L2_fea, [-1, N, nf, H//2, W//2])
L3_fea = fluid.layers.reshape(L3_fea, [-1, N, nf, H//4, W//4])
#L3_fea = fluid.layers.Print(L3_fea, message="L3_fea", summarize=20)
# pcd align
# ref_feature_list
ref_fea_l = [L1_fea[:, center, :, :, :], L2_fea[:, center, :, :, :], L3_fea[:, center, :, :, :]]
aligned_fea = []
for i in range(N):
nbr_fea_l = [L1_fea[:, i, :, :, :], L2_fea[:, i, :, :, :], L3_fea[:, i, :, :, :]]
aligned_fea.append(PCD_Align(nbr_fea_l, ref_fea_l, nf=nf, groups=groups, local_lr=local_lr))
if w_TSA:
aligned_fea = fluid.layers.stack(aligned_fea, axis=1) # [B, N, C, H, W]
#aligned_fea = fluid.layers.Print(aligned_fea, message="aligned_fea", summarize=20)
fea = TSA_Fusion(aligned_fea, nf=nf, nframes=nframes, center=center)
else:
aligned_fea = fluid.layers.concat(aligned_fea, axis=1) # [B, NC, H, W]
#aligned_fea = fluid.layers.Print(aligned_fea, message="aligned_fea", summarize=20)
fea = fluid.layers.conv2d(aligned_fea, nf, 1, stride=1, padding=0, name='tsa_fusion',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
# reconstructor
out = MakeMultiBlocks(fea, ResidualBlock_noBN, back_RBs, nf=nf, name='reconstructor', local_lr=local_lr)
#out = fluid.layers.Print(out, message="multiblocks_reconstructor", summarize=20)
out = fluid.layers.conv2d(out, nf*4, 3, stride=1, padding=1, name='upconv1',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
out = fluid.layers.pixel_shuffle(out, 2)
out = fluid.layers.leaky_relu(out, alpha=0.1)
out = fluid.layers.conv2d(out, 64*4, 3, stride=1, padding=1, name='upconv2',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
out = fluid.layers.pixel_shuffle(out, 2)
out = fluid.layers.leaky_relu(out, alpha=0.1)
out = fluid.layers.conv2d(out, 64, 3, stride=1, padding=1, name='HRconv',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
out = fluid.layers.leaky_relu(out, alpha=0.1)
out = fluid.layers.conv2d(out, 3, 3, stride=1, padding=1, name='conv_last',
param_attr=fluid.ParamAttr(learning_rate=local_lr),
bias_attr=fluid.ParamAttr(learning_rate=local_lr))
if HR_in:
base = x_center
else:
base = fluid.layers.resize_bilinear(x_center, scale=4, align_corners=False, align_mode=0)
out += base
#out = fluid.layers.Print(out, message="network output", summarize=20)
return out
class EDVRModel(object):
def __init__(self, nf=64, nframes=5, groups=8, front_RBs=5, back_RBs=10, center=None,
predeblur=False, HR_in=False, w_TSA=True, TSA_only=False, mode='train'):
self.nf = nf
self.nframes = nframes
self.groups = groups
self.front_RBs = front_RBs
self.back_RBs = back_RBs
self.center = center
self.predeblur = predeblur
self.HR_in = HR_in
self.w_TSA = w_TSA
self.mode = mode
self.TSA_only = TSA_only
def net(self, x):
return EDVRArch(x, nf = self.nf, nframes = self.nframes,
groups = self.groups,
front_RBs = self.front_RBs,
back_RBs = self.back_RBs,
center = self.center,
predeblur = self.predeblur,
HR_in = self.HR_in,
w_TSA = self.w_TSA,
TSA_only = self.TSA_only)
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import os
import wget
import logging
try:
from configparser import ConfigParser
except:
from ConfigParser import ConfigParser
import paddle.fluid as fluid
from .utils import download, AttrDict
WEIGHT_DIR = os.path.join(os.path.expanduser('~'), '.paddle', 'weights')
logger = logging.getLogger(__name__)
def is_parameter(var):
return isinstance(var, fluid.framework.Parameter)
class NotImplementError(Exception):
"Error: model function not implement"
def __init__(self, model, function):
super(NotImplementError, self).__init__()
self.model = model.__class__.__name__
self.function = function.__name__
def __str__(self):
return "Function {}() is not implemented in model {}".format(
self.function, self.model)
class ModelNotFoundError(Exception):
"Error: model not found"
def __init__(self, model_name, avail_models):
super(ModelNotFoundError, self).__init__()
self.model_name = model_name
self.avail_models = avail_models
def __str__(self):
msg = "Model {} Not Found.\nAvailiable models:\n".format(
self.model_name)
for model in self.avail_models:
msg += " {}\n".format(model)
return msg
class ModelBase(object):
def __init__(self, name, cfg, mode='train'):
assert mode in ['train', 'valid', 'test', 'infer'], \
"Unknown mode type {}".format(mode)
self.name = name
self.is_training = (mode == 'train')
self.mode = mode
self.cfg = cfg
self.dataloader = None
def build_model(self):
"build model struct"
raise NotImplementError(self, self.build_model)
def build_input(self, use_dataloader):
"build input Variable"
raise NotImplementError(self, self.build_input)
def optimizer(self):
"get model optimizer"
raise NotImplementError(self, self.optimizer)
def outputs():
"get output variable"
raise notimplementerror(self, self.outputs)
def loss(self):
"get loss variable"
raise notimplementerror(self, self.loss)
def feeds(self):
"get feed inputs list"
raise NotImplementError(self, self.feeds)
def fetches(self):
"get fetch list of model"
raise NotImplementError(self, self.fetches)
def weights_info(self):
"get model weight default path and download url"
raise NotImplementError(self, self.weights_info)
def get_weights(self):
"get model weight file path, download weight from Paddle if not exist"
path, url = self.weights_info()
path = os.path.join(WEIGHT_DIR, path)
if not os.path.isdir(WEIGHT_DIR):
logger.info('{} not exists, will be created automatically.'.format(
WEIGHT_DIR))
os.makedirs(WEIGHT_DIR)
if os.path.exists(path):
return path
logger.info("Download weights of {} from {}".format(self.name, url))
wget.download(url, path)
return path
def dataloader(self):
return self.dataloader
def epoch_num(self):
"get train epoch num"
return self.cfg.TRAIN.epoch
def pretrain_info(self):
"get pretrain base model directory"
return (None, None)
def get_pretrain_weights(self):
"get model weight file path, download weight from Paddle if not exist"
path, url = self.pretrain_info()
if not path:
return None
path = os.path.join(WEIGHT_DIR, path)
if not os.path.isdir(WEIGHT_DIR):
logger.info('{} not exists, will be created automatically.'.format(
WEIGHT_DIR))
os.makedirs(WEIGHT_DIR)
if os.path.exists(path):
return path
logger.info("Download pretrain weights of {} from {}".format(self.name,
url))
download(url, path)
return path
def load_pretrain_params(self, exe, pretrain, prog, place):
logger.info("Load pretrain weights from {}".format(pretrain))
state_dict = fluid.load_program_state(pretrain)
fluid.set_program_state(prog, state_dict)
def load_test_weights(self, exe, weights, prog, place):
params_list = list(filter(is_parameter, prog.list_vars()))
fluid.load(prog, weights, executor=exe, var_list=params_list)
def get_config_from_sec(self, sec, item, default=None):
if sec.upper() not in self.cfg:
return default
return self.cfg[sec.upper()].get(item, default)
class ModelZoo(object):
def __init__(self):
self.model_zoo = {}
def regist(self, name, model):
assert model.__base__ == ModelBase, "Unknow model type {}".format(
type(model))
self.model_zoo[name] = model
def get(self, name, cfg, mode='train'):
for k, v in self.model_zoo.items():
if k.upper() == name.upper():
return v(name, cfg, mode)
raise ModelNotFoundError(name, self.model_zoo.keys())
# singleton model_zoo
model_zoo = ModelZoo()
def regist_model(name, model):
model_zoo.regist(name, model)
def get_model(name, cfg, mode='train'):
return model_zoo.get(name, cfg, mode)
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import os
import wget
import tarfile
__all__ = ['decompress', 'download', 'AttrDict']
def decompress(path):
t = tarfile.open(path)
t.extractall(path=os.path.split(path)[0])
t.close()
os.remove(path)
def download(url, path):
weight_dir = os.path.split(path)[0]
if not os.path.exists(weight_dir):
os.makedirs(weight_dir)
path = path + ".tar.gz"
wget.download(url, path)
decompress(path)
class AttrDict(dict):
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, value):
if key in self.__dict__:
self.__dict__[key] = value
else:
self[key] = value
......@@ -27,7 +27,7 @@ import paddle.fluid as fluid
import cv2
from utils.config_utils import *
import models
#import models
from reader import get_reader
#from metrics import get_metrics
from utils.utility import check_cuda
......@@ -112,7 +112,7 @@ def infer(args):
infer_config = merge_configs(config, 'infer', vars(args))
print_configs(infer_config, "Infer")
model_path = '/workspace/video_test/video/for_eval/data/inference_model'
model_path = '/workspace/PaddleGAN/applications/EDVR/data/inference_model'
model_filename = 'EDVR_model.pdmodel'
params_filename = 'EDVR_params.pdparams'
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
......
......@@ -280,6 +280,7 @@ def read_img(path, size=None, is_gt=False):
#if not is_gt:
# #print(path)
# img = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
#print("path: ", path)
img = img.astype(np.float32) / 255.
if img.ndim == 2:
img = np.expand_dims(img, axis=2)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册