def _guided_backprop_matching_assert( self, model: Module, output_layer: Module, test_input: TensorOrTupleOfTensors ): out = model(test_input) attrib = GuidedBackprop(model) neuron_attrib = NeuronGuidedBackprop(model, output_layer) for i in range(out.shape[1]): gbp_vals = attrib.attribute(test_input, target=i) neuron_gbp_vals = neuron_attrib.attribute(test_input, (i,)) assertTensorAlmostEqual(self, gbp_vals, neuron_gbp_vals, delta=0.01)
def _guided_backprop_test_assert(self, model, test_input, expected, additional_input=None): guided_backprop = GuidedBackprop(model) attributions = guided_backprop.attribute( test_input, target=0, additional_forward_args=additional_input) for i in range(len(test_input)): assertTensorAlmostEqual(self, attributions[i], expected[i], delta=0.01)
def _guided_backprop_test_assert( self, model: Module, test_input: TensorOrTupleOfTensorsGeneric, expected: Tuple[List[List[float]], ...], additional_input: Any = None, ) -> None: guided_backprop = GuidedBackprop(model) attributions = guided_backprop.attribute( test_input, target=0, additional_forward_args=additional_input ) for i in range(len(test_input)): assertTensorAlmostEqual(self, attributions[i], expected[i], delta=0.01)
def __init__( self, model: Module, layer: Module, device_ids: Union[None, List[int]] = None ) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. Model cannot contain any in-place ReLU submodules; these are not supported by the register_full_backward_hook PyTorch API starting from PyTorch v1.9. layer (Module): Layer for which neuron attributions are computed. Attributions for a particular neuron in the output of this layer are computed using the argument neuron_selector in the attribute method. Currently, only layers with a single tensor output are supported. device_ids (list(int)): Device ID list, necessary only if forward_func applies a DataParallel model. This allows reconstruction of intermediate outputs from batched results across devices. If forward_func is given as the DataParallel model itself, then it is not necessary to provide this argument. """ NeuronAttribution.__init__(self, model, layer, device_ids) GradientAttribution.__init__(self, model) self.guided_backprop = GuidedBackprop(model)
def _guided_backprop_matching_assert( self, model: Module, output_layer: Module, test_input: TensorOrTupleOfTensorsGeneric, ): out = model(test_input) model_copy = copy.deepcopy(model) attrib = GuidedBackprop(model_copy) self.assertFalse(attrib.multiplies_by_inputs) neuron_attrib = NeuronGuidedBackprop(model, output_layer) for i in range(out.shape[1]): gbp_vals = attrib.attribute(test_input, target=i) neuron_gbp_vals = neuron_attrib.attribute(test_input, (i, )) assertTensorAlmostEqual(self, gbp_vals, neuron_gbp_vals, delta=0.01)
def __init__(self, model: Module, layer: Module, device_ids: Union[None, List[int]] = None) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. layer (torch.nn.Module): Layer for which GradCAM attributions are computed. Currently, only layers with a single tensor output are supported. device_ids (list(int)): Device ID list, necessary only if forward_func applies a DataParallel model. This allows reconstruction of intermediate outputs from batched results across devices. If forward_func is given as the DataParallel model itself, then it is not necessary to provide this argument. """ GradientAttribution.__init__(self, model) self.grad_cam = LayerGradCam(model, layer, device_ids) self.guided_backprop = GuidedBackprop(model)