Пример #1
0
 def gen_tracklet(self, framedata, x, action_score):
     x1, y1, x2, y2 = map(float, framedata['dets'][x])
     conf = float(framedata['confs'][x])
     feat = framedata['feats'][x]
     d = Det(x1,
             y1,
             x2 - x1,
             y2 - y1,
             confidence=conf,
             uid=self.new_id,
             fr=self.fr,
             status=1)
     t = Tracklet(d, feat)
     t.actions = deque(maxlen=self._max_len)
     t.gts = deque(maxlen=self._max_len)
     t.gts_keys = {}
     action_score = self.sigmoid_inv(action_score)
     t.actions.append((x, action_score, (x1, y1, x2, y2, conf), feat))
     t.cum_score = math.exp(-action_score)
     gid = -1
     if self._gtid_for_dets is not None:
         gid = self._gtid_for_dets[x]
     t.gts.append(gid)
     t.gts_keys[gid] = 1
     t.pre_score = action_score
     return t
Пример #2
0
 def append_new(self, j, framedata):
     x1, y1, x2, y2 = map(float, framedata['dets'][j])
     w = x2 - x1
     h = y2 - y1
     d = Det(x1,
             y1,
             w,
             h,
             cls=1,
             confidence=float(framedata['confs'][j]),
             uid=self.new_id,
             fr=self.fr,
             status=1)
     t = Tracklet(d, framedata['feats'][j])
     if self._use_gt:
         assert self._current_gt is not None
         gt_id = -1
         mxiou = -1
         for gdet in self._current_gt:
             iou = t.det.iou(gdet)
             if iou > 0.5 and iou > mxiou:
                 mxiou = iou
                 gt_id = gdet.uid
         t.gt_id = gt_id
     t.last_update = self.fr
     t.locs.append(((x1 + x2) / 2., (y1 + y2) / 2.))
     self.tracklets.append(t)
Пример #3
0
 def get_gt_sim(self, framedata):
     assert self._current_gt is not None
     gt_dets = {d.uid: d for d in self._current_gt}
     sims = []
     for t in self.tracklets:
         sims.append([])
         for i in range(len(framedata['dets'])):
             if t.gt_id not in gt_dets:
                 sims[-1].append(0)
             else:
                 gdet = gt_dets[t.gt_id]
                 x1, y1, x2, y2 = map(float, framedata['dets'][i])
                 ddet = Det(x1, y1, x2 - x1, y2 - y1)
                 iou = ddet.iou(gdet)
                 if iou > 0.5:
                     sims[-1].append(iou + (1 - t.det.uid / self.idc) / 100)
                 else:
                     sims[-1].append(0)
     return np.array(sims, dtype=np.float32)
Пример #4
0
def test_time1():
    a = Det(101.3, 413.5, 67.2, 201.1)
    assert (a.cx == approx(101.3 + 67.2 / 2))
    assert (a.cy == approx(413.5 + 201.1 / 2))
    assert (a.x1 == approx(101.3))
    assert (a.y1 == approx(413.5))
    assert (a.w == approx(67.2))
    assert (a.h == approx(201.1))
    assert (a.x2 == approx(101.3 + 67.2))
    assert (a.y2 == approx(413.5 + 201.1))
    assert (a.area() == approx(67.2 * 201.1))
    a.cx += 1
    assert (a.x1 == approx(101.3 + 1))
    assert (a.x2 == approx(101.3 + 67.2 + 1))
    a.y2 += 2
    assert (a.y1 == approx(413.5))
    assert (a.cy == approx(413.5 + 201.1 / 2 + 1))
    b = Det(94.5, 510.1, 70.9, 203.7)
    assert (a.intersection(b) == approx(
        (94.5 + 70.9 - 101.3 - 1) * (413.5 + 201.1 + 2 - 510.1)))
    assert (a.iou(b) == approx(
        a.intersection(b) / (67.2 * 203.1 + 70.9 * 203.7 - a.intersection(b))))
    c = 0.
    be = time.time()
    a_ = [a] * 5000000
    b_ = [b] * 5000000
    for i in range(5000000):
        c += a_[i].iou(b_[i])
    en = time.time()
    t1 = en - be
    # print(t1)
    # assert(t1 < 2)

    c = 0.
    be = time.time()
    for i in range(500000):
        a._trim(sz=(150, 150))
    en = time.time()
    t2 = en - be
