Skip to content

Commit 2e86599

Browse files
authored
Add doc for Apps and refine some code (PaddlePaddle#53)
* Add doc for Apps and refine some code * Add how to switch cpu and gpu
1 parent fb5f54b commit 2e86599

File tree

5 files changed

+344
-15
lines changed

5 files changed

+344
-15
lines changed

docs/apis/apps.md

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
# Applications接口说明
2+
3+
ppgan.apps包含超分、插针、上色、换妆、图像动画生成等应用,接口使用简洁,并内置了已训练好的模型,可以直接用来做应用。
4+
5+
## 公共用法
6+
7+
### CPU和GPU的切换
8+
9+
默认情况下,如果是GPU设备、并且安装了PaddlePaddle的GPU环境包,则默认使用GPU进行推理。否则,如果安装的是CPU环境包,则使用CPU进行推理。如果需要手动切换CPU、GPU,可以通过以下方式:
10+
11+
12+
```
13+
import paddle
14+
paddle.set_device('cpu')
15+
#paddle.set_device('gpu')
16+
17+
# from ppgan.apps import DeOldifyPredictor
18+
# deoldify = DeOldifyPredictor()
19+
# deoldify.run("docs/imgs/test_old.jpeg")
20+
```
21+
22+
## ppgan.apps.DeOldifyPredictor
23+
24+
```python
25+
ppgan.apps.DeOldifyPredictor(output='output', weight_path=None, render_factor=32)
26+
```
27+
28+
> 构建DeOldify实例。DeOldify是一个基于GAN的老照片上色模型。该接口可以对图片或视频做上色。建议视频使用mp4格式。
29+
>
30+
> **示例**
31+
>
32+
> ```python
33+
> from ppgan.apps import DeOldifyPredictor
34+
> deoldify = DeOldifyPredictor()
35+
> deoldify.run("docs/imgs/test_old.jpeg")
36+
> ```
37+
38+
> **参数**
39+
>
40+
> > - output (str): 设置输出图片的保存路径,默认是output。注意,保存路径为设置output/DeOldify。
41+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
42+
> > - render_factor (int): 图片渲染上色时的缩放因子,图片会缩放到边长为16xrender_factor的正方形, 再上色,例如render_factor默认值为32,输入图片先缩放到(16x32=512) 512x512大小的图片。通常来说,render_factor越小,计算速度越快,颜色看起来也更鲜活。较旧和较低质量的图像通常会因降低渲染因子而受益。渲染因子越高,图像质量越好,但颜色可能会稍微褪色。
43+
44+
### run
45+
46+
```python
47+
run(input)
48+
```
49+
50+
> 构建实例后的执行接口。
51+
52+
> **参数**
53+
>
54+
> > - input (str|np.ndarray|Image.Image): 输入的图片或视频文件。如果是图片,可以是图片的路径、np.ndarray、或PIL.Image类型。如果是视频,只能是视频文件路径。
55+
> >
56+
>
57+
> **返回值**
58+
>
59+
> > - tuple(pred_img(np.array), out_paht(str)): 当属输入时图片时,返回预测后的图片,类型PIL.Image,以及图片的保存的路径。
60+
> > - tuple(frame_path(str), out_path(str)): 当输入为视频时,frame_path为视频每帧上色后保存的图片路径,out_path为上色后视频的保存路径。
61+
62+
### run_image
63+
64+
```python
65+
run_image(img)
66+
```
67+
68+
> 图片上色的接口。
69+
70+
> **参数**
71+
>
72+
> > - img (str|np.ndarray|Image.Image): 输入图片,可以是图片的路径、np.ndarray、或PIL.Image类型。
73+
> >
74+
>
75+
> **返回值**
76+
>
77+
> > - pred_img(PIL.Image): 返回预测后的图片,为PIL.Image类型。
78+
79+
### run_video
80+
81+
```python
82+
run_video(video)
83+
```
84+
85+
> 视频上色的接口。
86+
87+
> **参数**
88+
>
89+
> > - Video (str): 输入视频文件的路径。
90+
>
91+
> **返回值**
92+
>
93+
> > - tuple(frame_path(str), out_path(str)): frame_path为视频每帧上色后保存的图片路径,out_path为上色后视频的保存路径。
94+
95+
96+
97+
## ppgan.apps.DeepRemasterPredictor
98+
99+
```python
100+
ppgan.apps.DeepRemasterPredictor(output='output', weight_path=None, colorization=False, reference_dir=None, mindim=360)
101+
```
102+
103+
> 构建DeepRemasterPredictor实例。DeepRemaster是一个基于GAN的老照片/视频修复、上色模型,该模型可以提供一个参考色的图片作为输入。该接口目前只支持视频输入,建议使用mp4格式。
104+
>
105+
> **示例**
106+
>
107+
> ```
108+
> from ppgan.apps import DeepRemasterPredictor
109+
> deep_remaster = DeepRemasterPredictor()
110+
> deep_remaster.run("docs/imgs/test_old.jpeg")
111+
> ```
112+
>
113+
>
114+
115+
> **参数**
116+
>
117+
> > - output (str): 设置输出图片的保存路径,默认是output。注意,保存路径为设置output/DeepRemaster。
118+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
119+
> > - colorization (bool): 是否打开上色功能,默认是False,既不打开,只执行修复功能。
120+
> > - reference_dir(str|None): 打开上色功能时,输入参考色图片路径,也可以不设置参考色图片。
121+
> > - mindim(int): 预测前图片会进行缩放,最小边长度。
122+
123+
### run
124+
125+
```python
126+
run(video_path)
127+
```
128+
129+
> 构建实例后的执行接口。
130+
131+
> **参数**
132+
>
133+
> > - video_path (str): 输入视频文件路径。
134+
> >
135+
> > 返回值
136+
> >
137+
> > - tuple(str, str)): 返回两个str类型,前者是视频上色后每帧图片的保存路径,后者是上色之后的视频保存路径。
138+
139+
140+
141+
## ppgan.apps.RealSRPredictor
142+
143+
```python
144+
ppgan.apps.RealSRPredictor(output='output', weight_path=None)
145+
```
146+
147+
> 构建RealSR实例。RealSR: Real-World Super-Resolution via Kernel Estimation and Noise Injection发表于CVPR 2020 Workshops的基于真实世界图像训练的超分辨率模型。此接口对输入图片或视频做4倍的超分辨率。建议视频使用mp4格式。
148+
>
149+
> **用例**
150+
>
151+
> ```
152+
> from ppgan.apps import RealSRPredictor
153+
> sr = RealSRPredictor()
154+
> sr.run("docs/imgs/test_sr.jpeg")
155+
> ```
156+
157+
> **参数**
158+
>
159+
> > - output (str): 设置输出图片的保存路径,默认是output。注意,保存路径为设置output/RealSR。
160+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
161+
162+
```python
163+
run(video_path)
164+
```
165+
166+
> 构建实例后的执行接口。
167+
168+
> **参数**
169+
>
170+
> > - video_path (str): 输入视频文件路径。
171+
> >
172+
>
173+
> **返回值**
174+
>
175+
> > - tuple(pred_img(np.array), out_paht(str)): 当属输入时图片时,返回预测后的图片,类型PIL.Image,以及图片的保存的路径。
176+
> > - tuple(frame_path(str), out_path(str)): 当输入为视频时,frame_path为超分后视频每帧图片的保存路径,out_path为超分后的视频保存路径。
177+
178+
### run_image
179+
180+
```python
181+
run_image(img)
182+
```
183+
184+
> 图片超分的接口。
185+
186+
> **参数**
187+
>
188+
> > - img (str|np.ndarray|Image.Image): 输入图片,可以是图片的路径、np.ndarray、或PIL.Image类型。
189+
>
190+
> **返回值**
191+
>
192+
> > - pred_img(PIL.Image): 返回预测后的图片,为PIL.Image类型。
193+
194+
### run_video
195+
196+
```python
197+
run_video(video)
198+
```
199+
200+
> 视频超分的接口。
201+
202+
> **参数**
203+
>
204+
> > - Video (str): 输入视频文件的路径。
205+
>
206+
> **返回值**
207+
>
208+
> > - tuple(frame_path(str), out_path(str)): frame_path为超分后视频每帧图片的保存路径,out_path为超分后的视频保存路径。
209+
210+
211+
212+
## ppgan.apps.EDVRPredictor
213+
214+
```python
215+
ppgan.apps.EDVRPredictor(output='output', weight_path=None)
216+
```
217+
218+
> 构建RealSR实例。EDVR: Video Restoration with Enhanced Deformable Convolutional Networks,论文链接: https://arxiv.org/abs/1905.02716 ,是一个针对视频超分的模型。该接口,对视频做2倍的超分。建议视频使用mp4格式。
219+
>
220+
> **示例**
221+
>
222+
> ```
223+
> from ppgan.apps import EDVRPredictor
224+
> sr = EDVRPredictor()
225+
> # 测试一个视频文件
226+
> sr.run("docs/imgs/test.mp4")
227+
> ```
228+
229+
> **参数**
230+
>
231+
> > - output (str): 设置输出图片的保存路径,默认是output。注意,保存路径为设置output/EDVR。
232+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
233+
234+
```python
235+
run(video_path)
236+
```
237+
238+
> 构建实例后的执行接口。
239+
240+
> **参数**
241+
>
242+
> > - video_path (str): 输入视频文件路径。
243+
>
244+
> **返回值**
245+
>
246+
> > - tuple(str, str): 前者超分后的视频每帧图片的保存路径,后者为昨晚超分的视频路径。
247+
248+
249+
250+
## ppgan.apps.DAINPredictor
251+
252+
```python
253+
ppgan.apps.DAINPredictor(output='output', weight_path=Nonetime_step=None, use_gpu=True, key_frame_thread=0remove_duplicates=False)
254+
```
255+
256+
> 构建插针DAIN模型的实例。DAIN: Depth-Aware Video Frame Interpolation,论文链接: https://arxiv.org/abs/1904.00830 ,对视频做插针,获得帧率更高的视频。
257+
>
258+
> **示例**
259+
>
260+
> ```
261+
> from ppgan.apps import DAINPredictor
262+
> dain = DAINPredictor()
263+
> # 测试一个视频文件
264+
> dain.run("docs/imgs/test.mp4")
265+
> ```
266+
267+
> **参数**
268+
>
269+
> > - output_path (str): 设置预测输出的保存路径,默认是output。注意,保存路径为设置output/DAIN。
270+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
271+
> > - time_step (float): 帧率变化的倍数为 1./time_step,例如,如果time_step为0.5,则2倍插针,为0.25,则为4倍插针。
272+
> > - use_gpu (bool): 是否使用GPU做预测,默认是True。
273+
> > - remove_duplicates (bool): 是否去除重复帧,默认是False。
274+
275+
```python
276+
run(video_path)
277+
```
278+
279+
> 构建实例后的执行接口。
280+
281+
> **参数**
282+
>
283+
> > - video_path (str): 输入视频文件路径。
284+
>
285+
> **返回值**
286+
>
287+
> > - tuple(str, str): 当输入为视频时,frame_path为视频每帧上色后保存的图片路径,out_path为上色后视频的保存路径。
288+
289+
290+
291+
## ppgan.apps.FirstOrderPredictor
292+
293+
```python
294+
ppgan.apps.FirstOrderPredictor(output='output', weight_path=Noneconfig=None, relative=False, adapt_scale=Falsefind_best_frame=False, best_frame=None)
295+
```
296+
297+
> 构建FirsrOrder模型的实例,此模型用来做Image Animation,既给定一张源图片和一个驱动视频,生成一段视频,其中住体是源图片,动作是驱动视频中的动作。论文是First Order Motion Model for Image Animation,论文链接: https://arxiv.org/abs/2003.00196
298+
>
299+
> **示例**
300+
>
301+
> ```
302+
> from ppgan.apps import FirstOrderPredictor
303+
> animate = FirstOrderPredictor()
304+
> # 测试一个视频文件
305+
> animate.run("source.png","driving.mp4")
306+
> ```
307+
308+
> **参数**
309+
>
310+
> > - output_path (str): 设置预测输出的保存路径,默认是output。注意,保存路径为设置output/result.mp4。
311+
> > - weight_path (str): 指定模型路径,默认是None,则会自动下载内置的已经训练好的模型。
312+
> > - config (dict|str|None): 设置模型的参数,可以是字典类型或YML文件,默认值是None,采用的默认的参数。当权重默认是None时,config也需采用默认值None。否则,这里的配置和对应权重保持一致
313+
> > - relative (bool): 使用相对还是绝对关键点坐标,默认是False。
314+
> > - adapt_scale (bool): 是否基于关键点凸包的自适应运动,默认是False。
315+
> > - find_best_frame (bool): 是否从与源图片最匹配的帧开始生成,仅仅适用于人脸应用,需要人脸对齐的库。
316+
> > - best_frame (int): 设置起始帧数,默认是None,从第1帧开始(从1开始计数)。
317+
318+
```python
319+
run(source_image,driving_video)
320+
```
321+
322+
> 构建实例后的执行接口,预测视频保存位置为output/result.mp4。
323+
324+
> **参数**
325+
>
326+
> > - source_image (str): 输入源图片。
327+
> > - driving_video (str): 输入驱动视频,支持mp4格式。
328+
>
329+
> **返回值**
330+
>
331+
> > 无。

