forked from OpenGVLab/Hulk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
67 lines (50 loc) · 1.92 KB
/
test.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
import json
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from inference_model import HumanHulk
def save_output(outputs, metadata_path):
with open(metadata_path, 'w') as f:
json.dump(outputs['metadata'], f)
if outputs['images']['kp_img'] is not None:
outputs['images']['kp_img'].save(outputs['metadata']['kp_path'])
if outputs['images']['CIHP_img'] is not None:
outputs['images']['CIHP_img'].save(outputs['metadata']['CIHP_path'])
def multi_inference(rank, world_size):
setup(rank, world_size)
device = torch.device(rank)
pipeline = HumanHulk(device)
img_path = 'your-image-path'
output = dict(metadata=dict(), images=dict())
output_path = 'metadata_path'
output['metadata']['box'] = pipeline.set_image(img_path)
if output['metadata']['box'] is not None:
output['metadata']['CIHP_path'] = 'cihp_img_path'
output['metadata']['kp_path'] = 'keypoint_img_path'
output['metadata']['keypoints'], output['images']['kp_img'] = pipeline.get_pose(img_path)
output['images']['CIHP_img'] = pipeline.get_parse()
else:
output['metadata']['CIHP_path'] = None
output['metadata']['kp_path'] = None
output['metadata']['keypoints'] = None
output['images']['kp_img'] = None
output['images']['CIHP_img'] = None
save_output(output, output_path)
cleanup()
def cleanup():
dist.destroy_process_group()
def setup(rank, world_size):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355'
# initialize the process group
dist.init_process_group("gloo", rank=rank, world_size=world_size)
def run(fn, world_size):
mp.spawn(fn,
args=(world_size,),
nprocs=world_size,
join=True)
if __name__ == "__main__":
n_gpus = torch.cuda.device_count()
world_size = n_gpus
run(multi_inference, world_size)