15 fast-reid 모델 빌드를 위한 cfg 생성 기능
17 cfg_path (str): 모델이 정의된 config 파일 경로
18 checkpoint_path (str): 모델의 파라미터가 저장된 경로
19 device (str): 모델이 구동될 디바이스
23 cfg.merge_from_file(cfg_path)
24 cfg.merge_from_list([
'MODEL.DEVICE', device])
25 cfg.merge_from_list([
'MODEL.WEIGHTS', checkpoint_path])
34 cfg_path (str): 모델이 정의된 config 파일 경로
35 checkpoint_path (str): 모델의 파라미터가 저장된 경로
36 device (str): 모델이 구동될 디바이스
39 reid_cfg =
make_reid_cfg(args.cfg_path, args.checkpoint_path, args.device)
47 이미지 한장에 대해 reid 모델을 inference 하는 기능
50 image (np.array): BGR 이미지
54 image = image[:, :, ::-1]
55 image = cv2.resize(image, tuple(model.cfg.INPUT.SIZE_TEST[::-1]), interpolation=cv2.INTER_CUBIC)
56 image = torch.as_tensor(image.astype(
"float32").transpose(2, 0, 1))[
None]
64 여러장의 이미지에 대해 reid 특징을 추출하는 기능
67 crop_images (list[str or np.array]): 사람 영역만 자른 이미지 저장 경로 또는 이미지
68 return (list[Tensor]):
73 for img
in crop_images:
75 if isinstance(img, str):
76 img = img = cv2.imread(img)
80 feats.append(inference_result)
96 쿼리와 갤러리 간의 유사도 행렬을 계산하는 기능
98 q_feats (list[tensor] or Tensor): 쿼리, reid 특징 벡터
99 g_feats (Tensor): 갤러리, reid 특징 벡터
105 if isinstance(q_feats, list):
106 q_feats = torch.cat(q_feats, dim=0)
112 if isinstance(g_feats, list):
113 g_feats = torch.cat(g_feats, dim=0)
116 simmat = torch.mm(q_feats, g_feats.t())
117 simmat = simmat.numpy()
123 유사도 매트릭스를 기반으로 임계치 이상의 유사도를 가지는 feature를 찾는 기능
125 simmat (np.array): 유사도 매트릭스
126 select_id (int): 유사도를 비교하려는 인덱스
127 threshold (float): 유사도 임계치
129 s_sim (np.array):select_id와 다른 특징과의 유사도
130 keep_index (np.array): 임계치 이상의 유사도를 가지는 인덱스
133 s_sim = simmat[select_id, :]
136 keep = s_sim > threshold
137 keep_index = np.where(keep)[0]
139 return s_sim, keep_index
147 keep_index (np.array): 인덱스
148 s_sim (np.array): 유사도
149 return (list[tuple]): (인덱스, 유사도) 튜플의 내림차순 정렬된 리스트
155 for i
in range(len(keep_index)):
156 index = keep_index[i]
158 sort_index.append((index,sim))
161 sorted_keep_index = sorted(sort_index, key=operator.itemgetter(1), reverse=
True)
163 return sorted_keep_index
make_reid_cfg(cfg_path, checkpoint_path, device='cuda:0')
get_reid_matrix(q_feats, g_feats=None)
get_reid_feats(model, crop_images)
inference_reid_model(model, image)
get_reid_image_index(simmat, select_id=1, threshold=0.8)
index_sort(keep_index, s_sim)