Ejemplo n.º 1
0
Archivo: app.py Proyecto: RuaHU/TLfPS
 def __init__(self, detector, det = None, name = 'SelectFrame'):
     wx.Frame.__init__(self, None, title=name)
     self.sizer = wx.BoxSizer(wx.VERTICAL)
     self.SetSizer(self.sizer)
     self.config = Config(detector)
     self.DA = DA('validation', self.config)
     self.fmts = ['*.jpg', '*.png', '*.bmp','*.jpeg']
     self.menuBar()
     self.Center()
     self.query = {}
     self.gallery = {}
     pub.subscribe(self.queryReid, 'query')
Ejemplo n.º 2
0
 def __init__(self,
              dataset,
              detector='yolov3',
              experiment_name='default',
              overwrite=False):
     self.detector = detector
     self.dataset = dataset
     self.overwrite = overwrite
     self.experiment_name = experiment_name
     self.checkdir()
     self.config = Config(detector)
     self.DA = DA('validation', self.config)
Ejemplo n.º 3
0
 def __init__(self,
              dataset,
              detector='yolov3',
              experiment_name='default',
              overwrite=False):
     if dataset is None: return
     self.detector = detector
     self.dataset = dataset
     self.overwrite = overwrite
     self.experiment_name = experiment_name
     self.checkdir()
     self.config = Config(detector)
     self.DA = DA('validation', self.config)
     self.load_gallery()
     self.load_query()
