-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_tissuenet.py
112 lines (86 loc) · 3.64 KB
/
test_tissuenet.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
from cellotype.trainer import *
from detectron2.engine import DefaultPredictor
from detectron2.utils.visualizer import Visualizer
from tqdm import tqdm
import cv2
from detectron2.utils.visualizer import ColorMode
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
# for poly lr schedule
add_deeplab_config(cfg)
add_maskdino_config(cfg)
args.config_file = './configs/maskdino_R50_bs16_50ep_4s_dowsample1_2048.yaml'
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.MODEL.IN_CHANS = 3
cfg.DATASETS.TRAIN = ("cell_train",)
cfg.DATASETS.TEST = ('cell_test',)
cfg.OUTPUT_DIR = 'output/tissuenet'
cfg.SOLVER.AMP.ENABLED = False
cfg.MODEL.WEIGHTS = './models/tissuenet_model_0019999.pth'
cfg.TEST.DETECTIONS_PER_IMAGE = 1000
cfg.MODEL.DEVICE = 'cuda'
cfg.freeze()
default_setup(cfg, args)
setup_logger(output=cfg.OUTPUT_DIR, distributed_rank=comm.get_rank(), name="cellotype")
return cfg
def main(args):
data_dir = 'data/example_tissuenet'
for d in ["train","val", "test"]:
DatasetCatalog.register("cell_" + d, lambda d=d: np.load(os.path.join(data_dir, 'dataset_dicts_cell_{}.npy'.format(d)), allow_pickle=True))
MetadataCatalog.get("cell_" + d).set(thing_classes=["cell"])
args.resume = True
cfg = setup(args)
print("Command cfg:", cfg)
if args.eval_only:
model = Trainer.build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
checkpointer = DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR)
checkpointer.resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
res = Trainer.test(cfg, model)
if cfg.TEST.AUG.ENABLED:
res.update(Trainer.test_with_TTA(cfg, model))
if comm.is_main_process():
verify_results(cfg, res)
return res
predictor = DefaultPredictor(cfg)
balloon_metadata = MetadataCatalog.get("cell_test")
ds_dict = np.load('data/example_tissuenet/dataset_dicts_cell_test.npy', allow_pickle=True)
rst = []
k = 0
for d in tqdm(ds_dict):
im = cv2.imread(d["file_name"])
outputs = predictor(im) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
instances = outputs["instances"].to("cpu")
confident_detections = instances[instances.scores > 0.3]
if k < 2:
v = Visualizer(im[:, :, ::-1],
metadata=balloon_metadata,
scale=2,
instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels. This option is only available for segmentation models
)
out = v.draw_instance_predictions(confident_detections)
cv2.imwrite(os.path.join('output/tissuenet', '{}_pred.png'.format(d['image_id'])), out.get_image()[:, :, ::-1])
k += 1
mask_array = confident_detections.pred_masks.numpy().copy()
num_instances = mask_array.shape[0]
output = np.zeros(mask_array.shape[1:])
for i in range(num_instances):
output[mask_array[i,:,:]==True] = i+1
output = output.astype(int)
rst.append(output)
rst = np.array(rst)
np.save('output/tissuenet/test.npy', rst)
if __name__ == "__main__":
parser = default_argument_parser()
parser.add_argument('--eval_only', action='store_true')
parser.add_argument('--EVAL_FLAG', type=int, default=1)
args = parser.parse_args()
main(args)