プリンキピア

第14章コンピュータビジョン

14.7.5. Exercises14.7.5 練習問題

  1. Can you improve the single-shot multibox detection by improving the loss function? For example, replace ℓ1 norm loss with smooth ℓ1 norm loss for the predicted offsets. This loss function uses a square function around zero for smoothness, which is controlled by the hyperparameter σ:
    英語のヒント
    損失関数を改良して、シングルショット・マルチボックス検出を改善できるか。たとえば、予測したオフセットについて、ℓ1ノルム損失をなめらかなℓ1ノルム損失に置き換えよ。この損失関数は、なめらかさのためにゼロの近くで2乗関数を使い、それはハイパーパラメータσで制御される。式は次のとおりである。
f(x)={(σx)2/2,if |x|<1/σ2|x|−0.5/σ2,otherwise(14.7.1)
When σ is very large, this loss is similar to the ℓ1 norm loss. When its value is smaller, the loss function is smoother.
英語のヒント
σがとても大きいとき、この損失はℓ1ノルム損失に似ている。その値が小さいほど、損失関数はなめらかになる。
def smooth_l1(data, scalar):
    out = []
    for i in data:
        if abs(i) < 1 / (scalar ** 2):
            out.append(((scalar * i) ** 2) / 2)
        else:
            out.append(abs(i) - 0.5 / (scalar ** 2))
    return torch.tensor(out)

sigmas = [10, 1, 0.5]
lines = ['-', '--', '-.']
x = torch.arange(-2, 2, 0.1)
d2l.set_figsize()

for l, s in zip(lines, sigmas):
    y = smooth_l1(x, scalar=s)
    d2l.plt.plot(x, y, l, label='sigma=%.1f' % s)
d2l.plt.legend();
Besides, in the experiment we used cross-entropy loss for class prediction: denoting by pj the predicted probability for the ground-truth class j, the cross-entropy loss is −log⁡pj. We can also use the focal loss (Lin et al., 2017): given hyperparameters γ>0 and α>0, this loss is defined as:
英語のヒント
さらに、実験ではクラスの予測に交差エントロピー損失を使った。正解のクラスjについて予測した確率をpjで表すと、交差エントロピー損失は−log⁡pjである。焦点損失(Lin et al., 2017)を使うこともできる。ハイパーパラメータγ>0とα>0が与えられたとき、この損失は次のように定義される。
−α(1−pj)γlog⁡pj.(14.7.2)
As we can see, increasing γ can effectively reduce the relative loss for well-classified examples (e.g., pj>0.5) so the training can focus more on those difficult examples that are misclassified.
英語のヒント
見てのとおり、γを大きくすると、よく分類された事例(たとえばpj>0.5)についての相対的な損失を効果的に減らせるので、訓練は誤分類されている難しい事例にもっと集中できる。
def focal_loss(gamma, x):
    return -(1 - x) ** gamma * torch.log(x)

x = torch.arange(0.01, 1, 0.01)
for l, gamma in zip(lines, [0, 1, 5]):
    y = d2l.plt.plot(x, focal_loss(gamma, x), l, label='gamma=%.1f' % gamma)
d2l.plt.legend();
  1. Due to space limitations, we have omitted some implementation details of the single shot multibox detection model in this section. Can you further improve the model in the following aspects:
    英語のヒント
    紙幅の都合で、この節ではシングルショット・マルチボックス検出のモデルの実装の詳細をいくつか省いた。次の点で、モデルをさらに改善できるか。
    1. When an object is much smaller compared with the image, the model could resize the input image bigger.
      英語のヒント
      物体が画像に比べてずっと小さいときは、モデルは入力画像を大きくすることができる。
    2. There are typically a vast number of negative anchor boxes. To make the class distribution more balanced, we could downsample negative anchor boxes.
      英語のヒント
      ふつう、負のアンカーボックスは膨大にある。クラスの分布をもっと釣り合ったものにするため、負のアンカーボックスをダウンサンプリングできる。
    3. In the loss function, assign different weight hyperparameters to the class loss and the offset loss.
      英語のヒント
      損失関数で、クラスの損失とオフセットの損失に異なる重みのハイパーパラメータを割り当てよ。
    4. Use other methods to evaluate the object detection model, such as those in the single shot multibox detection paper (Liu et al., 2016).
      英語のヒント
      シングルショット・マルチボックス検出の論文(Liu et al., 2016)にあるもののような、ほかの方法を使って物体検出のモデルを評価せよ。