Ejemplo n.º 4
0
class EVALUATION():
    def __init__(self,
                 dataset,
                 detector='yolov3',
                 experiment_name='default',
                 overwrite=False):
        self.detector = detector
        self.dataset = dataset
        self.overwrite = overwrite
        self.experiment_name = experiment_name
        self.checkdir()
        self.config = Config(detector)
        self.DA = DA('validation', self.config)

    def get_sims(self, gfeats, qfeat, _eval=True):
        '''
        gfeats: gallery features
        qfeat: query feature
        '''
        if _eval: return gfeats.dot(qfeat.ravel()).ravel()
        gfeats_norm = np.linalg.norm(gfeats, keepdims=True, axis=-1)
        qfeat_norm = np.linalg.norm(qfeat, keepdims=True)
        gfeats_nl = gfeats / gfeats_norm
        qfeat_nl = qfeat / qfeat_norm
        sim = gfeats_nl.dot(qfeat_nl.ravel()).ravel()
        return sim

    def checkdir(self, ):
        dirpath = os.path.join(parpath, 'experiment_results',
                               self.experiment_name)
        if os.path.exists(dirpath):
            print('experiment [%s] existed' % self.experiment_name)
            if self.overwrite:
                print('cleaning experiment [%s] [overwrite == True]' %
                      self.experiment_name)
                shutil.rmtree(dirpath, ignore_errors=True)
                if os.path.exists(dirpath):
                    print(
                        'it seems the experiment directory can not be deleted. please check the status of the directory %s'
                        % dirpath)
                os.mkdir(dirpath)
                assert os.path.exists(dirpath)
            else:
                print(
                    'the results of experiment [%s] will be reused [overwrite == False]'
                    % self.experiment_name)
        else:
            os.mkdir(dirpath)
            assert os.path.exists(dirpath)

    def load_model(self, ):
        self.reid_model = MODELS(config=self.config).load_model()

    def simple_evaluation(self, model, gallery_size=50):
        #extract query feature vectors
        TestG50 = loadmat(
            os.path.join(self.dataset,
                         'dataset/annotation/test/train_test/TestG50.mat')
        )['TestG50'].squeeze()
        qfeatures = []
        for item in TestG50['Query']:
            img_name = item['imname'][0, 0][0]
            roi = item['idlocate'][0, 0][0].astype(np.int32)
            img = cv2.imread(
                os.path.join(self.dataset, 'dataset/Image/SSM/', img_name))
            input_img, input_box, input_ids, meta = self.DA(img, [roi])
            feature = model.predict([
                np.stack([input_img]),
                np.stack([input_box]),
                np.stack([input_ids])
            ])
            qfeatures.append(
                [img_name, feature[0, :, 0, 0, :],
                 np.array([roi])])
            print("\r%d|%d" % (len(qfeatures), len(TestG50['Query'])), end='')
        print('')

        #extract gallery feature vectors
        filepath = os.path.join(
            parpath, 'experiment_results/cuhk_%s_gallery.pkl' % self.config.M)
        assert os.path.exists(filepath)
        f = open(filepath, 'rb')
        pre_gallery = pickle.load(f, encoding='latin1')
        gallery = []
        for item in pre_gallery:
            imname, features, boxes = item
            if features is None:
                features = np.zeros([0, 256], dtype=np.float32)
                boxes = np.zeros([0, 5], dtype=np.float32)
            img = cv2.imread(
                os.path.join(self.dataset, 'dataset/Image/SSM/', imname))
            #xyxy 2 xywh
            t_boxes = boxes.copy()
            t_boxes[:, 2:4] -= t_boxes[:, :2]
            input_img, input_box, input_ids, meta = self.DA(
                img, t_boxes[:, :4])
            feats = model.predict([
                np.stack([input_img]),
                np.stack([input_box]),
                np.stack([input_ids])
            ])
            gallery.append([imname, feats[0, :, 0, 0, :], boxes])
            print("\r%d|%d" % (len(gallery), len(pre_gallery)), end='')
        print('')
        name_to_det_feat = {}
        for img_name, features, boxes in gallery:
            name_to_det_feat[img_name] = (boxes, features)
        return self.evaluation(qfeatures,
                               name_to_det_feat,
                               _eval=False,
                               gallery_size=gallery_size)

    def evaluation(self, qfeatures, name_to_det_feat, _eval, gallery_size=100):
        fname = 'TestG{}'.format(gallery_size if (gallery_size != -1) else 50)
        protoc = loadmat(
            os.path.join(self.dataset, 'dataset/annotation/test/train_test',
                         fname + '.mat'))[fname].squeeze()
        aps, accs, topk = [], [], [1, 5, 10]
        all_recall_rate = []
        tape = {}
        for i in range(len(qfeatures)):
            y_true, y_score = [], []
            y_boxes, y_gname = [], []
            count_gt, count_tp = 0, 0
            qimg_name, qfeat, qbox = qfeatures[i]
            probe_imname = str(protoc['Query'][i]['imname'][0, 0][0])
            assert probe_imname == qimg_name
            tested = set([probe_imname])
            for item in protoc['Gallery'][i].squeeze():
                gallery_imname = str(item[0][0])
                gt = item[1][0].astype(np.int32)
                gt[2:] += gt[:2]
                count_gt += (gt.size > 0)
                if gallery_imname not in name_to_det_feat: continue
                gboxes, gfeatures = name_to_det_feat[gallery_imname]
                sim = self.get_sims(gfeatures, qfeat, _eval)
                label = np.zeros(len(sim), dtype=np.int32)
                if gt.size > 0:
                    w, h = gt[2] - gt[0], gt[3] - gt[1]
                    iou_thresh = min(0.5,
                                     (w * h * 1.0) / ((w + 10) * (h + 10)))
                    inds = np.argsort(sim)[::-1]
                    sim = sim[inds]
                    gboxes = gboxes[inds]
                    for j, roi in enumerate(gboxes[:, :4]):
                        if self._compute_iou(roi, gt) >= iou_thresh:
                            label[j] = 1
                            count_tp += 1
                            break

                y_true.extend(list(label))
                y_score.extend(list(sim))
                y_boxes.extend(list(gboxes))
                y_gname.extend([gallery_imname for _ in gboxes])
                tested.add(gallery_imname)
            if gallery_size == -1:
                for gallery_imname in name_to_det_feat.keys():
                    if gallery_imname in tested: continue
                    gboxes, gfeatures = name_to_det_feat[gallery_imname]
                    sim = self.get_sims(gfeatures, qfeat, _eval)
                    label = np.zeros(len(sim), dtype=np.int32)
                    y_true.extend(list(label))
                    y_score.extend(list(sim))
                    y_boxes.extend(list(gboxes))
                    y_gname.extend([gallery_imname for _ in gboxes])

            y_score = np.array(y_score)
            y_true = np.array(y_true)
            y_boxes = np.array(y_boxes)
            y_gname = np.array(y_gname)
            assert count_tp <= count_gt
            recall_rate = count_tp * 1.0 / count_gt
            all_recall_rate.append(recall_rate)
            ap = 0 if count_tp == 0 else \
                average_precision_score(y_true, y_score) * recall_rate
            aps.append(ap)

            inds = np.argsort(y_score)[::-1]
            y_score = y_score[inds]
            y_true = y_true[inds]
            y_boxes = y_boxes[inds]
            y_gname = y_gname[inds]
            acc = [min(1, sum(y_true[:k])) for k in topk]
            accs.append(acc)
            tape[qimg_name] = [
                i, qbox, ap, acc, recall_rate, y_score, y_true, y_boxes,
                y_gname
            ]
            print("\r%d:\t%d|%d|%.2f|%.2f" %
                  (gallery_size, len(aps), len(qfeatures), np.mean(aps),
                   np.mean(accs, axis=0)[0]),
                  end='')
        print('')
        print('search ranking:')
        print('aRR:%.4f' % np.mean(all_recall_rate))
        print('  mAP = {:.2%}'.format(np.mean(aps)))
        accs = np.mean(accs, axis=0)
        for i, k in enumerate(topk):
            print('  top-{:2d} = {:.2%}'.format(k, accs[i]))

        if gallery_size == -1:
            record_aps = []
            new_tape = {}
            for key in tape.keys():
                record_aps.append(tape[key][2])
            record_aps.sort()
            th = record_aps[50]
            for key in tape.keys():
                if tape[key][2] > th: continue
                new_tape[key] = tape[key]

            filepath = os.path.join(parpath, 'experiment_results',
                                    self.experiment_name,
                                    'cuhk_%s_tape.pkl' % self.config.M)
            pickle.dump(new_tape, filepath)

        return aps, accs

    def query_feature_extractor(self, ):
        filepath = os.path.join(parpath, 'experiment_results',
                                self.experiment_name,
                                'cuhk_%s_query_features.pkl' % self.config.M)
        if os.path.exists(filepath):
            return

        if not hasattr(self, 'reid_model'):
            self.load_model()

        if self.config.M == 'mrcnn' and not hasattr(self, 'anchors'):
            self.anchors = ANCHORS(self.config)

        TestG50 = loadmat(
            os.path.join(self.dataset,
                         'dataset/annotation/test/train_test/TestG50.mat')
        )['TestG50'].squeeze()
        query_features = []
        for item in TestG50['Query']:
            img_name = item['imname'][0, 0][0]
            roi = item['idlocate'][0, 0][0].astype(np.int32)
            img = cv2.imread(
                os.path.join(self.dataset, 'dataset/Image/SSM/', img_name))
            input_img, input_box, _, meta = self.DA(img, [roi])
            if self.config.M == 'mrcnn':
                feature = self.reid_model.predict([
                    np.stack([input_img]),
                    np.stack([input_box]),
                    np.stack(meta),
                    np.stack([self.anchors.get_anchors(input_img.shape)])
                ])[0]
            else:
                feature = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])[0]
            query_features.append([img_name, feature[0], np.array([roi])])
            print("\r%d|%d" % (len(query_features), len(TestG50['Query'])),
                  end='')
        print('')
        self.query_features = query_features
        f = open(filepath, 'wb')
        pickle.dump(query_features, f)
        f.close()
        return

    def oim_evaluation(self, gallery_size=100):
        gfilepath = os.path.join(parpath, 'experiment_results/oim_gallery.pkl')
        f = open(gfilepath, 'rb')
        gfeatures = pickle.load(f, encoding='latin1')
        f.close()
        qfilepath = os.path.join(parpath, 'experiment_results/oim_query.pkl')
        f = open(qfilepath, 'rb')
        qfeatures = pickle.load(f, encoding='latin1')
        f.close()
        name_to_det_feat = {}
        for imname, features, boxes in gfeatures:
            if features is None:
                features = np.zeros([0, 256], dtype=np.float32)
                boxes = np.zeros([0, 4], dtype=np.float32)
            name_to_det_feat[imname] = (boxes, features)

        res = {}
        gallery_length = [50, 100, 500, 1000, 2000, 4000, -1]
        for gallery_size in gallery_length:
            res[gallery_size] = self.evaluation(qfeatures,
                                                name_to_det_feat,
                                                _eval=True,
                                                gallery_size=gallery_size)
        f = open(os.path.join(parpath, 'experiment_results', 'oim_res.pkl'),
                 'wb')
        pickle.dump(res, f)
        f.close()

    def gallery_feature_extractor(self, ):
        filepath = os.path.join(parpath, 'experiment_results',
                                self.experiment_name,
                                'cuhk_%s_gallery_features.pkl' % self.config.M)
        if os.path.exists(filepath):
            return

        if not hasattr(self, 'reid_model'):
            self.load_model()

        if self.config.M == 'mrcnn' and not hasattr(self, 'anchors'):
            self.anchors = ANCHORS(self.config)

        pool_path = os.path.join(self.dataset, 'dataset/annotation/pool.mat')
        if not os.path.exists(pool_path):
            raise ValueError('cannot found %s' % pool_path)
        pool = loadmat(pool_path)['pool'].squeeze()
        imnames = [imname[0] for imname in pool]
        gallery = []
        for imname in imnames:
            img = cv2.imread(
                os.path.join(self.dataset, 'dataset/Image/SSM/', imname))
            input_img, input_box, _, meta = self.DA(img, [])
            if self.config.M == 'mrcnn':
                feats, _, _, _, det_features, det, _ = self.reid_model.predict(
                    [
                        np.stack([input_img]),
                        np.stack([input_box]),
                        np.stack(meta),
                        np.stack([self.anchors.get_anchors(input_img.shape)])
                    ])
            elif self.config.M == 'dla_34':
                feats, det_features, det, _ = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])
            else:
                feats, _, _, _, det_features, det, _ = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])
            det = self.DA.unmold(det[0], meta)
            det[:, 2:] += det[:, :2]
            gallery.append([imname, det_features[0], det])
            print("\r%d|%d" % (len(gallery), len(imnames)), end='')
        print('')
        f = open(filepath, 'wb')
        pickle.dump(gallery, f)
        f.close()
        return

    def private_detector_evaluation(self, gallery_size=100):
        print(
            'the results of this experiment using end-to-end detector [%s] + feature extractor [%s%s]'
            %
            (self.config.M, self.config.M, '_mgn' if self.config.mgn else ''))
        topk = [1, 5, 10]
        gallery_length = [50, -1, 100, 500, 1000, 2000, 4000]
        respath = os.path.join(parpath, 'experiment_results',
                               self.experiment_name,
                               'cuhk_%s_res.pkl' % self.config.M)
        if os.path.exists(respath):
            f = open(respath, 'rb')
            res = pickle.load(f)
            f.close()
            for gallery_size in gallery_length:
                aps, accs = res[gallery_size]
                print(gallery_size)
                print('  mAP = {:.2%}'.format(np.mean(aps)))
                for i, k in enumerate(topk):
                    print('  top-{:2d} = {:.2%}'.format(k, accs[i]))
            return

        qfilepath = os.path.join(parpath, 'experiment_results',
                                 self.experiment_name,
                                 'cuhk_%s_query_features.pkl' % self.config.M)
        assert os.path.exists(qfilepath)
        f = open(qfilepath, 'rb')
        qfeatures = pickle.load(f)
        f.close()
        gfilepath = os.path.join(
            parpath, 'experiment_results', self.experiment_name,
            'cuhk_%s_gallery_features.pkl' % self.config.M)
        assert os.path.exists(gfilepath)
        f = open(gfilepath, 'rb')
        gfeatures = pickle.load(f, encoding='latin1')
        f.close()
        name_to_det_feat = {}
        for img_name, features, boxes in gfeatures:
            name_to_det_feat[img_name] = (boxes, features)

        res = {}
        for gallery_size in gallery_length:
            res[gallery_size] = self.evaluation(qfeatures,
                                                name_to_det_feat,
                                                _eval=True,
                                                gallery_size=gallery_size)

        f = open(respath, 'wb')
        pickle.dump(res, f)
        f.close()

    def _compute_iou(self, box1, box2):
        a, b = box1.copy(), box2.copy()
        x1 = max(a[0], b[0])
        y1 = max(a[1], b[1])
        x2 = min(a[2], b[2])
        y2 = min(a[3], b[3])
        inter = max(0, x2 - x1) * max(0, y2 - y1)
        union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] -
                                                                 b[1]) - inter
        return inter * 1.0 / union
