5
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 1 year has passed since last update.

The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead.

Posted at

以前の書き方

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)
5
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
5
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?