예제 #1
0
    def evaluate(self):

        self.model.eval()

        print('extract features, this may take a few minutes')
        qf = extract_feature(self.model, tqdm(self.query_loader)).numpy()
        gf = extract_feature(self.model, tqdm(self.test_loader)).numpy()

        def rank(dist):
            r = cmc(dist,
                    self.queryset.ids,
                    self.testset.ids,
                    self.queryset.cameras,
                    self.testset.cameras,
                    separate_camera_set=False,
                    single_gallery_shot=False,
                    first_match_break=True)
            m_ap = mean_ap(dist, self.queryset.ids, self.testset.ids,
                           self.queryset.cameras, self.testset.cameras)

            return r, m_ap

        #########################   re rank##########################
        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)

        r, m_ap = rank(dist)

        print(
            '[With    Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
            .format(m_ap, r[0], r[2], r[4], r[9]))

        #########################no re rank##########################
        dist = cdist(qf, gf)

        r, m_ap = rank(dist)

        print(
            '[Without Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
            .format(m_ap, r[0], r[2], r[4], r[9]))
예제 #2
0
    def evaluate(self):

        self.model.eval()

        qf = extract_feature(self.model, tqdm(self.query_loader, desc='Extracting query features')).numpy()
        gf = extract_feature(self.model, tqdm(self.test_loader, desc='Extracting test features')).numpy()

        def rank(dist):
            r = cmc(dist, self.queryset.ids, self.testset.ids, self.queryset.cameras, self.testset.cameras,
                    separate_camera_set=False,
                    single_gallery_shot=False,
                    first_match_break=True)
            m_ap = mean_ap(dist, self.queryset.ids, self.testset.ids, self.queryset.cameras, self.testset.cameras)

            return r, m_ap

        #########################   re rank##########################
        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)

        r, m_ap = rank(dist)

        tqdm.write('[With    Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
              .format(m_ap, r[0], r[2], r[4], r[9]))

        retval = {
            're-ranking': (m_ap, r[0], r[2], r[4], r[9])
        }

        #########################no re rank##########################
        dist = cdist(qf, gf)

        r, m_ap = rank(dist)

        tqdm.write('[Without Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
              .format(m_ap, r[0], r[2], r[4], r[9]))

        retval['without'] = (m_ap, r[0], r[2], r[4], r[9])

        return retval
예제 #3
0
    def evaluate_ai(self):
        self.model.load_state_dict(torch.load('weights/AI_mgn/pyramid_v4.pth'))
        self.model.eval()

        print('extract features, this may take a few minutes')
        qf = extract_feature(self.model, tqdm(self.query_loader)).numpy()
        gf = extract_feature(self.model, tqdm(self.test_loader)).numpy()
        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)
        for i in range(dist.shape[0]):
            txt_list = np.zeros((100))
            for j in range(txt_list.shape[0]):
                txt_list[j] = int(np.argmin(dist[i])) + 1
                dist[i][int(txt_list[j]) - 1] = 100000
            txt_list = pd.DataFrame(txt_list, dtype=np.int32)
            txt_list.to_csv('save/{:0>6d}.txt'.format(i + 1),
                            header=0,
                            index=0)
예제 #4
0
    def evaluate(self):

        self.model.eval()

        print('extract features, this may take a few minutes')
        qf = extract_feature(self.model, tqdm(self.query_loader)).numpy()
        gf = extract_feature(self.model, tqdm(self.test_loader)).numpy()

        #########################   re rank##########################
        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)
        queryset = self.queryset
        testset = self.testset
        m_ap = mean_ap(dist, queryset, testset)

        print('[With    Re-Ranking] mAP: {:.4f}'.format(m_ap))

        #########################no re rank##########################
        dist = cdist(qf, gf)
        m_ap = mean_ap(dist, queryset, testset)

        print('[Without Re-Ranking] mAP: {:.4f}'.format(m_ap))