Ejemplo n.º 5
0
def main(argv):
    M = 'yolov3'
    gpu = '0'
    image_path = None
    try:
        opts, args = getopt.getopt(argv[1:], 'hm:g:p:',
                                   ['m=', 'gpu=', 'path='])
    except getopt.GetoptError:
        print(argv[0] + ' -m <M> -g <gpu>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(argv[0] + ' -m <M> -g <gpu>')
        elif opt in ['-m', '--M']:
            M = arg
        elif opt in ['-p', '--path']:
            image_path = arg
        elif opt in ['-g', '--gpu']:
            gpu = arg

    print('model: [%s] gpu: [%s]' % (M, gpu))
    os.environ['CUDA_VISIBLE_DEVICES'] = gpu

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    set_session(sess)

    if not os.path.exists(image_path):
        raise ValueError(
            'you must specify a image for testing %s' %
            ('' if image_path is None else 'bad image path: [%s]' %
             image_path))

    config = Config(M)
    model_path = os.path.join(parpath, 'pretrained_weights/%s.h5' % config.M)
    model = MODELS(config, model_type='detection').load_model(model_name='')

    #test
    da = DA('validation', config)
    img = cv2.imread(image_path)
    input_img, input_box, input_ids, meta = da(img, [])
    cv2.imwrite('input_img.jpg', (input_img * 255).astype(np.uint8))
    if config.M == 'mrcnn':
        anchors = ANCHORS(config)
        detection, detection_score = model.predict([
            np.stack([input_img]),
            np.stack([input_box]),
            np.stack(meta),
            np.stack([anchors.get_anchors(input_img.shape)])
        ])
    else:
        detection, detection_score = model.predict(
            [np.stack([input_img]),
             np.stack([input_box])])
    detection = da.unmold(detection[0], meta)
    image = draw(img, detection, detection_score)
    print(detection_score)
    print(detection)
    cv2.imwrite('%s_test.jpg' % config.M, image)
    print('detection results saved as: %s_test.jpg' % config.M)
Ejemplo n.º 6
0
Archivo: app.py Proyecto: RuaHU/TLfPS
class MyFrame(wx.Frame):
    def __init__(self, detector, det = None, name = 'SelectFrame'):
        wx.Frame.__init__(self, None, title=name)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.config = Config(detector)
        self.DA = DA('validation', self.config)
        self.fmts = ['*.jpg', '*.png', '*.bmp','*.jpeg']
        self.menuBar()
        self.Center()
        self.query = {}
        self.gallery = {}
        pub.subscribe(self.queryReid, 'query')
        
    def create_anchors(self,):
        '''
        only mask rcnn needs it
        '''
        if self.config.M == 'mrcnn' and not hasattr(self, 'anchors'):
            self.anchors = ANCHORS(self.config)
    def menuBar(self,):
        _gallery = wx.Menu()
        _gallery_Img = _gallery.Append(wx.ID_ANY, 'Gallery Image', 'Select a single image')
        _gallery_Dir = _gallery.Append(wx.ID_ANY, 'Gallery Dir', 'Select a Directory')
        _gallery_DirR = _gallery.Append(wx.ID_ANY, '&Gallery_Dir(R)...\tCtrl-G', 'Select a Directory Recursively')
        _gallery_clear = _gallery.Append(wx.ID_ANY, 'Clear Gallery', 'Clear gallery')
        _file = wx.Menu()
        _file_query = _file.Append(wx.ID_ANY, '&Query...\tCtrl-Q', 'Open query image')
        _file_gallery = _file.AppendSubMenu(_gallery, '&Gallery')
        _file_search = _file.Append(wx.ID_ANY, '&Search...\tCtrl-S', 'Search')
        _file.AppendSeparator()
        _file_save_gallery = _file.Append(wx.ID_ANY, 'Save Gallery', 'Save Gallery')
        _file_load_gallery = _file.Append(wx.ID_ANY, 'Load Gallery', 'load gallery from file')
        _file.AppendSeparator()
        _file_exit = _file.Append(2, '&Exit...\tCtrl-E', 'Exit program')
        _load = wx.Menu()
        _load_model = _load.Append(wx.ID_ANY, '&Load...\tCtrl-L', 'Load model')
        
        menuBar = wx.MenuBar()
        menuBar.Append(_file, "&File")
        menuBar.Append(_load, '&Load')
        
        # Give the menu bar to the frame
        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.onQuery, _file_query)
        
        self.Bind(wx.EVT_MENU, self.onGalleryImg, _gallery_Img)
        self.Bind(wx.EVT_MENU, self.onGalleryDir, _gallery_Dir)
        self.Bind(wx.EVT_MENU, self.onGalleryDirR, _gallery_DirR)
        self.Bind(wx.EVT_MENU, self.onClear, _gallery_clear)
        self.Bind(wx.EVT_MENU, self.onSearch, _file_search)
        
        self.Bind(wx.EVT_MENU, self.onSaveGallery, _file_save_gallery)
        self.Bind(wx.EVT_MENU, self.onLoadGallery, _file_load_gallery)
        
        self.Bind(wx.EVT_MENU, self.onExit, _file_exit)
        
        self.Bind(wx.EVT_MENU, self.onLoad, _load_model)
    
    def onLoadGallery(self, event):
        f = open('gallery.pkl', 'rb')
        gallery = pickle.load(f, encoding='latin1')
        f.close()
        for pimage in gallery:
            if pimage in self.gallery:continue
            self.gallery[pimage] = gallery[pimage]
            
    def onSaveGallery(self, event):
        f = open('gallery.pkl', 'wb')
        pickle.dump(self.gallery, f)
        f.close()
    
    def onExit(self, event):
        self.Close(True)
    def queryReid(self,):
        if not hasattr(self, 'panel'):return
        roi = self.panel.getQuery()
        if roi is None:return False
        if 'path' in self.query:
            img = cv2.imread(self.query['path'])
            input_img, input_box, _, meta = self.DA(img, [roi])
            feat = self.feature_extraction(input_img, input_box, meta)[0]
            self.query['feature'] = feat[0]
            self.query['roi'] = 'roi'
    
    def onSearch(self, event, top_n = 20, fnLen = 30):
        if 'feature' not in self.query:
            print('no query')
        query = self.query['feature']
        infolen = 0
        self.toplist = []
        for i, pimage in enumerate(self.gallery):
            features = self.gallery[pimage]['features']
            detection = self.gallery[pimage]['detection']
            if len(detection) == 0:continue
            sims = self.get_sims(features, query, _eval = True)
            maxids = np.argmax(sims)
            for _ in range(infolen):print("\x1b[1A\x1b[2K", end = '')
            self.toplist.append([pimage, sims[maxids], detection[maxids]])
            self.toplist.sort(key = lambda x : x[1], reverse = True)
            print("%d|%d>>%s"%(i+1, len(self.gallery), pimage))
            for item in self.toplist[:top_n]:
                print('\x1b[0;31;47m' + '%.6f\t'%item[1] + '\x1b[0m', '...%30s\t'%item[0][-fnLen:], item[2].astype(np.int32))
            infolen = len(self.toplist[:top_n]) + 1
            
    def onQuery(self, event):
        fDlg = wx.FileDialog(self, 'select query image.', 
                             wildcard = 'image (*.jpg;*.png;*.bmp;*.jpeg)|*.jpg;*.png;*.bmp;*.jpeg',
                             style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
                             )
        if fDlg.ShowModal() == wx.ID_CANCEL:
            return
        pimage = fDlg.GetPath()
        self.query['path'] = pimage
        print('get query image: %s'%pimage)
        image = cv2.imread(pimage)
        if not hasattr(self, 'panel'):
            self.panel = myPanel(self, image)
            self.sizer.Add(self.panel, 1, wx.EXPAND)
        else:
            self.panel.setImage(image)
        self.sizer.Fit(self)
        self.Fit()
        self.Center()
    
    def onClear(self,):
        self.gallery = {}
    
    def get_sims(self, gfeats, qfeat, _eval = True):
        '''
        gfeats: gallery features
        qfeat: query feature
        '''
        if _eval:return gfeats.dot(qfeat.ravel()).ravel()
        gfeats_norm = np.linalg.norm(gfeats, keepdims = True, axis = -1)
        qfeat_norm = np.linalg.norm(qfeat, keepdims = True)
        gfeats_nl = gfeats / gfeats_norm
        qfeat_nl = qfeat / qfeat_norm
        sim = gfeats_nl.dot(qfeat_nl.ravel()).ravel()
        return sim 
    
    def feature_extraction(self, input_img, input_box, meta):
        if not hasattr(self, 'model'):
            self.load()
        
        self.create_anchors()
        
        if self.config.M == 'mrcnn':
            feats, _, _, _, features, detection, _ = self.model.predict([np.stack([input_img]), np.stack([input_box]), np.stack([self.anchors.get_anchors(input_img.shape)])])
        elif self.config.M == 'dla_34':
            feats, det_features, det, _ = self.reid_model.predict([np.stack([input_img]), np.stack([input_box])])
        else:
            feats, _, _, _, features, detection, _ = self.model.predict([np.stack([input_img]), np.stack([input_box])])
            detection = self.DA.unmold(detection[0], meta)
        return feats[0], features[0], detection
    
    def load(self,):
        if hasattr(self, 'model'):
            del self.model
        self.model = MODELS(config = self.config).load_model()
    
    def onLoad(self, event):
        self.load()
    
    def extract(self, pimage):
        if pimage in self.gallery:
            #print('image existed:', pimage)
            return
        if not hasattr(self, 'model'):
            self.load()
        print('extracting>>%s'%pimage)
        img = cv2.imread(pimage)
        input_img, input_box, _, meta = self.DA(img, [])
        _, features, detection = self.feature_extraction(input_img, input_box, meta)
        self.gallery[pimage] = {'features':features, 'detection':detection}
    
    def onGalleryImg(self, event):
        fDlg = wx.FileDialog(self, 'select query image.', 
                             wildcard = 'image (*.jpg;*.png;*.bmp;*.jpeg)|*.jpg;*.png;*.bmp;*.jpeg',
                             style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
                             )
        if fDlg.ShowModal() == wx.ID_CANCEL:
            return
        pimage = fDlg.GetPath()
        self.extract(pimage)
    
    def parseGalleryPath(self, gallery_path, mode = ''):
        paths = []
        for fmt in self.fmts:paths+=glob.glob(os.path.join(gallery_path, fmt))
        if mode == 'r':
            for item in os.listdir(gallery_path):
                path = os.path.join(gallery_path, item)
                if os.path.isdir(path):
                    paths+=self.parseGalleryPath(path, mode = mode)
        return list(set(paths))
    
    def onGalleryDir(self, event):
        pDlg = wx.DirDialog(self, "select a directory",
                           style=wx.DD_DEFAULT_STYLE)
        
        if pDlg.ShowModal() == wx.ID_CANCEL:
            return
        gallery_path = pDlg.GetPath()
        gallery_images = list(set(self.parseGalleryPath(gallery_path)))
        for pimage in gallery_images:
            self.extract(pimage)
            
    
    def onGalleryDirR(self, event):
        pDlg = wx.DirDialog(self, "select a directory",
                           style=wx.DD_DEFAULT_STYLE)
        
        if pDlg.ShowModal() == wx.ID_CANCEL:
            return
        gallery_path = pDlg.GetPath()
        gallery_images = list(set(self.parseGalleryPath(gallery_path, mode = 'r')))
        for pimage in gallery_images:
            self.extract(pimage)
