-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
236 lines (200 loc) · 9.24 KB
/
infer.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
import os
import ast
import json
import cv2
import time
import torch
import models
import argparse
import numpy as np
import torch.nn as nn
import utils.utils
from PIL import Image
from tqdm import tqdm
from utils import utils
from collections import defaultdict
from dataloader.loader import Inference_Loader
# Folder name/ Experiment name/ image category/ all images.jpg
from distutils.dir_util import copy_tree, remove_tree
def get_argparser():
parser = argparse.ArgumentParser(
description='Cell instance segmentation using mask RCNN')
parser.add_argument('--out_dir', default="./outputs/",
help='directory where output will be saved')
parser.add_argument('--config_path', default="./config.json",
help='path of configuration file')
parser.add_argument('--max_instances', type=int, default=350,
help='maximum number of instances for maskRCNN default is 500')
args = parser.parse_args()
return args
class Inference:
def __init__(self, model_name, experiment_name, root_dir, weights_path, device, output_dir,
num_classes, classes, batch_size, backbone, num_instances):
self.thres = 0.5
self.device = device
self.classes = classes
self.out_dir = output_dir
#self.out_dir = root_dir
self.model_name = model_name
self.batch_size = batch_size
self.experiment_name = experiment_name
self.model = models.get_model(
model_name, weights_path, num_classes, num_instances, backbone)
self.model.to(device).eval()
#self.root_dir = root_dir
test_loader = Inference_Loader(root_dir)
self.iterator = torch.utils.data.DataLoader(test_loader,
batch_size=batch_size,
shuffle=False, num_workers=4,
pin_memory=True)
def infer(self):
for sample in tqdm(self.iterator):
directory, img_name, mask_img, unet_img, shape = sample
with torch.no_grad():
unet_img = unet_img.to(self.device)
mask_img = mask_img.to(self.device)
if self.model_name == "unet":
output = self.model(unet_img)
self.process_unet(output, img_name, directory,
self.experiment_name, shape)
elif self.model_name == "maskrcnn":
output = self.model(mask_img)
self.process_mask(output, img_name, directory,
self.experiment_name, mask_img)
elif self.model_name == "deeplab":
output = self.model(unet_img)["out"]
self.process_deeplab(
output, img_name, directory, self.experiment_name, shape)
def process_deeplab(self, output, img_name, directory, experiment_name, shape):
output = torch.max(output, 1)[1]
output = output.cpu().detach().numpy()
for i in range(len(output)):
output_dir = os.path.join(
self.out_dir, directory[i]+"_"+experiment_name)
os.makedirs(output_dir, exist_ok=True)
mask = output[i]
mask = cv2.resize(mask.astype("uint8"),
(shape[i][1], shape[i][0]))
cv2.imwrite(os.path.join(
output_dir+"/" + img_name[i]+".png"), mask)
def process_unet(self, output, img_name, directory, experiment_name, shape):
output = output.cpu().detach().numpy()
output = ((output > 0.5).astype(float)*255).astype("uint8")
for i in range(len(output)):
output_dir = os.path.join(
self.out_dir, directory[i]+"_"+experiment_name)
os.makedirs(output_dir, exist_ok=True)
mask = output[i]
mask = cv2.resize(mask, (shape[i][1], shape[i][0]))
cv2.imwrite(os.path.join(
output_dir+"/" + img_name[i]+".png"), mask)
def process_mask(self, outputs, img_name, directory, experiment_name, images):
images = images.cpu().detach().numpy()
for i, output in enumerate(outputs):
scores = output["scores"]
bboxes = output["boxes"]
mask = output["masks"].squeeze()
classes = output["labels"]
classes = classes[scores > self.thres]
bboxes = bboxes[scores > self.thres]
mask = mask[scores > self.thres]
scores = scores[scores > self.thres]
assert classes.shape[0] == bboxes.shape[0] == mask.shape[0]
mask = mask.cpu().detach().numpy()
img = np.transpose(images[i], [1, 2, 0])
output_dir = os.path.join(
self.out_dir, directory[i]+"_"+experiment_name)
overlay, colored_mask, instances = self.visualize(
img, mask, scores, bboxes, classes, self.thres)
output_boxes = torch.cat(
(classes.float().view(-1, 1), bboxes), 1).cpu().detach().numpy()
current_out_dir = os.path.join(
output_dir, "overlay")
os.makedirs(current_out_dir, exist_ok=True)
cv2.imwrite(current_out_dir+"/" +
img_name[i]+".png", overlay)
current_out_dir = os.path.join(
output_dir, "colored_mask")
os.makedirs(current_out_dir, exist_ok=True)
cv2.imwrite(current_out_dir+"/" +
img_name[i]+".png", colored_mask)
for j, instance_class in enumerate(["combined"]+self.classes):
current_out_dir = os.path.join(
output_dir, instance_class)
os.makedirs(current_out_dir, exist_ok=True)
cv2.imwrite(current_out_dir+"/" +
img_name[i]+".png", instances[j])
current_out_dir = os.path.join(
output_dir, "boxes")
os.makedirs(current_out_dir, exist_ok=True)
np.savetxt(current_out_dir+"/" +
img_name[i]+".txt", output_boxes.reshape(-1, 5))
def visualize(self, image, mask, scores, boxes, classes,
threshold=0.5):
final_mask = np.zeros((image.shape[0], image.shape[1], image.shape[2]))
instances = np.zeros(
(len(self.classes)+1, mask.shape[1], mask.shape[2])).astype("uint16")
instance_ids = [1]*(len(self.classes)+1)
for i, channel in enumerate(mask):
cls = classes[i]
channel[channel > threshold] = 1
channel[channel <= threshold] = 0
instances[0][channel == 1] = i+1
instances[cls][channel == 1] = instance_ids[cls]
instance_ids[cls] += 1
final_mask[channel == 1] = np.random.randint(
1, 255, size=3).tolist()
final_mask[final_mask > 255] = 255
for cls, box, score in zip(classes, boxes, scores):
cls = cls.cpu().detach().item()
score = score.cpu().detach().item()
cv2.rectangle(final_mask, (box[0], box[1]),
(box[2], box[3]), (0, 255, 255), 2)
cv2.putText(final_mask, self.classes[cls-1] + " " + str(score)[
:4], (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
final_mask = final_mask.astype("uint8")
image = image.astype(float)*255
image[final_mask != 0] -= 50
image += final_mask/2
image[image > 255] = 255
return image.astype("uint8"), final_mask, instances
def parse_cfg(path="./config.json"):
assert os.path.exists(path), "configuration file not found"
cfg = dict()
with open(path) as f:
parser = json.loads(f.read())
cfg["num_of_exp"] = parser["segmentation"]["num_of_exp"]
cfg["root_dir"] = parser["root_dir"]
cfg["experiments"] = parser["segmentation"]["experiments"]
return cfg
def move_results(output_dir, root_dir):
copy_tree(output_dir, root_dir)
remove_tree(output_dir)
def main():
args = get_argparser()
cfg = parse_cfg(args.config_path)
device = torch.device("cuda:0" if torch.cuda.is_available()
else "cpu")
number_of_experiments = int(cfg["num_of_exp"])
root_dir = cfg["root_dir"]
experiments = cfg["experiments"]
assert number_of_experiments == len(cfg["experiments"])
for i in range(number_of_experiments):
experiment_name = experiments[i]["name"]
model_name = experiments[i]["architecture"]
num_classes = int(experiments[i]["num_classes"])
classes = experiments[i]["class_names"]
batch_size = int(experiments[i]["batch_size"])
weights_path = experiments[i]["model_path"]
try:
backbone = experiments[i]["backbone"]
except:
backbone = None
print ("Processing for ", model_name)
inference = Inference(model_name, experiment_name, root_dir, weights_path,
device, root_dir, num_classes, classes,
batch_size, backbone, args.max_instances)
inference.infer()
# move_results(args.out_dir, root_dir)
if __name__ == "__main__":
main()