Ejemplo n.º 1
0
def negSamplingLossAndGradient(centerWordVec,
                               outsideWordIdx,
                               outsideVectors,
                               dataset,
                               K=10):
    negSampleWordIndices = getNegativeSamples(outsideWordIdx, dataset, K)
    indices = [outsideWordIdx] + negSampleWordIndices

    v_c = centerWordVec  #(W,)
    o = outsideWordIdx
    U = outsideVectors  #VxW
    u_o = U[o]
    u_k = U[negSampleWordIndices]
    loss = 0.0
    gradCenterVec = np.zeros(v_c.shape)
    gradOutsideVecs = np.zeros(U.shape)
    z = sigmoid(np.dot(u_o, v_c))
    z_k = sigmoid(-np.dot(u_k, v_c))
    loss += -np.log(z)
    loss += -np.sum(np.log(z_k))

    gradCenterVec += (z - 1) * u_o
    gradCenterVec -= np.dot((z_k - 1), u_k)

    gradOutsideVecs[o] += (z - 1) * v_c

    for i, ind in enumerate(negSampleWordIndices):
        gradOutsideVecs[ind] += (1 - z_k[i]) * v_c

    return loss, gradCenterVec, gradOutsideVecs
Ejemplo n.º 2
0
    def forward(self, input, lamda, plasticity):
        input = input.expand(input.data.shape[0], 3, 28, 28)
        out1 = sigmoid(self.g_conv1_d, plasticity) * self.conv1(input) + \
               sigmoid(self.g_conv1_t, plasticity) * self.conv1(input)
        feature = sigmoid(self.g_conv2_d, plasticity) * self.conv2(out1) + \
                  sigmoid(self.g_conv2_t, plasticity) * self.conv2(out1)
        feature = feature.view(-1, 48 * 4 * 4)
        class_prediction = self.feature_classifier(feature)
        reverse_feature = ReverseLayer.apply(feature, lamda)
        domain_prediction = self.domain_classifier(reverse_feature)

        return class_prediction, domain_prediction
Ejemplo n.º 3
0
def predict(loader, model):
    mask_dict = {}
    progress_bar = tqdm(iterable=enumerate(loader),
                        total=len(loader),
                        desc=f"Inference",
                        ncols=0)
    with torch.set_grad_enabled(False):
        for i, data in progress_bar:
            images = data["img"].to(device)
            batch = tta(model, images)
            for img, probability in zip(data["image_name"],
                                        batch.cpu().detach().numpy()):
                for i, prop in enumerate(probability):
                    mask_dict[img + str(i)] = sigmoid(prop).astype(np.float32)
    return mask_dict
Ejemplo n.º 4
0
    def predict_proba(self,IVs,is_ad_hoc=False):

        # compute soft label
        IVs = self._convert_iv(IVs)
        # w = self.w
        # conditional_accuracy = self.conditional_accuracy

        n,p = IVs.shape

        if self.use_canonical:
            # use canonical parameters
            pos_posterior = np.zeros(n)
            neg_posterior = np.zeros(n)
            for clique in self.prob_dict:
                iv_index_in_clique = [x for x in range(len(clique)) if list(clique)[x]!= str(p)]
                iv_index_in_IVs = np.array(list(clique))[iv_index_in_clique].astype(int)
                assignment = np.zeros([n,len(clique)]).astype(int)
                assignment[:,iv_index_in_clique] = IVs[:,iv_index_in_IVs]+1
                pos_assignment = np.copy(assignment)
                neg_assignment = np.copy(assignment)
                pos_assignment[:,list(clique).index(str(p))] = 2
                neg_assignment[:,list(clique).index(str(p))] = 0
                del assignment
                # find the index of the assignment
                pos_assignment_single_index = multi_index_to_index(pos_assignment)
                neg_assignment_single_index = multi_index_to_index(neg_assignment)
                # update positve posterior
                pos_posterior = pos_posterior + self.prob_dict[clique]["prob"][pos_assignment_single_index]
                # update negative posterior
                neg_posterior = neg_posterior + self.prob_dict[clique]["prob"][neg_assignment_single_index]

            Zprob = sigmoid(-(neg_posterior+np.log(self.class_balance_im[0]))+(pos_posterior+np.log(self.class_balance_im[1])))

        else:
            raise NotImplementedError("set use_cannonical=True in training!")

        return Zprob
Ejemplo n.º 5
0
def sigmoid_forward(X):
    out = util.sigmoid(X)
    cache = out
    return out, cache
