CMC
CMC全程是Cumulative Matching Characteristics, 是行人重識別問題中的經典評價指標。該曲線的橫坐標為rank,縱坐標為識別率百分比。rank n表示識別結果相似性降序排列中前n個結果包含目標。識別率是rank n 的數目#(rank n)占總的query樣本數的比例。如下圖

CMC曲線圖來源 https://blog.csdn.net/m0_37240250/article/details/79942939
代碼解釋
先貼代碼
代碼來源 [港中文Xiaogang Wang教授 主頁]
function C = cmc(D, varargin)
% CMC computes the Cumulative Match Characteristic.
% C = CMC(D) computes the CMC of given distance matrix D between each pair
% of gallery and probe person. Both gallery and probe persons are
% assumed to be unique, i.e., the i-th gallery person matches only to
% the i-th probe person. D(i, j) should be the distance between
% gallery instance i and probe instance j.
% 單個參數C = CMC(D)時表示gallery和probe中的identity都是獨一無二的,
% 即gallery中第i個person只匹配probe set第i個person
%
% C = CMC(D, G, P) computes the CMC with G and P are id labels of each
% person in gallery and probe sets. D is M*N where M and N are the lengths of
% vector G and P, respectively. This function will first randomly select
% exactly one instance among for each gallery identity and then calculate
% the CMC of this sub-matrix. This procedure will be repeated 100 times
% and the final result is the mean of them.
% 三個參數C = CMC(D, G, P),G和P是gallery和probe中person的id label,
% 所以D的高=G的個數,D的寬=P的個數
% 該腳本先對galleray中的每個identity隨機挑選一個instance(同一個identity可能有多個instance),
% 計算子矩陣的CMC,然后重復一百次,求均值
switch nargin
% nargin 針對當前正在執行的函數,返回函數調用中給定函數輸入參數的數目
% nargin(fun) 返回 fun 函數定義中出現的輸入參數的數目
% 如果該函數定義中包含 varargin,那么 nargin 返回輸入數目的負數
% 例如,如果 myFun 函數聲明輸入 a、b 和 varargin,那么 nargin('myFun') 返回
% -3,就可以確定varargin前有2個參數
case 1
assert(ismatrix(D));
assert(size(D, 1) == size(D, 2));% 判斷是矩陣且是方陣
case 3
G = varargin{1};
P = varargin{2};
assert(isvector(G));
assert(isvector(P));
assert(length(G) == size(D, 1));
assert(length(P) == size(D, 2));
otherwise
error('Invalid inputs');
end
if nargin == 1
C = cmc_core(D, 1:size(D, 1), 1:size(D, 1));
else
gtypes = union(G, []);%求集合,防止重復label
ngtypes = length(gtypes);
ntimes = 100;
C = zeros(ngtypes, ntimes);%寬是100,有100條曲線求均值
for t = 1:ntimes
subdist = zeros(ngtypes, size(D, 2));
for i = 1:ngtypes
j = find(G == gtypes(i));%尋找gtypes(i)相同的G中label
k = j(randi(length(j)));% j可能有多個值,從中隨機選一個
subdist(i, :) = D(k, :);%選取D中對應的行向量(該Label到probe set中每個label的距離的向量)
end
C(:, t) = cmc_core(subdist, gtypes, P);
end
C = mean(C, 2);
end
end
function C = cmc_core(D, G, P)
m = size(D, 1);
n = size(D, 2);
[~, order] = sort(D);%二維矩陣時,對矩陣的每一列分別進行升序排序,即query中每個label在gallery中相似度的升序
match = (G(order) == repmat(P, [m, 1]));%(m*n==m*n),找矩陣中對應相同元素
%repmat(P, [m, 1])是以P為unit構建m(unit)X1(unit)的矩陣
% G(order)與order/D大小一致,即按照order里面每列的排序重新排列G
C = cumsum(sum(match, 2) / n);
%sum(match, 2)每行累加
%cumsum是將矩陣按列進行求和,每一項是前面之和(類似於積分曲線)
end
原理解釋
腳本中單個參數CMC(D)是多參數的特殊形式,這里只解釋多參數調用
對於函數調用CMC(D, G, P),定義
- G為gallery中每個人的label向量,大小為Mx1
- P為probe sets(query)中每個person的label向量,大小為Nx1
- D是gallery和probe sets中兩兩person的特征距離矩陣,大小為MxN,
如下圖所示

對G中的label求集合,得到G’,即向量中不出現重復的label;
G’中每個label對應的D中的行向量組成新的特征距離矩陣D’。其中,對於某些label在G中多次出現,隨機選取其中的一個並抽取在D中的行向量。
如下圖所示,G2,G3,GM隨機選一個

對D’按列進行升序排列,逐列記錄下排列順序的矩陣order,並按照每列的排列順序重新排列G’(每一列重拍一次G’),得到label矩陣G’(order)。
order獲取示例(以一列向量為例):

G’(order)如下:

以P為單元復制[m,1]次,得到repmat(P)矩陣

求和G’(order)矩陣中相等的元素的位置,
match = (G(order) == repmat(P, [m, 1]))
得到0/1二值矩陣match,大小為m*n,其中1為該位置處兩個矩陣對應元素相等,0為不等
求CMC分數:
C = cumsum(sum(match, 2) / n)
sum(match, 2) / n是對match矩陣按行求均值(m*1),cumsum()是逐列求積分(m*1)。
cunsum示例

這樣就得到了一條CMC中的rank曲線,重復100次求均值即輸出。
mAP
Precision & Recall
一般來說,Precision就是檢索出來的條目(比如:文檔、網頁等)有多少是准確的,Recall就是所有准確的條目有多少被檢索出來了。
正確率 = 提取出的正確信息條數 / 提取出的信息條數
召回率 = 提取出的正確信息條數 / 樣本中的信息條數
准確率和召回率都是針對同一類別來說的,並且只有當檢索到當前類別時才進行計算(因為P-R曲線,只在recall的點算,而recall只在TP點算),比如在person re-id中,一個人的label為m1,在測試集中包含3張此人的圖像,檢索出來的圖像按照得分從高到低順序為m1、m2、m1、m3、m4、m1….,此時
第一次檢索到m1,提取出的正確信息條數=1,提取出的信息條數=1,樣本中的信息條數=3,正確率=1/1=100%,召回率=1/3=33.33%;
第二次檢索到m1,提取出的正確信息條數=2,提取出的信息條數=3,樣本中的信息條數=3,正確率=2/3=66.66%,召回率=2/3=66.66%;
第三次檢索到m1,提取出的正確信息條數=3,提取出的信息條數=6,樣本中的信息條數=3,正確率=3/6=50%,召回率=3/3=100%;
平均正確率AP=(100%+66.66%+50%)/3=72.22%
對Precision求Mean Average等同於求P-R曲線下的面積(積分)
mAP
而當需要檢索的不止一個人時,此時正確率則取所有人的平均mAP。
--轉載自CSDN
