-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsegearth_segmentor.py
More file actions
394 lines (341 loc) · 17.4 KB
/
segearth_segmentor.py
File metadata and controls
394 lines (341 loc) · 17.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import torch
import torch.nn as nn
import sys
sys.path.append("..")
from prompts.imagenet_template import *
from mmseg.models.segmentors import BaseSegmentor
from mmseg.models.data_preprocessor import SegDataPreProcessor
from mmengine.structures import PixelData
from mmseg.registry import MODELS
import torch.nn.functional as F
from open_clip import tokenizer, create_model
from BLIP.models.blip_retrieval import blip_retrieval
import gem
from simfeatup_dev.upsamplers import get_upsampler
@MODELS.register_module()
class SegEarthSegmentation(BaseSegmentor):
def __init__(self,
clip_type,
vit_type,
model_type,
name_path,
device=torch.device('cuda'),
ignore_residual=True,
prob_thd=0.0,
logit_scale=50,
slide_stride=112,
slide_crop=224,
cls_token_lambda=0,
bg_idx=0,
ssa_last_n_layers=1,
feature_up=True,
feature_up_cfg=dict(
model_name='jbu_one',
model_path='your/model/path')):
data_preprocessor = SegDataPreProcessor(
mean=[122.771, 116.746, 104.094],
std=[68.501, 66.632, 70.323],
bgr_to_rgb=True)
super().__init__(data_preprocessor=data_preprocessor)
if clip_type == 'CLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B/16', pretrained='openai', precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L-14', pretrained='openai', precision='fp16')
elif clip_type == 'CLIPSelf':
self.net = create_model('ViT-B/16', pretrained='checkpoint/openai_vitb16_coco_clipself_patches.pt', precision='fp16')
elif clip_type == 'AlignEarth':
if 'B' in vit_type:
self.net = create_model('ViT-B/16', pretrained='checkpoint/AlignEarth-SAR-ViT-B-16.pt', precision='fp16')
elif clip_type == 'RemoteCLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B/32', pretrained='checkpoint/RemoteCLIP-ViT-B-32.pt', precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L-14', pretrained='checkpoint/RemoteCLIP-ViT-L-14.pt', precision='fp16')
elif clip_type == 'GeoRSCLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B/32', pretrained='checkpoint/RS5M_ViT-B-32.pt', precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L-14', pretrained='checkpoint/RS5M_ViT-L-14.pt', precision='fp16')
elif 'H' in vit_type:
self.net = create_model('ViT-H-14', pretrained='checkpoint/RS5M_ViT-H-14.pt', precision='fp16')
elif clip_type == 'SkyCLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B/32', \
pretrained='checkpoint/SkyCLIP_ViT_B32_top50pct/epoch_20.pt', \
precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L-14', \
pretrained='checkpoint/SkyCLIP_ViT_L14_top30pct_filtered_by_CLIP_laion_RS/epoch_20.pt', \
precision='fp16')
elif clip_type == 'OpenCLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B/16', pretrained='laion2b_s34b_b88k', precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L-14', pretrained='laion2b_s32b_b82k', precision='fp16')
elif clip_type == 'MetaCLIP':
if 'B' in vit_type:
self.net = create_model('ViT-B-16-quickgelu', pretrained='checkpoint/MetaCLIP/b16_fullcc2.5b.pt', precision='fp16')
elif 'L' in vit_type:
self.net = create_model('ViT-L/14-quickgelu', pretrained='metaclip_fullcc', precision='fp16')
elif clip_type == 'BLIP':
if 'B' in vit_type:
self.net = blip_retrieval(pretrained='checkpoint/BLIP/model_base_14M.pth', image_size=slide_crop, vit='base')
elif 'L' in vit_type:
self.net = blip_retrieval(pretrained='checkpoint/model_large.pth', image_size=slide_crop, vit='large')
self.net = self.net.half()
elif clip_type == 'ALIP':
self.net = create_model('ViT-B/32', pretrained='checkpoint/ALIP_YFCC15M_B32.pt', precision='fp16')
if model_type == 'GEM':
if 'B' in vit_type:
if clip_type == 'CLIP':
self.net = gem.create_gem_model('ViT-B/16', 'openai', ignore_residual=ignore_residual, device=device, precision='fp16')
elif clip_type == 'OpenCLIP':
self.net = gem.create_gem_model('ViT-B/16', 'laion2b_s34b_b88k', ignore_residual=ignore_residual, device=device, precision='fp16')
elif clip_type == 'MetaCLIP':
self.net = gem.create_gem_model('ViT-B/16-quickgelu', 'metaclip_fullcc', ignore_residual=ignore_residual, device=device, precision='fp16')
elif 'L' in vit_type:
if clip_type == 'CLIP':
self.net = gem.create_gem_model('ViT-L-14', 'openai', ignore_residual=ignore_residual, device=device, precision='fp16')
elif clip_type == 'OpenCLIP':
self.net = gem.create_gem_model('ViT-L-14', 'laion2b_s32b_b82k', ignore_residual=ignore_residual, device=device, precision='fp16')
elif clip_type == 'MetaCLIP':
self.net = gem.create_gem_model('ViT-L-14-quickgelu', 'metaclip_fullcc', ignore_residual=ignore_residual, device=device, precision='fp16')
self.net = self.net.model
self.net.eval().to(device)
self.tokenizer = tokenizer.tokenize
self.clip_type = clip_type
self.vit_type = vit_type
self.model_type = model_type
self.feature_up = feature_up
self.cls_token_lambda = cls_token_lambda
self.output_cls_token = cls_token_lambda != 0
self.bg_idx = bg_idx
self.ssa_last_n_layers = ssa_last_n_layers
if self.clip_type == 'BLIP':
self.patch_size = self.net.visual_encoder.patch_size
else:
self.patch_size = self.net.visual.patch_size
query_words, self.query_idx = get_cls_idx(name_path)
self.num_queries = len(query_words)
self.num_classes = max(self.query_idx) + 1
self.query_idx = torch.Tensor(self.query_idx).to(torch.int64).to(device)
query_features = []
with torch.no_grad(): # sub_imagenet_template, openai_imagenet_template
for qw in query_words:
if self.clip_type == 'BLIP':
query =self.net.tokenizer([temp(qw) for temp in openai_imagenet_template], padding='max_length',
truncation=True, max_length=35,
return_tensors="pt").to(device)
text_output = self.net.text_encoder(query.input_ids, attention_mask=query.attention_mask,
mode='text')
feature = F.normalize(self.net.text_proj(text_output.last_hidden_state[:, 0, :]))
else:
query = self.tokenizer([temp(qw) for temp in openai_imagenet_template]).to(device)
feature = self.net.encode_text(query)
feature /= feature.norm(dim=-1, keepdim=True)
feature = feature.mean(dim=0)
feature /= feature.norm()
query_features.append(feature.unsqueeze(0))
self.query_features = torch.cat(query_features, dim=0)
self.dtype = self.query_features.dtype
self.ignore_residual = ignore_residual
self.logit_scale = logit_scale
self.prob_thd = prob_thd
self.slide_stride = slide_stride
self.slide_crop = slide_crop
if feature_up:
self.feat_dim = self.query_features.shape[-1]
self.upsampler = get_upsampler(feature_up_cfg['model_name'], self.feat_dim).cuda().half()
ckpt = torch.load(feature_up_cfg['model_path'])['state_dict']
weights_dict = {k[10:]: v for k, v in ckpt.items()}
self.upsampler.load_state_dict(weights_dict, strict=True)
def forward_feature(self, img, logit_size=None):
if type(img) == list:
img = img[0]
if self.clip_type == 'BLIP':
img = F.interpolate(img, size=(self.slide_crop, self.slide_crop), mode='bilinear', align_corners=False)
image_features = self.net.visual_encoder(img, self.ignore_residual)
image_features = self.net.vision_proj(image_features[:, 1:, ])
elif self.model_type == 'GEM':
image_features = self.net.visual(img)
else:
image_features = self.net.encode_image(img, self.model_type,
self.ignore_residual,
self.output_cls_token,
last_n_layers=self.ssa_last_n_layers)
if self.output_cls_token:
image_cls_token, image_features = image_features
image_cls_token /= image_cls_token.norm(dim=-1, keepdim=True)
cls_logits = image_cls_token @ self.query_features.T
# featup
if self.feature_up:
feature_w, feature_h = img[0].shape[-2] // self.patch_size[0], img[0].shape[-1] // self.patch_size[1]
image_w, image_h = img[0].shape[-2], img[0].shape[-1]
image_features = image_features.permute(0, 2, 1).view(1, self.feat_dim, feature_w, feature_h)
with torch.cuda.amp.autocast():
image_features = self.upsampler(image_features, img).half()
image_features = image_features.view(1, self.feat_dim, image_w * image_h).permute(0, 2, 1)
image_features /= image_features.norm(dim=-1, keepdim=True)
logits = image_features @ self.query_features.T
if self.output_cls_token:
logits = logits + cls_logits * self.cls_token_lambda
# # CLIP Surgery
# # weights to restrain influence of obvious classes on others
# prob = (cls_logits * 2).softmax(-1)
# w = prob / prob.mean(-1, keepdim=True)
# # element-wise multiplied features
# b, n_t, n_i, c = image_features.shape[0], self.query_features.shape[0], image_features.shape[1], image_features.shape[2]
# feats = image_features.reshape(b, n_i, 1, c) * self.query_features.reshape(1, 1, n_t, c)
# feats *= w.reshape(1, 1, n_t, 1)
# redundant_feats = feats.mean(2, keepdim=True) # along cls dim
# feats = feats - redundant_feats
# # sum the element-wise multiplied features as cosine similarity
# logits = feats.sum(-1)
if self.feature_up:
w, h = img[0].shape[-2], img[0].shape[-1]
else:
w, h = img[0].shape[-2] // self.patch_size[0], img[0].shape[-1] // self.patch_size[1]
out_dim = logits.shape[-1]
logits = logits.permute(0, 2, 1).reshape(-1, out_dim, w, h)
if logit_size == None:
logits = nn.functional.interpolate(logits, size=img.shape[-2:], mode='bilinear')
else:
logits = nn.functional.interpolate(logits, size=logit_size, mode='bilinear')
return logits
def forward_slide(self, img, img_metas, stride=112, crop_size=224):
"""Inference by sliding-window with overlap.
If h_crop > h_img or w_crop > w_img, the small patch will be used to
decode without padding.
"""
if type(img) == list:
img = img[0].unsqueeze(0)
if type(stride) == int:
stride = (stride, stride)
if type(crop_size) == int:
crop_size = (crop_size, crop_size)
h_stride, w_stride = stride
h_crop, w_crop = crop_size
batch_size, _, h_img, w_img = img.shape
out_channels = self.num_queries
h_grids = max(h_img - h_crop + h_stride - 1, 0) // h_stride + 1
w_grids = max(w_img - w_crop + w_stride - 1, 0) // w_stride + 1
preds = img.new_zeros((batch_size, out_channels, h_img, w_img))
count_mat = img.new_zeros((batch_size, 1, h_img, w_img))
for h_idx in range(h_grids):
for w_idx in range(w_grids):
y1 = h_idx * h_stride
x1 = w_idx * w_stride
y2 = min(y1 + h_crop, h_img)
x2 = min(x1 + w_crop, w_img)
y1 = max(y2 - h_crop, 0)
x1 = max(x2 - w_crop, 0)
crop_img = img[:, :, y1:y2, x1:x2]
# pad image when (image_size % patch_size != 0)
H, W = crop_img.shape[2:]
pad = self.compute_padsize(H, W, self.patch_size[0])
if any(pad):
crop_img = nn.functional.pad(crop_img, pad)
crop_seg_logit = self.forward_feature(crop_img)
# mask cutting for padded image
if any(pad):
l, t = pad[0], pad[2]
crop_seg_logit = crop_seg_logit[:, :, t:t + H, l:l + W]
preds += nn.functional.pad(crop_seg_logit,
(int(x1), int(preds.shape[3] - x2), int(y1),
int(preds.shape[2] - y2)))
count_mat[:, :, y1:y2, x1:x2] += 1
assert (count_mat == 0).sum() == 0
preds = preds / count_mat
img_size = img_metas[0]['ori_shape'][:2]
logits = nn.functional.interpolate(preds, size=img_size, mode='bilinear')
return logits
@torch.no_grad()
def predict(self, inputs, data_samples):
if data_samples is not None:
batch_img_metas = [
data_sample.metainfo for data_sample in data_samples
]
else:
batch_img_metas = [
dict(
ori_shape=inputs.shape[2:],
img_shape=inputs.shape[2:],
pad_shape=inputs.shape[2:],
padding_size=[0, 0, 0, 0])
] * inputs.shape[0]
inputs = inputs.half()
if self.slide_crop > 0:
seg_logits = self.forward_slide(inputs, batch_img_metas, self.slide_stride, self.slide_crop)
else:
seg_logits = self.forward_feature(inputs, batch_img_metas[0]['ori_shape'])
return self.postprocess_result(seg_logits, data_samples)
def postprocess_result(self, seg_logits, data_samples):
batch_size = seg_logits.shape[0]
for i in range(batch_size):
seg_logits = seg_logits[i] * self.logit_scale
seg_logits = seg_logits.softmax(0) # n_queries * w * h
num_cls, num_queries = max(self.query_idx) + 1, len(self.query_idx)
if num_cls != num_queries:
seg_logits = seg_logits.unsqueeze(0)
cls_index = nn.functional.one_hot(self.query_idx)
cls_index = cls_index.T.view(num_cls, num_queries, 1, 1)
seg_logits = (seg_logits * cls_index).max(1)[0]
seg_pred = seg_logits.argmax(0, keepdim=True)
seg_pred[seg_logits.max(0, keepdim=True)[0] < self.prob_thd] = self.bg_idx
# Best
# sorted_probs, _ = torch.sort(seg_logits, dim=0, descending=True)
# prob_top1 = sorted_probs[0, :, :]
# prob_top2 = sorted_probs[1, :, :]
# margin = prob_top1 - prob_top2
# seg_pred[margin.unsqueeze(0) < 0.1] = 255
# from collections import Counter
# print(Counter(seg_pred.cpu().numpy().reshape(-1)))
if data_samples is None:
return seg_pred
else:
data_samples[i].set_data({
'seg_logits':
PixelData(**{'data': seg_logits}),
'pred_sem_seg':
PixelData(**{'data': seg_pred})
})
return data_samples
def compute_padsize(self, H: int, W: int, patch_size: int):
l, r, t, b = 0, 0, 0, 0
if W % patch_size:
lr = patch_size - (W % patch_size)
l = lr // 2
r = lr - l
if H % patch_size:
tb = patch_size - (H % patch_size)
t = tb // 2
b = tb - t
return l, r, t, b
def _forward(data_samples):
"""
"""
def inference(self, img, batch_img_metas):
"""
"""
def encode_decode(self, inputs, batch_img_metas):
"""
"""
def extract_feat(self, inputs):
"""
"""
def loss(self, inputs, data_samples):
"""
"""
def get_cls_idx(path):
with open(path, 'r') as f:
name_sets = f.readlines()
num_cls = len(name_sets)
class_names, class_indices = [], []
for idx in range(num_cls):
names_i = name_sets[idx].split(',')
names_i = [i.strip() for i in names_i]
class_names += names_i
class_indices += [idx for _ in range(len(names_i))]
class_names = [item.replace('\n', '') for item in class_names]
return class_names, class_indices