コード例 #1
0
    def _weight_to_image(
            weight: Tensor,
            kernel_channels_last: bool = False) -> Optional[Tensor]:
        """Logs a weight as a TensorBoard image.

        Implementation from TensorFlow codebase, would have invoked theirs directly but they didn't make it a static
        method.
        """
        w_img = squeeze(weight)
        shape = backend.int_shape(w_img)
        if len(shape) == 1:  # Bias case
            w_img = reshape(w_img, [1, shape[0], 1, 1])
        elif len(shape) == 2:  # Dense layer kernel case
            if shape[0] > shape[1]:
                w_img = permute(w_img, [0, 1])
                shape = backend.int_shape(w_img)
            w_img = reshape(w_img, [1, shape[0], shape[1], 1])
        elif len(shape) == 3:  # ConvNet case
            if kernel_channels_last:
                # Switch to channels_first to display every kernel as a separate images
                w_img = permute(w_img, [2, 0, 1])
            w_img = expand_dims(w_img, axis=-1)
        elif len(shape) == 4:  # Conv filter with multiple input channels
            if kernel_channels_last:
                # Switch to channels first to display kernels as separate images
                w_img = permute(w_img, [3, 2, 0, 1])
            w_img = reduce_sum(
                abs(w_img),
                axis=1)  # Sum over the each channel within the kernel
            w_img = expand_dims(w_img, axis=-1)
        shape = backend.int_shape(w_img)
        # Not possible to handle 3D convnets etc.
        if len(shape) == 4 and shape[-1] in [1, 3, 4]:
            return w_img
コード例 #2
0
    def _convert_for_visualization(tensor: Tensor,
                                   tile: int = 99) -> np.ndarray:
        """Modify the range of data in a given input `tensor` to be appropriate for visualization.

        Args:
            tensor: Input masks, whose channel values are to be reduced by absolute value summation.
            tile: The percentile [0-100] used to set the max value of the image.

        Returns:
            A (batch X width X height) image after visualization clipping is applied.
        """
        if isinstance(tensor, torch.Tensor):
            channel_axis = 1
        else:
            channel_axis = -1
        flattened_mask = reduce_sum(abs(tensor),
                                    axis=channel_axis,
                                    keepdims=True)

        non_batch_axes = list(range(len(flattened_mask.shape)))[1:]

        vmax = percentile(flattened_mask,
                          tile,
                          axis=non_batch_axes,
                          keepdims=True)
        vmin = reduce_min(flattened_mask, axis=non_batch_axes, keepdims=True)

        return clip_by_value((flattened_mask - vmin) / (vmax - vmin), 0, 1)
コード例 #3
0
 def forward(self, data: List[Tensor], state: Dict[str,
                                                   Any]) -> List[Tensor]:
     results = []
     for elem in data:
         # L1 Distance
         x = reduce_sum(abs(expand_dims(elem, axis=1) - self.labels),
                        axis=-1)
         x = iwd(x, power=1.0, eps=self.eps)
         results.append(x)
     return results
コード例 #4
0
def iwd(tensor: Tensor,
        power: float = 1.0,
        max_prob: float = 0.95,
        pairwise_distance: float = 1.0,
        eps: Optional[Tensor] = None) -> Tensor:
    """Compute the Inverse Weighted Distance from the given input.

    This can be used as an activation function for the final layer of a neural network instead of softmax. For example,
    instead of: model.add(layers.Dense(classes, activation='softmax')), you could use:
    model.add(layers.Dense(classes, activation=lambda x: iwd(tf.nn.sigmoid(x))))

    This method can be used with Numpy data:
    ```python
    n = np.array([[0.5]*5, [0]+[1]*4])
    b = fe.backend.iwd(n)  # [[0.2, 0.2, 0.2, 0.2, 0.2], [0.95, 0.0125, 0.0125, 0.0125, 0.0125]]
    ```

    This method can be used with TensorFlow tensors:
    ```python
    t = tf.constant([[0.5]*5, [0]+[1]*4])
    b = fe.backend.iwd(n)  # [[0.2, 0.2, 0.2, 0.2, 0.2], [0.95, 0.0125, 0.0125, 0.0125, 0.0125]]
    ```

    This method can be used with PyTorch tensors:
    ```python
    p = torch.tensor([[0.5]*5, [0]+[1]*4])
    b = fe.backend.iwd(n)  # [[0.2, 0.2, 0.2, 0.2, 0.2], [0.95, 0.0125, 0.0125, 0.0125, 0.0125]]
    ```

    Args:
        tensor: The input value. Should be of shape (Batch, C) where every element in C corresponds to a (non-negative)
            distance to a target class.
        power: The power to raise the inverse distances to. 1.0 results in a fairly intuitive probability output. Larger
            powers can widen regions of certainty, whereas values between 0 and 1 can widen regions of uncertainty.
        max_prob: The maximum probability to assign to a class estimate when it is distance zero away from the target.
            For numerical stability this must be less than 1.0. We have found that using smaller values like 0.95 can
            lead to natural adversarial robustness.
        pairwise_distance: The distance to any other class when the distance to a target class is zero. For example, if
            you have a perfect match for class 'a', what distance should be reported to class 'b'. If you have a metric
            where this isn't constant, just use an approximate expected distance. In that case `max_prob` will only give
            you approximate control over the true maximum probability.
        eps: The numeric stability constant to be used when d approaches zero. If None then it will be computed using
            `max_prob` and `pairwise_distance`. If not None, then `max_prob` and `pairwise_distance` will be ignored.

    Returns:
        A probability distribution of shape (Batch, C) where smaller distances from `tensor` correspond to larger
        probabilities.
    """
    if eps is None:
        eps = np.array(pairwise_distance * math.pow(
            (1.0 - max_prob) / (max_prob * (tensor.shape[-1] - 1)), 1 / power),
                       dtype=TENSOR_TO_NP_DTYPE[tensor.dtype])
        eps = to_tensor(
            eps,
            target_type='torch' if isinstance(tensor, torch.Tensor) else
            'tf' if tf.is_tensor(tensor) else 'np')
        if isinstance(eps, torch.Tensor):
            eps = eps.to("cuda:0" if torch.cuda.is_available() else "cpu")
    tensor = maximum(tensor, eps)
    tensor = tensor_pow(1.0 / tensor, power)
    tensor = tensor / reshape(reduce_sum(tensor, axis=-1), shape=[-1, 1])
    return tensor