-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
52 lines (43 loc) · 1.68 KB
/
demo.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
from PIL import Image
import matplotlib.pyplot as plt
from torchvision import transforms
from segearth_segmentor import SegEarthSegmentation
img = Image.open('demo/oem_koeln_50.tif')
name_list = ['background', 'bareland,barren', 'grass', 'pavement', 'road',
'tree,forest', 'water,river', 'cropland', 'building,roof,house']
with open('./configs/my_name.txt', 'w') as writers:
for i in range(len(name_list)):
if i == len(name_list)-1:
writers.write(name_list[i])
else:
writers.write(name_list[i] + '\n')
writers.close()
img_tensor = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.48145466, 0.4578275, 0.40821073], [0.26862954, 0.26130258, 0.27577711]),
transforms.Resize((448, 448))
])(img)
img_tensor = img_tensor.unsqueeze(0).to('cuda')
model = SegEarthSegmentation(
clip_type='CLIP', # 'CLIP', 'BLIP', 'OpenCLIP', 'MetaCLIP', 'ALIP', 'SkyCLIP', 'GeoRSCLIP', 'RemoteCLIP'
vit_type='ViT-B/16', # 'ViT-B/16', 'ViT-L-14'
model_type='SegEarth', # 'vanilla', 'MaskCLIP', 'GEM', 'SCLIP', 'ClearCLIP', 'SegEarth'
ignore_residual=True,
feature_up=True,
feature_up_cfg=dict(
model_name='jbu_one',
model_path='simfeatup_dev/weights/xclip_jbu_one_million_aid.ckpt'),
cls_token_lambda=-0.3,
name_path='./configs/my_name.txt',
prob_thd=0.1,
)
seg_pred = model.predict(img_tensor, data_samples=None)
seg_pred = seg_pred.data.cpu().numpy().squeeze(0)
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
ax[0].imshow(img)
ax[0].axis('off')
ax[1].imshow(seg_pred, cmap='viridis')
ax[1].axis('off')
plt.tight_layout()
# plt.show()
plt.savefig('seg_pred.png', bbox_inches='tight')