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)
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)
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) blobs = lcfcn_loss.get_blobs(logits) pred_counts = (np.unique(blobs) != 0).sum() pred_blobs = blobs pred_probs = F.softmax(logits, dim=1) # 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 = 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.squeeze().cpu().numpy()) heatmap = hu.f2l(heatmap) haven_img.text_on_image(text="lcfcn heatmap", image=heatmap) img_mask = np.hstack([img_peaks, img_pred]) hu.save_image(savedir_image, img_mask)
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')
def test_get_images(self): # save a score_list hi.points_on_image([3,2], [3,2], np.ones((100,100, 3)), radius=3, c_list=[0, 1])