Skip to content

Commit

Permalink
polish code (PaddlePaddle#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
LielinJiang authored Nov 4, 2021
1 parent 7f628db commit 2328f9f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 91 deletions.
16 changes: 3 additions & 13 deletions ppgan/datasets/wav2lip_dataset.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
# Copyright (c) 2020 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.
# code was heavily based on https://github.com/Rudrabha/Wav2Lip
# Users should be careful about adopting these functions in any commercial matters.
# https://github.com/Rudrabha/Wav2Lip#license-and-citation

import cv2
import random
Expand Down
1 change: 1 addition & 0 deletions ppgan/models/criterions/perceptual_loss.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) MMEditing Authors.
import paddle
import paddle.nn as nn
import paddle.vision.models.vgg as vgg
Expand Down
14 changes: 1 addition & 13 deletions ppgan/models/generators/basicvsr.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
# Copyright (c) 2021 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.
# Copyright (c) MMEditing Authors.

import paddle

Expand Down
15 changes: 2 additions & 13 deletions ppgan/models/generators/deoldify.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
# Copyright (c) 2020 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.
# MIT License
# Copyright (c) 2018 Jason Antic

import numpy as np
import paddle
Expand Down
14 changes: 12 additions & 2 deletions ppgan/models/ugatit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,18 @@ def __init__(self,
cam_weight=1000.0):
"""Initialize the CycleGAN class.
Parameters:
opt (config)-- stores all the experiment flags; needs to be a subclass of Dict
Args:
generator (dict): config of generator.
discriminator_g (dict): config of discriminator_g.
discriminator_l (dict): config of discriminator_l.
l1_criterion (dict): config of l1_criterion.
mse_criterion (dict): config of mse_criterion.
bce_criterion (dict): config of bce_criterion.
direction (str): direction of dataset, default: 'a2b'.
adv_weight (float): adversial loss weight, default: 1.0.
cycle_weight (float): cycle loss weight, default: 10.0.
identity_weight (float): identity loss weight, default: 10.0.
cam_weight (float): cam loss weight, default: 1000.0.
"""
super(UGATITModel, self).__init__()
self.adv_weight = adv_weight
Expand Down
2 changes: 2 additions & 0 deletions ppgan/utils/animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# code was heavily based on https://github.com/AliaksandrSiarohin/first-order-model

import numpy as np
from scipy.spatial import ConvexHull

Expand Down
4 changes: 4 additions & 0 deletions ppgan/utils/audio.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# code was heavily based on https://github.com/Rudrabha/Wav2Lip
# Users should be careful about adopting these functions in any commercial matters.
# https://github.com/Rudrabha/Wav2Lip#license-and-citation

import numpy as np
from scipy import signal
from scipy.io import wavfile
Expand Down
46 changes: 23 additions & 23 deletions ppgan/utils/audio_config.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from easydict import EasyDict as edict
from .config import AttrDict

_C = edict()
_audio_cfg = AttrDict()

_C.num_mels = 80
_C.rescale = True
_C.rescaling_max = 0.9
_C.use_lws = False
_C.n_fft = 800
_C.hop_size = 200
_C.win_size = 800
_C.sample_rate = 16000
_C.frame_shift_ms = None
_C.signal_normalization = True
_C.allow_clipping_in_normalization = True
_C.symmetric_mels = True
_C.max_abs_value = 4.
_C.preemphasize = True
_C.preemphasis = 0.97
_C.min_level_db = -100
_C.ref_level_db = 20
_C.fmin = 55
_C.fmax = 7600
_C.fps = 25
_audio_cfg.num_mels = 80
_audio_cfg.rescale = True
_audio_cfg.rescaling_max = 0.9
_audio_cfg.use_lws = False
_audio_cfg.n_fft = 800
_audio_cfg.hop_size = 200
_audio_cfg.win_size = 800
_audio_cfg.sample_rate = 16000
_audio_cfg.frame_shift_ms = None
_audio_cfg.signal_normalization = True
_audio_cfg.allow_clipping_in_normalization = True
_audio_cfg.symmetric_mels = True
_audio_cfg.max_abs_value = 4.
_audio_cfg.preemphasize = True
_audio_cfg.preemphasis = 0.97
_audio_cfg.min_level_db = -100
_audio_cfg.ref_level_db = 20
_audio_cfg.fmin = 55
_audio_cfg.fmax = 7600
_audio_cfg.fps = 25


def get_audio_config():
return _C
return _audio_cfg
1 change: 0 additions & 1 deletion ppgan/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

class AttrDict(dict):
def __getattr__(self, key):
# return self[key]
try:
return self[key]
except KeyError:
Expand Down
45 changes: 21 additions & 24 deletions ppgan/utils/image_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,48 @@ class ImagePool():
This buffer enables us to update discriminators using a history of generated images
rather than the ones produced by the latest generators.
"""
def __init__(self, pool_size):
"""Initialize the ImagePool class
Parameters:
pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created
"""
Args:
pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created
"""
def __init__(self, pool_size, prob=0.5):
self.pool_size = pool_size
if self.pool_size > 0: # create an empty pool
self.prob = prob

if self.pool_size > 0:
self.num_imgs = 0
self.images = []

def query(self, images):
"""Return an image from the pool.
Parameters:
images: the latest generated images from the generator
Args:
images (paddle.Tensor): the latest generated images from the generator
Returns images from the buffer.
By 50/100, the buffer will return input images.
By 50/100, the buffer will return images previously stored in the buffer,
and insert the current images to the buffer.
"""
if self.pool_size == 0: # if the buffer size is 0, do nothing
# if the buffer size is 0, do nothing
if self.pool_size == 0:
return images
return_images = []
for image in images:
image = paddle.unsqueeze(image, 0)
if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer
# if the buffer is not full; keep inserting current images to the buffer
if self.num_imgs < self.pool_size:
self.num_imgs = self.num_imgs + 1
self.images.append(image)
return_images.append(image)
else:
p = random.uniform(0, 1)
if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer
random_id = random.randint(0, self.pool_size -
1) # randint is inclusive
# FIXME: clone
# tmp = (self.images[random_id]).detach() #.clone()
tmp = self.images[random_id] #.clone()
# by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer
if p > self.prob:
random_id = random.randint(0, self.pool_size - 1)
tmp = self.images[random_id].clone()
self.images[random_id] = image
return_images.append(tmp)
else: # by another 50% chance, the buffer will return the current image
else:
# by another 50% chance, the buffer will return the current image
return_images.append(image)
return_images = paddle.concat(return_images,
0) # collect all the images and return
# collect all the images and return
return_images = paddle.concat(return_images, 0)
return return_images
3 changes: 1 addition & 2 deletions ppgan/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ def setup_logger(output=None, name="ppgan"):
if local_rank > 0:
filename = filename + ".rank{}".format(local_rank)

# PathManager.mkdirs(os.path.dirname(filename))
# make dir if path not exist
os.makedirs(os.path.dirname(filename), exist_ok=True)

# fh = logging.StreamHandler(_cached_log_stream(filename)
fh = logging.FileHandler(filename, mode='a')
fh.setLevel(logging.DEBUG)
fh.setFormatter(plain_formatter)
Expand Down
2 changes: 2 additions & 0 deletions ppgan/utils/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def get(self, name):
return ret


# code was based on mmcv
# Copyright (c) Copyright (c) OpenMMLab.
def build_from_config(cfg, registry, default_args=None):
"""Build a class from config dict.
Expand Down

0 comments on commit 2328f9f

Please sign in to comment.