NAIC2021-AI+视觉特征编码第一阶段开源方案

NAIC2021-AI+视觉特征编码第一阶段开源方案

赛题地址:

https://naic.pcl.ac.cn/contest/10/35

项目地址:

https://aistudio.baidu.com/aistudio/projectdetail/3413350?contributionType=1

AI Studio项目,fork后可一键运行。

赛题背景

为配合国家“加快数字化发展,建设数字中国”战略,促进视觉信息智能编码技术领域的原始创新积累,本次人工智能大赛设立“ AI+视觉特征编码赛道”(以下简称赛道)。

赛道面向视觉大数据应用,针对码率约束条件下的机器视觉任务,探索视觉信息压缩技术与智能处理分析技术的融合。传统方案中视觉信息编码和机器视觉任务作为独立模块分别优化。区别于传统路线,本赛道综合考虑了视觉信息编码效率和机器视觉任务性能,结合传统信号处理技术与人工智能技术,提升精度,优化效率,改善泛化性。

初赛任务

初赛任务
初赛任务包括行人/车辆视觉特征编码和行人/车辆再识别单独的两部分。
赛道主办方提供行人/车辆的原始视觉特征,选手按规定的预设码率对特征进行压缩和重建。根据原始视觉特征的重建误差得分。
在初赛第一阶段,选手需要进行再识别任务,获取再识别性能得分。第一阶段排名前 200 的团队将进入初赛第二阶段,进行重建特征任务的测评,获取重建误差得分。
初赛得分为重建误差得分和再识别性能得分的加权平均值。
若初赛得分一致,将依据特征重建任务中提供的模型算法先进性与创新性进行排序。
单幅图像的特征压缩操作点(Operating Point)预设 3 个码率:64 字节、128 字节和 256 字节。视觉特征压缩超过预设码率视作无效。

数据描述

  • 初赛所用的训练集将提供行人/车辆的原始视觉特征,选手按规定的预设码率对特征进行压缩和重建。根据原始视觉特征的重建误差得分。
  • 所有参赛队伍需要基于大赛提供的训练集进行模型训练与调优,并在给定的测试集上运行结果。
  • 初赛、复赛、决赛的难度将递增。
1、初赛训练集

含有 259,450 个训练特征文件和对应的ID标签,可以用于模型训练
每个特征文件提供行人或车辆的对应ID标签,共有 259,478 个匹配对应关系
标注文件将由文本文件提供。文本文件每一行提供一个标注
标注格式为:文件名 ID
文件组织结构如下:

train
├── train_feature
└── train_list.txt
2、初赛测试集

初赛测试集仅用于第一阶段(再识别任务)的测评,测试集分 A/B 榜,不提供 ID 标签,其中:

A榜( 开放时间:2021-12-20 (12:00:00 中午) 至 2022-02-13 (12:00:00 中午) ):测试集由 gallery_feature_A 与 query_feature_A 组成,其中 query_feature_A 包含 20,000 个特征文件,gallery_feature_A 包含 428,794 个特征文件,用于参赛队伍模型评估;
B榜( 开放时间: 2022-02-13 (12:00:00 中午)至 2022-02-15 (12:00:00 中午)):测试集由 gallery_feature_B 与 query_feature_B 组成,其中 query_feature_B 包含 10,000 个特征文件,gallery_feature_B 包含 210,939 个特征文件。测试集将于B榜提交开始后在大赛页面提供下载,用于第一阶段最终的成绩评定和排名。
文件组织结构如下:

test_A
├── gallery_feature_A
└── query_feature_A
test_B
├── gallery_feature_B
└── query_feature_B

本方案为第一阶段方案,直接使用测试集进行特征检索。

快速开始

1.解压数据集

本项目以包含数据集,运行以下命令解压数据集到/home/aistduio/data目录下。

%cd /home/aistudio/data/
!unzip data124219/train.zip
!unzip data124219/test_A.zip
2.安装依赖包

本方案参考pp-shitu使用faiss工作进行特征检索,使用以下命令安装。

!pip install faiss-cpu==1.6.1
3.建立特征库

运行以下代码建立特征库。

其中index_method为检索方法,可选下面三种检索方法。

  • HNSW32: 一种图索引方法。检索精度较高,速度较快。但是特征库只支持添加图像功能,不支持删除图像特征功能。(默认方法)
  • IVF:倒排索引检索方法。速度较快,但是精度略低。特征库支持增加、删除图像特征功能。
  • FLAT: 暴力检索算法。精度最高,但是数据量大时,检索速度较慢。特征库支持增加、删除图像特征功能。

