InvincibleMeta Balaji23 commited on
Commit
554d3e4
·
verified ·
1 Parent(s): fe14097

Changes for statless GPU running torch.cuda (#5)

Browse files

- Changes for statless GPU running torch.cuda (2d417ae15dfd712ecb8efe0cb1c8bc1d1be14df0)


Co-authored-by: rao <Balaji23@users.noreply.huggingface.co>

Files changed (1) hide show
  1. preprocess/openpose/run_openpose.py +84 -84
preprocess/openpose/run_openpose.py CHANGED
@@ -1,84 +1,84 @@
1
- import pdb
2
-
3
- # import config
4
- from pathlib import Path
5
- import sys
6
-
7
- PROJECT_ROOT = Path(__file__).absolute().parents[0].absolute()
8
- sys.path.insert(0, str(PROJECT_ROOT))
9
- import os
10
-
11
- import cv2
12
- import einops
13
- import numpy as np
14
- import random
15
- import time
16
- import json
17
-
18
- # from pytorch_lightning import seed_everything
19
- from preprocess.openpose.annotator.util import resize_image, HWC3
20
- from preprocess.openpose.annotator.openpose import OpenposeDetector
21
-
22
- import argparse
23
- from PIL import Image
24
- import torch
25
- import pdb
26
-
27
- # os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3'
28
-
29
- class OpenPose:
30
- def __init__(self, gpu_id: int):
31
- self.gpu_id = gpu_id
32
- torch.cuda.set_device(gpu_id)
33
- self.preprocessor = OpenposeDetector()
34
-
35
- def __call__(self, input_image, resolution=384):
36
- torch.cuda.set_device(self.gpu_id)
37
- if isinstance(input_image, Image.Image):
38
- input_image = np.asarray(input_image)
39
- elif type(input_image) == str:
40
- input_image = np.asarray(Image.open(input_image))
41
- else:
42
- raise ValueError
43
- with torch.no_grad():
44
- input_image = HWC3(input_image)
45
- input_image = resize_image(input_image, resolution)
46
- H, W, C = input_image.shape
47
- assert (H == 512 and W == 384), 'Incorrect input image shape'
48
- pose, detected_map = self.preprocessor(input_image, hand_and_face=False)
49
-
50
- candidate = pose['bodies']['candidate']
51
- subset = pose['bodies']['subset'][0][:18]
52
- for i in range(18):
53
- if subset[i] == -1:
54
- candidate.insert(i, [0, 0])
55
- for j in range(i, 18):
56
- if(subset[j]) != -1:
57
- subset[j] += 1
58
- elif subset[i] != i:
59
- candidate.pop(i)
60
- for j in range(i, 18):
61
- if(subset[j]) != -1:
62
- subset[j] -= 1
63
-
64
- candidate = candidate[:18]
65
-
66
- for i in range(18):
67
- candidate[i][0] *= 384
68
- candidate[i][1] *= 512
69
-
70
- keypoints = {"pose_keypoints_2d": candidate}
71
- # with open("/home/aigc/ProjectVTON/OpenPose/keypoints/keypoints.json", "w") as f:
72
- # json.dump(keypoints, f)
73
- #
74
- # # print(candidate)
75
- # output_image = cv2.resize(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB), (768, 1024))
76
- # cv2.imwrite('/home/aigc/ProjectVTON/OpenPose/keypoints/out_pose.jpg', output_image)
77
-
78
- return keypoints
79
-
80
-
81
- if __name__ == '__main__':
82
-
83
- model = OpenPose()
84
- model('./images/bad_model.jpg')
 
1
+ import pdb
2
+
3
+ # import config
4
+ from pathlib import Path
5
+ import sys
6
+
7
+ PROJECT_ROOT = Path(__file__).absolute().parents[0].absolute()
8
+ sys.path.insert(0, str(PROJECT_ROOT))
9
+ import os
10
+
11
+ import cv2
12
+ import einops
13
+ import numpy as np
14
+ import random
15
+ import time
16
+ import json
17
+
18
+ # from pytorch_lightning import seed_everything
19
+ from preprocess.openpose.annotator.util import resize_image, HWC3
20
+ from preprocess.openpose.annotator.openpose import OpenposeDetector
21
+
22
+ import argparse
23
+ from PIL import Image
24
+ import torch
25
+ import pdb
26
+
27
+ # os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3'
28
+
29
+ class OpenPose:
30
+ def __init__(self, gpu_id: int):
31
+ # self.gpu_id = gpu_id
32
+ # torch.cuda.set_device(gpu_id)
33
+ self.preprocessor = OpenposeDetector()
34
+
35
+ def __call__(self, input_image, resolution=384):
36
+ # torch.cuda.set_device(self.gpu_id)
37
+ if isinstance(input_image, Image.Image):
38
+ input_image = np.asarray(input_image)
39
+ elif type(input_image) == str:
40
+ input_image = np.asarray(Image.open(input_image))
41
+ else:
42
+ raise ValueError
43
+ with torch.no_grad():
44
+ input_image = HWC3(input_image)
45
+ input_image = resize_image(input_image, resolution)
46
+ H, W, C = input_image.shape
47
+ assert (H == 512 and W == 384), 'Incorrect input image shape'
48
+ pose, detected_map = self.preprocessor(input_image, hand_and_face=False)
49
+
50
+ candidate = pose['bodies']['candidate']
51
+ subset = pose['bodies']['subset'][0][:18]
52
+ for i in range(18):
53
+ if subset[i] == -1:
54
+ candidate.insert(i, [0, 0])
55
+ for j in range(i, 18):
56
+ if(subset[j]) != -1:
57
+ subset[j] += 1
58
+ elif subset[i] != i:
59
+ candidate.pop(i)
60
+ for j in range(i, 18):
61
+ if(subset[j]) != -1:
62
+ subset[j] -= 1
63
+
64
+ candidate = candidate[:18]
65
+
66
+ for i in range(18):
67
+ candidate[i][0] *= 384
68
+ candidate[i][1] *= 512
69
+
70
+ keypoints = {"pose_keypoints_2d": candidate}
71
+ # with open("/home/aigc/ProjectVTON/OpenPose/keypoints/keypoints.json", "w") as f:
72
+ # json.dump(keypoints, f)
73
+ #
74
+ # # print(candidate)
75
+ # output_image = cv2.resize(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB), (768, 1024))
76
+ # cv2.imwrite('/home/aigc/ProjectVTON/OpenPose/keypoints/out_pose.jpg', output_image)
77
+
78
+ return keypoints
79
+
80
+
81
+ if __name__ == '__main__':
82
+
83
+ model = OpenPose()
84
+ model('./images/bad_model.jpg')