Esempio n. 1
0
 def log_prob(self, x: nd.NDArray) -> nd.NDArray:
     mean = self.get_param_maybe_repeated('mean')
     if x.ndim > mean.ndim:
         mean = nd.expand_dims(mean, 0)
     np_x = x.asnumpy().astype(np.int32).astype(np.float32)
     np.testing.assert_almost_equal(x.asnumpy(), np_x)
     return x * nd.log(mean) - mean - nd.gammaln(x + 1.)
Esempio n. 2
0
def plot_features(features: nd.NDArray, scope: int = 9):
    """
    visualize feature maps per channel.

    :param features: feature map with shape 1xCxHxW or CxHxW
    :param scope: the index of feature maps to visualize is [0, scope)
    """
    if len(features.shape) == 4:
        scope = scope if scope < features.shape[1] else features.shape[1]
        feature_maps = nd.squeeze(features, axis=0).asnumpy()
    else:
        scope = scope if scope < features.shape[0] else features.shape[0]
        feature_maps = features.asnumpy()
    feature_map_combination = []
    # separate visualization
    row, col = get_row_col(scope)
    plt.figure()
    for i in range(0, scope):
        feature_map = feature_maps[i, :, :]
        feature_map_combination.append(feature_map)
        plt.subplot(row, col, i + 1)
        plt.imshow(feature_map)
        axis('off')
        # title(f"feature map {i}")
    plt.show()
    # overlaps
    feature_map_sum = sum(ele for ele in feature_map_combination)
    plt.imshow(feature_map_sum)
    axis('off')
    plt.show()
Esempio n. 3
0
def format_to_plot(tensor: nd.NDArray) -> np.ndarray:
    """format the input tensor from NCHW/CHW to HWC/HW for plotting"""
    if len(tensor.shape) == 4:
        tensor = nd.squeeze(tensor, axis=0)
    if tensor.shape[0] == 1:
        tensor = nd.squeeze(tensor, axis=0)
    else:
        tensor = nd.transpose(tensor, axes=(1, 2, 0))
    return tensor.asnumpy()
Esempio n. 4
0
def misclassified_pixels(prob: nd.NDArray,
                         label: nd.NDArray,
                         ignore_label: int = -1) -> np.ndarray:
    """
    return misclassified pixels.
    :param prob: the predicted probability with shape CHW
    :param label: the ground truth label with shape HW
    :param ignore_label: ignored label
    :return: numpy array of shape HW where 0 indicates misclassified pixels
    """
    # needs to process on cpu
    prob = prob.as_in_context(mx.cpu())
    label = label.as_in_context(mx.cpu())

    # determine equal or not to get misclassified pixels
    pred = nd.squeeze(nd.argmax(prob, axis=0)).astype('int32')
    mis_classify = (pred == label).asnumpy()

    # deal with ignored label via numpy
    label = label.asnumpy()
    mis_classify[label == ignore_label] = 1

    return mis_classify
Esempio n. 5
0
def _allclose(a: nd.NDArray, b: nd.NDArray):
    return np.allclose(a.asnumpy(), b.asnumpy(), atol=1e-6)