def on_batch_end(self, runner: "IRunner"): """Batch end hook. Args: runner: current runner """ if self.step % self.summary_step == 0: pred_mask = runner.output[self.output_key][0] pred_mask = self.activation(pred_mask) pred_mask = utils.detach(pred_mask) pred_mask = self._prob2mask(pred_mask) if self.input_mask_key is not None: gt_mask = runner.input[self.input_mask_key][0] gt_mask = utils.detach(gt_mask) gt_mask = self._prob2mask(gt_mask) else: gt_mask = None if self.input_image_key is not None: image = runner.input[self.input_image_key][0].cpu() image = tensor_to_ndimage(image) else: # white background image = np.ones_like(pred_mask, dtype=np.uint8) * 255 pred_colors = self._get_colors(pred_mask) image_over_predicted_mask = label2rgb(pred_mask, image, bg_label=0, colors=pred_colors) if gt_mask is not None: gt_colors = self._get_colors(gt_mask) image_over_gt_mask = label2rgb(gt_mask, image, bg_label=0, colors=gt_colors) else: image_over_gt_mask = None self._draw_masks( self.loggers[runner.loader_key], runner.global_sample_step, image_over_predicted_mask, image_over_gt_mask, ) self.step += 1
def next_data(self): # Code copy pasted for performance reasons try: batch = next(self.iter)['image'] logits, tables = self.model(batch) mask = np.array([ utils.detach(logit[0].sigmoid() > 0.07).astype(bool) for logit in logits ]) table = tables.detach().numpy() return mask, table except StopIteration: return None, None
def on_batch_end(self, state: RunnerState): if state.loader_name.startswith("valid"): detections = decode_centernet_predictions(state.output["hm"], state.output["wh"], state.output["reg"], K=self.max_objs) detections = detach(detections).reshape( (detections.shape[0], -1, detections.shape[2])) detections[:, :, :4] *= self.down_ratio bboxes = detections[:, :, :4].astype(int) scores = detections[:, :, 4] labels = detections[:, :, 5].astype(int) result = dict( bboxes=bboxes, labels=labels, scores=scores, ) state.output.update(result)
def predict_table_mask(self, batch: torch.Tensor) -> np.ndarray: return np.array([ utils.detach(logit[0].sigmoid() > 0.07).astype(bool) for logit in self.model(batch)[0] ])
), ) ) ) print(type(predictions)) print(predictions.shape) threshold = 0.5 max_count = 5 for i, (features, logits) in enumerate(zip(test_dataset, predictions)): image = utils.tensor_to_ndimage(features["image"]) mask_ = torch.from_numpy(logits[0]).sigmoid() mask = utils.detach(mask_ > threshold).astype("float") show_examples(name="", image=image, mask=mask) if i >= max_count: break batch = next(iter(loaders["valid"])) # saves to `logdir` and returns a `ScriptModule` class runner.trace(model=model, batch=batch, logdir=logdir, fp16=is_fp16_used) !ls {logdir}/trace/ from catalyst.utils import tracing if is_fp16_used:
threshold = 0.5 max_count = 5 for i, (features, logits) in enumerate(zip(train_dataset, predictions)): image = utils.tensor_to_ndimage(features["image"], denormalize=False) # filename_mask = os.path.splitext(features["filename"])[0] filename_mask = os.path.splitext(features["filename_mask"])[0] gt = imread(mask_path / f"{filename_mask}.png") gt_res = gt.copy() gt_res.resize((224, 224)) gt_im = Image.fromarray(gt_res * 255) mask_ = torch.from_numpy(logits[0]).sigmoid() mask = utils.detach(mask_ > threshold).astype("uint8") # # Replace mask with real image # # filename_mask = os.path.splitext(features["filename_img"])[0] # # mask = imread(test_image_path / f"{filename_mask}.jpg") show_examples( name="", image=image, mask=mask, gt=gt_res, save=True, fig_path=f"predictions/{model.name}/", fig_name=f"prediction_{i}.png", overlay=True, )