-
Notifications
You must be signed in to change notification settings - Fork 0
/
seggpt_engine.py
214 lines (165 loc) · 6.85 KB
/
seggpt_engine.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'''
File : seggpt_engine.py
Date : 2024/1/8
Author : Feiyu Zhu
Detail : map_to_color_class函数
'''
import torch
import torch.nn.functional as F
import numpy as np
import cv2
from PIL import Image
imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_std = np.array([0.229, 0.224, 0.225])
def map_to_color_class(rgb_pixel):
if np.all(rgb_pixel < 85): # 像素值较低,映射为黑色
return (0, 0, 0)
elif np.all(rgb_pixel > 170): # 像素值较高,映射为白色
return (255, 255, 255)
elif np.argmax(rgb_pixel) == 0: # 红色通道值最高,映射为红色
return (255, 0, 0)
else: # 其他情况,映射为绿色
return (0, 255, 0)
def rgb_to_grayscale(rgb_image):
# 使用彩色到灰度图像的标准转换公式
return np.dot(rgb_image[...,:3], [0.2989, 0.5870, 0.1140])
class Cache(list):
def __init__(self, max_size=0):
super().__init__()
self.max_size = max_size
def append(self, x):
if self.max_size <= 0:
return
super().append(x)
if len(self) > self.max_size:
self.pop(0)
@torch.no_grad()
def run_one_image(img, tgt, model, device):
x = torch.tensor(img)
# make it a batch-like
x = torch.einsum('nhwc->nchw', x)
tgt = torch.tensor(tgt)
# make it a batch-like
tgt = torch.einsum('nhwc->nchw', tgt)
bool_masked_pos = torch.zeros(model.patch_embed.num_patches)
bool_masked_pos[model.patch_embed.num_patches//2:] = 1
bool_masked_pos = bool_masked_pos.unsqueeze(dim=0)
valid = torch.ones_like(tgt)
if model.seg_type == 'instance':
seg_type = torch.ones([valid.shape[0], 1])
else:
seg_type = torch.zeros([valid.shape[0], 1])
feat_ensemble = 0 if len(x) > 1 else -1
_, y, mask = model(x.float().to(device), tgt.float().to(device), bool_masked_pos.to(device), valid.float().to(device), seg_type.to(device), feat_ensemble)
y = model.unpatchify(y)
y = torch.einsum('nchw->nhwc', y).detach().cpu()
output = y[0, y.shape[1]//2:, :, :]
output = torch.clip((output * imagenet_std + imagenet_mean) * 255, 0, 255)
return output
def inference_image(model, device, img_path, img2_paths, tgt2_paths, out_path):
res, hres = 448, 448
image = Image.open(img_path).convert("RGB")
input_image = np.array(image)
size = image.size
image = np.array(image.resize((res, hres))) / 255.
image_batch, target_batch = [], []
# print(len(img2_paths), len(tgt2_paths))
for img2_path, tgt2_path in zip(img2_paths, tgt2_paths):
img2 = Image.open(img2_path).convert("RGB")
img2 = img2.resize((res, hres))
img2 = np.array(img2) / 255.
tgt2 = Image.open(tgt2_path).convert("RGB")
tgt2 = tgt2.resize((res, hres), Image.NEAREST)
tgt2 = np.array(tgt2) / 255.
tgt = tgt2 # tgt is not available
tgt = np.concatenate((tgt2, tgt), axis=0)
img = np.concatenate((img2, image), axis=0)
assert img.shape == (2*res, res, 3), f'{img.shape}'
# normalize by ImageNet mean and std
img = img - imagenet_mean
img = img / imagenet_std
assert tgt.shape == (2*res, res, 3), f'{img.shape}'
# normalize by ImageNet mean and std
tgt = tgt - imagenet_mean
tgt = tgt / imagenet_std
image_batch.append(img)
target_batch.append(tgt)
img = np.stack(image_batch, axis=0)
tgt = np.stack(target_batch, axis=0)
"""### Run SegGPT on the image"""
# make random mask reproducible (comment out to make it change)
torch.manual_seed(2)
output = run_one_image(img, tgt, model, device)
output = F.interpolate(
output[None, ...].permute(0, 3, 1, 2),
size=[size[1], size[0]],
mode='nearest',
).permute(0, 2, 3, 1)[0].numpy()
mapped_output = np.apply_along_axis(map_to_color_class, -1, output)
# gray_image = rgb_to_grayscale(output).astype(np.uint8)
# mapped_output = np.vectorize(map_to_nearest_class)(gray_image)
# rgb_from_gray = np.stack((mapped_output,)*3, axis=-1)
# output = Image.fromarray((input_image * (0.6 * output / 255 + 0.4)).astype(np.uint8))
output = Image.fromarray(((mapped_output)).astype(np.uint8))
output.save(out_path)
def inference_video(model, device, vid_path, num_frames, img2_paths, tgt2_paths, out_path):
res, hres = 448, 448
cap = cv2.VideoCapture(vid_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(out_path, fourcc, fps, (width, height), True)
if img2_paths is None:
_, frame = cap.read()
img2 = Image.fromarray(frame[:, :, ::-1]).convert('RGB')
else:
img2 = Image.open(img2_paths[0]).convert("RGB")
img2 = img2.resize((res, hres))
img2 = np.array(img2) / 255.
tgt2 = Image.open(tgt2_paths[0]).convert("RGB")
tgt2 = tgt2.resize((res, hres), Image.NEAREST)
tgt2 = np.array(tgt2) / 255.
frames_cache, target_cache = Cache(num_frames), Cache(num_frames)
while True:
ret, frame = cap.read()
if not ret:
break
image_batch, target_batch = [], []
image = Image.fromarray(frame[:, :, ::-1]).convert('RGB')
input_image = np.array(image)
size = image.size
image = np.array(image.resize((res, hres))) / 255.
for prompt, target in zip([img2] + frames_cache, [tgt2] + target_cache):
tgt = target # tgt is not available
tgt = np.concatenate((target, tgt), axis=0)
img = np.concatenate((prompt, image), axis=0)
assert img.shape == (2*res, res, 3), f'{img.shape}'
# normalize by ImageNet mean and std
img = img - imagenet_mean
img = img / imagenet_std
assert tgt.shape == (2*res, res, 3), f'{img.shape}'
# normalize by ImageNet mean and std
tgt = tgt - imagenet_mean
tgt = tgt / imagenet_std
image_batch.append(img)
target_batch.append(tgt)
img = np.stack(image_batch, axis=0)
tgt = np.stack(target_batch, axis=0)
torch.manual_seed(2)
output = run_one_image(img, tgt, model, device)
frames_cache.append(image)
target_cache.append(
output.mean(-1) \
.gt(128).float() \
.unsqueeze(-1).expand(-1, -1, 3) \
.numpy()
)
output = F.interpolate(
output[None, ...].permute(0, 3, 1, 2),
size=[size[1], size[0]],
mode='nearest',
).permute(0, 2, 3, 1)[0].numpy()
output = input_image * (0.6 * output / 255 + 0.4)
video_writer.write(np.ascontiguousarray(output.astype(np.uint8)[:, :, ::-1]))
video_writer.release()