プリンキピア

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

14.1.1. Common Image Augmentation Methods14.1.1 よく使われる画像の拡張の手法

In our investigation of common image augmentation methods, we will use the following 400×500 image an example.
英語のヒント
よく使われる画像の拡張の手法を調べるにあたり、次の400×500の画像を例として使う。
d2l.set_figsize()
img = d2l.Image.open('../img/cat1.jpg')
d2l.plt.imshow(img);
Most image augmentation methods have a certain degree of randomness. To make it easier for us to observe the effect of image augmentation, next we define an auxiliary function apply. This function runs the image augmentation method aug multiple times on the input image img and shows all the results.
英語のヒント
ほとんどの画像の拡張の手法には、ある程度のランダムさがある。画像の拡張の効果を観察しやすくするため、次に補助関数applyを定義する。この関数は、入力画像imgに画像の拡張の手法augを複数回実行し、すべての結果を表示する。
def apply(img, aug, num_rows=2, num_cols=4, scale=1.5):
    Y = [aug(img) for _ in range(num_rows * num_cols)]
    d2l.show_images(Y, num_rows, num_cols, scale=scale)

14.1.1.1. Flipping and Cropping14.1.1.1 反転と切り抜き

Flipping the image left and right usually does not change the category of the object. This is one of the earliest and most widely used methods of image augmentation. Next, we use the transforms module to create the RandomHorizontalFlip instance, which flips an image left and right with a 50% chance.
英語のヒント
画像を左右に反転しても、ふつう物体のカテゴリは変わらない。これは、画像の拡張の最も古く、最も広く使われている手法の一つである。次に、transformsモジュールを使って、50%の確率で画像を左右に反転するRandomHorizontalFlipインスタンスを作る。
apply(img, torchvision.transforms.RandomHorizontalFlip())
Flipping up and down is not as common as flipping left and right. But at least for this example image, flipping up and down does not hinder recognition. Next, we create a RandomVerticalFlip instance to flip an image up and down with a 50% chance.
英語のヒント
上下に反転することは、左右に反転することほど一般的ではない。しかし、少なくともこの例の画像では、上下に反転しても認識の妨げにはならない。次に、50%の確率で画像を上下に反転するRandomVerticalFlipインスタンスを作る。
apply(img, torchvision.transforms.RandomVerticalFlip())
In the example image we used, the cat is in the middle of the image, but this may not be the case in general. In Section 7.5, we explained that the pooling layer can reduce the sensitivity of a convolutional layer to the target position. In addition, we can also randomly crop the image to make objects appear in different positions in the image at different scales, which can also reduce the sensitivity of a model to the target position.
英語のヒント
使った例の画像では、猫は画像の中央にいるが、一般にはそうとは限らない。7.5節では、プーリング層が、畳み込み層の目標の位置に対する敏感さを減らせることを説明した。さらに、画像をランダムに切り抜いて、物体が画像の中のさまざまな位置にさまざまな縮尺で現れるようにすることもでき、これもモデルの目標の位置に対する敏感さを減らせる。
In the code below, we randomly crop an area with an area of 10%∼100% of the original area each time, and the ratio of width to height of this area is randomly selected from 0.5∼2. Then, the width and height of the region are both scaled to 200 pixels. Unless otherwise specified, the random number between a and b in this section refers to a continuous value obtained by random and uniform sampling from the interval [a,b].
英語のヒント
下のコードでは、毎回、元の面積の10%∼100%の面積の領域をランダムに切り抜き、この領域の幅と高さの比は0.5∼2からランダムに選ばれる。それから、領域の幅と高さはどちらも200画素に拡大縮小される。特に断らない限り、この節でのaとbのあいだの乱数は、区間[a,b]から一様にランダムに抽出して得た連続値を指す。
shape_aug = torchvision.transforms.RandomResizedCrop(
    (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
apply(img, shape_aug)

14.1.1.2. Changing Colors14.1.1.2 色を変える

Another augmentation method is changing colors. We can change four aspects of the image color: brightness, contrast, saturation, and hue. In the example below, we randomly change the brightness of the image to a value between 50% (1−0.5) and 150% (1+0.5) of the original image.
英語のヒント
もう一つの拡張の手法は、色を変えることである。画像の色は、明るさ、コントラスト、彩度、色相の四つの面で変えられる。下の例では、画像の明るさを、元の画像の50%(1−0.5)から150%(1+0.5)のあいだの値にランダムに変える。
apply(img, torchvision.transforms.ColorJitter(
    brightness=0.5, contrast=0, saturation=0, hue=0))
Similarly, we can randomly change the hue of the image.
英語のヒント
同じように、画像の色相をランダムに変えられる。
apply(img, torchvision.transforms.ColorJitter(
    brightness=0, contrast=0, saturation=0, hue=0.5))
We can also create a RandomColorJitter instance and set how to randomly change the brightness, contrast, saturation, and hue of the image at the same time.
英語のヒント
RandomColorJitterインスタンスを作り、画像の明るさ(brightness)、コントラスト(contrast)、彩度(saturation)、色相(hue)を同時にランダムに変える方法を設定することもできる。
color_aug = torchvision.transforms.ColorJitter(
    brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
apply(img, color_aug)

14.1.1.3. Combining Multiple Image Augmentation Methods14.1.1.3 複数の画像の拡張の手法を組み合わせる

In practice, we will combine multiple image augmentation methods. For example, we can combine the different image augmentation methods defined above and apply them to each image via a Compose instance.
英語のヒント
実際には、複数の画像の拡張の手法を組み合わせる。たとえば、上で定義したさまざまな画像の拡張の手法を組み合わせ、Composeインスタンスを通じて各画像に当てはめられる。
augs = torchvision.transforms.Compose([
    torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])
apply(img, augs)