open-reid icon indicating copy to clipboard operation
open-reid copied to clipboard

How to output the top 10 match pictures?

Open wanfb opened this issue 6 years ago • 6 comments

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.

wanfb avatar Mar 20 '18 03:03 wanfb

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)

FightForCS avatar Mar 20 '18 05:03 FightForCS

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?

wanfb avatar Mar 20 '18 14:03 wanfb

The smaller the distance is , the greater the similarity

miraclebiu avatar Mar 21 '18 01:03 miraclebiu

Sincerely appreciate for your reply. Get it!

wanfb avatar Mar 21 '18 11:03 wanfb

Where dose pairwise_distance() import?

CheshireLin avatar Apr 13 '18 07:04 CheshireLin

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

xiangdeyizhang avatar Nov 15 '18 03:11 xiangdeyizhang