解决:ModuleNotFoundError: No module named ‘torchvision’


解决:ModuleNotFoundError: No module named ‘torchvision’





背景

在使用之前的代码时,报错:
Traceback (most recent call last):
File “xxx”, line xx, in
import torchvision
ModuleNotFoundError: No module named ‘torchvision’



报错问题


  Traceback (most recent call last): 
    File "xxx", line xx, in  
          import torchvision 
  ModuleNotFoundError: No module named 'torchvision'



报错翻译

主要报错信息内容翻译如下所示:


  Traceback (most recent call last): 
    File "xxx", line xx, in  
          import torchvision 
  ModuleNotFoundError: No module named 'torchvision'

翻译:


追溯(最近一次通话):
文件“xxx”,第xx行,在
导入torchvision
ModuleNotFoundError:没有名为“torchvision”的模块



报错位置代码


...
      import torchvision 
...



报错原因

经过查阅资料,发现是这个错误通常是由于缺少torchvision的依赖库,就会出现这样的提示。

小伙伴们按下面的解决方法即可解决!!!



解决方法

要解决这个错误,需要安装torchvision的依赖库,即可解决。

方法一,直接安装

在window下安装torchvision

pip install torchvision

如果你嫌下载慢,可以更改下载源,

比如,利用豆瓣的下载源

pip install torchvision -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

附,国内各大源列表:

名称地址
阿里https://mirrors.aliyun.com/pypi/simple
豆瓣http://pypi.douban.com/simple/
清华大学https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学https://pypi.mirrors.ustc.edu.cn/simple
华中理工大学http://pypi.hustunique.com/simple
山东理工大学http://pypi.sdutlinux.org/simple
网易https://mirrors.163.com/pypi/simple/
腾讯https://mirrors.cloud.tencent.com/pypi/simple

如果你使用的是conda环境,请尝试使用conda install安装torchvision。

conda install -c https://conda.anaconda.org/menpo torchvision

如果你使用的是pip安装torchvision,可以尝试使用以下命令安装:

pip install --no-cache-dir torchvision

方法二,手动下载安装

将安装包手动下载到本地进行安装,然后在cmd 中进入安装包的路径下面用:cd +路径,进入。

然后输入以下命令:

pip install torchvision-0.17.0-cp312-cp312-win_amd64.whl

就可以进行安装了。

附下载链接,点击下载对应版本的dilb链接
https://pypi.org/project/torchvision/#description

方法三,编译安装

如果上述方法都不能解决问题,你可以尝试从源代码编译安装torchvision。具体步骤可以参考torchvision的官方文档。

torchvision 简介

torchvision是PyTorch项目的一个图形库,主要用于构建计算机视觉模型。该库包括一些加载数据的函数及常用的数据集接口、常用的模型结构(含预训练模型)、常用的图像变换以及一些有用的方法。

torchvision库中的数据集接口可以用来下载和加载常用的视觉数据集,这些数据集可以帮助用户训练和测试模型。模型结构部分提供了许多流行的模型,如AlexNet、VGG、ResNet等,用户可以直接使用这些预训练模型进行迁移学习或者微调。图像变换部分则提供了常用的图像操作,如裁剪、旋转等。

torchvision库的主要目标是方便用户快速构建和测试计算机视觉模型,它与PyTorch深度学习框架紧密集成,是构建计算机视觉应用的重要工具之一。

Github地址:https://github.com/pytorch/pytorch
官方pip:https://pypi.org/search/?q=torchvision



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

  • 19
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
boston_housing module: Boston housing price regression dataset. cifar10 module: CIFAR10 small images classification dataset. cifar100 module: CIFAR100 small images classification dataset. fashion_mnist module: Fashion-MNIST dataset. imdb module: IMDB sentiment classification dataset. mnist module: MNIST handwritten digits dataset. reuters module: Reuters topic classification dataset. import tensorflow as tf from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() cifar100 = keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar100.load_data() cifar10 = keras.datasets.cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() imdb = keras.datasets.imdb (x_train, y_train), (x_test, y_test) = imdb.load_data() # word_index is a dictionary mapping words to an integer index word_index = imdb.get_word_index() # We reverse it, mapping integer indices to words reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) # We decode the review; note that our indices were offset by 3 # because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown". decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in x_train[0]]) print(decoded_review) boston_housing = keras.datasets.boston_housing (x_train, y_train), (x_test, y_test) = boston_housing.load_data() reuters= keras.datasets.reuters (x_train, y_train), (x_test, y_test) = reuters.load_data() tf.keras.datasets.reuters.get_word_index( path='reuters_word_index.json' )

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ninghes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值