open-reid
open-reid copied to clipboard
How to output the top 10 match pictures?
Dear author, Thanks for your great work. It's really helpful to me who is new to this area. But I came across a problem when trying to adjust your code. Could you kindly tell me how to output the top 10 match pictures when given a query of only one picture? Because you are doing multi-queries computing all the time and there are many matrix operations in your code, it's hard to figure out what are the top 10 match pictures when given a query of only one picture. It really matters to me, hope to get your answers.
This might help
from PIL import Image
from reid.utils.data import transforms as T
import numpy as np
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
test_transformer = T.Compose([
T.RectScale(height=256, width=128),
T.ToTensor(),
normalizer,
])
def extract_features_single(model, fpath):
model.eval()
img = Image.open(fpath).convert('RGB')
img = test_transformer(img)
img = np.expand_dims(img, axis=0)
outputs = extract_cnn_feature(model, img)
features = outputs
return features
fpath = 'examples/data/viper/images/00000000_00_0000.jpg'
outputs = extract_features_single(model, fpath)
distmat = pairwise_distance(features, dataset.query[:1], dataset.gallery)
distmat.neg().topk(2)
Thank you for your kindly reply, it really helps me understand more about the code. But why do you use distmat.neg( ).topk(2) as the final result, instead of distmat.topk(2)? Do you think the negative numbers mean the query picture and the gallery pictures have more similarities?
The smaller the distance is , the greater the similarity
Sincerely appreciate for your reply. Get it!
Where dose pairwise_distance() import?
Sincerely appreciate for your reply. Get it!
I have trained the model ,but I don't know how to set "extract_features_single(model, fpath)",the model is model_best.pth.tar? and I test
from PIL import Image from reid.utils.data import transforms as T import numpy as np
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) test_transformer = T.Compose([ T.RectScale(height=256, width=128), T.ToTensor(), normalizer, ])
def extract_features_single(model, fpath): model.eval() img = Image.open(fpath).convert('RGB') img = test_transformer(img) img = np.expand_dims(img, axis=0) outputs = extract_cnn_feature(model, img) features = outputs
return features
fpath = './examples/data/viper/images/00000000_00_0000.jpg' model = '/home/xz/open-reid/logs/softmax-loss/viper-resnet50/model_best.pth.tar' outputs = extract_features_single(model, fpath) distmat = pairwise_distance(features, dataset.query[:1], dataset.gallery) distmat.neg().topk(2)
but there is an error :AttributeError: 'str' object has no attribute 'eval' can you help me ?thank