shlok pandey
shlok pandey

Reputation: 11

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

I am working on healthcare image dataset for image segmentation. More specific, it is "Spinal Cord Gray Matter Segmentation Using PyTorch". When I am trying to install libraries initially using this code:

!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

it is showing some errors in between required satisfied like this:

1st screenshot

2nd screenshot

After that I am importing libraries:

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

This importing is throwing an error like this:

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------

can someone help me resolve this?

Upvotes: 1

Views: 515

Answers (2)

phelps
phelps

Reputation: 11

As far as I know, torch._six is deprecated a while ago. You can replicate the same effect using (comment out the torch._six part):

string_classes = str 
int_classes = int

Furthermore, you may come up with a similar problem with the following:

from torch._six import container_abcs

You should fix it as following:

import collections.abc as container_abcs 

Upvotes: 1

Steven
Steven

Reputation: 5162

In pytorch 1.9 int_classes variable in torch._six was removed. facebookresearch/TimeSformer#47 Use this code instead.

from torch._six import string_classes
int_classes = (bool, int)

See source here: https://github.com/visionml/pytracking/issues/272

Upvotes: 0

Related Questions