LoginSignup
0
2

More than 5 years have passed since last update.

PyTorch で複数の画像を同じ角度でランダムに回転する方法

Last updated at Posted at 2019-05-23

動機

画像系の深層学習において、画像データの増強(data augmentation)が必要であります。その中、画像セグメンテーションのend-2-end学習において、入力画像と教師画像を同じ角度でランダムに回転する必要があります。それを実現するために、random変数を用いる方法をまとめます。

実現方法

from torchvision import transforms
import random
from PIL import Image
import os
from matplotlib import pyplot as plt

# 画像を読み込む
d = '../data/homes/test_input/'
img0 = Image.open(os.path.join(d, '352.jpg'))
img1 = Image.open(os.path.join(d, '95.jpg'))

# ランダム変数を生成
s = random.randint(0, 2 ** 32)

# 同じランダム変数を設定して画像を同じ角度に回転することが可能
random.seed(s)
img0 = transforms.RandomRotation(180)(img0)
plt.imshow(img0)
plt.show()

random.seed(s)
img1 = transforms.RandomRotation(180)(img1)
plt.imshow(img1)
plt.show()
0
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
0
2