nauyan / NC-Net

Nuclei Instance Segmentation Pipeline for Histopathology Datasets
7 stars 0 forks source link

absent of inference.py #4

Closed 2ndsecond closed 10 months ago

2ndsecond commented 1 year ago

I have trained NC-Net from the begin, and I would like to follow the readme.md to check the outcome, but I found that the inference.py was absent. I was wondering if you could release this file? thanks a lot.

nauyan commented 1 year ago

@2ndsecond I am sorry I forgot to add the inference.py file. However, you can use the code snippet below for generating outputs from trained models.

dataset = Dataset(config.test_dir, preprocessing=get_preprocessing(None))

model = torch.load("./{}/{}".format(config.checkpoints_dir,
                                    config.inference_weights))
model.eval()

metrics = [[], [], [], [], [], []]
for idx in tqdm(range(len(dataset)), total=len(dataset)):
    train_image, mask = dataset[idx]
    if int(np.unique(mask[:, :, 2]).shape[0]) == 1:
        continue
    train_image = torch.from_numpy(train_image).to(config.device).unsqueeze(0)

    model.eval()
    pred_mask = model(train_image).squeeze().cpu().detach().numpy()

    nuclei_map = pred_mask[:2, :, :].argmax(axis=0)
    prob_map = pred_mask[2, :, :]

    temp_prob_map = prob_map + 0
    temp_nuclei_map = nuclei_map + 0

    temp_prob_marker = np.where(temp_prob_map > config.watershed_threshold, 1,
                                0)
    temp_inst_map = mask[:, :, 2]
    pred_inst_map = apply_watershed(
        distance_transform=temp_prob_map,
        prob_map=temp_prob_marker,
        foreground_mask=temp_nuclei_map,
    )