Fréchet distance
Fréchet distance經常被用於描述路徑相似性。
Fréchet distance(弗雷歇距離)是法國數學家Maurice René Fréchet在1906年提出的一種路徑空間相似形描述( 此外還在這篇論文里定義了 度量空間),這種描述同時還考慮進路徑空間距離的因素[1],對於空間路徑的相似性比較適用。
[1] Fréchet, M. Maurice. "Sur quelques points du calcul fonctionnel." Rendiconti del Circolo Matematico di Palermo (1884-1940) 22.1 (1906): 1-72.
直觀的理解,Fréchet distance就是最短的狗繩長度:主人走路徑A,狗走路徑B,各自走完這兩條路徑過程中所需要的最短狗繩長度。
Fréchet distance的直觀理解
在數學中,Fréchet距離是曲線之間相似性的度量,它考慮了沿曲線的點的位置和順序。它以Maurice Fréchet命名。
Intuitive definition
Imagine a person traversing a finite curved path while walking their dog on a leash, with the dog traversing a separate finite curved path. Each can vary their speed to keep slack in the leash, but neither can move backwards. The Fréchet distance between the two curves is the length of the shortest leash sufficient for both to traverse their separate paths from start to finish. Note that the definition is symmetric with respect to the two curves—the Fréchet distance would be the same if the dog were walking its owner.
想象一下,一個人用皮帶牽着狗走在一條有限彎曲的路徑上,而狗走在另一條有限彎曲的路徑上。人和狗都可以改變速度來放松皮帶,但都不能后退。兩條曲線之間的Fréchet距離是最短的狗繩長度,該長度足以讓人和狗從起點到終點穿越各自的路徑。請注意,這個定義關於兩條曲線是對稱的, Fréchet距離將是相同的,如果看作狗是在遛它的主人~。
直觀地看, Fréchet距離是狗繩的最短長度,同時也是兩條曲線之間最大的距離。
Formal definition
從嚴格的數學定義上看,Fréchet距離被定義為兩條曲線A(alpha(t))和B(beta(t))之間距離最大值的下確界。如何理解這句話?
我們可以將t理解為時間,alpha(t)和beta(t)控制人和狗的速度,那么A(alpha(t))和B(beta(t))就代表t時刻人和狗的位置。現在人和狗要穿越各自的路徑(曲線),因為人和狗的速度是可以任意調整的(即,alpha(t)和beta(t)兩個映射是任意的)(我們假設狗繩可以無限伸長),所以必然存在很多種方式可以做到人和狗穿越各自的路徑。在每一種速度方案下都能計算該方案下穿越路徑時人狗之間的最大值,這個最大值是變的,其取決於二者的速度方案。但是可以想象,在所有的速度方案中一定已存在一種(狗繩)最緊的方式。也就是Fréchet距離取的是距離最大值的下確界。
Fréchet度量考慮到了兩條曲線的流動,因為其距離對Fréchet距離有貢獻的點對沿着各自的曲線連續掃過。這使得Fréchet距離比任意點集的Hausdorff距離等替代品更能衡量曲線的相似性。兩條曲線有可能有較小的Hausdorff距離,但有較大的Fréchet距離。
另一種直觀的理解方式是,將兩條曲線看做是兩條河流,現在從各自的端點開始開閘放水,考察兩條河流水頭之間的距離。因為我們可以調整每次開閘放水兩條河流水頭的速度,這樣就有不同的組合方案。Fréchet距離取的是不同速度方案下水頭之間距離最大值的下確界。
對於Fréchet距離,這兩條曲線的長度不一定相同(對於離散情況下,不要求兩條曲線包含相同數量的點,兩條曲線的點集數目不一定相等)。
該距離度量的嚴格定義參考:
路徑相似性描述:Fréchet distance
離散化Fréchet distance
兩個離散序列之間的Fréchet distance
關於Discrete Fréchet Distance的計算參考:
T. Eiter, H. Mannila, Computing Discrete Fréchet Distance, Tech. Report CD-TR 94/64, Information Systems Department, Technical University of Vienna, 1994.
matlab實現(待驗證)
https://www.mathworks.com/matlabcentral/fileexchange/41956-frechet-distance-calculator
%Tristan Ursell %Frechet Distance between two curves %May 2013 % % f = frechet(X1,Y1,X2,Y2) % f = frechet(X1,Y1,X2,Y2,res) % % (X1,Y1) are the x and y coordinates of the first curve (column vector). % (X2,Y2) are the x and y coordinates of the second curve (column vector). % % The lengths of the two curves do not have to be the same. % % 'res' is an optional parameter to set the resolution of 'f', the time to % compute scales linearly with 'res'. 'res' must be positive, and if 'res' % is larger than the largest distance between any two points on the curve % the function will throw a warning. If 'res' is unspecified, the function % will select a reasonable value, given the inputs. % % This function estimates the Frechet Distance, which is a measure of the % dissimilarity between two curves in space (in this case in 2D). It is a % scalar value that is symmetric with respect to the two curves (i.e. % switching X1->X2 and Y1->Y2 does not change the value). Roughly % speaking, this distance metric is the minimum length of a line that % connects a point on each curve, and allows one to traverse both curves % from start to finish. (wiki: Frechet Distance) % % The function requires column input vectors, and the function 'bwlabel' % from the image processing toolbox. % % %EXAMPLE: compare three curves to find out which two are most similar % % %curve 1 %t1=0:1:50; %X1=(2*cos(t1/5)+3-t1.^2/200)/2; %Y1=2*sin(t1/5)+3; % % %curve 2 %X2=(2*cos(t1/4)+2-t1.^2/200)/2; %Y2=2*sin(t1/5)+3; % % %curve 3 %X3=(2*cos(t1/4)+2-t1.^2/200)/2; %Y3=2*sin(t1/4+2)+3; % %f12=frechet(X1',Y1',X2',Y2'); %f13=frechet(X1',Y1',X3',Y3'); %f23=frechet(X2',Y2',X3',Y3'); %f11=frechet(X1',Y1',X1',Y1'); %f22=frechet(X2',Y2',X2',Y2'); %f33=frechet(X3',Y3',X3',Y3'); % %figure; %subplot(2,1,1) %hold on %plot(X1,Y1,'r','linewidth',2) %plot(X2,Y2,'g','linewidth',2) %plot(X3,Y3,'b','linewidth',2) %legend('curve 1','curve 2','curve 3','location','eastoutside') %xlabel('X') %ylabel('Y') %axis equal tight %box on %title(['three space curves to compare']) %legend % %subplot(2,1,2) %imagesc([[f11,f12,f13];[f12,f22,f23];[f13,f23,f33]]) %xlabel('curve') %ylabel('curve') %cb1=colorbar('peer',gca); %set(get(cb1,'Ylabel'),'String','Frechet Distance') %axis equal tight % function f = frechet(X1,Y1,X2,Y2,varargin) %get path point length L1=length(X1); L2=length(X2); %check vector lengths if or(L1~=length(Y1),L2~=length(Y2)) error('Paired input vectors (Xi,Yi) must be the same length.') end %check for column inputs if or(or(size(X1,1)<=1,size(Y1,1)<=1),or(size(X2,1)<=1,size(Y2,1)<=1)) error('Input vectors must be column vectors.') end %create maxtrix forms X1_mat=ones(L2,1)*X1'; Y1_mat=ones(L2,1)*Y1'; X2_mat=X2*ones(1,L1); Y2_mat=Y2*ones(1,L1); %calculate frechet distance matrix frechet1=sqrt((X1_mat-X2_mat).^2+(Y1_mat-Y2_mat).^2); fmin=min(frechet1(:)); fmax=max(frechet1(:)); %handle resolution if ~isempty(varargin) res=varargin{1}; if res<=0 error('The resolution parameter must be greater than zero.') elseif ((fmax-fmin)/res)>10000 warning('Given these two curves, and that resolution, this might take a while.') elseif res>=(fmax-fmin) warning('The resolution is too low given these curves to compute anything meaningful.') f=fmax; return end else res=(fmax-fmin)/1000; end %compute frechet distance for q3=fmin:res:fmax im1=bwlabel(frechet1<=q3); %get region number of beginning and end points if and(im1(1,1)~=0,im1(1,1)==im1(end,end)) f=q3; break end end
Fréchet Inception Distance
除了測量曲線之間的距離外,Fréchet距離還可以用來測量概率分布之間的差異。As a distance between probability distributions (the FID score)
Fréchet Inception Distance簡稱FID最近被提出用於評估生成模型的質量,如生成的圖片。
FID was introduced by Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler and Sepp Hochreiter in "GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium", see https://arxiv.org/abs/1706.08500
FID is a measure of similarity between two datasets of images. It was shown to correlate well with human judgement of visual quality and is most often used to evaluate the quality of samples of Generative Adversarial Networks. FID is calculated by computing the Fréchet distance between two Gaussians fitted to feature representations of the Inception network.
FID是兩個圖像數據集之間相似度的度量。它被證明與人類對視覺質量的判斷有很好的相關性,並且最常被用於評估生成式對抗網絡樣本的質量。FID是通過計算擬合到Inception網絡特征表示的兩個高斯函數之間的Fréchet距離來計算的。
FID得分在Pytorch中的實現參考:
https://github.com/mseitzer/pytorch-fid
參考:
路徑相似性描述:Fréchet distance
https://en.wikipedia.org/wiki/Fréchet_distance