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.)
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()
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()
def ce_loss(y: nd.NDArray, y_true: nd.NDArray) -> nd.NDArray: """ :param y: n*num_cls的预测分布矩阵 :param y_true: n*num_cls的实际分布矩阵 :return:交叉熵 n*1对应每个样本的交叉熵 """ ret = y_true * (-y.log()) #求交叉熵 ret = nd.sum(ret, axis=1) return ret
def misclassified_prop(mis_pixels: np.ndarray, prob: nd.NDArray) -> np.ndarray: """ get the probability distribution of misclassified pixels. :param mis_pixels: misclassified pixels of shape HW where 0 indicates misclassified pixels :param prob: the predicted probability with shape CHW :return: numpy array of shape NC where N is the total number of misclassified pixels """ prob = prob.as_in_context(mx.cpu()) prob = nd.transpose(nd.reshape(prob, shape=(0, -1)), axes=(1, 0)) flag_bit = np.reshape(mis_pixels, newshape=(-1)) mis_index = nd.array(np.where(flag_bit == 0)).squeeze() return prob[mis_index, :].asnumpy()
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
def transform(data: nd.NDArray, label: nd.NDArray): """ 转换函数 得到0-1之间的图像和onehot型的label :param data: :param label: :return: """ def getonehot(tlabel): if one_hot: olabel = np.zeros(shape=(self.num_class, ), dtype=np.float32) olabel[tlabel] = 1 else: olabel = tlabel return olabel if type(label) == int or type(label) == np.int32: olabel = getonehot(label) else: olabel = np.array([getonehot(l) for l in label]) olabel = nd.array(olabel) # dt转换 dt = data.astype(np.float32) / 255 #转换大小 这个转换极端的tmd低效 我希望 if not (self.target_size is None): dt = image.imresize(dt.as_in_context(cpu()), w=self.target_size[1], h=self.target_size[0]).as_in_context( gpu()).astype(np.float32) l = len(dt.shape) if flatten: if l == 3: dt = dt.reshape((data.shape[0] * data.shape[1], )) else: #带batch dt = dt.reshape( (data.shape[0], data.shape[1] * data.shape[2])) else: if l == 3: dt = dt.reshape((dt.shape[2], dt.shape[0], dt.shape[1])) else: dt = dt.reshape( (dt.shape[0], dt.shape[3], dt.shape[1], dt.shape[2])) return dt, olabel
def _allclose(a: nd.NDArray, b: nd.NDArray): return np.allclose(a.asnumpy(), b.asnumpy(), atol=1e-6)