You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch脊髓灰质分割项目报错求助:无法从torch._six导入int_classes

问题:PyTorch脊髓灰质分割项目依赖导入错误

项目背景

  • 基于PyTorch开展脊髓灰质分割医疗图像分割项目

依赖安装操作

执行了以下依赖安装命令:

!pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-linux_x86_64.whl
!pip3 install torchvision
!pip install medicaltorch
!pip3 install numpy==1.14.1

安装过程中出现依赖相关报错(对应两张截图)

导入代码与报错信息

执行以下导入代码:

from collections import defaultdict
import time
import os

import numpy as np

from tqdm import tqdm

from medicaltorch import datasets as mt_datasets
from medicaltorch import models as mt_models
from medicaltorch import transforms as mt_transforms
from medicaltorch import losses as mt_losses
from medicaltorch import metrics as mt_metrics
from medicaltorch import filters as mt_filters

import torch
from torchvision import transforms
from torch.utils.data import DataLoader
from torch import autograd, optim
import torch.backends.cudnn as cudnn
import torch.nn as nn

import torchvision.utils as vutils

cudnn.benchmark = True
import matplotlib.pyplot as plt
%matplotlib inline

触发ImportError,具体报错:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-8-80b8c583d1fe> in <module>()
     20 
     21 
---> 22 from medicaltorch import datasets as mt_datasets
     23 from medicaltorch import models as mt_models
     24 from medicaltorch import transforms as mt_transforms

/usr/local/lib/python3.7/dist-packages/medicaltorch/datasets.py in <module>()
     11 from torch.utils.data import Dataset
     12 import torch
---> 13 from torch._six import string_classes, int_classes
     14 
     15 from PIL import Image

ImportError: cannot import name 'int_classes' from 'torch._six' (/usr/local/lib/python3.7/dist-packages/torch/_six.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

解决方法

核心原因

medicaltorch版本与安装的PyTorch 0.4.0不兼容。torch._six中的int_classes在PyTorch 1.x及以后版本被移除/重命名,而当前安装的medicaltorch版本依赖高版本PyTorch的API。

具体步骤

  1. 卸载冲突依赖
    先清理现有不兼容的包:

    !pip3 uninstall -y torch torchvision medicaltorch numpy
    
  2. 版本匹配安装
    两种可选方案:

    • 方案一(推荐):升级PyTorch到1.x版本(0.4.0过于老旧,多数库已停止支持)
      以适配CUDA 11.1的PyTorch 1.8.0为例,执行:
      !pip3 install torch==1.8.0+cu111 torchvision==0.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
      !pip install medicaltorch
      !pip3 install numpy==1.21.0
      
      若你的CUDA版本不同,可替换对应版本号。
    • 方案二:安装适配PyTorch 0.4.0的旧版medicaltorch
      查找并安装兼容的旧版本(如0.2.0,需确认兼容性):
      !pip3 install medicaltorch==0.2.0
      
  3. 验证修复
    重新运行导入代码,检查是否仍有报错。若问题持续,确认numpy版本与PyTorch兼容(PyTorch 0.4.0建议numpy≤1.16.0)。

内容的提问来源于stack exchange,提问作者shlok pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:36:29