Пример #5
0
    def __call__(self, framedata, gt=None):
        self.fr += 1
        # print(self.fr)
        if gt is not None:
            framedata['gt_dets'] = np.array(
                [(d.x1, d.y1, d.x2, d.y2) for d in gt], dtype=np.float32)
            framedata['gt_ids'] = np.array([d.uid for d in gt], dtype=np.int)
            framedata['gt_vis'] = np.array(
                [d.conf for d in gt], dtype=np.float32)
            framedata['gt_inds'] = np.array([1 for d in gt], dtype=np.bool8)
            framedata['gt_dets'][:, 0::2] /= framedata['origin_size'][0]
            framedata['gt_dets'][:, 1::2] /= framedata['origin_size'][1]
            framedata['inds'] = np.array(
                [1 for d in framedata['dets'][0]], dtype=np.bool8)

        if isinstance(framedata['dets'], (list, tuple)):
            framedata['dets'] = framedata['dets'][0]
        if isinstance(framedata['confs'], (list, tuple)):
            framedata['confs'] = framedata['confs'][0]
        if isinstance(framedata['feats'], (list, tuple)):
            framedata['feats'] = framedata['feats'][0]
        framedata_on_device = to_device(
            framedata, device=self.device, make_batch_dim=True)
        # print(framedata_on_device['dets'])

        with torch.no_grad():
            out, _ = self.track_model(framedata_on_device)
            if gt is not None:
                # print(framedata_on_device['gt_dets'])
                _ = self.track_model.map_gt_ids(out, framedata_on_device)
            out_dets = out['output_dets']
            out_ids = out['output_ids']
            print(out['conf_mat'].shape, framedata['dets'].shape)
            # print(framedata['confs'])
            # print(out['conf_mat'])
            print(self.track_model._tracks['gt_ids'][0, :30])
            print(self.track_model._tracks['act_sc'][0, :30])
            input()
            if isinstance(out_dets, list):
                out_dets = out_dets[0]
                out_ids = out_ids[0]
            print(out_dets.shape)
            out_dets[:, 0::2] *= framedata['origin_size'][0]
            out_dets[:, 1::2] *= framedata['origin_size'][1]
            for uid, det in zip(out_ids, out_dets):
                uid = int(uid)
                x1, y1, x2, y2 = map(float, det)
                d = Det(x1, y1, x2 - x1 + 1, y2 - y1 +
                        1, cls=1, uid=uid, fr=self.fr)
                self.outputs.append_data(d)
Пример #6
0
 def update_binary(self, i, j, framedata):
     self.tracklets[i].feat = framedata['feats'][j]
     x1, y1, x2, y2 = map(float, framedata['dets'][j])
     w = x2 - x1
     h = y2 - y1
     d = Det(x1,
             y1,
             w,
             h,
             cls=1,
             confidence=float(framedata['confs'][j]),
             uid=self.tracklets[i].det.uid,
             fr=self.fr,
             status=1)
     self.cur_delta[0] += (d.cx - self.tracklets[i].det.cx) / \
         (self.fr - self.tracklets[i].last_update)
     self.cur_delta[1] += (d.cy - self.tracklets[i].det.cy) / \
         (self.fr - self.tracklets[i].last_update)
     self.tracklets[i].det = d
     self.tracklets[i].last_update = self.fr
     self.tracklets[i].locs.append(((x1 + x2) / 2., (y1 + y2) / 2.))
Пример #7
0
 def __call__(self, framedata):
     framedata = to_torch(framedata, device=self.device)
     # print(framedata)
     self.fr += 1
     with torch.no_grad():
         out = self.model(framedata)
     for i, (score, box) in enumerate(
             zip(out['results']['cls'], out['results']['reg'])):
         if score > 0.6:
             x1, y1, x2, y2 = map(float, box *
                                  1000)  # * 1000 / math.sqrt(4 / 512))
             uid = int(out['ids'][i].argmax())
             d = Det(x1,
                     y1,
                     x2 - x1,
                     y2 - y1,
                     uid=uid,
                     fr=self.fr,
                     status=1)
             # print(d)
             self.outputs.append_data(d)
     self.dump('/home/toka/data/tmp_dnc.txt')