dist_type是特征匹配过程中使用的相似度计算方式。

  • IP 内积相似度计算方式
  • L2 欧式距离计算方法
import os
import pickle
import glob

import cv2
import faiss
import numpy as np
from tqdm import tqdm

from sklearn.preprocessing import normalize

dataset_root="/home/aistudio/data"

def read_dat(path):
    return np.fromfile(path, dtype=np.float32)


index_method = "Flat" 
dist_type = "IP"

# when remove data in index, do not need extract fatures
test_gallery_path = np.array(glob.glob(dataset_root + '/test_A/gallery_feature_A/*.dat'))
test_gallery_path = np.array(test_gallery_path)
gallery_features = [read_dat(path) for path in tqdm(test_gallery_path)]
gallery_features = np.vstack(gallery_features)
gallery_features = normalize(gallery_features)
index_dir = dataset_root + '/index_dir'
if not os.path.exists(index_dir):
    os.makedirs(index_dir, exist_ok=True)



#dist_type
dist_type = faiss.METRIC_INNER_PRODUCT if dist_type == "IP" else faiss.METRIC_L2
embedding_size = 2048
#build index

index = faiss.index_factory(embedding_size,
                            index_method, dist_type)
index = faiss.IndexIDMap2(index)
ids = {}

if index_method == "HNSW32":
    print(
        "The HNSW32 method dose not support 'remove' operation")


# calculate id for new data
start_id = max(ids.keys()) + 1 if ids else 0
ids_now = (
    np.arange(0, gallery_features.shape[0]) + start_id).astype(np.int64)

# only train when new index file

index.train(gallery_features)

index.add_with_ids(gallery_features, ids_now)

for i, d in zip(list(ids_now), test_gallery_path):
    ids[i] = d


faiss.write_index(
    index, os.path.join(index_dir, "vector.index"))

with open(os.path.join(index_dir, "id_map.pkl"), 'wb') as fd:
    pickle.dump(ids, fd)



4. 特征检索

运行以下代码进行特征检索,检索后会在/home/aistudio目录下生成submit.json。下载之后去比赛也没提交即可。

import json
import pickle
import faiss
import glob
import numpy as np
from tqdm import tqdm
import os

from sklearn.preprocessing import normalize

return_k = 100
dataset_root="/home/aistudio/data"
index_dir = dataset_root + '/index_dir'
Searcher = faiss.read_index(
    os.path.join(index_dir, "vector.index"))
with open(os.path.join(index_dir, "id_map.pkl"), "rb") as fd:
    id_map = pickle.load(fd)

def read_dat(path):
    return np.fromfile(path, dtype=np.float32)

test_query_path = glob.glob(dataset_root + '/test_A/query_feature_A/*.dat')
test_query_path = np.array(test_query_path)
test_query = [read_dat(path) for path in tqdm(test_query_path)]
test_query = np.vstack(test_query)
test_query = normalize(test_query)

test_gallery_path = np.array(glob.glob(dataset_root + '/test_A/gallery_feature_A/*.dat'))
test_gallery_path = np.array(test_gallery_path)


ids = {}
# calculate id for new data
start_id = max(ids.keys()) + 1 if ids else 0
ids_now = (
        np.arange(0, len(test_gallery_path)) + start_id).astype(np.int64)


with open('/home/aistudio/sub_a.json') as up:
    sub = json.load(up)

total_idx = 0
for idx in range(test_query.shape[0]//1000 + 1):
    scores, docs = Searcher.search(test_query[idx*1000: (idx+1)*1000], return_k)
    for ids in docs:
        ids_path = test_gallery_path[ids]
        sub_name = os.path.basename(test_query_path[total_idx])
        sub[sub_name] = [os.path.basename(x) for x in ids_path]
        total_idx += 1
with open('/home/aistudio/submit.json', 'w') as up:
    json.dump(sub, up, indent=4)

最终得分0.89,目前排在前200名内,可以进入第二阶段。

欢迎大家扫描左侧的二维码关注我的公众号:人工智能研习社
获取最新的比赛Baseline,可在后台回复比赛名称或比赛网址,我会尽量为大家提供Baseline。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人工智能研习社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值