SY-Xuan / IICS

Implementation "Intra-Inter Camera Similarity for Unsupervised Person Re-Identification" in pytorch (CVPR2021)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The results of infer is not good

xddlj opened this issue · comments

commented

import time
import numpy as np
import sys
import torch
from PIL import Image
from reid import models
from reid.feature_extraction import extract_cnn_feature
from reid.utils.data import transforms as T
from reid.utils.serialization import load_checkpoint
torch.multiprocessing.set_sharing_strategy('file_system')

normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
test_transformer = T.Compose([
T.Resize((256, 128), interpolation=3),
T.ToTensor(),
normalizer
])

image_path_query = "./example/data/market1501/raw/Market-1501-v15.09.15/query/0001_c1s1_001051_00.jpg"
image_path_gallery = "./example/data/market1501/raw/Market-1501-v15.09.15/query/0001_c5s1_001426_00.jpg"

image_path_gallery = "./example/data/market1501/raw/Market-1501-v15.09.15/query/0003_c5s3_065187_00.jpg"

image_query = Image.open(image_path_query).convert('RGB')
image_query = test_transformer(image_query)
image_query = image_query.unsqueeze(0).to('cuda')

image_gallery = Image.open(image_path_gallery).convert('RGB')
image_gallery = test_transformer(image_gallery)
image_gallery = image_gallery.unsqueeze(0).to('cuda')

def cosine_distance(a,b):
result = np.dot(a,b)/(np.linalg.norm(a)*(np.linalg.norm(b)))
return result

start_time = time.time()
model = models.create("ft_net_inter", num_classes=0, stride=1)
checkpoint = load_checkpoint("./market.pth.tar")
param_dict = model.state_dict()
for k, v in checkpoint['state_dict'].items():
if 'model' in k:
param_dict[k] = v
model.load_state_dict(param_dict)
model.cuda()
model.eval()
model_time = time.time()
print("load model time", model_time - start_time)

evaluator_query = extract_cnn_feature(model, image_query)
evaluator_gallery = extract_cnn_feature(model, image_gallery)

extractor_time = time.time()
print("features extract time", (extractor_time - model_time) / 2)

evaluator_query_arr = evaluator_query.detach().cpu().numpy()[0]
evaluator_gallery_arr = evaluator_gallery.detach().cpu().numpy()[0]
dismat = cosine_distance(evaluator_query_arr, evaluator_gallery_arr)
print("dismat", dismat)

the cosine_distance between 0001_c1s1_001051_00.jpg and 0001_c5s1_001426_00.jpg is 0.9907226
the cosine_distance between 0001_c1s1_001051_00.jpg and 0003_c5s3_065187_00.jpg is 0.98081183
the similarity between 0001_c1s1_001051_00.jpg and 0003_c5s3_065187_00.jpg is so high.
I test other images,the cosine_distance is still high.
market.pth.tar is provided model. Is there something wrong with my program?
Some suggestions will be appreciated !

Yes, you are right. The cosine similarity is very high. But it has no effect on the final performance. ReID just cares about ranking of images.