Пример #8
0
    def __init__(self, length=200, n=15, min_len=100) -> None:
        self.n = n
        self.seqlen = length
        ts = TrackSet()
        bound_w = 500
        bound_h = 1200
        avg_h = 162
        avg_w = 54
        sigma = 0.1
        self.min_len = min_len
        calibrator = Calibrator(x=bound_w / 2, y=-200, z=avg_h * 2)
        for i in range(n):
            L = random.randint(self.min_len, self.seqlen)
            start = random.randint(1, length - L + 1)
            end = random.randint(start + L - 1, length)
            sx, sy = random.randint(1, bound_w), random.randint(1, bound_h)
            tx, ty = random.randint(1, bound_w), random.randint(1, bound_h)
            d = ((sx - tx)**2 + (tx - ty)**2)**0.5
            mx, my = (sx + tx) / 2, (sy + ty) / 2
            mx += random.betavariate(2, 5) * d * randsign()
            my += random.betavariate(2, 5) * d * randsign()

            ps = BCurve([(sx, sy), (mx, my), (tx, ty)], end - start + 1)

            w = avg_w * (1 + random.gauss(0, sigma))
            h = avg_h * (1 + random.gauss(0, sigma))

            lt = []
            for x, y in ps:
                lt.append((x - w / 2, y, h))
            rt = []
            for x, y in ps:
                rt.append((x + w / 2, y, h))
            lb = []
            for x, y in ps:
                lb.append((x - w / 2, y, 0))
            rb = []
            for x, y in ps:
                rb.append((x + w / 2, y, 0))

            # print(lt[0], rt[0], lb[0], rb[0])

            lt = calibrator(lt)
            rt = calibrator(rt)
            lb = calibrator(lb)
            rb = calibrator(rb)

            # print(lt[0], rt[0], lb[0], rb[0])

            x1_1 = list(map(lambda x: x[0], lt))
            x1_2 = list(map(lambda x: x[0], lb))
            x1 = list(map(lambda a: sum(a) / 2, zip(x1_1, x1_2)))
            x2_1 = list(map(lambda x: x[0], rt))
            x2_2 = list(map(lambda x: x[0], rb))
            x2 = list(map(lambda a: sum(a) / 2, zip(x2_1, x2_2)))
            y1_1 = list(map(lambda x: x[1], lt))
            y1_2 = list(map(lambda x: x[1], rt))
            y1 = list(map(lambda a: sum(a) / 2, zip(y1_1, y1_2)))
            y2_1 = list(map(lambda x: x[1], lb))
            y2_2 = list(map(lambda x: x[1], rb))
            y2 = list(map(lambda a: sum(a) / 2, zip(y2_1, y2_2)))

            for fr, one in enumerate(zip(x2, y1, x1, y2)):
                xx, yy, u, v = one
                ww = u - xx
                hh = v - yy
                d = Det(xx, yy, ww, hh, uid=i + 1, fr=fr + start)
                if d.trim((calibrator.w, calibrator.h)).iou(d) > 0.2:
                    d.status = 1
                else:
                    d.status = 0
                d.conf = 1
                ts.append_data(d)
        self.data = ts
        self.gt = ts
        self._feat_dim = 508
        self._trans_mat = np.random.randn(self._feat_dim,
                                          self._feat_dim).astype(np.float32)
        with open('/home/toka/data/psd-1/gt.txt', 'w') as fd:
            self.data.dump(fd, formatter='fr.i,id.i,x1,y1,w,h,st.i,1,st.i')
Пример #9
0
def test_dets():
    a = Det(101.3, 413.5, 67.2, 201.1)
    assert (a.cx == approx(101.3 + 67.2 / 2))
    assert (a.cy == approx(413.5 + 201.1 / 2))
    assert (a.x1 == approx(101.3))
    assert (a.y1 == approx(413.5))
    assert (a.w == approx(67.2))
    assert (a.h == approx(201.1))
    assert (a.x2 == approx(101.3 + 67.2))
    assert (a.y2 == approx(413.5 + 201.1))
    assert (a.area() == approx(67.2 * 201.1))
    a.cx += 1
    assert (a.x1 == approx(101.3 + 1))
    assert (a.x2 == approx(101.3 + 67.2 + 1))
    a.y2 += 2
    assert (a.y1 == approx(413.5))
    assert (a.cy == approx(413.5 + 201.1 / 2 + 1))
    b = Det(94.5, 510.1, 70.9, 203.7)
    assert (a.intersection(b) == approx(
        (94.5 + 70.9 - 101.3 - 1) * (413.5 + 201.1 + 2 - 510.1)))
    assert (a.iou(b) == approx(
        a.intersection(b) / (67.2 * 203.1 + 70.9 * 203.7 - a.intersection(b))))
    a.cx -= 150
    assert (a._trim(sz=(1920, 1080), toInt=False).x1 == approx(0))
    assert (a._trim(sz=(1920, 1080), toInt=False).cx == approx(
        (101.3 + 1 + 67.2 - 150) / 2.))
    assert (a._trim(sz=(1920, 1080), toInt=True).cx == approx(
        int((101.3 + 1 + 67.2 - 150) / 2))
            or a._trim(sz=(1920, 1080), toInt=True).cx == approx(
                (int(101.3 + 1 + 67.2 - 150) / 2)))
