[yolo series: Running error AttributeError: module 'torch.nn' has no attribute 'Mish']

An error occurred recently when running yolov7AttributeError: module ‘torch.nn‘ has no attribute ‘Mish‘

I have searched a series of error reporting methods on the Internet but none of them are easy to solve. So here are the specific solutions and some reference articles from others.

Here I will explain my own first, and then give the corresponding error reports of others.

Reason for the error:
The reason for this problem is that the Mish function does not exist in the torch.nn function table in this version of torch. A higher version of torch is required. In the official documentation, we can check whether this version contains this module. Here I will teach you how to view it.

First use win+r, then cmd to enter management

Use your command here to enter your own virtual environment.
The one you entered is the one you set in the pycharm interpreter. Enter the following commands in sequence.

activate your_env_name
python
import torch
print(torch.__version__)

Insert image description here
In this way, you can know the version of your torch. Here it is 1.8.1. Knowing the version, we go to the official website to check whether our version has this module.
Pytorch official website
Insert image description here
Click on the inverted triangle of 1.13 in the upper left corner
Insert image description here
to select your own version.
Insert image description here
After clicking in, the upper left corner will become 1.8.1 and
Insert image description here
start searching for the module that does not exist if you report an error. For example, here is AttributeError: module ‘torch.nn‘ has no attribute ‘Mish‘what we search for, and then start in the blank space on the right Press ctrl+f everywhere and search for mish, but it does not show up here, indicating that torch1.8.1 does not have a module for mish, so change torch.
Insert image description here
Search version 1.12.0 has mish, so it is recommended to download version 1.12.0. You can also download other versions. It depends on what modules you are missing. Secondly, your cuda must match it.
Insert image description here
It is recommended to create a new yolo environment here. You can refer to this
[yolov series: Xiaobai yolov7 running data set to establish an environment]
and refer to the above to configure it yourself. If it is the same as my configuration, you can choose the above article to create a new environment, or in the current data environment, install directly.
It is recommended to create a new environment.

pip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 --extra-index-url https://download.pytorch.org/whl/cu113 

Then just run it again.

Other references to the same error but in different modules are also given here.

AttributeError: module 'torch.nn' has no attribute 'module' This article is about adding a new module but the letters are spelled incorrectly. Just change nn.module in the code to nn.Module.

AttributeError: module 'torch.nn' has no attribute 'relu' This article has nothing to do with yolo. It is a calling error and the module is not called correctly.

AttributeError: module 'torch.nn' has no attribute 'SiLU' This article is the same as our error, it is also a version error, you can refer to it for details.

AttributeError: module 'torch.nn' has no attribute 'LocalResponseNorm' This also requires updating the torch version.
Give the update command, recorded as follows.
Enter the environment

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp35-cp35m-win_amd64.whl
pip3 install torchvision

Guess you like

Origin blog.csdn.net/weixin_47869094/article/details/132260947