以前の書き方
ResNet50 を転移学習するサンプルコードで、以下のようにpretrained
を引数に指定しているものがあります。
from torchvision.models import resnet50
pretrain_model = resnet50(pretrained=True)
これは古い書き方で、v0.15以降では使えなくなります。v0.13.1時点では次のようなメッセージが表示されます。
UserWarning: The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead.
解決策
公式のドキュメントを引用します。
from torchvision.models import resnet50, ResNet50_Weights
# Old weights with accuracy 76.130%
resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)
# New weights with accuracy 80.858%
resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
# Best available weights (currently alias for IMAGENET1K_V2)
# Note that these weights may change across versions
resnet50(weights=ResNet50_Weights.DEFAULT)
# Strings are also supported
resnet50(weights="IMAGENET1K_V2")
# No weights - random initialization
resnet50(weights=None)
古いサンプルコードと同じ結果を得たければIMAGENET1K_V1
を、精度の良い転移学習を行いたい場合は IMAGENET1K_V1
または DEFAULT
を選ぶのが良さそうです。
今回の場合は前者なので、下記が解決策となります。
from torchvision.models import resnet50, ResNet50_Weights
#pretrain_model = resnet50(pretrained=True)
pretrain_model = resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)