Ejemplo n.º 1
0
    def __init__(self,
                 output='output',
                 weight_path=None,
                 colorization=False,
                 reference_dir=None,
                 mindim=360):
        self.output = os.path.join(output, 'DeepRemaster')
        self.colorization = colorization
        self.reference_dir = reference_dir
        self.mindim = mindim

        if weight_path is None:
            weight_path = get_path_from_url(DEEPREMASTER_WEIGHT_URL)

        self.weight_path = weight_path

        state_dict = paddle.load(weight_path)

        self.modelR = NetworkR()
        self.modelR.load_dict(state_dict['modelR'])
        self.modelR.eval()
        if colorization:
            self.modelC = NetworkC()
            self.modelC.load_dict(state_dict['modelC'])
            self.modelC.eval()
    def __init__(self,
                 output='output',
                 weight_path=None,
                 config=None,
                 relative=False,
                 adapt_scale=False,
                 find_best_frame=False,
                 best_frame=None,
                 ratio=1.0,
                 filename='result.mp4'):
        if config is not None and isinstance(config, str):
            self.cfg = yaml.load(config, Loader=yaml.SafeLoader)
        elif isinstance(config, dict):
            self.cfg = config
        elif config is None:
            self.cfg = {
                'model_params': {
                    'common_params': {
                        'num_kp': 10,
                        'num_channels': 3,
                        'estimate_jacobian': True
                    },
                    'kp_detector_params': {
                        'temperature': 0.1,
                        'block_expansion': 32,
                        'max_features': 1024,
                        'scale_factor': 0.25,
                        'num_blocks': 5
                    },
                    'generator_params': {
                        'block_expansion': 64,
                        'max_features': 512,
                        'num_down_blocks': 2,
                        'num_bottleneck_blocks': 6,
                        'estimate_occlusion_map': True,
                        'dense_motion_params': {
                            'block_expansion': 64,
                            'max_features': 1024,
                            'num_blocks': 5,
                            'scale_factor': 0.25
                        }
                    }
                }
            }
            if weight_path is None:
                vox_cpk_weight_url = 'https://paddlegan.bj.bcebos.com/applications/first_order_model/vox-cpk.pdparams'
                weight_path = get_path_from_url(vox_cpk_weight_url)

        self.weight_path = weight_path
        if not os.path.exists(output):
            os.makedirs(output)
        self.output = output
        self.filename = filename
        self.relative = relative
        self.adapt_scale = adapt_scale
        self.find_best_frame = find_best_frame
        self.best_frame = best_frame
        self.ratio = ratio
        self.generator, self.kp_detector = self.load_checkpoints(
            self.cfg, self.weight_path)
    def __init__(self, output=None, weight_path=None):
        """
        output (str|None): output path, if None, do not write
            depth map to pfm and png file.
        weight_path (str|None): weight path, if None, load default
            MiDaSv2.1 model.
        """
        self.output_path = os.path.join(output, 'MiDaS') if output else None

        self.net_h, self.net_w = 384, 384
        if weight_path is None:
            midasv2_weight_url = 'https://paddlegan.bj.bcebos.com/applications/midas.pdparams'
            weight_path = get_path_from_url(midasv2_weight_url)
        self.weight_path = weight_path

        self.model = self.load_checkpoints()

        self.transform = Compose([
            Resize(
                self.net_w,
                self.net_h,
                resize_target=None,
                keep_aspect_ratio=True,
                ensure_multiple_of=32,
                resize_method="upper_bound",
                image_interpolation_method=cv2.INTER_CUBIC,
            ),
            NormalizeImage(mean=[0.485, 0.456, 0.406],
                           std=[0.229, 0.224, 0.225]),
            PrepareForNet(),
        ])
Ejemplo n.º 4
0
    def __init__(self, output='output', weight_path=None):
        self.input = input
        self.output = os.path.join(output, 'RealSR')
        self.model = RRDBNet(3, 3, 64, 23)
        if weight_path is None:
            weight_path = get_path_from_url(REALSR_WEIGHT_URL)

        state_dict = paddle.load(weight_path)
        self.model.load_dict(state_dict)
        self.model.eval()
    def __init__(self, output='output', weight_path=None):
        self.input = input
        self.output = os.path.join(output, 'EDVR')

        if weight_path is None:
            weight_path = get_path_from_url(EDVR_WEIGHT_URL)

        self.weight_path = weight_path

        self.build_inference_model()
