|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +class Excluder: |
| 8 | + """ |
| 9 | + In the DukeMTMC-reID evaluation, we need to exclude distractions |
| 10 | + """ |
| 11 | + def __init__(self, gallery_fids): |
| 12 | + # Setup a regexp for extracing the PID and camera (CID) form a FID. |
| 13 | + self.regexp = re.compile('(\S+)_c(\d+)_.*') |
| 14 | + |
| 15 | + # Parse the gallery_set |
| 16 | + self.gallery_pids, self.gallery_cids = self._parse(gallery_fids) |
| 17 | + |
| 18 | + def __call__(self, query_fids): |
| 19 | + # Extract both the PIDs and CIDs from the query filenames: |
| 20 | + query_pids, query_cids = self._parse(query_fids) |
| 21 | + |
| 22 | + # Ignore same pid image within the same camera |
| 23 | + cid_matches = self.gallery_cids[None] == query_cids[:,None] |
| 24 | + pid_matches = self.gallery_pids[None] == query_pids[:,None] |
| 25 | + mask = np.logical_and(cid_matches, pid_matches) |
| 26 | + |
| 27 | + # Remove all "junk" with the -1 pid. |
| 28 | + junk_images = np.repeat(self.gallery_pids[None] == '-1', len(query_pids), 0) |
| 29 | + mask = np.logical_or(mask, junk_images) |
| 30 | + |
| 31 | + return mask |
| 32 | + |
| 33 | + def _parse(self, fids): |
| 34 | + """ Return the PIDs and CIDs extracted from the FIDs. """ |
| 35 | + pids = [] |
| 36 | + cids = [] |
| 37 | + for fid in fids: |
| 38 | + filename = os.path.splitext(os.path.basename(fid))[0] |
| 39 | + pid, cid = self.regexp.match(filename).groups() |
| 40 | + pids.append(pid) |
| 41 | + cids.append(cid) |
| 42 | + return np.asarray(pids), np.asarray(cids) |
0 commit comments