Пример #1
0
def get_blob_list(mask_dict, points_mask, img_pil, split_inst=False):
    blob_list = []
    mask = preds = mask_dict['preds']
    probs = mask_dict['probs']
    assert probs.shape[1] == preds.shape[0]
    assert probs.shape[2] == preds.shape[1]
    
    imask = np.zeros(mask.shape)
    cmask = np.zeros(mask.shape)
    
    blob_id = 1
    for c in np.unique(mask):
        if c == 0:
            continue
        probs_class = probs[c]
        point_ind = points_mask == c
        inst_mask = morphology.label(mask==c)
        for l in np.unique(inst_mask):
            if l == 0:
                continue
            blob_ind = inst_mask == l
            locs = np.where(blob_ind * point_ind)
            y_list, x_list = locs
            n_points = len(y_list)
            if n_points == 0:
                continue
            if n_points > 1 and split_inst: 
                # split multiple points
                img_points = hi.points_on_image(y_list, x_list, img_pil)
                img_masks = hi.mask_on_image(img_pil, mask)
                img_masks = hi.mask_on_image(img_points.copy(), blob_ind)
                hu.save_image('tmp.jpg', (img_points)*0.5 + hu.f2l(hi.gray2cmap(probs_class))*0.5)
                hu.save_image('tmp.jpg', img_masks)
                
                for yi, xi in zip(y_list, x_list):
                    imask, cmask, blob_list, blob_id = add_mask(yi, xi, points_mask, 
                                                            blob_ind, 
                                                            n_points, blob_list, imask, cmask,
                                                            blob_id)
            else:
                # add for that single point
                yi, xi = y_list[0], x_list[0]
                imask, cmask, blob_list, blob_id = add_mask(yi, xi, points_mask, 
                                                            blob_ind, 
                                                            n_points, blob_list, imask, cmask,
                                                            blob_id)
                

    return blob_list, cmask.astype('uint8'), imask.astype('uint8')
Пример #2
0
    def save(self, img_pil, y_list, x_list, mask):
        img_p = hi.points_on_image(y_list, x_list, img_pil)

        img_maskspil = hi.mask_on_image(img_p,
                                        mask.astype('uint8'),
                                        add_bbox=True)
        hu.save_image('masker.jpg', img_maskspil)
Пример #3
0
    def vis_on_batch(self, batch, savedir_image):
        self.eval()
        images = batch["images"].to('cpu')  #helen changed this from .cuda()
        #print(images)
        #print(images.shape)
        points = batch["points"].long().to(
            'cpu')  #helen changed this from .cuda()
        logits = self.model.forward(images)
        probs = logits.sigmoid().cpu().numpy()

        blobs = lcfcn_loss.get_blobs(probs=probs)

        pred_counts = (np.unique(blobs) != 0).sum()
        pred_blobs = blobs
        pred_probs = probs.squeeze()

        # loc
        pred_count = pred_counts.ravel()[0]
        pred_blobs = pred_blobs.squeeze()

        img_org = hu.get_image(batch["images"], denorm="rgb")

        # true points
        y_list, x_list = np.where(batch["points"][0].long().numpy().squeeze())
        img_peaks = hi.points_on_image(y_list, x_list, img_org, radius=11)
        text = "%s ground truth" % (batch["points"].sum().item())
        hi.text_on_image(text=text, image=img_peaks)

        # pred points
        pred_points = lcfcn_loss.blobs2points(pred_blobs).squeeze()
        y_list, x_list = np.where(pred_points.squeeze())
        img_pred = hi.mask_on_image(img_org, pred_blobs)
        #img_pred = hi.points_on_image(y_list, x_list, img_org)
        text = "%s predicted" % (len(y_list))
        hi.text_on_image(text=text, image=img_pred)

        # ***************            helen added this code
        plt.imshow(
            img_pred
        )  #these lines of code display the image with the model's predications on it
        plt.show()
        # ***************            end of helen's code

        # heatmap
        heatmap = hi.gray2cmap(pred_probs)
        heatmap = hu.f2l(heatmap)
        hi.text_on_image(text="lcfcn heatmap", image=heatmap)

        img_mask = np.hstack([img_peaks, img_pred, heatmap])

        hu.save_image(savedir_image, img_mask)
