LoginSignup
1
2

More than 1 year has passed since last update.

【Python】Chamfer Distanceを実装した

Posted at

概要

機械学習で生成した3Dモデルがターゲットの3Dモデルと比較してどれくらいの精度かを評価するために、「Chamfer Distance」が用いられることがあります。

そこで本記事では、Chamfer DistanceをPythonで実装する方法を記載しています。

Chamfer Distanceとは

Chamfer Distanceは、2つの点群間の位置誤差を測る指標です。

点群$X$と点群$Y$間のChamfer Distanceを計算する式は以下のように定義されます。

CD(X, Y)=\frac{1}{|X|}\sum_{x \in X}\min_{y \in Y}\|x-y\|_2^2+\frac{1}{|Y|}\sum_{y \in Y}\min_{x\in X}\|x-y\|^2_2

具体的な処理は以下のようになります。

  1. 点群$X$内の点$x$と、点群$Y$の全ての点のユークリッド距離を算出し、2乗する。
  2. 1.で求めた2乗ユークリッド距離の最小値を、加算する。
  3. 加算した最小距離の平均を計算する。
  4. 同様の処理を点群$Y$で行う(こちらの場合は点$y$と点群$X$の全ての点を比較する)。
  5. 算出した点群$X$における最小距離と点群$Y$における最小距離を加算する。

Pythonでの実装

今回はnumpyライブラリを用いて実装を行いました。

def av_dist(array1, array2):
    """
    arguments:
        array1, array2: both size: (num_points, num_feature)
    returns:
        distances: size: (1,)
    """
    distances = np.sum(np.square(np.expand_dims(array1, axis=1) - np.expand_dims(array2, axis=0)), axis=-1)
    distances = np.min(distances, axis=1)
    distances = np.mean(distances)
    return distances

def chamfer_distance(pointsA, pointsB):
	"""Chamfer Distance評価指標

	Args:
		pointsA (np.array): 点群情報A
		pointsB (np.array): 点群情報B

	Returns:
		float: CD値
	"""
    dist_A = av_dist(pointsA, pointsB)
    dist_B = av_dist(pointsB, pointsA) 
    return dist_A + dist_B

おわり

実装が誤っている点があればご指摘いただけますと幸いです。

参考サイト

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2