Skip to content

Commit

Permalink
Refine code (PaddlePaddle#44)
Browse files Browse the repository at this point in the history
* refine code

* test

* fix apps

* update readme

* rm unused code

* fix apps output when input is image

* clean code

* update requirements.txt
  • Loading branch information
LielinJiang authored Oct 22, 2020
1 parent 359db9c commit 3e4a59f
Show file tree
Hide file tree
Showing 45 changed files with 560 additions and 1,014 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pip install -v -e . # or "python setup.py develop"
Please refer to [data prepare](./docs/data_prepare.md) for dataset preparation.

## Get Start
Please refer [get stated](./docs/get_started.md) for the basic usage of PaddleGAN.
Please refer [get started](./docs/get_started.md) for the basic usage of PaddleGAN.

## Model tutorial
* [Pixel2Pixel and CycleGAN](./docs/tutorials/pix2pix_cyclegan.md)
Expand Down
11 changes: 5 additions & 6 deletions configs/cyclegan_cityscapes.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 200
isTrain: True
output_dir: output_dir
lambda_A: 10.0
lambda_B: 10.0
Expand Down Expand Up @@ -39,12 +38,12 @@ dataset:
transforms:
- name: Resize
size: [286, 286]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
- name: RandomCrop
output_size: [256, 256]
size: [256, 256]
- name: RandomHorizontalFlip
prob: 0.5
- name: Permute
- name: Transpose
- name: Normalize
mean: [127.5, 127.5, 127.5]
std: [127.5, 127.5, 127.5]
Expand All @@ -60,8 +59,8 @@ dataset:
transforms:
- name: Resize
size: [256, 256]
interpolation: 2 #cv2.INTER_CUBIC
- name: Permute
interpolation: 'bicubic' #cv2.INTER_CUBIC
- name: Transpose
- name: Normalize
mean: [127.5, 127.5, 127.5]
std: [127.5, 127.5, 127.5]
Expand Down
9 changes: 4 additions & 5 deletions configs/cyclegan_horse2zebra.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 200
isTrain: True
output_dir: output_dir
lambda_A: 10.0
lambda_B: 10.0
Expand Down Expand Up @@ -38,12 +37,12 @@ dataset:
transforms:
- name: Resize
size: [286, 286]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
- name: RandomCrop
output_size: [256, 256]
- name: RandomHorizontalFlip
prob: 0.5
- name: Permute
- name: Transpose
- name: Normalize
mean: [127.5, 127.5, 127.5]
std: [127.5, 127.5, 127.5]
Expand All @@ -60,8 +59,8 @@ dataset:
transform:
- name: Resize
size: [256, 256]
interpolation: 2 #cv2.INTER_CUBIC
- name: Permute
interpolation: 'bicubic' #cv2.INTER_CUBIC
- name: Transpose
- name: Normalize
mean: [127.5, 127.5, 127.5]
std: [127.5, 127.5, 127.5]
Expand Down
5 changes: 2 additions & 3 deletions configs/makeup.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 100
isTrain: True
output_dir: tmp
checkpoints_dir: checkpoints
lambda_A: 10.0
Expand All @@ -24,14 +23,14 @@ dataset:
train:
name: MakeupDataset
trans_size: 256
dataroot: MT-Dataset
dataroot: data/MT-Dataset
cls_list: [non-makeup, makeup]
phase: train
pool_size: 16
test:
name: MakeupDataset
trans_size: 256
dataroot: MT-Dataset
dataroot: data/MT-Dataset
cls_list: [non-makeup, makeup]
phase: test
pool_size: 16
Expand Down
11 changes: 5 additions & 6 deletions configs/pix2pix_cityscapes.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 200
isTrain: True
output_dir: output_dir
lambda_L1: 100

Expand Down Expand Up @@ -36,15 +35,15 @@ dataset:
transforms:
- name: Resize
size: [286, 286]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: PairedRandomCrop
output_size: [256, 256]
size: [256, 256]
keys: [image, image]
- name: PairedRandomHorizontalFlip
prob: 0.5
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand All @@ -63,9 +62,9 @@ dataset:
transforms:
- name: Resize
size: [256, 256]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand Down
11 changes: 5 additions & 6 deletions configs/pix2pix_cityscapes_2gpus.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 200
isTrain: True
output_dir: output_dir
lambda_L1: 100

Expand Down Expand Up @@ -35,15 +34,15 @@ dataset:
transforms:
- name: Resize
size: [286, 286]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: PairedRandomCrop
output_size: [256, 256]
size: [256, 256]
keys: [image, image]
- name: PairedRandomHorizontalFlip
prob: 0.5
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand All @@ -62,9 +61,9 @@ dataset:
transforms:
- name: Resize
size: [256, 256]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand Down
11 changes: 5 additions & 6 deletions configs/pix2pix_facades.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
epochs: 200
isTrain: True
output_dir: output_dir
lambda_L1: 100

Expand Down Expand Up @@ -35,15 +34,15 @@ dataset:
transforms:
- name: Resize
size: [286, 286]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: PairedRandomCrop
output_size: [256, 256]
size: [256, 256]
keys: [image, image]
- name: PairedRandomHorizontalFlip
prob: 0.5
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand All @@ -62,9 +61,9 @@ dataset:
transforms:
- name: Resize
size: [256, 256]
interpolation: 2 #cv2.INTER_CUBIC
interpolation: 'bicubic' #cv2.INTER_CUBIC
keys: [image, image]
- name: Permute
- name: Transpose
keys: [image, image]
- name: Normalize
mean: [127.5, 127.5, 127.5]
Expand Down
6 changes: 4 additions & 2 deletions ppgan/apps/base_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import cv2
from PIL import Image
import paddle


Expand Down Expand Up @@ -61,9 +62,10 @@ def base_forward(self, inputs):

return out

def is_video(self, input):
def is_image(self, input):
try:
cv2.VideoCapture(input)
img = Image.open(input)
_ = img.size
return True
except:
return False
Expand Down
10 changes: 6 additions & 4 deletions ppgan/apps/deoldify_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ def run_video(self, video):
return frame_pattern_combined, vid_out_path

def run(self, input):
if self.is_video(input):
if not self.is_image(input):
return self.run_video(input)
else:
pred_img = self.run_image(input)

out_path = None
if self.output:
base_name = os.path.basename(input)
pred_img.save(os.path.join(self.output, base_name + '.png'))
base_name = os.path.splitext(os.path.basename(input))[0]
out_path = os.path.join(self.output, base_name + '.png')
pred_img.save(out_path)

return pred_img
return pred_img, out_path
13 changes: 9 additions & 4 deletions ppgan/apps/realsr_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,18 @@ def run_video(self, video):
return frame_pattern_combined, vid_out_path

def run(self, input):
if self.is_video(input):
if not os.path.exists(self.output):
os.makedirs(self.output)

if not self.is_image(input):
return self.run_video(input)
else:
pred_img = self.run_image(input)

out_path = None
if self.output:
base_name = os.path.basename(input)
pred_img.save(os.path.join(self.output, base_name + '.png'))
base_name = os.path.splitext(os.path.basename(input))[0]
out_path = os.path.join(self.output, base_name + '.png')
pred_img.save(out_path)

return pred_img
return pred_img, out_path
4 changes: 2 additions & 2 deletions ppgan/datasets/paired_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, cfg):
"""Initialize this dataset class.
Args:
cfg (dict) -- stores all the experiment flags
cfg (dict): configs of datasets.
"""
BaseDataset.__init__(self, cfg)
self.dir_AB = os.path.join(cfg.dataroot,
Expand All @@ -42,7 +42,7 @@ def __getitem__(self, index):
"""
# read a image given a random integer index
AB_path = self.AB_paths[index]
AB = cv2.imread(AB_path)
AB = cv2.cvtColor(cv2.imread(AB_path), cv2.COLOR_BGR2RGB)

# split AB image into A and B
h, w = AB.shape[:2]
Expand Down
2 changes: 1 addition & 1 deletion ppgan/datasets/transforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .transforms import RandomCrop, Resize, RandomHorizontalFlip, PairedRandomCrop, PairedRandomHorizontalFlip, Normalize, Permute
from .transforms import PairedRandomCrop, PairedRandomHorizontalFlip
1 change: 1 addition & 0 deletions ppgan/datasets/transforms/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __call__(self, data):
try:
data = f(data)
except Exception as e:
print(f)
stack_info = traceback.format_exc()
print("fail to perform transform [{}] with error: "
"{} and stack:\n{}".format(f, e, str(stack_info)))
Expand Down
2 changes: 1 addition & 1 deletion ppgan/datasets/transforms/makeup_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_makeup_transform(cfg, pic="image"):
if pic == "image":
transform = T.Compose([
T.Resize(size=cfg.trans_size),
T.Permute(to_rgb=False),
T.Transpose(),
])
else:
transform = T.Resize(size=cfg.trans_size,
Expand Down
Loading

0 comments on commit 3e4a59f

Please sign in to comment.