Ejemplo n.º 6
0
def im_detect(net, im):
    im = im.astype(np.float32, copy=False)
    im = cv2.resize(im, (int(cfg.IMAGE_WIDTH), int(cfg.IMAGE_HEIGHT)))
    im = im - cfg.PIXEL_MEANS
    im_ = np.transpose(im, (2, 0, 1))
    im_ = im_[np.newaxis, ...]

    conv1_shadow = np.zeros((1, 64, 192, 624), dtype=np.float32)

    net_time = Timer()
    npy_time = Timer()

    # reshape network inputs
    net_time.tic()
    net.blobs['data'].reshape(*(im_.shape))
    net.blobs['conv1_shadow'].reshape(*(conv1_shadow.shape))

    # do forward
    forward_kwargs = {'data': im_.astype(np.float32, copy = False), \
                      'conv1_shadow': conv1_shadow}

    blobs_outs = net.forward(**forward_kwargs)
    net_time.toc()

    npy_time.tic()
    pred_class_probs = blobs_outs['pred_class_probs']
    pred_conf = blobs_outs['pred_conf']
    pred_box_delta = blobs_outs['pred_box_delta']

    # For pred class probs part
    pred_class_probs = np.transpose(pred_class_probs, (0, 2, 3, 1))
    pred_class_probs = np.reshape(pred_class_probs, (cfg.NUM_ANCHORS, 3))
    pred_class_sft_probs = softmax(pred_class_probs)
    pred_class_sft_probs = np.reshape(pred_class_sft_probs,
                                      (-1, cfg.NUM_ANCHORS, cfg.NUM_CLASSES))

    # For pred conf part
    pred_conf = np.transpose(pred_conf, (0, 2, 3, 1))
    pred_conf = np.reshape(pred_conf, (-1, cfg.NUM_ANCHORS))
    pred_sgm_conf = sigmoid(pred_conf)

    # For pred box delta
    pred_box_delta = np.transpose(pred_box_delta, (0, 2, 3, 1))
    pred_box_delta = np.reshape(pred_box_delta, (-1, cfg.NUM_ANCHORS, 4))
    delta_x = pred_box_delta[:, :, 0]
    delta_y = pred_box_delta[:, :, 1]
    delta_w = pred_box_delta[:, :, 2]
    delta_h = pred_box_delta[:, :, 3]

    # Get anchors
    ANCHORS = set_anchors()
    anchor_x = ANCHORS[:, 0]
    anchor_y = ANCHORS[:, 1]
    anchor_w = ANCHORS[:, 2]
    anchor_h = ANCHORS[:, 3]
    box_center_x = anchor_x + delta_x * anchor_w
    box_center_y = anchor_y + delta_y * anchor_h
    box_width = anchor_w * np.exp(delta_w)
    box_height = anchor_h * np.exp(delta_h)

    # convert (centerx, centery, w, h) into (xmin, ymin, xmax, ymax)
    xmins, ymins, xmaxs, ymaxs = bbox_transform([box_center_x, box_center_y, \
                                                 box_width, box_height])
    # clip bounding boxes
    xmins, ymins, xmaxs, ymaxs = clip_box([xmins, ymins, xmaxs, ymaxs])

    # convert (xmin, ymin, xmax, ymax) into (centerx, centery, w, h)
    bbox = bbox_transform_inv([xmins, ymins, xmaxs, ymaxs])
    det_bbox = np.array(bbox)
    det_bbox = np.transpose(det_bbox, (1, 2, 0))

    # get probs
    det_probs = np.multiply(pred_class_sft_probs, \
                np.reshape(pred_sgm_conf, (-1, cfg.NUM_ANCHORS, 1)))
    det_probs_max = np.max(np.reshape(det_probs, (-1, 3)), axis=1)
    det_probs_max = np.reshape(det_probs_max, (1, -1))

    # get class
    det_class = np.argmax(det_probs, 2)

    # filter bounding boxes with nms
    final_boxes, final_probs, final_class = filter_prediction(det_bbox[0], \
                                                    det_probs_max[0], det_class[0])

    # filter bounding boxes with probability threshold
    keep_idx = [idx for idx in range(len(final_probs))\
                    if final_probs[idx] > cfg.DRAW_THRESHOLD]
    final_boxes = [final_boxes[idx] for idx in keep_idx]
    final_probs = [final_probs[idx] for idx in keep_idx]
    final_class = [final_class[idx] for idx in keep_idx]

    npy_time.toc()
    print('net time: {:.3f}, npy time: {:.3f}'.format(net_time.average_time,
                                                      npy_time.average_time))

    return final_boxes, final_probs, final_class
Ejemplo n.º 7
0
 def act_fn(self, x): return sigmoid(x)
 def deriv_act_fn(self, x): return deriv_sigmoid(x)