generated from cavalleria/pytorch-template
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbase_inference.py
173 lines (140 loc) · 4.99 KB
/
base_inference.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#------------------------------------------------------------------------------
# Libraries
#------------------------------------------------------------------------------
import cv2, torch
import numpy as np
from time import time
from torch.nn import functional as F
#------------------------------------------------------------------------------
# BaseInference
#------------------------------------------------------------------------------
class BaseInference(object):
def __init__(self, model, color_f=[255,0,0], color_b=[0,0,255], kernel_sz=25, sigma=0, background_path=None):
self.model = model
self.color_f = color_f
self.color_b = color_b
self.kernel_sz = kernel_sz
self.sigma = sigma
self.background_path = background_path
if background_path is not None:
self.background = cv2.imread(background_path)[...,::-1]
self.background = self.background.astype(np.float32)
def load_image(self):
raise NotImplementedError
def preprocess(self, image, *args):
raise NotImplementedError
def predict(self, X):
raise NotImplementedError
def draw_matting(self, image, mask):
"""
image (np.uint8) shape (H,W,3)
mask (np.float32) range from 0 to 1, shape (H,W)
"""
mask = 255*(1.0-mask)
mask = np.expand_dims(mask, axis=2)
mask = np.tile(mask, (1,1,3))
mask = mask.astype(np.uint8)
image_alpha = cv2.add(image, mask)
return image_alpha
def draw_transperency(self, image, mask):
"""
image (np.uint8) shape (H,W,3)
mask (np.float32) range from 0 to 1, shape (H,W)
"""
mask = mask.round()
alpha = np.zeros_like(image, dtype=np.uint8)
alpha[mask==1, :] = self.color_f
alpha[mask==0, :] = self.color_b
image_alpha = cv2.add(image, alpha)
return image_alpha
def draw_background(self, image, mask):
"""
image (np.uint8) shape (H,W,3)
mask (np.float32) range from 0 to 1, shape (H,W)
"""
image = image.astype(np.float32)
mask_filtered = cv2.GaussianBlur(mask, (self.kernel_sz, self.kernel_sz), self.sigma)
mask_filtered = np.expand_dims(mask_filtered, axis=2)
mask_filtered = np.tile(mask_filtered, (1,1,3))
image_alpha = image*mask_filtered + self.background*(1-mask_filtered)
return image_alpha.astype(np.uint8)
#------------------------------------------------------------------------------
# VideoInference
#------------------------------------------------------------------------------
class VideoInference(BaseInference):
def __init__(self, model, video_path, input_size, use_cuda=True, draw_mode='matting',
color_f=[255,0,0], color_b=[0,0,255], kernel_sz=25, sigma=0, background_path=None):
# Initialize
super(VideoInference, self).__init__(model, color_f, color_b, kernel_sz, sigma, background_path)
self.input_size = input_size
self.use_cuda = use_cuda
self.draw_mode = draw_mode
if draw_mode=='matting':
self.draw_func = self.draw_matting
elif draw_mode=='transperency':
self.draw_func = self.draw_transperency
elif draw_mode=='background':
self.draw_func = self.draw_background
else:
raise NotImplementedError
# Preprocess
self.mean = np.array([0.485,0.456,0.406])[None,None,:]
self.std = np.array([0.229,0.224,0.225])[None,None,:]
# Read video
self.video_path = video_path
self.cap = cv2.VideoCapture(video_path)
_, frame = self.cap.read()
self.H, self.W = frame.shape[:2]
def load_image(self):
_, frame = self.cap.read()
image = frame[...,::-1]
return image
def preprocess(self, image):
image = cv2.resize(image, (self.input_size,self.input_size), interpolation=cv2.INTER_LINEAR)
image = image.astype(np.float32) / 255.0
image = (image - self.mean) / self.std
X = np.transpose(image, axes=(2, 0, 1))
X = np.expand_dims(X, axis=0)
X = torch.tensor(X, dtype=torch.float32)
return X
def predict(self, X):
with torch.no_grad():
if self.use_cuda:
mask = self.model(X.cuda())
mask = F.interpolate(mask, size=(self.H, self.W), mode='bilinear', align_corners=True)
mask = F.softmax(mask, dim=1)
mask = mask[0,1,...].cpu().numpy()
else:
mask = self.model(X)
mask = F.interpolate(mask, size=(self.H, self.W), mode='bilinear', align_corners=True)
mask = F.softmax(mask, dim=1)
mask = mask[0,1,...].numpy()
return mask
def run(self):
while(True):
# Read frame from camera
start_time = time()
image = self.load_image()
read_cam_time = time()
# Preprocess
X = self.preprocess(image)
preproc_time = time()
# Predict
mask = self.predict(X)
predict_time = time()
# Draw result
image_alpha = self.draw_func(image, mask)
draw_time = time()
# Wait for interupt
cv2.imshow('webcam', image_alpha[..., ::-1])
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Print runtime
read = read_cam_time-start_time
preproc = preproc_time-read_cam_time
pred = predict_time-preproc_time
draw = draw_time-predict_time
total = read + preproc + pred + draw
fps = 1 / total
print("read: %.3f [s]; preproc: %.3f [s]; pred: %.3f [s]; draw: %.3f [s]; total: %.3f [s]; fps: %.2f [Hz]" %
(read, preproc, pred, draw, total, fps))