예제 #5
0
    def evaluate(self, epoch):

        self.model.eval()

        print('extract features, this may take a few minutes')
        qf = extract_feature(self.model, tqdm(self.query_loader)).numpy()
        gf = extract_feature(self.model, tqdm(self.test_loader)).numpy()
        epoch_json = 'metric/metric_epoch' + str(epoch)
        os.makedirs(epoch_json)

        def result(distmat,
                   query_ids=None,
                   gallery_ids=None,
                   query_cams=None,
                   gallery_cams=None,
                   title=None):
            m, n = distmat.shape
            # Fill up default values
            if query_ids is None:
                query_ids = np.arange(m)
            if gallery_ids is None:
                gallery_ids = np.arange(n)
            if query_cams is None:
                query_cams = np.zeros(m).astype(np.int32)
            if gallery_cams is None:
                gallery_cams = np.ones(n).astype(np.int32)
            # Ensure numpy array
            query_ids = np.asarray(query_ids)
            gallery_ids = np.asarray(gallery_ids)
            query_cams = np.asarray(query_cams)
            gallery_cams = np.asarray(gallery_cams)
            # Sort and find correct matches
            indices = np.argsort(distmat, axis=1)

            dd = []
            for i in range(m):
                # Filter out the same id and same camera
                d = {}
                d['query_id'] = query_ids[i].astype(np.int32).tolist()
                valid = ((gallery_ids[indices[i]] != query_ids[i]) &
                         (gallery_cams[indices[i]] != query_cams[i]))
                ans_ids = gallery_ids[indices[i]][valid]
                d['ans_ids'] = ans_ids.astype(np.int32).tolist()
                dd.append(d)
            with open(epoch_json + '/' + title + '.json',
                      'w',
                      encoding='utf-8') as json_file:
                json.dump(dd, json_file, ensure_ascii=False)
            print('json finished')

        #########################no re rank##########################
        dist = cdist(qf, gf)
        result(dist,
               self.queryset.ids,
               self.testset.ids,
               title='without rerank')

        #########################   re rank##########################
        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)
        result(dist, self.queryset.ids, self.testset.ids, title='rerank')

        #########################   query expansion##########################
        qf_new = []
        T = 9
        for i in range(len(dist)):
            indice = np.argsort(dist[i])[:T]
            temp = np.concatenate((qf[i][np.newaxis, :], gf[indice]), axis=0)
            qf_new.append(np.mean(temp, axis=0, keepdims=True))

        qf = np.squeeze(np.array(qf_new))
        # feature norm
        q_n = np.linalg.norm(qf, axis=1, keepdims=True)
        qf = qf / q_n

        q_g_dist = np.dot(qf, np.transpose(gf))
        q_q_dist = np.dot(qf, np.transpose(qf))
        g_g_dist = np.dot(gf, np.transpose(gf))
        dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)

        result(dist,
               self.queryset.ids,
               self.testset.ids,
               title='query_expansion',
               epoch=epoch)
# feature norm
g_n = np.linalg.norm(gf, axis=1, keepdims=True)
gf = gf / g_n

epoch_json = 'metric_final'
os.makedirs(epoch_json)

#########################no re rank##########################
dist = cdist(qf, gf)
result(dist, data_jitter.queryset.ids, data_jitter.testset.ids, title='without rerank')

#########################   re rank##########################
q_g_dist = np.dot(qf, np.transpose(gf))
q_q_dist = np.dot(qf, np.transpose(qf))
g_g_dist = np.dot(gf, np.transpose(gf))
dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)
result(dist, data_jitter.queryset.ids, data_jitter.testset.ids, title='rerank')

#########################   query expansion##########################
qf_new = []
T = 9
for i in range(len(dist)):
    indice = np.argsort(dist[i])[:T]
    temp = np.concatenate((qf[i][np.newaxis, :], gf[indice]), axis=0)
    qf_new.append(np.mean(temp, axis=0, keepdims=True))

qf = np.squeeze(np.array(qf_new))
# feature norm
q_n = np.linalg.norm(qf, axis=1, keepdims=True)
qf = qf / q_n