Ejemplo n.º 6
0
    def __init__(self,
                 output='output',
                 weight_path=None,
                 artistic=False,
                 render_factor=32):
        self.output = os.path.join(output, 'DeOldify')
        if not os.path.exists(self.output):
            os.makedirs(self.output)
        self.render_factor = render_factor
        self.model = build_model(
            model_type='artistic' if artistic else 'stable')
        if weight_path is None:
            if artistic:
                weight_path = get_path_from_url(DEOLDIFY_ART_WEIGHT_URL)
            else:
                weight_path = get_path_from_url(DEOLDIFY_STABLE_WEIGHT_URL)

        state_dict = paddle.load(weight_path)
        self.model.load_dict(state_dict)
        self.model.eval()
Ejemplo n.º 7
0
    def __init__(self, output='output', weight_path=None, render_factor=32):
        # self.input = input
        self.output = os.path.join(output, 'DeOldify')
        self.render_factor = render_factor
        self.model = build_model()
        if weight_path is None:
            weight_path = get_path_from_url(DEOLDIFY_WEIGHT_URL)

        state_dict = paddle.load(weight_path)
        self.model.load_dict(state_dict)
        self.model.eval()
Ejemplo n.º 8
0
 def __init__(self, output_index: int = 26) -> None:
     super().__init__()
     arch = 'caffevgg19'
     weights_path = get_path_from_url(model_urls[arch][0],
                                      model_urls[arch][1])
     data_dict: dict = np.load(weights_path,
                               encoding='latin1',
                               allow_pickle=True).item()
     self.features = self.make_layers(self.cfg, data_dict)
     del data_dict
     self.features = nn.Sequential(
         *self.features.sublayers()[:output_index])
     mean = paddle.to_tensor([103.939, 116.779, 123.68])
     self.mean = mean.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
Ejemplo n.º 9
0
    def __init__(self,
                 output='output',
                 weight_path=None,
                 time_step=None,
                 use_gpu=True,
                 remove_duplicates=False):
        self.output_path = os.path.join(output, 'DAIN')
        if weight_path is None:
            weight_path = get_path_from_url(DAIN_WEIGHT_URL)

        self.weight_path = weight_path
        self.time_step = time_step
        self.key_frame_thread = 0
        self.remove_duplicates = remove_duplicates

        self.build_inference_model()
Ejemplo n.º 10
0
 def __init__(self,
              output_path='output_dir',
              weight_path=None,
              use_adjust_brightness=True):
     self.output_path = output_path
     self.input_size = (256, 256)
     self.use_adjust_brightness = use_adjust_brightness
     if weight_path is None:
         vox_cpk_weight_url = 'https://paddlegan.bj.bcebos.com/models/animeganv2_hayao.pdparams'
         weight_path = get_path_from_url(vox_cpk_weight_url)
     self.weight_path = weight_path
     self.generator = self.load_checkpoints()
     self.transform = T.Compose([
         ResizeToScale((256, 256), 32),
         T.Transpose(),
         T.Normalize([127.5, 127.5, 127.5], [127.5, 127.5, 127.5])
     ])
    def __init__(self,
                 output_path='output_dir',
                 weight_path=None,
                 model_type=None,
                 seed=None,
                 size=1024,
                 style_dim=512,
                 n_mlp=8,
                 channel_multiplier=2):
        self.output_path = output_path

        if weight_path is None and model_type != 'default':
            if model_type in model_cfgs.keys():
                weight_path = get_path_from_url(
                    model_cfgs[model_type]['model_urls'])
                size = model_cfgs[model_type].get('size', size)
                style_dim = model_cfgs[model_type].get('style_dim', style_dim)
                n_mlp = model_cfgs[model_type].get('n_mlp', n_mlp)
                channel_multiplier = model_cfgs[model_type].get(
                    'channel_multiplier', channel_multiplier)
                checkpoint = paddle.load(weight_path)
            else:
                raise ValueError(
                    'Predictor need a weight path or a pretrained model type')
        else:
            checkpoint = paddle.load(weight_path)

        opts = checkpoint.pop('opts')
        opts = AttrDict(opts)
        opts['size'] = size
        opts['style_dim'] = style_dim
        opts['n_mlp'] = n_mlp
        opts['channel_multiplier'] = channel_multiplier

        self.generator = Pixel2Style2Pixel(opts)
        self.generator.set_state_dict(checkpoint)
        self.generator.eval()

        if seed is not None:
            paddle.seed(seed)
            random.seed(seed)
            np.random.seed(seed)

        self.model_type = 'default' if model_type is None else model_type
