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 | def __run__(img, path, config, checkpoint, device = "cpu", min_size_test = 1000, img_side = 2000, over_n = 100, nms_crit = 0.5, thresh = 0, testing = False):
"""Run entire script with.
Args:
weights (str): path to pretrained weights
img (str): name of image to perform inference
path (str): path containing img and to save everything on; need to contain
min_size_test (int, optional): Shortest side of individual tile to be resized to, recommend original divided by 2 if resolution is same as training set. Defaults to 1000.
img_side (int, optional): Length of the side of individual tiles. Defaults to 2000.
over_n (int, optional): Overlap between tiles. Defaults to 100.
thresh (int, optional): Threshold score during tiling; nominally a value between 0 and 1. Defaults to 0.
"""
if thresh > 1:
thresh = 1
elif thresh < 0:
thresh = 0
else:
pass
if not testing:
model = detector(config, checkpoint, device)
image = cv2.imread(path + "/" + img)
if image.shape[0] < img_side:
image = pad_img(image, img_side)
elif image.shape[1] < img_side:
image = pad_img(image, img_side)
else:
pass
n,m = img_slice(image, path, img_size=img_side, overlap=over_n)
import glob
import numpy as np
images = glob.glob(path + '/imgs/*.jpg')
for imageName in images:
img_ = imageName
if "imgs\\" in img_:
name = imageName.split("imgs\\")[1]
else:
name = imageName.split("/")[len(imageName.split("/"))-1]
if not testing:
result = inference_detector(model, img_)
if os.path.exists(path + "/instance_res") == True:
pass
else:
os.makedirs(path + "/instance_res")
#perform mask NMS for the first time
labs = torch.ones((len(result[0][0])))
scores = torch.Tensor(result[0][0][:,4])
nms_res = mask_nms(torch.Tensor(np.asarray(result[1][0])), labs, scores)
new_result = nms_remove(result, nms_res, nms_crit)
#perform 2nd NMS to make sure everything is removed
labs = torch.ones((len(new_result[0][0])))
scores = torch.Tensor(new_result[0][0][:,4])
nms_res = mask_nms(torch.Tensor(np.asarray(new_result[1][0])), labs, scores)
new_result = nms_remove(new_result, nms_res, nms_crit)
model.show_result(img_, result, out_file=path + "/instance_res/" +name + ".jpg")
label_img = create_label_image(new_result, img_side)
if os.path.exists(path + "/labels") == True:
pass
else:
os.makedirs(path + "/labels")
np.savez(path + "/labels/full_" + name, bb = new_result[0], mask = new_result[1])
grid = (n,m)
orig_shape = (image.shape[0], image.shape[1])
img_side = img_side
over_n = over_n
tile_run_path = path + "/labels"
tiled_img, score_dict = tile_run(tile_run_path, grid, orig_shape, img_side, over_n, score_thresh = thresh)
np.save(path + "/labels/res.npy", tiled_img)
np.savez(path + "/labels/full_res.npz", img = tiled_img, scores = score_dict)
#combine real img and panoptic res:
img = plt.imread(path + "/" + img)
pan = np.load(path + "/labels/res.npy")
pan_mask = pan != 0
pan_mask = pan_mask.astype("float")
fig, ax = plt.subplots(1,1,figsize = (12,12))
ax.imshow(img)
ax.imshow(pan, alpha = pan_mask*0.5)
fig.savefig(path + "/labels/labelled_img", dpi = 500)
return None
|