Пример #4
0
    def vis_on_batch(self, batch, savedir):
        self.eval()
        os.makedirs(savedir, exist_ok=True)
        overlayed = hi.mask_on_image(batch['image_pil'][0],
                                     np.array(batch['inst_pil'][0]),
                                     add_bbox=True)
        overlayed = Image.fromarray((overlayed * 255).astype('uint8'))

        images = batch['images']
        img = images[0]

        prediction = self.model([img.to('cuda:0')])
        org_img = Image.fromarray(img.mul(255).permute(1, 2, 0).byte().numpy())
        pred_list = []
        score_list = prediction[0]['scores']
        for i in range(len(score_list)):
            if score_list[i] < 0.5:
                break
            pred = ((prediction[0]['masks'][i, 0] >
                     0.5).mul(255)).byte().cpu().numpy()
            pred_list += [Image.fromarray(pred)]

        img_name = batch['meta'][0]['name']
        for pred in pred_list:
            org_img = Image.fromarray(
                (hi.mask_on_image(org_img, pred) * 255).astype('uint8'))

        fname = os.path.join(savedir, '%s.jpg' % img_name)

        overlayed = hi.text_on_image(
            'gt', np.array(overlayed.resize((350, 200), 2).copy()))
        org_img = hi.text_on_image('preds',
                                   np.array(org_img.resize((350, 200))).copy())

        img = np.concatenate([org_img, overlayed.astype(float)],
                             axis=1).astype('float32') / 255.
        hu.save_image(fname=fname, img=img)
        print('image saved: %s' % fname)
Пример #5
0
    def vis_on_batch_helen(self, batch, savedir_image):
        self.eval()
        images = batch.to('cpu')
        #print(images)          #print statement to check what images is for debugging
        #print(images.shape)    #print statement to check images has the correct shape
        logits = self.model.forward(images)
        probs = logits.sigmoid().cpu().numpy()

        blobs = lcfcn_loss.get_blobs(probs=probs)

        pred_counts = (np.unique(blobs) != 0).sum()
        pred_blobs = blobs
        pred_probs = probs.squeeze()

        # loc
        pred_count = pred_counts.ravel()[0]
        pred_blobs = pred_blobs.squeeze()

        img_org = hu.get_image(images, denorm="rgb")
        #this is what was originally written: img_org = hu.get_image(batch["images"],denorm="rgb")

        #the following lines are commented out because my own data does not have labels and will thus throw errors
        # true points
        #y_list, x_list = np.where(batch["points"][0].long().numpy().squeeze())
        #img_peaks = hi.points_on_image(y_list, x_list, img_org, radius=11)
        #text = "%s ground truth" % (batch["points"].sum().item())
        #hi.text_on_image(text=text, image=img_peaks)

        # pred points
        pred_points = lcfcn_loss.blobs2points(pred_blobs).squeeze()
        y_list, x_list = np.where(pred_points.squeeze())
        img_pred = hi.mask_on_image(img_org, pred_blobs)
        # img_pred = hi.points_on_image(y_list, x_list, img_org)
        text = "%s predicted" % (len(y_list))
        hi.text_on_image(text=text, image=img_pred)

        # these lines of code display the image with the model's predications on it
        plt.imshow(img_pred)
        plt.show()

        # heatmap
        heatmap = hi.gray2cmap(pred_probs)
        heatmap = hu.f2l(heatmap)
        hi.text_on_image(text="lcfcn heatmap", image=heatmap)

        img_mask = np.hstack([img_pred, heatmap])  #helen took out im_peaks
        #this is what was originally written: img_mask = np.hstack([img_peaks, img_pred, heatmap])

        hu.save_image(savedir_image, img_mask)
Пример #6
0
    def vis_on_batch(self, batch, savedir_image):
        self.eval()
        images = batch["images"].cuda()
        points = batch["points"].long().cuda()
        logits = self.model_base.forward(images)
        probs = logits.sigmoid().cpu().numpy()

        blobs = lcfcn_loss.get_blobs(probs=probs)

        pred_counts = (np.unique(blobs)!=0).sum()
        pred_blobs = blobs
        pred_probs = probs.squeeze()

        # loc 
        pred_count = pred_counts.ravel()[0]
        pred_blobs = pred_blobs.squeeze()
        
        img_org = hu.get_image(batch["images"],denorm="rgb")

        # true points
        y_list, x_list = np.where(batch["points"][0].long().numpy().squeeze())
        img_peaks = haven_img.points_on_image(y_list, x_list, img_org)
        text = "%s ground truth" % (batch["points"].sum().item())
        haven_img.text_on_image(text=text, image=img_peaks)

        # pred points 
        pred_points = lcfcn_loss.blobs2points(pred_blobs).squeeze()
        y_list, x_list = np.where(pred_points.squeeze())
        img_pred = hi.mask_on_image(img_org, pred_blobs)
        # img_pred = haven_img.points_on_image(y_list, x_list, img_org)
        text = "%s predicted" % (len(y_list))
        haven_img.text_on_image(text=text, image=img_pred)

        # heatmap 
        heatmap = hi.gray2cmap(pred_probs)
        heatmap = hu.f2l(heatmap)
        haven_img.text_on_image(text="lcfcn heatmap", image=heatmap)
        
        
        img_mask = np.hstack([img_peaks, img_pred, heatmap])
        
        hu.save_image(savedir_image, img_mask)