Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights. warnings.warn(msg)解决办法
时间: 2024-03-16 07:45:47 浏览: 256
这个问题是因为在使用ResNet18模型时,传入的`weights`参数值不是权重枚举(`ResNet18_Weights`)或`None`。在0.13版本之后,除了这两种情况外,其他参数都将被弃用。解决方法是将传入的参数值改为权重枚举或`None`,或者使用`weights=ResNet18_Weights.DEFAULT`来获取最新的权重。同时,建议在代码中加上以下语句以忽略该警告:
```
import warnings
warnings.filterwarnings('ignore')
```
相关问题
Arguments other than a weight enum or None for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing weights=ResNet18_Weights.IMAGENET1K_V1. You can also use weights=ResNet18_Weights.DEFAULT to get the most up-to-date weights. warnings.warn(msg)解决办法解决办法示例
以下是解决这个问题的示例代码:
```python
from torchvision.models import resnet18
from torchvision.models.resnet import ResNet, BasicBlock, Bottleneck, model_urls
class ResNet18(ResNet):
def __init__(self, pretrained=True):
super(ResNet18, self).__init__(BasicBlock, [2, 2, 2, 2])
if pretrained:
# 使用预训练的模型权重
self.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
model = ResNet18(weights=None) # 或者使用 weights=ResNet18_Weights.DEFAULT
```
这里的解决方法是将传入的`weights`参数值改为`None`或`ResNet18_Weights.DEFAULT`。同时,建议在代码中加上以下语句以忽略该警告:
```python
import warnings
warnings.filterwarnings('ignore')
```
UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=None`.
这是一个警告信息,意味着在某个版本之后,传递给 'weights' 参数的非权重枚举或 `None` 以外的参数将被弃用,并且在将来的版本中将被移除。当前的行为相当于传递 `weights=None`。建议更新代码以避免使用被弃用的参数。
阅读全文