プリンキピア

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

14.5.1. Multiscale Anchor Boxes14.5.1 マルチスケールのアンカーボックス

You may realize that it is not difficult to reduce anchor boxes on an image. For instance, we can just uniformly sample a small portion of pixels from the input image to generate anchor boxes centered on them. In addition, at different scales we can generate different numbers of anchor boxes of different sizes. Intuitively, smaller objects are more likely to appear on an image than larger ones. As an example, 1×1, 1×2, and 2×2 objects can appear on a 2×2 image in 4, 2, and 1 possible ways, respectively. Therefore, when using smaller anchor boxes to detect smaller objects, we can sample more regions, while for larger objects we can sample fewer regions.
英語のヒント
画像上のアンカーボックスを減らすのは難しくないと気づくかもしれない。たとえば、入力画像から画素の小さな部分を一様に抽出し、それを中心とするアンカーボックスを生成すればよい。さらに、異なるスケールで、異なる大きさのアンカーボックスを異なる数だけ生成できる。直観的には、小さな物体のほうが大きな物体より画像に現れる可能性が高い。例として、1×1、1×2、2×2の物体は、2×2の画像に、それぞれ4通り、2通り、1通りで現れうる。したがって、小さなアンカーボックスを使って小さな物体を検出するときは多くの領域を抽出でき、大きな物体には少ない領域を抽出すればよい。
To demonstrate how to generate anchor boxes at multiple scales, let’s read an image. Its height and width are 561 and 728 pixels, respectively.
英語のヒント
複数のスケールでアンカーボックスをどう生成するかを示すため、画像を読み込もう。その高さと幅はそれぞれ561画素と728画素である。
%matplotlib inline
import torch
from d2l import torch as d2l

img = d2l.plt.imread('../img/catdog.jpg')
h, w = img.shape[:2]
h, w
(561, 728)
Recall that in Section 7.2 we call a two-dimensional array output of a convolutional layer a feature map. By defining the feature map shape, we can determine centers of uniformly sampled anchor boxes on any image.
英語のヒント
7.2節では、畳み込み層の2次元配列の出力を特徴マップと呼んだことを思い出そう。特徴マップの形状を定義することで、任意の画像上で一様に抽出したアンカーボックスの中心を決められる。
The display_anchors function is defined below. We generate anchor boxes (anchors) on the feature map (fmap) with each unit (pixel) as the anchor box center. Since the (x,y)-axis coordinate values in the anchor boxes (anchors) have been divided by the width and height of the feature map (fmap), these values are between 0 and 1, which indicate the relative positions of anchor boxes in the feature map.
英語のヒント
display_anchors関数を下に定義する。特徴マップ(fmap)上で、各ユニット(画素)をアンカーボックスの中心としてアンカーボックス(anchors)を生成する。アンカーボックス(anchors)の(x,y)軸座標の値は特徴マップ(fmap)の幅と高さで割られているので、これらの値は0から1のあいだにあり、特徴マップの中でのアンカーボックスの相対的な位置を示す。
Since centers of the anchor boxes (anchors) are spread over all units on the feature map (fmap), these centers must be uniformly distributed on any input image in terms of their relative spatial positions. More concretely, given the width and height of the feature map fmap_w and fmap_h, respectively, the following function will uniformly sample pixels in fmap_h rows and fmap_w columns on any input image. Centered on these uniformly sampled pixels, anchor boxes of scale s (assuming the length of the list s is 1) and different aspect ratios (ratios) will be generated.
英語のヒント
アンカーボックス(anchors)の中心は特徴マップ(fmap)のすべてのユニットに広がっているので、これらの中心は、相対的な空間の位置の点で、どの入力画像の上でも一様に分布していなければならない。もっと具体的には、特徴マップの幅と高さをそれぞれfmap_wとfmap_hとすると、次の関数は、どの入力画像の上でもfmap_h行fmap_w列の画素を一様に抽出する。これらの一様に抽出した画素を中心として、スケールs(リストsの長さは1とする)と異なる縦横比(ratios)のアンカーボックスが生成される。
def display_anchors(fmap_w, fmap_h, s):
    d2l.set_figsize()
    # Values on the first two dimensions do not affect the output
    fmap = torch.zeros((1, 10, fmap_h, fmap_w))
    anchors = d2l.multibox_prior(fmap, sizes=s, ratios=[1, 2, 0.5])
    bbox_scale = torch.tensor((w, h, w, h))
    d2l.show_bboxes(d2l.plt.imshow(img).axes,
                    anchors[0] * bbox_scale)
First, let’s consider detection of small objects. In order to make it easier to distinguish when displayed, the anchor boxes with different centers here do not overlap: the anchor box scale is set to 0.15 and the height and width of the feature map are set to 4. We can see that the centers of the anchor boxes in 4 rows and 4 columns on the image are uniformly distributed.
英語のヒント
まず、小さな物体の検出を考えよう。表示したときに見分けやすくするため、ここでは中心の異なるアンカーボックスは重ならない。アンカーボックスのスケールは0.15に設定し、特徴マップの高さと幅は4に設定する。画像上で4行4列のアンカーボックスの中心が一様に分布していることがわかる。
display_anchors(fmap_w=4, fmap_h=4, s=[0.15])
We move on to reduce the height and width of the feature map by half and use larger anchor boxes to detect larger objects. When the scale is set to 0.4, some anchor boxes will overlap with each other.
英語のヒント
続いて、特徴マップの高さと幅を半分にし、もっと大きなアンカーボックスを使って大きな物体を検出する。スケールを0.4に設定すると、いくつかのアンカーボックスが互いに重なる。
display_anchors(fmap_w=2, fmap_h=2, s=[0.4])
Finally, we further reduce the height and width of the feature map by half and increase the anchor box scale to 0.8. Now the center of the anchor box is the center of the image.
英語のヒント
最後に、特徴マップの高さと幅をさらに半分にし、アンカーボックスのスケールを0.8に上げる。これで、アンカーボックスの中心は画像の中心になる。
display_anchors(fmap_w=1, fmap_h=1, s=[0.8])