2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

自作のTransformの作成方法

Posted at

参考ページ:Pytorch – torchvision で使える Transform まとめ

画像データの前処理に利用するtransformsでは、Lambda関数を渡すことでユーザ定義のTransformが作れる。

from torchvision import transforms
import  cv2
import matplotlib.pyplot as plt

img = cv2.imread("sample.jpeg")

plt.imshow(img)

ダウンロード.png

def gray(img):
    """
    RGBに変換してグレースケール化
    """
    
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    
    return img

transform = transforms.Lambda(gray)

img_transformed = transform(img)
plt.imshow(img_transformed)

ダウンロード (1).png

この処理をComposeでつなげれば、pytorchのtransformのpipelineに組み込むことができる。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?