-
Notifications
You must be signed in to change notification settings - Fork 21
/
wrapper_hassan.py
131 lines (109 loc) · 3.8 KB
/
wrapper_hassan.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
import sys
print("Running {}".format(sys.argv[0]))
test_pre = sys.argv[1]
test_post = sys.argv[2]
test_localization = sys.argv[3]
test_damage = sys.argv[4]
import torch, torchvision
from detectron2.evaluation.xview_evaluation import *
# Some basic setup
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
# import some common libraries
import numpy as np
import cv2
# import some common detectron2 utilities
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import os
import numpy as np
import json
import shutil
import cv2
import random
import glob
import matplotlib.pyplot as plt
# Load damage model.
cfg_damage = get_cfg()
DAMAGE_MODEL_CONFIG = "./configs/xview/joint-11.yaml"
cfg_damage.merge_from_file(DAMAGE_MODEL_CONFIG)
# Load damage checkpoint.
cfg_damage.MODEL.WEIGHTS = os.path.join("model_weights.pth")
cfg_damage.MODEL.DEVICE = "cpu"
# cfg_damage.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set the testing threshold for this model
predictor_damage = DefaultPredictor(cfg_damage)
# Load localization model.
cfg_localization = get_cfg()
LOCALIZATION_MODEL_CONFIG = "./configs/xview/joint-11.yaml"
cfg_localization.merge_from_file(LOCALIZATION_MODEL_CONFIG)
cfg_localization.MODEL.WEIGHTS = os.path.join("hassan_model_weights.pth")
cfg_localization.MODEL.DEVICE = "cpu"
predictor_localization = DefaultPredictor(cfg_localization)
temp = np.zeros((1024, 1024)).astype(int)
temp_localization = np.zeros((1024, 1024)).astype(int)
# Load the images.
image = cv2.imread(test_post)
pre_image = cv2.imread(test_pre)
# tl
temp_image = image[0:512,0:512]
temp_pre_image = pre_image[0:512,0:512]
outputs = predictor_damage(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
tl = np.array(output, dtype=np.int)
# tr
temp_image = image[0:512,512:1024]
temp_pre_image = pre_image[0:512,512:1024]
outputs = predictor_damage(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
tr = np.array(output, dtype=np.int)
# bl
temp_image = image[512:1024,0:512]
temp_pre_image = pre_image[512:1024,0:512]
outputs = predictor_damage(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
bl = np.array(output, dtype=np.int)
# br
temp_image = image[512:1024,512:1024]
temp_pre_image = pre_image[512:1024,512:1024]
outputs = predictor_damage(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
br = np.array(output, dtype=np.int)
temp[0:512,0:512] = tl
temp[0:512,512:1024] = tr
temp[512:1024,0:512] = bl
temp[512:1024,512:1024] = br
# tl
temp_image = image[0:512,0:512]
temp_pre_image = pre_image[0:512,0:512]
outputs = predictor_localization(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
tl = np.array(output, dtype=np.int)
# tr
temp_image = image[0:512,512:1024]
temp_pre_image = pre_image[0:512,512:1024]
outputs = predictor_localization(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
tr = np.array(output, dtype=np.int)
# bl
temp_image = image[512:1024,0:512]
temp_pre_image = pre_image[512:1024,0:512]
outputs = predictor_localization(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
bl = np.array(output, dtype=np.int)
# br
temp_image = image[512:1024,512:1024]
temp_pre_image = pre_image[512:1024,512:1024]
outputs = predictor_localization(temp_image, temp_pre_image)
output = outputs["sem_seg"].argmax(dim=0).cpu()
br = np.array(output, dtype=np.int)
temp_localization[0:512,0:512] = tl
temp_localization[0:512,512:1024] = tr
temp_localization[512:1024,0:512] = bl
temp_localization[512:1024,512:1024] = br
# Write to filepaths.
cv2.imwrite(test_localization, temp_localization)
cv2.imwrite(test_damage, temp)