-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmmagic_inference_demo.py
142 lines (126 loc) · 4.15 KB
/
mmagic_inference_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (c) OpenMMLab. All rights reserved.
# isort: off
from argparse import ArgumentParser
from mmengine import DictAction
from mmagic.apis import MMagicInferencer
def parse_args():
parser = ArgumentParser()
parser.add_argument(
'--img', type=str, default=None, help='Input image file.')
parser.add_argument(
'--video', type=str, default=None, help='Input video file.')
parser.add_argument(
'--label',
type=int,
default=None,
help='Input label for conditional models.')
parser.add_argument(
'--trimap', type=str, default=None, help='Input for matting models.')
parser.add_argument(
'--mask',
type=str,
default=None,
help='path to input mask file for inpainting models')
parser.add_argument(
'--text',
type=str,
default='',
help='text input for text2image models')
parser.add_argument(
'--result-out-dir',
type=str,
default=None,
help='Output img or video path.')
parser.add_argument(
'--model-name',
type=str,
default=None,
help='Pretrained mmagic algorithm')
parser.add_argument(
'--model-setting',
type=int,
default=None,
help='Pretrained mmagic algorithm setting')
parser.add_argument(
'--config-name',
type=str,
default=None,
help='Pretrained mmagic algorithm config name')
parser.add_argument(
'--model-config',
type=str,
default=None,
help='Path to the custom config file of the selected mmagic model.')
parser.add_argument(
'--model-ckpt',
type=str,
default=None,
help='Path to the custom checkpoint file of the selected det model.')
parser.add_argument(
'--device',
type=str,
default='cuda',
help='Device used for inference.')
parser.add_argument(
'--extra-parameters',
nargs='+',
action=DictAction,
help='Other customized kwargs for different model')
parser.add_argument(
'--seed',
type=int,
default=2022,
help='The random seed used in inference.')
# print supported tasks and models
parser.add_argument(
'--print-supported-models',
action='store_true',
help='print all supported models for inference.')
parser.add_argument(
'--print-supported-tasks',
action='store_true',
help='print all supported tasks for inference.')
parser.add_argument(
'--print-task-supported-models',
type=str,
default=None,
help='print all supported models for one task')
args, unknown = parser.parse_known_args()
return args, unknown
def main():
args, unknown = parse_args()
assert len(unknown) % 2 == 0, (
'User defined arguments must be passed in pair, but receive '
f'{len(unknown)} arguments.')
user_defined = {}
for idx in range(len(unknown) // 2):
key, val = unknown[idx * 2], unknown[idx * 2 + 1]
assert key.startswith('--'), (
'Key of user define arguments must be start with \'--\', but '
f'receive \'{key}\'.')
key = key.replace('-', '_')
val = int(val) if val.isdigit() else val
user_defined[key[2:]] = val
user_defined.update(vars(args))
if args.print_supported_models:
inference_supported_models = \
MMagicInferencer.get_inference_supported_models()
print('all supported models:')
print(inference_supported_models)
return
if args.print_supported_tasks:
supported_tasks = MMagicInferencer.get_inference_supported_tasks()
print('all supported tasks:')
print(supported_tasks)
return
if args.print_task_supported_models:
task_supported_models = \
MMagicInferencer.get_task_supported_models(
args.print_task_supported_models)
print('translation models:')
print(task_supported_models)
return
editor = MMagicInferencer(**vars(args))
editor.infer(**user_defined)
if __name__ == '__main__':
main()