Ejemplo n.º 7
0

import cv2
import time
import os, sys
parpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
curpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(parpath)
from tools.load_weights import load_weights_by_name
from tools.config import Config
from tools.DataAugmentation import DataAugmentation as DA
sys.path.remove(parpath)
if __name__ == '__main__':
    config = Config('dla_34')
    model = DLASeg(config).model(model_type='detection')
    load_weights_by_name(model, 'dla_34.h5')
    da = DA('validation', config)
    while 1:
        start = time.time()
        img0 = cv2.imread('test.jpg')
        img, _, _, meta = da(img0, [])
        dets, scores = model.predict([[img], np.zeros([1, 1, 4])])
        boxes, scores = da.unmold(dets[0], meta), scores[0]
        for i in range(0, boxes.shape[0]):
            bbox = boxes[i][0:4]
            cv2.rectangle(img0, (bbox[0], bbox[1]),
                          (bbox[0] + bbox[2], bbox[1] + bbox[3]), (0, 255, 0),
                          2)
        cv2.imwrite('dets.jpg', img0)
        break
Ejemplo n.º 8
0
def main(argv):
    M = 'yolov3'
    gpu = '0'
    CUHK_SYSU = "/home/ronghua/Projects/data/dataset-v2/"
    PRW = '/home/ronghua/Projects/data/PRW-v16.04.20/'
    try:
        opts, args = getopt.getopt(argv[1:], 'hm:g:p:c:',
                                   ['m=', 'gpu=', 'prw=', 'cuhk='])
    except getopt.GetoptError:
        print(argv[0] + ' -m <M> -g <gpu>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(argv[0] + ' -m <M> -g <gpu>')
        elif opt in ['-m', '--M']:
            M = arg
        elif opt in ['-p', '--path']:
            PRW = arg
        elif opt in ['-c', '--cuhk']:
            CUHK_SYSU = arg
        elif opt in ['-g', '--gpu']:
            gpu = arg

    print('model: [%s] gpu: [%s], CUHK_SYSU: [%s], PRW: [%s]' %
          (M, gpu, CUHK_SYSU, PRW))
    os.environ['CUDA_VISIBLE_DEVICES'] = gpu

    dataset = False
    if os.path.exists(CUHK_SYSU):
        dataset = True
    if os.path.exists(PRW):
        dataset = True

    if not dataset:
        raise ValueError(
            'you should specify at least one dataset [CUHK_SYSU or PRW]')

    config = Config(M)
    model_path = os.path.join(parpath, 'pretrained_weights/%s.h5' % config.M)
    model = MODELS(config, model_type='detection').load_model(model_name='')
    if config.M == 'mrcnn':
        anchors = ANCHORS(config)

    load_weights_by_name(model, model_path)
    da = DA('validation', config)

    if os.path.exists(CUHK_SYSU):
        filepath = os.path.join(parpath, 'experiment_results',
                                'cuhk_%s_gallery.pkl' % config.M)
        if os.path.exists(filepath):
            print('cuhk_%s_gallery.pkl exists.' % config.M)
        else:
            print('creating cuhk-sysu gallery for %s' % config.M)
            gallery = []
            pool_path = os.path.join(CUHK_SYSU, 'dataset/annotation/pool.mat')
            if not os.path.exists(pool_path):
                raise ValueError('cannot found %s' % pool_path)
            pool = loadmat(pool_path)['pool'].squeeze()
            imnames = [imname[0] for imname in pool]
            for imname in imnames:
                img = cv2.imread(
                    os.path.join(CUHK_SYSU, 'dataset/Image/SSM/', imname))
                input_img, input_box, input_ids, meta = da(img, [])
                if config.M == 'mrcnn':
                    detection, scores = model.predict([
                        np.stack([input_img]),
                        np.stack([input_box]),
                        np.stack(meta),
                        np.stack([anchors.get_anchors(input_img.shape)])
                    ])
                else:
                    detection, scores = model.predict(
                        [np.stack([input_img]),
                         np.stack([input_box])])
                detection = da.unmold(detection[0], meta)
                detection[:, 2:] += detection[:, :2]
                features = np.zeros([len(detection), 0])
                gallery.append([imname, features, detection])
                print("\r%d|%d" % (len(gallery), len(imnames)), end='')
            print('')
            f = open(filepath, 'wb')
            pickle.dump(gallery, f)
            f.close()

    if os.path.exists(PRW):
        filepath = os.path.join(parpath, 'experiment_results',
                                'prw_%s_gallery.pkl' % config.M)
        if os.path.exists(filepath):
            print('prw_%s_gallery.pkl exists.' % config.M)
        else:
            print('creating prw gallery for %s' % config.M)
            gallery = []
            frame_test_path = os.path.join(PRW, 'frame_test.mat')
            if not os.path.exists(frame_test_path):
                raise ValueError('cannot found %s' % frame_test_path)
            frame_indices = loadmat(
                frame_test_path)['img_index_test'].squeeze()
            imnames = [imname[0] + '.jpg' for imname in frame_indices]
            for imname in imnames:
                img = cv2.imread(os.path.join(PRW, 'frames', imname))
                input_img, input_box, input_ids, meta = da(img, [])
                if config.M == 'mrcnn':
                    detection, scores = model.predict([
                        np.stack([input_img]),
                        np.stack([input_box]),
                        np.stack(meta),
                        np.stack([anchors.get_anchors(input_img.shape)])
                    ])
                else:
                    detection, scores = model.predict(
                        [np.stack([input_img]),
                         np.stack([input_box])])
                detection = da.unmold(detection[0], meta)
                detection[:, 2:] += detection[:, :2]
                features = np.zeros([len(detection), 0])
                gallery.append([imname, features, detection])
                print("\r%d|%d" % (len(gallery), len(imnames)), end='')
            print('')
            f = open(filepath, 'wb')
            pickle.dump(gallery, f)
            f.close()
Ejemplo n.º 9
0
class EVALUATION():
    def __init__(self,
                 dataset,
                 detector='yolov3',
                 experiment_name='default',
                 overwrite=False):
        if dataset is None: return
        self.detector = detector
        self.dataset = dataset
        self.overwrite = overwrite
        self.experiment_name = experiment_name
        self.checkdir()
        self.config = Config(detector)
        self.DA = DA('validation', self.config)
        self.load_gallery()
        self.load_query()

    def get_sims(self, gfeats, qfeat, _eval=True):
        '''
        gfeats: gallery features
        qfeat: query feature
        '''
        if _eval: return gfeats.dot(qfeat.ravel()).ravel()
        gfeats_norm = np.linalg.norm(gfeats, keepdims=True, axis=-1)
        qfeat_norm = np.linalg.norm(qfeat, keepdims=True)
        gfeats_nl = gfeats / gfeats_norm
        qfeat_nl = qfeat / qfeat_norm
        sim = gfeats_nl.dot(qfeat_nl.ravel()).ravel()
        return sim

    def checkdir(self, ):
        dirpath = os.path.join(parpath, 'experiment_results',
                               self.experiment_name)
        if os.path.exists(dirpath):
            print('experiment [%s] existed' % self.experiment_name)
            if self.overwrite:
                print('cleaning experiment [%s] [overwrite == True]' %
                      self.experiment_name)
                shutil.rmtree(dirpath, ignore_errors=True)
                if os.path.exists(dirpath):
                    print(
                        'it seems the experiment directory can not be deleted. please check the status of the directory %s'
                        % dirpath)
                os.mkdir(dirpath)
                assert os.path.exists(dirpath)
            else:
                print(
                    'the results of experiment [%s] will be reused [overwrite == False]'
                    % self.experiment_name)
        else:
            os.mkdir(dirpath)
            assert os.path.exists(dirpath)

    def load_model(self, ):
        self.reid_model = MODELS(config=self.config).load_model()

    def load_gallery(self):
        self.gallery_dict = {}
        frame_test = scipy.io.loadmat(
            os.path.join(self.dataset, 'frame_test.mat'))
        frame_indices = frame_test['img_index_test']
        for index, im_name in enumerate(frame_indices[:, 0]):
            mat = scipy.io.loadmat(
                os.path.join(self.dataset, 'annotations',
                             im_name[0] + '.jpg.mat'))
            boxes = mat[list(mat.keys())[-1]]
            ids = boxes[:, 0]
            boxes = boxes[:, 1:5]
            self.gallery_dict[im_name[0] +
                              '.jpg'] = [im_name[0] + '.jpg', boxes, ids]

    def load_query(self):
        file = open(os.path.join(self.dataset, 'query_info.txt'))
        self.query_list = []
        for line in file:
            items = line.split()
            ids, roi = int(items[0]), [
                float(items[1]),
                float(items[2]),
                float(items[3]),
                float(items[4])
            ]
            self.query_list.append([items[-1] + '.jpg', roi, ids])

    def query_feature_extractor(self, ):
        filepath = os.path.join(parpath, 'experiment_results',
                                self.experiment_name,
                                'prw_%s_query_features.pkl' % self.config.M)
        if os.path.exists(filepath):
            return

        if not hasattr(self, 'reid_model'):
            self.load_model()

        if self.config.M == 'mrcnn' and not hasattr(self, 'anchors'):
            self.anchors = ANCHORS(self.config)

        query_features = []
        for item in self.query_list:
            img_name, roi, _ = item
            img = cv2.imread(os.path.join(self.dataset, 'frames', img_name))
            input_img, input_box, _, meta = self.DA(img, [roi])
            if self.config.M == 'mrcnn':
                feature = self.reid_model.predict([
                    np.stack([input_img]),
                    np.stack([input_box]),
                    np.stack(meta),
                    np.stack([self.anchors.get_anchors(input_img.shape)])
                ])[0]
            else:
                feature = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])[0]
            query_features.append([img_name, feature[0], np.array([roi])])
            print("\r%d|%d" % (len(query_features), len(self.query_list)),
                  end='')
        print('')
        self.query_features = query_features
        f = open(filepath, 'wb')
        pickle.dump(query_features, f)
        f.close()
        return

    def gallery_feature_extractor(self, ):
        filepath = os.path.join(parpath, 'experiment_results',
                                self.experiment_name,
                                'prw_%s_gallery_features.pkl' % self.config.M)
        if os.path.exists(filepath):
            return

        if not hasattr(self, 'reid_model'):
            self.load_model()

        if self.config.M == 'mrcnn' and not hasattr(self, 'anchors'):
            self.anchors = ANCHORS(self.config)

        gallery = []
        for imname in self.gallery_dict.keys():
            img = cv2.imread(os.path.join(self.dataset, 'frames', imname))
            input_img, input_box, _, meta = self.DA(img, [])
            if self.config.M == 'mrcnn':
                feats, _, _, _, det_features, det, _ = self.reid_model.predict(
                    [
                        np.stack([input_img]),
                        np.stack([input_box]),
                        np.stack(meta),
                        np.stack([self.anchors.get_anchors(input_img.shape)])
                    ])
            elif self.config.M == 'dla_34':
                feats, det_features, det, _ = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])
            else:
                feats, _, _, _, det_features, det, _ = self.reid_model.predict(
                    [np.stack([input_img]),
                     np.stack([input_box])])
            det = self.DA.unmold(det[0], meta)
            det[:, 2:] += det[:, :2]
            gallery.append([imname, det_features[0], det])
            print("\r%d|%d" % (len(gallery), len(self.gallery_dict)), end='')
        print('')
        f = open(filepath, 'wb')
        pickle.dump(gallery, f)
        f.close()
        return

    def simple_evaluation(self, model, gallery_size=50):
        #extract query feature vectors
        qfeatures = []
        for item in self.query_list:
            img_name, roi, _ = item
            img = cv2.imread(os.path.join(self.dataset, 'frames', img_name))
            input_img, input_box, input_ids, meta = self.DA(img, [roi])
            feature = model.predict([
                np.stack([input_img]),
                np.stack([input_box]),
                np.stack([input_ids])
            ])
            qfeatures.append([img_name, feature[0], np.array([roi])])
            print("\r%d|%d" % (len(qfeatures), len(self.query_list)), end='')
        print('')
        #extract gallery feature vectors
        filepath = os.path.join(
            parpath, 'experiment_results/prw_%s_gallery.pkl' % self.config.M)
        assert os.path.exists(filepath)
        f = open(filepath, 'rb')
        oim_gallery = pickle.load(f, encoding='latin1')
        gallery = []
        for item in oim_gallery:
            imname, oim_features, oim_boxes = item
            if oim_features is None:
                oim_features = np.zeros([0, 256], dtype=np.float32)
                oim_boxes = np.zeros([0, 5], dtype=np.float32)
            img = cv2.imread(os.path.join(self.dataset, 'frames', imname))
            #xyxy 2 xywh
            toim_boxes = oim_boxes.copy()
            toim_boxes[:, 2:4] -= toim_boxes[:, :2]
            input_img, input_box, input_ids, meta = self.DA(
                img, toim_boxes[:, :4])
            feats = model.predict([
                np.stack([input_img]),
                np.stack([input_box]),
                np.stack([input_ids])
            ])
            gallery.append([imname, feats[0, :, 0, 0, :], oim_boxes])
            print("\r%d|%d" % (len(gallery), len(oim_gallery)), end='')
        print('')

        name_to_det_feat = {}
        for img_name, features, boxes in gallery:
            name_to_det_feat[img_name] = (boxes, features)
        return self.evaluation(qfeatures, name_to_det_feat, _eval=True)

    def evaluation(self, qfeatures, name_to_det_feat, _eval):
        aps, accs, topk = [], [], [1, 5, 10]
        log = open('log.txt', 'w')
        sysout = sys.stdout
        all_recall_rate = []
        #tape = {}
        for i, query in enumerate(self.query_list):
            sys.stdout = log
            qimg_name, qroi, qid = query
            y_true, y_score = [], []
            count_gt, count_tp = 0, 0
            qfeat = qfeatures[i][1].ravel()
            gallery_items = [
                self.gallery_dict[key] for key in self.gallery_dict
                if qid in self.gallery_dict[key][-1] and key != qimg_name
            ]
            gallery_gts = {}
            for item in gallery_items:
                gallery_gts[item[0]] = item[1][item[2] == qid]
            gallery_imgs = [
                key for key in self.gallery_dict if key != qimg_name
            ]
            imgs, y_boxes, y_gname = [], [], []
            for gallery_imname in gallery_imgs:
                count_gt += (gallery_imname in gallery_gts)
                if gallery_imname not in name_to_det_feat: continue
                gboxes, gfeatures = name_to_det_feat[gallery_imname]
                sim = self.get_sims(gfeatures, qfeat, _eval)
                label = np.zeros(len(sim), dtype=np.int32)
                if gallery_imname in gallery_gts:

                    gt = gallery_gts[gallery_imname].ravel()
                    w, h = gt[2], gt[3]
                    gt[2], gt[3] = gt[0] + gt[2], gt[1] + gt[3]
                    iou_thresh = min(0.5,
                                     (w * h * 1.0) / ((w + 10) * (h + 10)))
                    inds = np.argsort(sim)[::-1]
                    sim = sim[inds]
                    gboxes = gboxes[inds]
                    for j, roi in enumerate(gboxes[:, :4]):
                        if self._compute_iou(roi, gt) >= iou_thresh:
                            label[j] = 1
                            count_tp += 1
                            break

                y_true.extend(list(label))
                y_score.extend(list(sim))
                y_boxes.extend(list(gboxes))
                y_gname.extend([gallery_imname for _ in gboxes])

            y_score = np.array(y_score)
            y_true = np.array(y_true)
            y_boxes = np.array(y_boxes)
            y_gname = np.array(y_gname)
            assert count_tp <= count_gt
            recall_rate = count_tp * 1.0 / count_gt
            all_recall_rate.append(recall_rate)
            ap = 0 if count_tp == 0 else \
                average_precision_score(y_true, y_score) * recall_rate
            aps.append(ap)

            inds = np.argsort(y_score)[::-1]
            y_score = y_score[inds]
            y_true = y_true[inds]
            y_boxes = y_boxes[inds]
            y_gname = y_gname[inds]
            acc = [min(1, sum(y_true[:k])) for k in topk]
            accs.append(acc)
            #tape[qimg_name] = [qid, qroi, ap, acc, recall_rate, y_score, y_true, y_boxes, y_gname]
            sys.stdout = sysout
            print("\r%d:\t%d|%d|%.2f|%.2f" %
                  (-1, len(aps), len(qfeatures), np.mean(aps),
                   np.mean(accs, axis=0)[0]),
                  end='')
        print('')
        print('search ranking:')
        print('aRR:%.4f' % np.mean(all_recall_rate))
        print('  mAP = {:.2%}'.format(np.mean(aps)))
        accs = np.mean(accs, axis=0)
        for i, k in enumerate(topk):
            print('  top-{:2d} = {:.2%}'.format(k, accs[i]))

        #record_aps = []
        #new_tape = {}
        #for key in tape.keys():
        #    record_aps.append(tape[key][2])
        #record_aps.sort()
        #th = record_aps[50]
        #for key in tape.keys():
        #    if tape[key][2] > th:continue
        #    new_tape[key] = tape[key]

        #filepath = os.path.join(parpath, 'experiment_results', self.experiment_name, 'prw_%s_tape.pkl'%self.config.M)
        #pickle.dump(new_tape, filepath)

        return aps, accs

    def private_detector_evaluation(self):
        print(
            'the results of this experiment using end-to-end detector [%s] + feature extractor [%s%s]'
            %
            (self.config.M, self.config.M, '_mgn' if self.config.mgn else ''))
        topk = [1, 5, 10]
        respath = os.path.join(parpath, 'experiment_results',
                               self.experiment_name,
                               'prw_%s_res.pkl' % self.config.M)
        if os.path.exists(respath):
            f = open(respath, 'rb')
            res = pickle.load(f)
            f.close()
            aps, accs = res
            print('  mAP = {:.2%}'.format(np.mean(aps)))
            for i, k in enumerate(topk):
                print('  top-{:2d} = {:.2%}'.format(k, accs[i]))
            return

        qfilepath = os.path.join(parpath, 'experiment_results',
                                 self.experiment_name,
                                 'prw_%s_query_features.pkl' % self.config.M)
        assert os.path.exists(qfilepath)
        f = open(qfilepath, 'rb')
        qfeatures = pickle.load(f)
        f.close()
        gfilepath = os.path.join(parpath, 'experiment_results',
                                 self.experiment_name,
                                 'prw_%s_gallery_features.pkl' % self.config.M)
        assert os.path.exists(gfilepath)
        f = open(gfilepath, 'rb')
        gfeatures = pickle.load(f, encoding='latin1')
        f.close()
        name_to_det_feat = {}
        for img_name, features, boxes in gfeatures:
            name_to_det_feat[img_name] = (boxes, features)

        res = self.evaluation(qfeatures, name_to_det_feat, _eval=True)

        f = open(respath, 'wb')
        pickle.dump(res, f)
        f.close()

    def _compute_iou(self, box1, box2):
        a, b = box1.copy(), box2.copy()
        x1 = max(a[0], b[0])
        y1 = max(a[1], b[1])
        x2 = min(a[2], b[2])
        y2 = min(a[3], b[3])
        inter = max(0, x2 - x1) * max(0, y2 - y1)
        union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] -
                                                                 b[1]) - inter
        return inter * 1.0 / union