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)
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)
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) 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)