def __call__(self, engine: Union[SupervisedTrainer, SupervisedEvaluator], batchdata: Dict[str, torch.Tensor]): if batchdata is None: raise ValueError("Must provide batch data for current iteration.") if np.random.choice( [True, False], p=[self.deepgrow_probability, 1 - self.deepgrow_probability]): # Run the inner loop only once inputs, _ = engine.prepare_batch(batchdata) inputs = inputs.to(engine.state.device) engine.fire_event(IterationEvents.INNER_ITERATION_STARTED) engine.network.eval() with torch.no_grad(): if engine.amp: with torch.cuda.amp.autocast(): predictions = engine.inferer(inputs, engine.network) else: predictions = engine.inferer(inputs, engine.network) batchdata.update({CommonKeys.PRED: predictions}) # decollate/collate batchdata to execute click transforms batchdata_list = decollate_batch(batchdata, detach=True) for i in range(len(batchdata_list)): batchdata_list[i][self.click_probability_key] = 1.0 batchdata_list[i] = self.transforms(batchdata_list[i]) batchdata = list_data_collate(batchdata_list) engine.fire_event(IterationEvents.INNER_ITERATION_COMPLETED) # else: # zero out input guidance channels batchdata_list = decollate_batch(batchdata, detach=True) for i in range(1, len(batchdata_list[0][CommonKeys.IMAGE])): batchdata_list[0][CommonKeys.IMAGE][i] *= 0 batchdata = list_data_collate(batchdata_list) # first item in batch only engine.state.batch = batchdata return engine._iteration(engine, batchdata)
def __call__(self, engine: Union[SupervisedTrainer, SupervisedEvaluator], batchdata: Dict[str, torch.Tensor]): if batchdata is None: raise ValueError("Must provide batch data for current iteration.") for j in range(self.max_interactions): inputs, _ = engine.prepare_batch(batchdata) inputs = inputs.to(engine.state.device) engine.fire_event(IterationEvents.INNER_ITERATION_STARTED) engine.network.eval() with torch.no_grad(): if engine.amp: with torch.cuda.amp.autocast(): predictions = engine.inferer(inputs, engine.network) else: predictions = engine.inferer(inputs, engine.network) engine.fire_event(IterationEvents.INNER_ITERATION_COMPLETED) batchdata.update({CommonKeys.PRED: predictions}) # decollate batch data to execute click transforms batchdata_list = decollate_batch(batchdata, detach=True) for i in range(len(batchdata_list)): batchdata_list[i][self.key_probability] = ((1.0 - ( (1.0 / self.max_interactions) * j)) if self.train else 1.0) batchdata_list[i] = self.transforms(batchdata_list[i]) # collate list into a batch for next round interaction batchdata = list_data_collate(batchdata_list) return engine._iteration(engine, batchdata)
def test_type_shape(self, input_data, expected_type, expected_shape): result = list_data_collate(input_data) self.assertIsInstance(result, expected_type) if isinstance(result, dict): data = result['image'] else: data = result[0] self.assertEqual(data.shape, expected_shape)
def test_type_shape(self, input_data, expected_type, expected_shape): result = list_data_collate(input_data) self.assertIsInstance(result, expected_type) if isinstance(result, dict): image = result["image"] label = result["label"] else: image = result[0] label = result[1] self.assertEqual(image.shape, expected_shape) self.assertEqual(label.shape, expected_shape) self.assertTrue(isinstance(label, MetaTensor)) self.assertTrue(label.is_batch, True)
def __call__(self, engine: Union[SupervisedTrainer, SupervisedEvaluator], batchdata: Dict[str, torch.Tensor]): if batchdata is None: raise ValueError("Must provide batch data for current iteration.") pos_click_sum = 0 neg_click_sum = 0 if np.random.choice( [True, False], p=[self.deepgrow_probability, 1 - self.deepgrow_probability]): pos_click_sum += 1 # increase pos_click_sum by 1-click for AddInitialSeedPointd pre_transform for j in range(self.max_interactions): # print("Inner iteration (click simulations running): ", str(j)) inputs, _ = engine.prepare_batch(batchdata) inputs = inputs.to(engine.state.device) engine.fire_event(IterationEvents.INNER_ITERATION_STARTED) engine.network.eval() with torch.no_grad(): if engine.amp: with torch.cuda.amp.autocast(): predictions = engine.inferer( inputs, engine.network) else: predictions = engine.inferer(inputs, engine.network) batchdata.update({CommonKeys.PRED: predictions}) # decollate/collate batchdata to execute click transforms batchdata_list = decollate_batch(batchdata, detach=True) for i in range(len(batchdata_list)): batchdata_list[i][self.click_probability_key] = ( (1.0 - ((1.0 / self.max_interactions) * j)) if self.train else 1.0) batchdata_list[i] = self.transforms(batchdata_list[i]) batchdata = list_data_collate(batchdata_list) # first item in batch only pos_click_sum += (batchdata_list[0].get("is_pos", 0)) * 1 neg_click_sum += (batchdata_list[0].get("is_neg", 0)) * 1 engine.fire_event(IterationEvents.INNER_ITERATION_COMPLETED) else: # zero out input guidance channels batchdata_list = decollate_batch(batchdata, detach=True) for i in range(len(batchdata_list)): batchdata_list[i][CommonKeys.IMAGE][-1] *= 0 batchdata_list[i][CommonKeys.IMAGE][-2] *= 0 batchdata = list_data_collate(batchdata_list) # first item in batch only engine.state.batch = batchdata engine.state.batch.update( {"pos_click_sum": torch.tensor(pos_click_sum)}) engine.state.batch.update( {"neg_click_sum": torch.tensor(neg_click_sum)}) return engine._iteration(engine, batchdata)