-
Notifications
You must be signed in to change notification settings - Fork 138
/
predict.py
298 lines (272 loc) · 9.94 KB
/
predict.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import sys
import json
from typing import Optional
import cv2
import torch
from PIL import Image
import mmcv
from mmdet.core.visualization.image import imshow_det_bboxes
import numpy as np
import pycocotools.mask as maskUtils
from transformers import CLIPProcessor, CLIPModel
from transformers import AutoProcessor, CLIPSegForImageSegmentation
from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation
from transformers import BlipProcessor, BlipForConditionalGeneration
from cog import BasePredictor, Input, Path, BaseModel
sys.path.append("..")
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
sys.path.insert(0, "scripts")
from configs.ade20k_id2label import CONFIG as CONFIG_ADE20K_ID2LABEL
from configs.coco_id2label import CONFIG as CONFIG_COCO_ID2LABEL
from clip import clip_classification
from clipseg import clipseg_segmentation
from oneformer import oneformer_coco_segmentation, oneformer_ade20k_segmentation
from blip import open_vocabulary_classification_blip
MODEL_CACHE = "model_cache"
class ModelOutput(BaseModel):
json_out: Optional[Path]
img_out: Path
class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
sam_checkpoint = "sam_vit_h_4b8939.pth"
model_type = "default"
self.sam = sam_model_registry[model_type](checkpoint=sam_checkpoint).to("cuda")
self.generator = SamAutomaticMaskGenerator(self.sam, output_mode="coco_rle")
# semantic segmentation
rank = 0
# the following models are pre-downloaded and cached to MODEL_CACHE to speed up inference
self.clip_processor = CLIPProcessor.from_pretrained(
"openai/clip-vit-large-patch14",
cache_dir=MODEL_CACHE,
local_files_only=True,
)
self.clip_model = CLIPModel.from_pretrained(
"openai/clip-vit-large-patch14",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to(rank)
self.oneformer_ade20k_processor = OneFormerProcessor.from_pretrained(
"shi-labs/oneformer_ade20k_swin_large",
cache_dir=MODEL_CACHE,
local_files_only=True,
)
self.oneformer_ade20k_model = OneFormerForUniversalSegmentation.from_pretrained(
"shi-labs/oneformer_ade20k_swin_large",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to(rank)
self.oneformer_coco_processor = OneFormerProcessor.from_pretrained(
"shi-labs/oneformer_coco_swin_large",
cache_dir=MODEL_CACHE,
local_files_only=True,
)
self.oneformer_coco_model = OneFormerForUniversalSegmentation.from_pretrained(
"shi-labs/oneformer_coco_swin_large",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to(rank)
self.blip_processor = BlipProcessor.from_pretrained(
"Salesforce/blip-image-captioning-large",
cache_dir=MODEL_CACHE,
local_files_only=True,
)
self.blip_model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-large",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to(rank)
self.clipseg_processor = AutoProcessor.from_pretrained(
"CIDAS/clipseg-rd64-refined",
cache_dir=MODEL_CACHE,
local_files_only=True,
)
self.clipseg_model = CLIPSegForImageSegmentation.from_pretrained(
"CIDAS/clipseg-rd64-refined",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to(rank)
self.clipseg_processor.image_processor.do_resize = False
def predict(
self,
image: Path = Input(description="Input image"),
output_json: bool = Input(default=True, description="return raw json output"),
) -> ModelOutput:
"""Run a single prediction on the model"""
img = cv2.imread(str(image))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
masks = self.generator.generate(img)
seg_json = "/tmp/mask.json"
with open(seg_json, "w") as f:
json.dump(masks, f)
json_out = "/tmp/seg_out.json"
seg_out = "/tmp/seg_out.png"
semantic_annotation_pipeline(
seg_json,
str(image),
json_out,
seg_out,
clip_processor=self.clip_processor,
clip_model=self.clip_model,
oneformer_ade20k_processor=self.oneformer_ade20k_processor,
oneformer_ade20k_model=self.oneformer_ade20k_model,
oneformer_coco_processor=self.oneformer_coco_processor,
oneformer_coco_model=self.oneformer_coco_model,
blip_processor=self.blip_processor,
blip_model=self.blip_model,
clipseg_processor=self.clipseg_processor,
clipseg_model=self.clipseg_model,
)
return ModelOutput(
json_out=Path(json_out) if output_json else None, img_out=Path(seg_out)
)
def semantic_annotation_pipeline(
seg_json,
image,
json_out,
seg_out,
rank=0,
scale_small=1.2,
scale_large=1.6,
clip_processor=None,
clip_model=None,
oneformer_ade20k_processor=None,
oneformer_ade20k_model=None,
oneformer_coco_processor=None,
oneformer_coco_model=None,
blip_processor=None,
blip_model=None,
clipseg_processor=None,
clipseg_model=None,
):
anns = mmcv.load(seg_json)
img = mmcv.imread(image)
bitmasks, class_names = [], []
class_ids_from_oneformer_coco = oneformer_coco_segmentation(
Image.fromarray(img), oneformer_coco_processor, oneformer_coco_model, 0
)
class_ids_from_oneformer_ade20k = oneformer_ade20k_segmentation(
Image.fromarray(img), oneformer_ade20k_processor, oneformer_ade20k_model, 0
)
for ann in anns:
valid_mask = torch.tensor(maskUtils.decode(ann["segmentation"])).bool()
# get the class ids of the valid pixels
coco_propose_classes_ids = class_ids_from_oneformer_coco[valid_mask]
ade20k_propose_classes_ids = class_ids_from_oneformer_ade20k[valid_mask]
top_k_coco_propose_classes_ids = (
torch.bincount(coco_propose_classes_ids.flatten()).topk(1).indices
)
top_k_ade20k_propose_classes_ids = (
torch.bincount(ade20k_propose_classes_ids.flatten()).topk(1).indices
)
local_class_names = set()
local_class_names = set.union(
local_class_names,
set(
[
CONFIG_ADE20K_ID2LABEL["id2label"][str(class_id.item())]
for class_id in top_k_ade20k_propose_classes_ids
]
),
)
local_class_names = set.union(
local_class_names,
set(
(
[
CONFIG_COCO_ID2LABEL["refined_id2label"][str(class_id.item())]
for class_id in top_k_coco_propose_classes_ids
]
)
),
)
patch_small = mmcv.imcrop(
img,
np.array(
[
ann["bbox"][0],
ann["bbox"][1],
ann["bbox"][0] + ann["bbox"][2],
ann["bbox"][1] + ann["bbox"][3],
]
),
scale=scale_small,
)
patch_large = mmcv.imcrop(
img,
np.array(
[
ann["bbox"][0],
ann["bbox"][1],
ann["bbox"][0] + ann["bbox"][2],
ann["bbox"][1] + ann["bbox"][3],
]
),
scale=scale_large,
)
patch_huge = mmcv.imcrop(
img,
np.array(
[
ann["bbox"][0],
ann["bbox"][1],
ann["bbox"][0] + ann["bbox"][2],
ann["bbox"][1] + ann["bbox"][3],
]
),
scale=scale_large,
)
valid_mask_huge_crop = mmcv.imcrop(
valid_mask.numpy(),
np.array(
[
ann["bbox"][0],
ann["bbox"][1],
ann["bbox"][0] + ann["bbox"][2],
ann["bbox"][1] + ann["bbox"][3],
]
),
scale=scale_large,
)
op_class_list = open_vocabulary_classification_blip(
patch_large, blip_processor, blip_model, rank
)
local_class_list = list(
set.union(local_class_names, set(op_class_list))
) # , set(refined_imagenet_class_names)
mask_categories = clip_classification(
patch_small,
local_class_list,
3 if len(local_class_list) > 3 else len(local_class_list),
clip_processor,
clip_model,
rank,
)
class_ids_patch_huge = clipseg_segmentation(
patch_huge, mask_categories, clipseg_processor, clipseg_model, rank
).argmax(0)
top_1_patch_huge = (
torch.bincount(
class_ids_patch_huge[torch.tensor(valid_mask_huge_crop)].flatten()
)
.topk(1)
.indices
)
top_1_mask_category = mask_categories[top_1_patch_huge.item()]
ann["class_name"] = str(top_1_mask_category)
ann["class_proposals"] = mask_categories
class_names.append(ann["class_name"])
bitmasks.append(maskUtils.decode(ann["segmentation"]))
mmcv.dump(anns, json_out)
imshow_det_bboxes(
img,
bboxes=None,
labels=np.arange(len(bitmasks)),
segms=np.stack(bitmasks),
class_names=class_names,
font_size=25,
show=False,
out_file=seg_out,
)