Пример #10
0
def test_trackset():
    with NamedTemporaryFile(delete=False, mode='w') as ntf:
        ntf.write('1,0,10,10,20,20\n'
                  '2,0,10,10,20,20\n'
                  '3,0,10,10,20,20\n'
                  '4,0,10,10,20,20\n'
                  '1,2,110,10,20,20\n'
                  '2,2,110,10,20,20\n'
                  '3,2,110,10,20,20\n'
                  '4,2,110,10,20,201\n')
    try:
        ts = TrackSet(ntf.name)
        assert (False)
    except Exception as e:
        if type(e) != AssertionError:
            assert (e.args[0] == 'Unknown Format')
        else:
            raise e
    ts = ts = TrackSet(ntf.name, formatter='fr.i,id.i,x1,y1,w,h')
    for i in range(1, 5):
        assert (len(ts[i]) == 2)
        assert (ts.fr_count(i) == 2)
    assert (len(ts[0]) == 0)
    assert (len(ts[5]) == 0)
    assert (ts.fr_count() == 4)
    assert (ts.id_count() == 2)
    assert (ts.id_count(1) == 0)
    assert (ts.id_count(2) == 4)
    assert (ts.count() == 8)
    ts.append_data(Det(5, 5, 5, 5, fr=1, uid=3))
    assert (ts.fr_count() == 4)
    assert (ts.id_count() == 3)
    assert (ts.count() == 9)

    tsa = ts(1)
    assert (tsa is None)
    tsa = ts(2)
    tsa2 = ts(2)
    assert (id(tsa) == id(tsa2))
    assert (tsa.count() == 4 and tsa.fr_count(4) == 1
            and tsa[4][0].area() == 4020)
    ts.delete(tsa[4][0])
    ts.delete(ts[4][0])
    assert (ts.fr_count() == 3)
    assert (ts.id_count() == 3)
    assert (ts.count() == 7)
    ts[2:4] = tsa[2:4]
    assert (ts.fr_count() == 3)
    assert (ts.id_count() == 3)
    assert (ts.count() == 5)
    tsa[2:] = None
    assert (tsa.fr_count() == 1)
    assert (tsa.id_count() == 1)
    assert (tsa.count() == 1)
    tsa[:2] = None
    assert (tsa.fr_count() == 0)
    assert (tsa.id_count() == 0)
    assert (tsa.count() == 0)
    with NamedTemporaryFile(delete=False, mode='w') as ntf:
        ntf.write('1,0,10,10,20,20\n'
                  '2,0,10,10,20,20\n'
                  '3,0,10,10,20,20\n'
                  '4,0,10,10,20,20\n'
                  '1,2,110,10,20,20\n'
                  '2,2,110,10,20,20\n'
                  '3,2,110,10,20,20\n'
                  '4,2,110,10,20,201\n'
                  '2,3,110,10,20,20\n'
                  '3,3,110,10,20,20\n'
                  '4,3,110,10,20,201\n')
    ts = TrackSet(ntf.name, formatter='fr.i,id.i,x1,y1,w,h')
    a = ts(0)
    b = ts(2)
    c = ts(3)
    tsa = a + b
    assert (tsa.fr_count() == 4)
    assert (tsa.id_count() == 2)
    assert (tsa.count() == 8)

    assert (c.fr_count() == 3)
    assert (c.id_count() == 1)
    assert (c.count() == 3)

    tsb = b + c

    assert (b.fr_count() == 4)
    assert (b.id_count() == 1)
    assert (b.count() == 4)

    assert (tsb.fr_count() == 4)
    assert (tsb.id_count() == 2)
    assert (tsb.count() == 7)

    iddr = id(tsb)

    tsb += a

    assert (tsb.fr_count() == 4)
    assert (tsb.id_count() == 3)
    assert (tsb.count() == 11)
    assert (id(tsb) == iddr)

    assert (b.fr_count() == 4)
    assert (b.id_count() == 1)
    assert (b.count() == 4)