Ejemplo n.º 12
0
    def __init__(
            self,
            layer_name_list,
            vgg_type='vgg19',
            use_input_norm=True,
            pretrained_url='https://paddlegan.bj.bcebos.com/model/vgg19.pdparams'
    ):
        super(PerceptualVGG, self).__init__()

        self.layer_name_list = layer_name_list
        self.use_input_norm = use_input_norm

        # get vgg model and load pretrained vgg weight
        _vgg = getattr(vgg, vgg_type)()

        if pretrained_url:
            weight_path = get_path_from_url(pretrained_url)
            state_dict = paddle.load(weight_path)
            _vgg.load_dict(state_dict)
            print('PerceptualVGG loaded pretrained weight.')

        num_layers = max(map(int, layer_name_list)) + 1
        assert len(_vgg.features) >= num_layers

        # only borrow layers that will be used from _vgg to avoid unused params
        self.vgg_layers = nn.Sequential(
            *list(_vgg.features.children())[:num_layers])

        if self.use_input_norm:
            # the mean is for image with range [0, 1]
            self.register_buffer(
                'mean',
                paddle.to_tensor([0.485, 0.456, 0.406]).reshape([1, 3, 1, 1]))
            # the std is for image with range [-1, 1]
            self.register_buffer(
                'std',
                paddle.to_tensor([0.229, 0.224, 0.225]).reshape([1, 3, 1, 1]))

        for v in self.vgg_layers.parameters():
            v.trainable = False
Ejemplo n.º 13
0
    def __init__(self,
                 output_path='output_dir',
                 weight_path=None,
                 model_type=None,
                 seed=None,
                 size=1024,
                 style_dim=512,
                 n_mlp=8,
                 channel_multiplier=2):
        self.output_path = output_path

        if weight_path is None:
            if model_type in model_cfgs.keys():
                weight_path = get_path_from_url(
                    model_cfgs[model_type]['model_urls'])
                size = model_cfgs[model_type].get('size', size)
                style_dim = model_cfgs[model_type].get('style_dim', style_dim)
                n_mlp = model_cfgs[model_type].get('n_mlp', n_mlp)
                channel_multiplier = model_cfgs[model_type].get(
                    'channel_multiplier', channel_multiplier)
                checkpoint = paddle.load(weight_path)
            else:
                raise ValueError(
                    'Predictor need a weight path or a pretrained model type')
        else:
            checkpoint = paddle.load(weight_path)

        self.generator = StyleGANv2Generator(size, style_dim, n_mlp,
                                             channel_multiplier)
        self.generator.set_state_dict(checkpoint)
        self.generator.eval()

        if seed is not None:
            paddle.seed(seed)
            random.seed(seed)
            np.random.seed(seed)
Ejemplo n.º 14
0
    def __init__(self,
                 path_to_enhance=None,
                 size=512,
                 batch_size=1
                 ):
        super(FaceEnhancement, self).__init__()

        # Initialise the face detector
        if path_to_enhance is None:
            model_weights_path = get_path_from_url(GPEN_weights)
            model_weights = paddle.load(model_weights_path)
        else:
            model_weights = paddle.load(path_to_enhance)

        self.face_enhance = GPEN(size=512, style_dim=512, n_mlp=8)
        self.face_enhance.load_dict(model_weights)
        self.face_enhance.eval()
        self.size = size
        self.mask = np.zeros((512, 512), np.float32)
        cv2.rectangle(self.mask, (26, 26), (486, 486), (1, 1, 1), -1, cv2.LINE_AA)
        self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
        self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
        self.mask = paddle.tile(paddle.to_tensor(self.mask).unsqueeze(0).unsqueeze(-1),
                                repeat_times=[batch_size, 1, 1, 3]).numpy()
