diff --git a/docs/en_US/tutorials/psgan.md b/docs/en_US/tutorials/psgan.md index c5046bfbc866331858fe7439d08b9ae65ab48a39..278ee451f811674be23a5442edd86af9d958d298 100644 --- a/docs/en_US/tutorials/psgan.md +++ b/docs/en_US/tutorials/psgan.md @@ -33,8 +33,8 @@ python tools/psgan_infer.py \ 2. Downloading the landmarks [data](https://paddlegan.bj.bcebos.com/landmarks.tar), and uncompress it 3. Runnint the following command to substitute files: ``` -mv landmarks/makeup MT-Dataset/landmarks/makeup -mv landmarks/non-makeup MT-Dataset/landmarks/non-makeup +rm -rf MT-Dataset/landmarks/makeup && mv landmarks/makeup MT-Dataset/landmarks/ +rm -rf MT-Dataset/landmarks/non-makeup && mv landmarks/non-makeup MT-Dataset/landmarks/ cp landmarks/train_makeup.txt MT-Dataset/train_makeup.txt cp landmarks/train_non-makeup.txt MT-Dataset/train_non-makeup.txt ``` diff --git a/docs/zh_CN/tutorials/psgan.md b/docs/zh_CN/tutorials/psgan.md index 5b5b2de71f91cd8e53bb41d81afbad4874ebb256..1f33638d020a4b070ff40cf82c143efad34026ce 100644 --- a/docs/zh_CN/tutorials/psgan.md +++ b/docs/zh_CN/tutorials/psgan.md @@ -33,8 +33,8 @@ python tools/psgan_infer.py \ 2. 下载landmarks数据[lmks](https://paddlegan.bj.bcebos.com/landmarks.tar),并解压 3. 运行如下命令进行文件夹及文件替换: ``` -mv landmarks/makeup MT-Dataset/landmarks/makeup -mv landmarks/non-makeup MT-Dataset/landmarks/non-makeup +rm -rf MT-Dataset/landmarks/makeup && mv landmarks/makeup MT-Dataset/landmarks/ +rm -rf MT-Dataset/landmarks/non-makeup && mv landmarks/non-makeup MT-Dataset/landmarks/ cp landmarks/train_makeup.txt MT-Dataset/train_makeup.txt cp landmarks/train_non-makeup.txt MT-Dataset/train_non-makeup.txt ``` diff --git a/ppgan/apps/dain_predictor.py b/ppgan/apps/dain_predictor.py index 28e0a6212b9942034bc6295f022d286f6cbf1a3d..332cf58c0068e043571a9c937e60ca854d5625d7 100644 --- a/ppgan/apps/dain_predictor.py +++ b/ppgan/apps/dain_predictor.py @@ -269,6 +269,7 @@ class DAINPredictor(BasePredictor): return sum([2**i for (i, v) in enumerate(diff.flatten()) if v]) hashes = {} + max_interp = 9 image_paths = sorted(glob.glob(os.path.join(paths, '*.png'))) for image_path in image_paths: image = cv2.imread(image_path) @@ -283,7 +284,16 @@ class DAINPredictor(BasePredictor): last_index = int( hashed_paths[-1].split('/')[-1].split('.')[-2]) + 1 gap = 2 * (last_index - first_index) - 1 - if gap > 9: + if gap > 2 * max_interp: + cut1 = len(hashed_paths) // 3 + cut2 = cut1 * 2 + for p in hashed_paths[1:cut1 - 1]: + os.remove(p) + for p in hashed_paths[cut1 + 1:cut2]: + os.remove(p) + for p in hashed_paths[cut2 + 1:]: + os.remove(p) + if gap > max_interp: mid = len(hashed_paths) // 2 for p in hashed_paths[1:mid - 1]: os.remove(p) diff --git a/ppgan/apps/psgan_predictor.py b/ppgan/apps/psgan_predictor.py index 29fa0e8190a3b03d0ac66490912099d47a6c043b..46b59220ae5146786a73635ed2cb3f07e2ca432c 100644 --- a/ppgan/apps/psgan_predictor.py +++ b/ppgan/apps/psgan_predictor.py @@ -43,16 +43,6 @@ def toImage(net_output): return img -def mask2image(mask: np.array, format="HWC"): - H, W = mask.shape - - canvas = np.zeros((H, W, 3), dtype=np.uint8) - for i in range(int(mask.max())): - color = np.random.rand(1, 1, 3) * 255 - canvas += (mask == i)[:, :, None] * color.astype(np.uint8) - return canvas - - PS_WEIGHT_URL = "https://paddlegan.bj.bcebos.com/models/psgan_weight.pdparams" @@ -81,6 +71,7 @@ class PreProcess: self.down_ratio, self.width_ratio) np_image = np.array(image) + image_trans = self.transform(np_image) mask = self.face_parser.parse( np.float32(cv2.resize(np_image, (512, 512)))) mask = cv2.resize(mask.numpy(), (self.img_size, self.img_size), @@ -88,7 +79,8 @@ class PreProcess: mask = mask.astype(np.uint8) mask_tensor = paddle.to_tensor(mask) - lms = futils.dlib.landmarks(image, face) * self.img_size / image.width + lms = futils.dlib.landmarks( + image, face) / image_trans.shape[:2] * self.img_size lms = lms.round() P_np = generate_P_from_lmks(lms, self.img_size, self.img_size, @@ -96,10 +88,8 @@ class PreProcess: mask_aug = generate_mask_aug(mask, lms) - image = self.transform(np_image) - return [ - self.norm(image).unsqueeze(0), + self.norm(image_trans).unsqueeze(0), np.float32(mask_aug), np.float32(P_np), np.float32(mask) @@ -212,6 +202,9 @@ class PSGANPredictor(BasePredictor): image = postprocess(source_crop, image) ref_img_name = os.path.split(reference_path)[1] + if not os.path.exists(self.output_path): + os.makedirs(sefl.output_path) + save_path = os.path.join(self.output_path, 'transfered_ref_' + ref_img_name) image.save(save_path) diff --git a/ppgan/models/makeup_model.py b/ppgan/models/makeup_model.py index 74834f779baf88d415a4e06a12d990fdf9091b2e..1031fd52adf3a43e23122b15766bc7f18947aa31 100644 --- a/ppgan/models/makeup_model.py +++ b/ppgan/models/makeup_model.py @@ -11,6 +11,7 @@ # 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 numpy as np import paddle diff --git a/tools/psgan_infer.py b/tools/psgan_infer.py index 2eff1902e96cf3d9b1fc812a0ee21b4ae92fe1c7..1ed2f2801ad822ea92b01fd4ca84d5117c73d38d 100644 --- a/tools/psgan_infer.py +++ b/tools/psgan_infer.py @@ -12,7 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os +import sys import argparse + +cur_path = os.path.abspath(os.path.dirname(__file__)) +root_path = os.path.split(cur_path)[0] +sys.path.append(root_path) + from ppgan.utils.options import parse_args from ppgan.utils.config import get_config from ppgan.apps.psgan_predictor import PSGANPredictor