Esempio n. 1
0
def compute_shifts(stimuli, scale):
    """ Get shifts for given tuning bandwidth """
    x = utils.get_population2(stimuli,
                              npoints=npoints,
                              kind='circular',
                              scale=scale).astype(
                                  defaults._DEFAULT_FLOATX_NP).transpose(
                                      0, 2, 3, 1)  #transpose to bhwc
    start = timer()
    with tf.device('/gpu:0'):
        ctx = ContextualCircuit()
        ctx.run(x)  #builds tf graph with shape of x
    end = timer()
    print(end - start)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        start = timer()
        if profile:
            #chrome://tracing
            run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
            sess.run(
                tf.group(tf.global_variables_initializer(),
                         tf.local_variables_initializer()))
            y = sess.run(ctx.out_O,
                         feed_dict={ctx.X: x},
                         options=run_options,
                         run_metadata=run_metadata)

            #Profiling
            tl = timeline.Timeline(run_metadata.step_stats)
            ctf = tl.generate_chrome_trace_format()
            with open('timeline.json', 'w') as f:
                f.write(ctf)
        else:
            sess.run(
                tf.group(tf.global_variables_initializer(),
                         tf.local_variables_initializer()))
            y = sess.run(ctx.out_O, feed_dict={ctx.X: x})
        end = timer()
        print(end - start)

    #transpose x and y back to bchw
    x = x.transpose(0, 3, 1, 2)
    y = y.transpose(0, 3, 1, 2)

    # compute absolute and relative dshifts
    xdec = utils.decode(x[:, :, cpt[0], cpt[1]], axis=1, kind=decoder_type)
    cdec = utils.decode(y[:, :, cpt[0], cpt[1]], axis=1, kind=decoder_type)
    sdec = utils.decode(y[:, :, spt[0], spt[1]], axis=1, kind=decoder_type)
    dshift_a = cdec - xdec
    dshift_r = (cdec - sdec) / (cval - sval)
    return dshift_a, dshift_r
Esempio n. 2
0
def adb_sync(args, serial=''):
    _prefix = ['adb -s %s' % serial] if serial else ['adb']
    _command = ' '.join(_prefix + args)
    s = subprocess.Popen((str(_command)),
                         stderr=(subprocess.PIPE),
                         stdout=(subprocess.PIPE),
                         shell=True)
    _out, _err = s.communicate()
    err = utils.decode(_err)
    out = utils.decode(_out)
    if s.returncode == 0:
        return (None, out)
    print('cmd : ', _command, 'adb err : ', err + out)
    return (err, None)
Esempio n. 3
0
 def runInThread(args, kwargs):
     _serial = kwargs['serial'] if 'serial' in kwargs.keys() else ''
     _prefix = ['adb -s %s' % _serial] if _serial else ['adb']
     _command = ' '.join(_prefix + args)
     callback = kwargs['callback']
     proc = subprocess.Popen((str(_command)),
                             stderr=(subprocess.PIPE),
                             stdout=(subprocess.PIPE),
                             shell=True)
     _out, _err = proc.communicate()
     if proc.returncode == 0:
         out = utils.decode(_out)
         callback(None, out)
     else:
         err = utils.decode(_err)
         callback(err, None)
Esempio n. 4
0
    def GIoULoss(pred, target, anchor, weight=None):
        pred_boxes = decode(pred.reshape(-1, 4), anchor.reshape(-1, 4))
        pred_x1 = pred_boxes[:, 0]
        pred_y1 = pred_boxes[:, 1]
        pred_x2 = pred_boxes[:, 2]
        pred_y2 = pred_boxes[:, 3]
        pred_x2 = torch.max(pred_x1, pred_x2)
        pred_y2 = torch.max(pred_y1, pred_y2)
        pred_area = (pred_x2 - pred_x1) * (pred_y2 - pred_y1)

        gt_boxes = decode(target.reshape(-1, 4), anchor.reshape(-1, 4))
        target_x1 = gt_boxes[:, 0]
        target_y1 = gt_boxes[:, 1]
        target_x2 = gt_boxes[:, 2]
        target_y2 = gt_boxes[:, 3]
        target_area = (target_x2 - target_x1) * (target_y2 - target_y1)

        x1_intersect = torch.max(pred_x1, target_x1)
        y1_intersect = torch.max(pred_y1, target_y1)
        x2_intersect = torch.min(pred_x2, target_x2)
        y2_intersect = torch.min(pred_y2, target_y2)
        area_intersect = torch.zeros(pred_x1.size()).to(pred)
        mask = (y2_intersect > y1_intersect) * (x2_intersect > x1_intersect)
        area_intersect[mask] = (x2_intersect[mask] - x1_intersect[mask]) * (
            y2_intersect[mask] - y1_intersect[mask])

        x1_enclosing = torch.min(pred_x1, target_x1)
        y1_enclosing = torch.min(pred_y1, target_y1)
        x2_enclosing = torch.max(pred_x2, target_x2)
        y2_enclosing = torch.max(pred_y2, target_y2)
        area_enclosing = (x2_enclosing - x1_enclosing) * (y2_enclosing -
                                                          y1_enclosing) + 1e-7

        area_union = pred_area + target_area - area_intersect + 1e-7
        ious = area_intersect / area_union
        gious = ious - (area_enclosing - area_union) / area_enclosing

        losses = 1 - gious

        if weight is not None and weight.sum() > 0:
            return losses * weight
        else:
            assert losses.numel() != 0
            return losses