Ejemplo n.º 15
0
import os
import argparse

from ppgan.utils.download import get_path_from_url

CYCLEGAN_URL_ROOT = 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/'

parser = argparse.ArgumentParser(description='download datasets')
parser.add_argument('--name',
                    type=str,
                    required=True,
                    help='dataset name, \
                    support dataset name: apple2orange, summer2winter_yosemite, \
                    horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, \
                    vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, \
                    ae_photos, cityscapes')

if __name__ == "__main__":
    args = parser.parse_args()

    data_url = CYCLEGAN_URL_ROOT + args.name + '.zip'

    if args.name == 'cityscapes':
        data_url = 'https://paddlegan.bj.bcebos.com/datasets/cityscapes.zip'

    path = get_path_from_url(data_url)

    dst = os.path.join('data', args.name)
    print('symlink {} to {}'.format(path, dst))
    os.symlink(path, dst)
Ejemplo n.º 16
0
    def __init__(self,
                 output='output',
                 weight_path=None,
                 config=None,
                 relative=False,
                 adapt_scale=False,
                 find_best_frame=False,
                 best_frame=None,
                 ratio=1.0,
                 filename='result.mp4',
                 face_detector='sfd',
                 multi_person=False,
                 image_size=256,
                 face_enhancement=False,
                 batch_size=1,
                 mobile_net=False):
        if config is not None and isinstance(config, str):
            with open(config) as f:
                self.cfg = yaml.load(f, Loader=yaml.SafeLoader)
        elif isinstance(config, dict):
            self.cfg = config
        elif config is None:
            self.cfg = {
                'model': {
                    'common_params': {
                        'num_kp': 10,
                        'num_channels': 3,
                        'estimate_jacobian': True
                    },
                    'generator': {
                        'kp_detector_cfg': {
                            'temperature': 0.1,
                            'block_expansion': 32,
                            'max_features': 1024,
                            'scale_factor': 0.25,
                            'num_blocks': 5
                        },
                        'generator_cfg': {
                            'block_expansion': 64,
                            'max_features': 512,
                            'num_down_blocks': 2,
                            'num_bottleneck_blocks': 6,
                            'estimate_occlusion_map': True,
                            'dense_motion_params': {
                                'block_expansion': 64,
                                'max_features': 1024,
                                'num_blocks': 5,
                                'scale_factor': 0.25
                            }
                        }
                    }
                }
            }
        self.image_size = image_size
        if weight_path is None:
            if mobile_net:
                vox_cpk_weight_url = 'https://paddlegan.bj.bcebos.com/applications/first_order_model/vox_mobile.pdparams'

            else:
                if self.image_size == 512:
                    vox_cpk_weight_url = 'https://paddlegan.bj.bcebos.com/applications/first_order_model/vox-cpk-512.pdparams'
                else:
                    vox_cpk_weight_url = 'https://paddlegan.bj.bcebos.com/applications/first_order_model/vox-cpk.pdparams'
            weight_path = get_path_from_url(vox_cpk_weight_url)

        self.weight_path = weight_path
        if not os.path.exists(output):
            os.makedirs(output)
        self.output = output
        self.filename = filename
        self.relative = relative
        self.adapt_scale = adapt_scale
        self.find_best_frame = find_best_frame
        self.best_frame = best_frame
        self.ratio = ratio
        self.face_detector = face_detector
        self.generator, self.kp_detector = self.load_checkpoints(
            self.cfg, self.weight_path)
        self.multi_person = multi_person
        self.face_enhancement = face_enhancement
        self.batch_size = batch_size
        if face_enhancement:
            from face_enhancement import FaceEnhancement
            self.faceenhancer = FaceEnhancement(batch_size=batch_size)