def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ check_label_shapes(labels, preds) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') check_label_shapes(label, pred_label) pred_label_ = pred_label.flat label_ = label.flat for i in range(len(pred_label_)): if label_[i] == self.c: if pred_label_[i] == self.c: self.TP += 1 else: self.FN += 1 elif pred_label_[i] == self.c: self.FP += 1
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. """ labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') labels, preds = check_label_shapes(label, pred_label) valid = (labels.reshape(-1, 1) != self.ignore_labels).all(axis=-1) self.sum_metric += np.logical_and(pred_label.flat == label.flat, valid).sum() self.num_inst += np.sum(valid)
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. """ labels, preds = metric.check_label_shapes(labels, preds, True) temp = preds[0] for i in range(1, len(preds)): temp = mx.nd.concat(temp, preds[i], dim=0) preds = [temp] for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = mx.ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') # flatten before checking shapes to avoid shape miss match label = label.flat pred_label = pred_label.flat metric.check_label_shapes(label, pred_label) self.sum_metric += (pred_label == label).sum() self.num_inst += len(pred_label)
def update(self, labels: nd.array, preds: nd.array) -> float: metric.check_label_shapes(labels, preds) label, pred = labels[0].asnumpy(), preds[0].asnumpy() error = ((pred - label) ** 2).sum() self.sum_metric += error self.num_inst += NUM_ANCHORS return error / NUM_ANCHORS
def update(self, labels: nd.array, preds: nd.array) -> float: metric.check_label_shapes(labels, preds) label = nd.array( reformat(labels[1]).asnumpy().ravel().round().astype(np.int)) pred = nd.flatten(preds[1]).T.as_in_context(label.context) error = nd.softmax_cross_entropy(pred, label).asscalar() self.sum_metric += error self.num_inst += NUM_ANCHORS return error / NUM_ANCHORS
def update(self, labels: nd.array, preds: nd.array) -> float: metric.check_label_shapes(labels, preds) pred = preds[2].asnumpy() pred_box = IOURegressionOutputWithMask.reformat(preds[0]) label_box = IOURegressionOutputWithMask.reformat(labels[0]) ious = batches_iou(pred_box, label_box) error = ((pred - ious) ** 2).sum() self.sum_metric += error self.num_inst += NUM_ANCHORS return error / NUM_ANCHORS
def update(self, labels, preds): check_label_shapes(labels, preds) for label, pred in zip(labels, preds): pred = pred.asnumpy().reshape(pred.shape[0], pred.shape[1], -1) label = label.asnumpy().astype(np.int32) valid_index = label != 255 prob = np.swapaxes(pred, 0, 1) prob = prob[:, valid_index] label = label[valid_index] loss = np.sum(-np.log(prob[label, np.arange(len(label))])) self.sum_metric += loss self.num_inst += valid_index.sum()
def update(self, labels, preds): labels = labels[0] preds = preds[0] check_label_shapes(labels, preds) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = mx.nd.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') check_label_shapes(label, pred_label) self.sum_metric += (pred_label.flat == label.flat).sum() self.num_inst += len(pred_label.flat)
def update(self, labels, preds): check_label_shapes(labels, preds) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = mx.nd.argmax(pred_label, axis=self.axis, keepdims=True) label = label.astype('int32') pred_label = pred_label.astype('int32').as_in_context(label.context) check_label_shapes(label, pred_label) correct = mx.nd.sum( (label == pred_label) * (label != self.ignore_label) ).asscalar() total = mx.nd.sum( (label != self.ignore_label) ).asscalar() self.sum_metric += correct self.num_inst += total
def update(self, labels, preds): check_label_shapes(labels, preds) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = ndarray.argmax_channel(pred_label) pred_label = pred_label.asnumpy().astype(np.int32).reshape( pred_label.shape[0], -1) label = label.asnumpy().astype(np.int32) check_label_shapes(label, pred_label) valid_index = label != 255 self.sum_metric += ( label[valid_index] == pred_label[valid_index]).sum() self.num_inst += valid_index.sum()
def update(self, labels, preds, masks=None): # pylint: disable=arguments-differ """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. masks : list of `NDArray` or None, optional Masks for samples, with the same shape as `labels`. value of its element must be either 1 or 0. If None, all samples are considered valid. """ labels, preds = check_label_shapes(labels, preds, True) masks = [None] * len(labels) if masks is None else masks for label, pred_label, mask in zip(labels, preds, masks): if pred_label.shape != label.shape: # TODO(haibin) topk does not support fp16. Issue tracked at: # https://github.com/apache/incubator-mxnet/issues/14125 # topk is used because argmax is slow: # https://github.com/apache/incubator-mxnet/issues/11061 pred_label = ndarray.topk(pred_label.astype('float32', copy=False), k=1, ret_typ='indices', axis=self.axis) # flatten before checking shapes to avoid shape miss match pred_label = pred_label.astype('int32', copy=False).reshape((-1, )) label = label.astype('int32', copy=False).reshape((-1, )) check_label_shapes(label, pred_label) if mask is not None: mask = mask.astype('int32', copy=False).reshape((-1, )) check_label_shapes(label, mask) num_correct = ((pred_label == label) * mask).sum().asscalar() num_inst = mask.sum().asscalar() else: num_correct = (pred_label == label).sum().asscalar() num_inst = len(label) self.sum_metric += num_correct self.global_sum_metric += num_correct self.num_inst += num_inst self.global_num_inst += num_inst
def update(self, labels, preds): check_label_shapes(labels, preds) for label, pred in zip(labels, preds): label = (label.asnumpy() * self.std3d + self.mean3d).reshape( (nJoints, 3)) pred = (pred.asnumpy() * self.std3d + self.mean3d).reshape( (nJoints, 3)) if self.pa: #add root to extend to 17 joints label = np.vstack((np.zeros(3), label)) pred = np.vstack((np.zeros(3), pred)) _, Z, T, b, c = compute_similarity_transform( label, pred, compute_optimal_scale=True) pred = (b * pred.dot(T)) + c diff = label - pred self.sum_metric += np.sqrt((diff * diff).sum(axis=1)) #nJointsx1 self.num_inst += 1
def update(self, labels, preds): check_label_shapes(labels, preds) for label, pred_label in zip(labels, preds): pred_label = ndarray.argmax_channel(pred_label).asnumpy().astype('int32') label = label.reshape((label.size,)).asnumpy().astype('int32') check_label_shapes(label, pred_label) #mask = np.logical_and(label!=self.mask, label!=0) #pred_label = pred_label[mask] #label = label[mask] #logging.debug("EVAL: label = {0}, pred = {1}".format(str(label), str(pred_label))) self.sum_metric += (pred_label.flat == label.flat).sum() self.num_inst += len(pred_label.flat)
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred in zip(labels, preds): if pred.shape != label.shape: pred = nd.argmax(pred, axis=1) pred = pred.asnumpy().astype('int32').flat label = label.asnumpy().astype('int32').flat check_label_shapes(label, pred) assert len(pred) % 3 == 0 for i in range(0, len(pred), 3): if (pred[i] == label[i]) or (pred[i] == label[i + 1]) or ( pred[i] == label[i + 2]): self.sum_metric += 1 self.num_inst += len(pred) / 3
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred in zip(labels, preds): label, pred = _reshape_data([label, pred]) error = label - pred self.sum_metric += np.abs(np.linalg.norm(error, axis=2)).mean() self.num_inst += 1
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): label = label.asnumpy().astype('int32').ravel() pred_label = pred_label.asnumpy().astype('int32').ravel() self.sum_metric += (np.abs(label - pred_label) < self.n).sum() self.num_inst += len(label)
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ #labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): if len(pred_label.shape) > 2: pred_label = mx.nd.reshape(pred_label, shape=[-1, pred_label.shape[-1]]) label = mx.nd.reshape(pred_label, shape=[-1]) # Using argpartition here instead of argsort is safe because # we do not care about the order of top k elements. It is # much faster, which is important since that computation is # single-threaded due to Python GIL. pred_label = np.argpartition( pred_label.asnumpy().astype('float32'), -self.top_k) label = label.asnumpy().astype('int32') check_label_shapes(label, pred_label) num_dims = len(pred_label.shape) mask = (label != self.ignore_label).astype(np.int32) num_samples = mask.sum() num_classes = pred_label.shape[1] top_k = min(num_classes, self.top_k) for j in range(top_k): num_correct = ( (pred_label[:, num_classes - 1 - j].flat == label.flat) * mask).sum() self.sum_metric += num_correct self.global_sum_metric += num_correct self.num_inst += num_samples self.global_num_inst += num_samples
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = mx.nd.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') # flatten before checking shapes to avoid shape miss match label = label.flat pred_label = pred_label.flat check_label_shapes(label, pred_label) mask = (label != self.ignore_label).astype(np.int32) num_correct = ((pred_label == label) * mask).sum() self.sum_metric += num_correct self.global_sum_metric += num_correct self.num_inst += np.sum(mask) self.global_num_inst += np.sum(mask)
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred in zip(labels, preds): label, pred = _reshape_data([label, pred]) angle = _get_track_heading(pred) error = label - pred error[:, :, 0] *= np.sin(angle) error[:, :, 1] *= -np.cos(angle) self.sum_metric += np.abs(np.sum(error, axis=2)).mean() self.num_inst += 1
def update(self, labels, preds): labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): label = label.asnumpy().astype('int32').ravel() pred_label = pred_label.asnumpy().astype('int32').ravel() mu = np.mean(label) sigma = np.sqrt(np.mean(np.power(label - mu, 2))) self.sum_metric += np.sum(1 - np.exp(-np.power(pred_label - mu, 2) / (2 * np.power(sigma, 2)))) self.num_inst += len(label)
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. """ labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') labels, preds = check_label_shapes(label, pred_label) valid = (labels.reshape(-1, 1) != self.ignore_labels).all(axis=-1) self.sum_metric += np.logical_and(pred_label.flat == label.flat, valid).sum() self.num_inst += np.sum(valid)
def get_incorrect_preds(self, labels, preds, names): """Method to get incorrect predictions. Incorrect predictions are updated on self.pred_status attribute """ labels, preds = check_label_shapes(labels, preds, True) #print(f'labels ahape {len(labels)}, {len(preds)}, {len(names)}') for label, pred_label, name in zip(labels, preds, names): if pred_label.shape != label.shape: pred_label = ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') zipped = zip(pred_label, label, name) for p, l, n in zipped: if np.all(p == l) is np.bool_(False): #print(f'***** p: {p} l: {l} n: {n} ********') self.pred_status[n.asscalar().astype('int32')] = [p, l]
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. """ labels, preds = check_label_shapes(labels, preds, True) num_joints = preds[0].shape[1] for label, pred in zip(labels, preds): norm = 1.0 h = pred.shape[2] w = pred.shape[3] if self.hm_type == 'gaussian': pred, _ = get_max_pred(pred) label, _ = get_max_pred(label) norm = np.ones((pred.shape[0], 2)) * np.array([h, w]) / 10 # norm = np.ones((pred.shape[0], 2)) * np.array([h, h]) / 10 pred = pred.asnumpy() label = label.asnumpy() dists = self._calc_dists(pred, label, norm) acc = 0 sum_acc = 0 cnt = 0 for i in range(num_joints): acc = self._dist_acc(dists[i]) if acc >= 0: sum_acc += acc cnt += 1 self.sum_metric += sum_acc self.num_inst += cnt
def update(self, labels, preds): """Implementation of update method. Updates accuracy: sum_metric and num_inst. Item is treated as accurate if all the labels from prediction matches with all the given labels Args: lables: Acutals preds: Predication """ labels, preds = check_label_shapes(labels, preds, True) for label, pred_label in zip(labels, preds): if pred_label.shape != label.shape: pred_label = ndarray.argmax(pred_label, axis=self.axis) pred_label = pred_label.asnumpy().astype('int32') label = label.asnumpy().astype('int32') zipped = zip(pred_label, label) zumba = [np.all((a == b)) for a, b in zipped] #print(f'pred_label: {len(pred_label)}') self.sum_metric += sum(zumba) #print(f'sum_metric: {self.sum_metric}') self.num_inst += len(pred_label)
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data with class indices as values, one per sample. preds : list of `NDArray` Prediction values for samples. Each prediction value can either be the class index, or a vector of likelihoods for all classes. """ labels, preds = check_label_shapes(labels, preds, True) num_joints = preds[0].shape[1] for label, pred in zip(labels, preds): norm = 1.0 h = pred.shape[2] w = pred.shape[3] if self.hm_type == 'gaussian': pred, _ = get_max_pred(pred) label, _ = get_max_pred(label) norm = np.ones((pred.shape[0], 2)) * np.array([h, w]) / 10 pred = pred.asnumpy() label = label.asnumpy() dists = self._calc_dists(pred, label, norm) acc = 0 sum_acc = 0 cnt = 0 for i in range(num_joints): acc = self._dist_acc(dists[i]) if acc >= 0: sum_acc += acc cnt += 1 self.sum_metric += sum_acc self.num_inst += cnt
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ check_label_shapes(labels, preds) for label, pred in zip(labels, preds): # start_all = time.time() pred = pred.reshape((pred.shape[0], pred.shape[1], -1)) # print label.shape, pred.shape if pred.shape != label.shape: # start = time.time() if self.threshold == 0.5 and self.num_class <= 2: pred = ndarray.argmax(pred, axis=self.axis) else: pred = pred[:, self.c, :] > self.threshold # end = time.time() # print 'bb2: %f' % (end - start) # print pred # check_label_shapes(label, pred) if label.shape != pred.shape: raise ValueError("Shape of labels {} does not match shape of " "predictions {}".format( label.shape, pred.shape)) # print label.shape # print pred_label.shape # end = time.time() # print 'bb: %f' % (end - start) if self.c is not None: pred = pred.copyto(mx.cpu(0)) # print pred.context # print label.context pred = ndarray.flatten(pred) label = ndarray.flatten(label) # print pred.shape, label.shape p_1 = pred == 1 p_0 = pred == 0 l_1 = label == 1 l_0 = label == 0 # quit() TP = ndarray.sum((p_1 * l_1), axis=[0, 1]) FP = ndarray.sum(p_1 * l_0, axis=[0, 1]) FN = ndarray.sum(p_0 * l_1, axis=[0, 1]) # start = time.time() self.TP += TP self.FP += FP self.FN += FN # print TP,FP,FN # print self.TP, self.FP, self.FN # self.TP += TP.asnumpy()[0] # self.FP += FP.asnumpy()[0] # self.FN += FN.asnumpy()[0] # end = time.time() # print 'cc2: %f' % (end - start) # quit() # print 'cc1: %f' % (end - start) # region 效率太低 # start = time.time() # pred = pred.asnumpy().astype('int32') # label = label.asnumpy().astype('int32') # pred_label_ = pred.flat # label_ = label.flat # for i in range(len(pred_label_)): # if label_[i] == self.c: # if pred_label_[i] == self.c: # self.TP += 1 # else: # self.FN += 1 # elif pred_label_[i] == self.c: # self.FP += 1 # print self.TP, self.FP, self.FN # self.MeanIoU.append(float(self.TP) / (self.TP + self.FN + self.FP)) # endregion else: # todo:这部分代码有问题 pass