ppgan/apps/dain_predictor.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@
3232

3333
class DAINPredictor(BasePredictor):
3434
def __init__(self,
35-
output_path='output',
35+
output='output',
3636
weight_path=None,
3737
time_step=None,
3838
use_gpu=True,
39-
key_frame_thread=0.,
4039
remove_duplicates=False):
41-
self.output_path = os.path.join(output_path, 'DAIN')
40+
self.output_path = os.path.join(output, 'DAIN')
4241
if weight_path is None:
4342
cur_path = os.path.abspath(os.path.dirname(__file__))
4443
weight_path = get_path_from_url(DAIN_WEIGHT_URL, cur_path)
4544

4645
self.weight_path = weight_path
4746
self.time_step = time_step
48-
self.key_frame_thread = key_frame_thread
47+
self.key_frame_thread = 0
4948
self.remove_duplicates = remove_duplicates
5049

5150
self.build_inference_model()
@@ -134,15 +133,15 @@ def run(self, video_path):
134133
img_first = imread(first)
135134
img_second = imread(second)
136135
'''--------------Frame change test------------------------'''
137-
img_first_gray = np.dot(img_first[..., :3], [0.299, 0.587, 0.114])
138-
img_second_gray = np.dot(img_second[..., :3], [0.299, 0.587, 0.114])
139-
140-
img_first_gray = img_first_gray.flatten(order='C')
141-
img_second_gray = img_second_gray.flatten(order='C')
142-
corr = np.corrcoef(img_first_gray, img_second_gray)[0, 1]
143-
key_frame = False
144-
if corr < self.key_frame_thread:
145-
key_frame = True
136+
#img_first_gray = np.dot(img_first[..., :3], [0.299, 0.587, 0.114])
137+
#img_second_gray = np.dot(img_second[..., :3], [0.299, 0.587, 0.114])
138+
139+
#img_first_gray = img_first_gray.flatten(order='C')
140+
#img_second_gray = img_second_gray.flatten(order='C')
141+
#corr = np.corrcoef(img_first_gray, img_second_gray)[0, 1]
142+
#key_frame = False
143+
#if corr < self.key_frame_thread:
144+
# key_frame = True
146145
'''-------------------------------------------------------'''
147146

148147
X0 = img_first.astype('float32').transpose((2, 0, 1)) / 255

ppgan/apps/deepremaster_predictor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __init__(self,
7979
if weight_path is None:
8080
cur_path = os.path.abspath(os.path.dirname(__file__))
8181
weight_path = get_path_from_url(DEEPREMASTER_WEIGHT_URL, cur_path)
82+
print(weight_path)
8283

8384
self.weight_path = weight_path
8485

ppgan/models/cycle_gan_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import paddle
2-
from paddle.distributed import ParallelEnv
32
from .base_model import BaseModel
43

54
from .builder import MODELS

0 commit comments

Comments
 (0)