def sample(match, cls_pred, iou, ratio=3, min_sample=0, threshold=0.5, do=True): if do is False: ones = nd.ones_like(match) sample = nd.where(match > -0.5, ones, ones*-1) return sample sample = nd.zeros_like(match) num_pos = nd.sum(match > -0.5, axis=-1) requre_neg = ratio * num_pos neg_mask = nd.where(match < -0.5, nd.max(iou, axis=-1) < threshold, sample) max_neg = neg_mask.sum(axis=-1) num_neg = nd.minimum(max_neg, nd.maximum(requre_neg, min_sample)).astype('int') neg_prob = cls_pred[:,:,0] max_value = nd.max(cls_pred, axis=-1, keepdims=True) score = max_value[:,:,0] - neg_prob + nd.log( nd.sum( nd.exp(cls_pred-max_value), axis=-1)) score = nd.where(neg_mask, score, nd.zeros_like(score)) argmax = nd.argsort(score, axis=-1, is_ascend=False) sample = nd.where(match > -0.5, nd.ones_like(sample), sample) for i, num in enumerate(num_neg): sample[i, argmax[i,:num.asscalar()]] = -1 return sample
def bbox_ious(boxes1, boxes2, x1y1x2y2=True): if x1y1x2y2: mx = nd.min(boxes1[0], boxes2[0]) Mx = nd.max(boxes1[2], boxes2[2]) my = nd.min(boxes1[1], boxes2[1]) My = nd.max(boxes1[3], boxes2[3]) w1 = boxes1[2] - boxes1[0] h1 = boxes1[3] - boxes1[1] w2 = boxes2[2] - boxes2[0] h2 = boxes2[3] - boxes2[1] else: mx = nd.min(boxes1[0] - boxes1[2] / 2.0, boxes2[0] - boxes2[2] / 2.0) Mx = nd.max(boxes1[0] + boxes1[2] / 2.0, boxes2[0] + boxes2[2] / 2.0) my = nd.min(boxes1[1] - boxes1[3] / 2.0, boxes2[1] - boxes2[3] / 2.0) My = nd.max(boxes1[1] + boxes1[3] / 2.0, boxes2[1] + boxes2[3] / 2.0) w1 = boxes1[2] h1 = boxes1[3] w2 = boxes2[2] h2 = boxes2[3] uw = Mx - mx uh = My - my cw = w1 + w2 - uw ch = h1 + h2 - uh mask = ((cw <= 0) + (ch <= 0) > 0) area1 = w1 * h1 area2 = w2 * h2 carea = cw * ch carea[mask] = 0 uarea = area1 + area2 - carea return carea / uarea
def bbox_overlaps(anchors: mx.nd.NDArray, gt: mx.nd.NDArray): """ Get IoU of the anchors and ground truth bounding boxes. The shape of anchors and gt should be (N, 4) and (M, 4) So the shape of return value is (N, M) """ ret = [] for i in range(gt.shape[0]): cgt = gt[i].reshape((1, 4)).broadcast_to(anchors.shape) # inter x0 = nd.max(nd.stack(anchors[:, 0], cgt[:, 0]), axis=0) y0 = nd.max(nd.stack(anchors[:, 1], cgt[:, 1]), axis=0) x1 = nd.min(nd.stack(anchors[:, 2], cgt[:, 2]), axis=0) y1 = nd.min(nd.stack(anchors[:, 3], cgt[:, 3]), axis=0) inter = _get_area( nd.concatenate([ x0.reshape((-1, 1)), y0.reshape((-1, 1)), x1.reshape((-1, 1)), y1.reshape((-1, 1)) ], axis=1)) outer = _get_area(anchors) + _get_area(cgt) - inter iou = inter / outer ret.append(iou.reshape((-1, 1))) ret = nd.concatenate(ret, axis=1) return ret
def bbox_overlaps(anchors: mx.nd.NDArray, gt: mx.nd.NDArray): """ Get IoU of the anchors and ground truth bounding boxes. The shape of anchors and gt should be (N, 4) and (M, 4) So the shape of return value is (N, M) """ N, M = anchors.shape[0], gt.shape[0] anchors_mat = anchors.reshape((N, 1, 4)).broadcast_to((N, M, 4)).reshape( (-1, 4)) gt_mat = gt.reshape((1, M, 4)).broadcast_to((N, M, 4)).reshape((-1, 4)) # inter x0 = nd.max(nd.stack(anchors_mat[:, 0], gt_mat[:, 0]), axis=0) y0 = nd.max(nd.stack(anchors_mat[:, 1], gt_mat[:, 1]), axis=0) x1 = nd.min(nd.stack(anchors_mat[:, 2], gt_mat[:, 2]), axis=0) y1 = nd.min(nd.stack(anchors_mat[:, 3], gt_mat[:, 3]), axis=0) inter = _get_area( nd.concatenate([ x0.reshape((-1, 1)), y0.reshape((-1, 1)), x1.reshape((-1, 1)), y1.reshape((-1, 1)) ], axis=1)) outer = _get_area(anchors_mat) + _get_area(gt_mat) - inter iou = inter / outer iou = iou.reshape((N, M)) return iou
def softmax(x): if x.ndim == 2: x = x.T x = x - nd.max(x, axis=0) y = nd.exp(x) / nd.sum(nd.exp(x), axis=0) return y.T x = x - nd.max(x) # avoid overflow return nd.exp(x) / nd.sum(nd.exp(x))
def quantize(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).floor() * (1 / f) return y return x
def int_inference(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).round() return y.astype('int8'), frac_len return x.astype('int8'), 0
def box_ciou(b1, b2): """ 输入为: ---------- b1: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh b2: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh 返回为: ------- ciou: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 1) """ # 求出预测框左上角右下角 b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # 求出真实框左上角右下角 b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half # 求真实框和预测框所有的iou intersect_mins = nd.max(b1_mins, b2_mins) intersect_maxes = nd.min(b1_maxes, b2_maxes) intersect_wh = nd.max(intersect_maxes - intersect_mins, nd.zeros_like(intersect_maxes)) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] union_area = b1_area + b2_area - intersect_area iou = intersect_area / nd.clip(union_area, a_min=1e-6) # 计算中心的差距 center_distance = nd.sum(nd.power((b1_xy - b2_xy), 2), axis=-1) # 找到包裹两个框的最小框的左上角和右下角 enclose_mins = nd.min(b1_mins, b2_mins) enclose_maxes = nd.max(b1_maxes, b2_maxes) enclose_wh = nd.max(enclose_maxes - enclose_mins, nd.zeros_like(intersect_maxes)) # 计算对角线距离 enclose_diagonal = nd.sum(nd.power(enclose_wh, 2), axis=-1) ciou = iou - 1.0 * (center_distance) / nd.clip(enclose_diagonal, a_min=1e-6) v = (4 / (math.pi**2)) * nd.power( (nd.arctan(b1_wh[..., 0] / nd.clip(b1_wh[..., 1], min=1e-6)) - nd.arctan(b2_wh[..., 0] / nd.clip(b2_wh[..., 1], a_min=1e-6))), 2) alpha = v / nd.clip((1.0 - iou + v), a_max=1e-6) ciou = ciou - alpha * v return ciou
def int_quantize(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).floor() y = nd.clip(y, a_min=-128, a_max=127) return y, frac_len return x, 0
def hard_example_mining(dist_mat, labels, return_inds=False): """For each anchor, find the hardest positive and negative sample. Args: dist_mat: pytorch Variable, pair wise distance between samples, shape [N, N] labels: pytorch LongTensor, with shape [N] return_inds: whether to return the indices. Save time if `False`(?) Returns: dist_ap: pytorch Variable, distance(anchor, positive); shape [N] dist_an: pytorch Variable, distance(anchor, negative); shape [N] p_inds: pytorch LongTensor, with shape [N]; indices of selected hard positive samples; 0 <= p_inds[i] <= N - 1 n_inds: pytorch LongTensor, with shape [N]; indices of selected hard negative samples; 0 <= n_inds[i] <= N - 1 NOTE: Only consider the case in which all labels have same num of samples, thus we can cope with all anchors in parallel. """ assert len(dist_mat.shape) == 2 assert dist_mat.shape[0] == dist_mat.shape[1] N = dist_mat.shape[0] # shape [N, N] is_pos = nd.equal(labels.broadcast_to((N, N)), labels.broadcast_to((N, N)).T).astype('float32') is_neg = nd.not_equal(labels.broadcast_to((N, N)), labels.broadcast_to((N, N)).T).astype('float32') # `dist_ap` means distance(anchor, positive) # both `dist_ap` and `relative_p_inds` with shape [N, 1] dist_pos = dist_mat * is_pos dist_ap = nd.max(dist_pos, axis=1) # `dist_an` means distance(anchor, negative) # both `dist_an` and `relative_n_inds` with shape [N, 1] dist_neg = dist_mat * is_neg + nd.max(dist_mat, axis=1, keepdims=True) * is_pos dist_an = nd.min(dist_neg, axis=1) # shape [N] # if return_inds: # # shape [N, N] # ind = (labels.new().resize_as_(labels) # .copy_(torch.arange(0, N).long()) # .unsqueeze(0).expand(N, N)) # # shape [N, 1] # p_inds = torch.gather( # ind[is_pos].contiguous().view(N, -1), 1, relative_p_inds.data) # n_inds = torch.gather( # ind[is_neg].contiguous().view(N, -1), 1, relative_n_inds.data) # # shape [N] # p_inds = p_inds.squeeze(1) # n_inds = n_inds.squeeze(1) # return dist_ap, dist_an, p_inds, n_inds return dist_ap, dist_an
def forward(self, is_train, req, in_data, out_data, aux): x = in_data[0] y = out_data[0] in_max = nd.max(nd.abs(x)) if in_max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).round() * (1 / f) y = x self.assign(out_data[0], req[0], mx.nd.array(y))
def match(iou, threshould=0.5, share_max=False): B, N, M = iou.shape if share_max: result = nd.argmax(iou, axis=-1) result = nd.where(nd.max(iou, axis=-1) > threshould, result, nd.ones_like(result)*-1) else: match = [getUniqueMatch(i) for i in iou] result = nd.concat(*match, dim=0) argmax_row = nd.argmax(iou, axis=-1) max_row = nd.max(iou, axis=-1) argmax_row = nd.where(max_row > threshould, argmax_row, nd.ones_like(argmax_row)*-1) result = nd.where(result > -0.5, result, argmax_row) return result
def DtransImage(img): sz = img.shape mmin, mmax = 0, 1 if len(sz) == 2: mmin = nd.min(img, keepdims=True) mmax = nd.max(img, keepdims=True) elif len(sz) == 3: mmin = nd.min(img, axis=(1, 2), keepdims=True) mmax = nd.max(img, axis=(1, 2), keepdims=True) elif len(sz) == 4: mmin = nd.min(img, axis=(2, 3), keepdims=True) mmax = nd.max(img, axis=(2, 3), keepdims=True) # print 'mmin shape:', mmin.shape,'img shape:',img.shape imgs = (img - mmin) / (mmax - mmin) return imgs
def hard_example_mining(dist_mat, labels): assert len(dist_mat.shape) == 2 assert dist_mat.shape[0] == dist_mat.shape[1] N = dist_mat.shape[0] # shape [N, N] is_pos = nd.equal(labels.broadcast_to((N, N)), labels.broadcast_to((N, N)).T).astype('float32') is_neg = nd.not_equal(labels.broadcast_to((N, N)), labels.broadcast_to((N, N)).T).astype('float32') dist_pos = dist_mat * is_pos dist_ap = nd.max(dist_pos, axis=1) dist_neg = dist_mat * is_neg + nd.max(dist_mat, axis=1, keepdims=True) * is_pos dist_an = nd.min(dist_neg, axis=1) return dist_ap, dist_an
def softmax(y_linear): exp = nd.exp(y_linear - nd.max(y_linear)) norms = exp.sum() softmax_output = exp / (norms + .0001) return softmax_output
def train_policy_net(self, imgs, actions, rs, terminals): """ Train one batch. Arguments: imgs - b x (f + 1) x C x H x W numpy array, where b is batch size, f is num frames, h is height and w is width. actions - b x 1 numpy array of integers rewards - b x 1 numpy array terminals - b x 1 numpy boolean array (currently ignored) Returns: average loss """ batch_size = actions.shape[0] states = imgs[:, :-1, :, :, :] next_states = imgs[:, 1:, :, :, :] s = states.shape states = states.reshape( (s[0], -1, s[-2], s[-1])) # batch x (f x C) x H x W next_states = next_states.reshape( (s[0], -1, s[-2], s[-1])) # batch x (f x C) x H x W st = nd.array(states, ctx=self.ctx, dtype=np.float32) / 255.0 at = nd.array(actions[:, 0], ctx=self.ctx) rt = nd.array(rs[:, 0], ctx=self.ctx) tt = nd.array(terminals[:, 0], ctx=self.ctx) st1 = nd.array(next_states, ctx=self.ctx, dtype=np.float32) / 255.0 next_qs = self.target_net(st1) next_q_out = nd.max(next_qs, axis=1) target = rt + next_q_out * (1.0 - tt) * DISCOUNT with autograd.record(): current_qs = self.policy_net(st) current_q = nd.pick(current_qs, at, 1) loss = self.loss_func(target, current_q) # diff = nd.abs(current_q - target) # quadratic_part = nd.clip(diff, -1, 1) # loss = 0.5 * nd.sum(nd.square(quadratic_part)) + nd.sum(diff - quadratic_part) # print('current_qs', current_qs) # print('current_q', current_q) # print('diff', diff) # print('quadratic_part', quadratic_part) # print('loss', loss) loss.backward() # 梯度裁剪 if GRAD_CLIPPING_THETA is not None: params = [ p.data() for p in self.policy_net.collect_params().values() ] g_utils.grad_clipping(params, GRAD_CLIPPING_THETA, self.ctx) self.trainer.step(batch_size) total_loss = loss.mean().asscalar() return total_loss
def softmax(y_linear): # exponent of a negative value is always between 0 and 1. exp = nd.exp(y_linear - nd.max(y_linear)) # Finding the total sum of all the exponents norms = nd.sum(exp, axis=0, exclude=True).reshape((-1, 1)) # Retrning value of exponent by total sum such that all of them overall sum up to 1. return exp / norms
def softmax(y_linear): # here, elementwise subtraction of the max value stabilizes the score # before it is exponentiated; this keeps higher scores from corresponding # to disproportionately high probabilities exp = nd.exp(y_linear-nd.max(y_linear, axis=1).reshape((-1,1))) norms = nd.sum(exp, axis=1).reshape((-1,1)) return exp / norms
def _block(x): lower = nd.min(x, axis=0) upper = nd.max(x, axis=0) node._box._init_param("min_list", lower) node._box._init_param("max_list", upper) extent = nd.sum(upper - lower) if (extent > 0): with self.name_scope(): l_node = self._new_node(parent=node) r_node = self._new_node(parent=node) self._structure[node] = {l_node: -1, r_node: 1} self._weightlayer.add(*[l_node._box, r_node._box]) self._embeddlayer.add(*[l_node, r_node]) e = nd.random.exponential(1 / extent) parent_tau = 0 if (node._box._parent is not None): parent_tau = node._box._parent._box._tau.data() node._box._init_param("tau", parent_tau + e) dim = nd.random.multinomial((upper - lower) / extent) split = nd.random.uniform(lower[dim], upper[dim]) with node.name_scope(): node._decision = Decision(split, dim, self._new_gate) self._routerlayer.add(*[node._decision]) decision = node._decision.forward(x, crisp=True) _shard(decision, x, _sample(l_node), _sample(r_node)) else: self._structure[node] = None
def norm(tensor, order=2, axis=None): """Computes the l-`order` norm of tensor Parameters ---------- tensor : ndarray order : int axis : int or tuple Returns ------- float or tensor If `axis` is provided returns a tensor. """ # handle difference in default axis notation if axis is None: axis = () if order == 'inf': res = nd.max(nd.abs(tensor), axis=axis) elif order == 1: res = nd.sum(nd.abs(tensor), axis=axis) elif order == 2: res = nd.sqrt(nd.sum(tensor**2, axis=axis)) else: res = nd.sum(nd.abs(tensor)**order, axis=axis)**(1/order) if res.shape == (1,): return res.asscalar() return res
def int_quantize_double(self, x, w): max1 = nd.max(nd.abs(x)) max2 = nd.max(nd.abs(w)) if max1 > max2: max = max1 else: max = max2 if max != 0: int_len = (nd.ceil(nd.log2(max))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') int_x = ((x * f)).floor() int_w = ((w * f)).floor() return int_x, int_w, frac_len return x, w, 0
def forward(self, am, bm, alpha_r, beta_r): av = self.inference_composition_a( am) # (batch_size, seq_len, hidden*2) (32,45,600) bv = self.inference_composition_b(bm) max_pool_a = nd.max(av, axis=1) max_pool_b = nd.max(bv, axis=1) mean_pool_a = nd.mean(av, axis=1) mean_pool_b = nd.mean(bv, axis=1) weight_pool_weight_a = nd.softmax(self.weight_pooling_dense_a(alpha_r)) weight_pool_weight_b = nd.softmax(self.weight_pooling_dense_b(beta_r)) aw = nd.sum(weight_pool_weight_a * av, axis=1) bw = nd.sum(weight_pool_weight_b * bv, axis=1) out = self.final_mlp( nd.concat(max_pool_a, mean_pool_a, aw, max_pool_b, mean_pool_b, bw)) return out
def evaluate_accuracy(data_loader, model, kclasses): if (data_loader == None): return (0, 0) n_accum = 0.0 accuracy_accum = 0.0 for i, (data, label) in enumerate(data_loader): data = data.as_in_context(mx.cpu()) label = label.as_in_context(mx.cpu()) log_c = model(data) # standard practice classification accuracy accuracy = (nd.argmax( log_c, axis=1).astype('int32') == label.astype('int32') ).astype('float32') accuracy_accum += nd.sum(accuracy) n_accum += len(label) avg_accuracy = (accuracy_accum / n_accum).asscalar() avg_c_max = nd.exp(nd.max(log_c, axis=1)).mean().asscalar( ) # maximum value in classification vector return (avg_accuracy, avg_c_max)
def store_samples(self, data, y, query_network, store_prob, context): if not (self.memory_replacement_strategy == "no_replacement" and self.max_stored_samples != -1 and self.key_memory.shape[0] >= self.max_stored_samples): num_pus = len(data) sub_batch_sizes = [data[i][0][0].shape[0] for i in range(num_pus)] num_inputs = len(data[0][0]) num_outputs = len(y) mx_context = context[0] if len(self.key_memory) == 0: self.key_memory = nd.empty(0, ctx=mx.cpu()) self.value_memory = [] self.label_memory = []#nd.empty((num_outputs, 0), ctx=mx.cpu()) ind = [nd.sample_multinomial(store_prob, sub_batch_sizes[i]).as_in_context(mx_context) for i in range(num_pus)] max_inds = [nd.max(ind[i]) for i in range(num_pus)] if any(max_inds): to_store_values = [] for i in range(num_inputs): tmp_values = [] for j in range(0, num_pus): if max_inds[j]: if isinstance(tmp_values, list): tmp_values = nd.contrib.boolean_mask(data[j][0][i].as_in_context(mx_context), ind[j]) else: tmp_values = nd.concat(tmp_values, nd.contrib.boolean_mask(data[j][0][i].as_in_context(mx_context), ind[j]), dim=0) to_store_values.append(tmp_values) to_store_labels = [] for i in range(num_outputs): tmp_labels = [] for j in range(0, num_pus): if max_inds[j]: if isinstance(tmp_labels, list): tmp_labels = nd.contrib.boolean_mask(y[i][j].as_in_context(mx_context), ind[j]) else: tmp_labels = nd.concat(tmp_labels, nd.contrib.boolean_mask(y[i][j].as_in_context(mx_context), ind[j]), dim=0) to_store_labels.append(tmp_labels) to_store_keys = query_network(*to_store_values[0:self.query_net_num_inputs]) if self.key_memory.shape[0] == 0: self.key_memory = to_store_keys.as_in_context(mx.cpu()) for i in range(num_inputs): self.value_memory.append(to_store_values[i].as_in_context(mx.cpu())) for i in range(num_outputs): self.label_memory.append(to_store_labels[i].as_in_context(mx.cpu())) elif self.memory_replacement_strategy == "replace_oldest" and self.max_stored_samples != -1 and self.key_memory.shape[0] >= self.max_stored_samples: num_to_store = to_store_keys.shape[0] self.key_memory = nd.concat(self.key_memory[num_to_store:], to_store_keys.as_in_context(mx.cpu()), dim=0) for i in range(num_inputs): self.value_memory[i] = nd.concat(self.value_memory[i][num_to_store:], to_store_values[i].as_in_context(mx.cpu()), dim=0) for i in range(num_outputs): self.label_memory[i] = nd.concat(self.label_memory[i][num_to_store:], to_store_labels[i].as_in_context(mx.cpu()), dim=0) else: self.key_memory = nd.concat(self.key_memory, to_store_keys.as_in_context(mx.cpu()), dim=0) for i in range(num_inputs): self.value_memory[i] = nd.concat(self.value_memory[i], to_store_values[i].as_in_context(mx.cpu()), dim=0) for i in range(num_outputs): self.label_memory[i] = nd.concat(self.label_memory[i], to_store_labels[i].as_in_context(mx.cpu()), dim=0)
def viterbi_decode(self, state_feats): """ :param state_feats: shape=(batch_size, seq_len, tag_size) :return: path_score: shape=(batch_size,) best_path: shape=(batch_size, seq_len) """ backpointers = [] state_feats_tmp = state_feats.transpose((1, 0, 2)) max_score = state_feats_tmp[0] if state_feats_tmp.shape[0] > 1: for feat in state_feats_tmp[1:]: next_tag_score = max_score.expand_dims(1) + ( feat.expand_dims(1) + self._transitions.data()).transpose( (0, 2, 1)) backpointers.append(nd.argmax(next_tag_score, axis=-1)) max_score = nd.max(next_tag_score, axis=-1) best_tag = nd.argmax(max_score, axis=-1) path_score = nd.pick(max_score, best_tag) best_path = [best_tag] for bp in reversed(backpointers): best_path.append(nd.pick(bp, best_path[-1])) best_path.reverse() best_path = nd.concat(*map(lambda x: x.expand_dims(0), best_path), dim=0).transpose() return path_score, best_path
def train(self,epochs): for i in range(epochs): efficiency = 0 cumuLoss = 0 for j in range(self.nbIter): z = nd.round(nd.random.uniform(0,1,(self.batchSize,self.code.k),ctx=self.ctx)) x = nd.dot(z,self.code.G)%2 noiseBSC = nd.random.uniform(0.01,0.99,(self.batchSize,self.code.n),ctx=self.ctx) noiseBSC = nd.floor(noiseBSC/nd.max(noiseBSC,axis=(1,)).reshape((self.batchSize,1))) y = (x + noiseBSC)%2 with autograd.record(): zHat = self.net(y) loss = self.SE(zHat,z) loss.backward() self.adam(self.params,self.vs,self.sqrs, self.lr, self.batchSize, self.t) self.t+=1 cumuLoss += loss.asscalar() zHat = nd.round(zHat) efficiency += nd.sum(nd.equal(zHat,z)).asscalar() Pc = efficiency/(self.batchSize*self.nbIter*self.code.k) Pe = 1 - Pc normCumuLoss = cumuLoss/(self.batchSize*self.nbIter*self.code.k) print("Epochs %d: Pe = %lf , loss = %lf" % (i,Pe,normCumuLoss))
def forward(self, ious): matches = nd.argmax(ious, axis=-1) # 每个锚框最高得分 max_iou_pre_anchor = nd.max(ious, axis=-1) # 将所有锚框都初始化为0,ignore samples = nd.zeros_like(max_iou_pre_anchor) # 计算每个ground_truth 的最高iou max_all_ious = nd.max(ious, axis=0, keepdims=True) # 标记处mask中最高分值的那一行为1 mask = nd.broadcast_greater(ious + self._eps, max_all_ious) mask = nd.sum(mask, axis=-1) # 将最高分数的锚框标记为 1 正类 samples = nd.where(mask, nd.ones_like(samples), samples) # 下面标记大于 pos_iou_thresh的样本为正例 samples = nd.where(max_iou_pre_anchor > self._pos_iou_thresh, nd.ones_like(samples), samples) # 标记小于neg_iou_thresh的样本为负类 tmp = (max_iou_pre_anchor < self._neg_iou_thresh) * (max_iou_pre_anchor > 0) samples = nd.where(tmp, nd.ones_like(samples) * -1, samples) # 将其转换为 numnpy samples = samples.asnumpy() # 下面进行采样 # 首先对正样本进行采样 num_pos = int((samples > 0).sum()) if num_pos > self._max_pos: discard_indices = np.random.choice(np.where((samples > 0))[0], size=(num_pos - self._max_pos), replace=False) samples[discard_indices] = 0 # 将多余部分设置为忽略 num_neg = int((samples < 0).sum()) max_neg = self._num_sample - min(self._max_pos, num_pos) if num_neg > max_neg: discard_indices = np.random.choice(np.where((samples < 0))[0], size=(num_neg - max_neg), replace=False) samples[discard_indices] = 0 # 最后将其转化为ndarray samples = nd.array(samples, ctx=matches.context) return samples, matches
def clip(tensor, a_min=None, a_max=None, indlace=False): if a_min is not None and a_max is not None: if indlace: nd.max(nd.min(tensor, a_max, out=tensor), a_min, out=tensor) else: tensor = nd.maximum(nd.minimum(tensor, a_max), a_min) elif min is not None: if indlace: nd.max(tensor, a_min, out=tensor) else: tensor = nd.maximum(tensor, a_min) elif max is not None: if indlace: nd.min(tensor, a_max, out=tensor) else: tensor = nd.minimum(tensor, a_max) return tensor
def add_node(x, leaf): N = x.shape[0] x_mean = nd.mean(x, axis=0) x_var = (N**-1) * nd.sum((x - x_mean)**2, axis=0) x_radius = 2 * (nd.max(x_var)**0.5) node = Node(x_mean, x_radius) tree.nodes.add(node) leaf.parent['node'] = node
def log_sum_exp(vec): # max_score shape: (self.tagset_size, batch_size, 1) max_score = nd.max(vec, axis=-1, keepdims=True) # score shape: (self.tagset_size, batch_size, 1) score = nd.log(nd.sum(nd.exp(vec - max_score), axis=-1, keepdims=True)) + max_score # return NDArray shape: (self.tagset_size, batch_size, ) return nd.squeeze(score, axis=-1)
def get_max_pred(batch_heatmaps): batch_size = batch_heatmaps.shape[0] num_joints = batch_heatmaps.shape[1] width = batch_heatmaps.shape[3] heatmaps_reshaped = batch_heatmaps.reshape((batch_size, num_joints, -1)) idx = nd.argmax(heatmaps_reshaped, 2) maxvals = nd.max(heatmaps_reshaped, 2) maxvals = maxvals.reshape((batch_size, num_joints, 1)) idx = idx.reshape((batch_size, num_joints, 1)) preds = nd.tile(idx, (1, 1, 2)).astype(np.float32) preds[:, :, 0] = (preds[:, :, 0]) % width preds[:, :, 1] = nd.floor((preds[:, :, 1]) / width) pred_mask = nd.tile(nd.greater(maxvals, 0.0), (1, 1, 2)) pred_mask = pred_mask.astype(np.float32) preds *= pred_mask return preds, maxvals
def softmax(y_linear, temperature=1.0): lin = (y_linear-nd.max(y_linear)) / temperature exp = nd.exp(lin) partition = nd.sum(exp, axis=0, exclude=True).reshape((-1,1)) return exp / partition