Cysu / open-reid

Open source person re-identification library in python
https://cysu.github.io/open-reid/
MIT License
1.34k stars 349 forks source link

How to output the top 10 match pictures? #60

Open wanfb opened 6 years ago

wanfb commented 6 years ago

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.

FightForCS commented 6 years ago

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)
wanfb commented 6 years ago

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?

miraclebiu commented 6 years ago

The smaller the distance is , the greater the similarity

wanfb commented 6 years ago

Sincerely appreciate for your reply. Get it!

CheshireLin commented 6 years ago

Where dose pairwise_distance() import?

xiangdeyizhang commented 5 years ago

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