Esempio n. 5
0
    def _calc_be_invited(self, data, req, timer):
        """
        """
        inviter_id = utils.decode(req.invite_code, utils.INVITE_CODE_KEY)
        if inviter_id == "":
            inviter_id = 0
        else:
            inviter_id = long(inviter_id)

        cache_proxy = DataProxy()
        #查询玩家所在房间人数
        cache_proxy.search("user", inviter_id)
        defer = cache_proxy.execute()
        defer.addCallback(self._check_inviter_id, data, inviter_id, req, timer)
        return defer
    def predict(model, pr_loader, do_plot=False):
        sample = pr_loader.get_random_sample()

        dim_sample = sample.unsqueeze(0)
        model.eval()
        output = model(dim_sample)

        tokens = output.softmax(2).argmax(2)
        tokens = tokens.squeeze(1).numpy()

        text = utils.decode(tokens[0], pr_loader.__get_chars__())

        if do_plot:
            i = transforms.ToPILImage().__call__(sample)
            #     i.save('my.png')
            plt.imshow(i)
            plt.show()

        return text
Esempio n. 7
0
                        loss = criterion(preds, text, preds_size, length)

                    # 后处理解码
                    if config.ATTENTION.ENABLE:
                        _, preds = preds.max(1)
                        index = 0
                        for label in labels:
                            pre = "".join(str(preds[index:index + len(label)]))
                            index += len(label)
                            if pre == label:
                                n_correct += 1
                    else:
                        _, preds = preds.max(2)
                        preds = preds.transpose(1, 0).contiguous().view(-1)
                        sim_preds = decode(preds.data,
                                           preds_size.data,
                                           config.DICT,
                                           raw=False)
                        for pred, target in zip(sim_preds, labels):
                            if pred == target:
                                n_correct += 1

            # 抓一个batch来显示
            if not config.ATTENTION.ENABLE:
                raw_preds = decode(preds.data,
                                   preds_size.data,
                                   config.DICT,
                                   raw=True)[:config.TEST.NUM_TEST_DISP]
                for raw_pred, pred, gt in zip(raw_preds, sim_preds, labels):
                    print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt))
            now_acc = n_correct * 1.0 / test_num
            print("best_acc:{} correct:{} total:{}".format(
Esempio n. 8
0
def post_process(cfg, c_batch, box_batch, iou_batch, anchors, resized_size):
    total_boxes = []

    for c_fpn, box_fpn, iou_fpn, anchor_fpn in zip(c_batch, box_batch,
                                                   iou_batch, anchors):
        N, _, H, W = c_fpn.shape
        A = box_fpn.size(1) // 4  # 'A' means num_anchors per location
        C = c_fpn.size(1) // A

        c_fpn = permute_and_flatten(c_fpn, N, A, C, H,
                                    W)  # shape: (n, num_anchor, 80)
        c_fpn = c_fpn.sigmoid()

        box_fpn = permute_and_flatten(box_fpn, N, A, 4, H,
                                      W)  # shape: (n, num_anchor, 4)
        box_fpn = box_fpn.reshape(N, -1, 4)

        iou_fpn = permute_and_flatten(iou_fpn, N, A, 1, H, W)
        iou_fpn = iou_fpn.reshape(N, -1).sigmoid()

        # multiply classification and IoU to get the score
        score_fpn = (c_fpn * iou_fpn[:, :, None]).sqrt()

        # use class score to do the pre-threshold
        candi_i_fpn = c_fpn > cfg.nms_score_thre  # TODO: if use score_fpn to do score threshold?
        nms_topk_fpn = candi_i_fpn.reshape(N, -1).sum(dim=1)
        nms_topk_fpn = nms_topk_fpn.clamp(max=cfg.nms_topk)

        results = []
        for score, box, nms_topk, candi_i, size in zip(score_fpn, box_fpn,
                                                       nms_topk_fpn,
                                                       candi_i_fpn,
                                                       resized_size):
            score = score[
                candi_i]  # TODO: too much thre is not elegant, too handcrafted
            score, topk_i = score.topk(
                nms_topk, sorted=False)  # use score to get the topk

            candi_i = candi_i.nonzero()[topk_i, :]

            box_selected = box[candi_i[:, 0], :].reshape(-1, 4)
            anchor_selected = anchor_fpn.box[candi_i[:, 0], :].reshape(-1, 4)

            anchor_selected = anchor_selected.cuda()

            box_decoded = decode(box_selected, anchor_selected)

            boxlist = BoxList(box_decoded,
                              size,
                              mode='x1y1x2y2',
                              label=candi_i[:, 1] + 1,
                              score=score)
            boxlist.clip_to_image(remove_empty=False)
            boxlist.remove_small_box(min_size=0)

            results.append(boxlist)

        total_boxes.append(results)

    box_list_fpn_batch = list(
        zip(*total_boxes)
    )  # bind together the fpn box_lists which belong to the same batch
    box_list_batch = [cat_boxlist(aa) for aa in box_list_fpn_batch]

    return select_over_all_levels(cfg, box_list_batch)
Esempio n. 9
0
        def runInThread():
            try:
                is_up_8_0 = True if self.apiLevel() > 25 else False
                _cmd = 'adb shell top -O RSS -d 1 ' if is_up_8_0 else 'adb shell top -m 50 -s rss -d 1'
                p = subprocess.Popen(_cmd,
                                     shell=True,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
                tmp_memory_total = 0
                pkg_cpu = 0.0
                while self.cpuHasRun and subprocess.Popen.poll(p) is None:
                    output = utils.decode(p.stdout.readline().strip())
                    # print(output)
                    if is_up_8_0 == False:
                        r = re.search('K\\s+(\\d+)K', output)
                        if r is not None:
                            tmp_memory_total = tmp_memory_total + int(
                                r.group(1))
                            if pkg in output:
                                arr = output.split()
                                if is_up_8_0 == False:
                                    pkg_cpu += float(
                                        arr[self._Adb__cpu_index].replace(
                                            '%', ''))
                                    self._props['cpu'] = '{}%'.format(pkg_cpu)
                        elif 'CPU' in output:
                            self._props['cpu'] = '{}%'.format(pkg_cpu)
                            pkg_cpu = 0.0
                            if is_up_8_0 == False:
                                self._props['total_memory'] = tmp_memory_total
                                tmp_memory_total = 0
                            if not self._Adb__has_cpu_index:
                                arr = output.split()
                                for i in range(len(arr)):
                                    if 'CPU' in arr[i]:
                                        self._Adb__cpu_index = i
                                        self._Adb__has_cpu_index = True
                                    if 'RSS' in arr[i]:
                                        self._Adb__memory_index = i - 1 if is_up_8_0 else i

                        else:
                            if 'User' in output:
                                r = is_up_8_0 or re.search(
                                    'User\\s(\\d+)%[\\S\\s]*System\\s(\\d+)%',
                                    output)
                                if r:
                                    r = list(
                                        map(lambda it: int(it), r.groups()))
                                    self._props['total_cpu'] = '{}%'.format(
                                        sum(r))
                    else:
                        if 'user' in output:
                            if is_up_8_0 and self._Adb__cpu_count is not None:
                                rr = re.search('(\\d+)%cpu', output)
                                if rr:
                                    self._Adb__cpu_count = int(
                                        rr.group(1)) / 100
                                r = re.search(
                                    '\\s(\\S+)%user[\\s\\S]*\\s(\\S+)%sys',
                                    output)
                                if r:
                                    # r = list(map(lambda it: float(it), r.groups()))
                                    # self._props['total_cpu'] = '{}%'.format(sum(r))
                                    r = list(
                                        map(lambda it: float(it), r.groups()))
                                    total = sum(
                                        r
                                    ) / self._Adb__cpu_count if self._Adb__cpu_count else sum(
                                        r)
                                    self._props['total_cpu'] = '{}%'.format(
                                        total)

                        elif 'Mem:' in output and is_up_8_0:
                            r = re.search('(\\d+)k\\sused', output)
                            if r:
                                self._props['total_memory'] = int(
                                    r.groups()[0])

                        elif 'CPU' in output:
                            self._props['cpu'] = '{}%'.format(pkg_cpu)
                            pkg_cpu = 0.0
                            if not self._Adb__has_cpu_index:
                                arr = output.split()
                                for i in range(len(arr)):
                                    if 'CPU' in arr[i]:
                                        self._Adb__cpu_index = i
                                        # print('self._Adb__cpu_index==%s'%self._Adb__cpu_index)
                                        self._Adb__has_cpu_index = True
                                    if 'RSS' in arr[i]:
                                        self._Adb__memory_index = i - 1 if is_up_8_0 else i

                        else:
                            if pkg in output:
                                arr = output.split()
                                _cpu = float(
                                    arr[self._Adb__cpu_index]
                                ) / self._Adb__cpu_count if self._Adb__cpu_count else arr[
                                    self._Adb__cpu_index]
                                # print('{},{}'.format(pkg_cpu,_cpu))
                                pkg_cpu += _cpu
                                # print(pkg_cpu)
                                # pkg_cpu += float(arr[self._Adb__cpu_index].replace('%', ''))
                                self._props['cpu'] = '{}%'.format(pkg_cpu)
                self.cpuHasRun = False
                p.stdout.close()
                p.kill()
            except Exception as e:
                try:
                    self.cpuHasRun = False
                finally:
                    e = None
                    del e
Esempio n. 10
0
    def __call__(self, c_pred, box_pred, iou_pred, box_list_batch):
        # c_init_batch: (bs * num_anchor,), 0 for background, -1 for ignored
        # offset_init_batch: (bs * num_anchor, 4)
        # index_init_batch: (bs * num_anchor,), -1 for bakground, -2 for ignored
        self.anchor_cat = cat_boxlist(self.anchors)

        c_init_batch, offset_init_batch, index_init_batch = self.initial_preparation(
            box_list_batch)
        pos_i_init = torch.nonzero(c_init_batch > 0).reshape(-1)

        c_pred_f, box_pred_f, iou_pred_f, anchor_f = concat_fpn_pred(
            c_pred, box_pred, iou_pred, self.anchor_cat)

        if pos_i_init.numel() > 0:
            with torch.no_grad(
            ):  # compute anchor scores (losses) for all anchors, gradient is not needed.
                c_loss = focal_loss_cuda(c_pred_f, c_init_batch,
                                         self.cfg.fl_gamma, self.cfg.fl_alpha)
                box_loss = self.GIoULoss(box_pred_f,
                                         offset_init_batch,
                                         anchor_f,
                                         weight=None)
                box_loss = box_loss[c_init_batch > 0].reshape(-1)

                box_loss_full = torch.full((c_loss.shape[0], ),
                                           fill_value=10000,
                                           device=c_loss.device)
                assert box_loss.max(
                ) < 10000, 'box_loss_full initial value error'
                box_loss_full[pos_i_init] = box_loss

                score_batch = c_loss.sum(dim=1) + box_loss_full
                assert not torch.isnan(
                    score_batch).any()  # all the elements should not be nan

            # compute labels and targets using PAA
            final_c_batch, final_offset_batch = self.compute_paa(
                box_list_batch, c_init_batch, score_batch, index_init_batch)

            pos_i_final = torch.nonzero(final_c_batch > 0).reshape(-1)
            num_pos = pos_i_final.numel()

            box_pred_f = box_pred_f[pos_i_final]
            final_offset_batch = final_offset_batch[pos_i_final]
            anchor_f = anchor_f[pos_i_final]
            iou_pred_f = iou_pred_f[pos_i_final]

            gt_boxes = decode(final_offset_batch, anchor_f)
            box_pred_decoded = decode(box_pred_f, anchor_f).detach()
            iou_gt = self.compute_ious(gt_boxes, box_pred_decoded)

            cls_loss = focal_loss_cuda(c_pred_f, final_c_batch.int(),
                                       self.cfg.fl_gamma, self.cfg.fl_alpha)
            box_loss = self.GIoULoss(box_pred_f,
                                     final_offset_batch,
                                     anchor_f,
                                     weight=iou_gt)
            box_loss = box_loss[final_c_batch[pos_i_final] > 0].reshape(-1)
            iou_pred_loss = self.iou_bce_loss(iou_pred_f, iou_gt)
            iou_gt_sum = iou_gt.sum().item()
        else:
            box_loss = box_pred_f.sum()

        category_loss = cls_loss.sum() / num_pos
        box_loss = box_loss.sum() / iou_gt_sum * self.cfg.box_loss_w
        iou_pred_loss = iou_pred_loss / num_pos * self.cfg.iou_loss_w

        return category_loss, box_loss, iou_pred_loss