Can’t import 'ConvBNReLU' from 'torchvision.models.mobilenet' while using GPU

Problem
I’m following this tutorial to train an object detection model for Lens Studio. I want to use a GPU to train the model, either Google Colab’s cloud GPU or my local through Jupyter.

When I try to import:

import torchvision
from torchvision.models.mobilenet import ConvBNReLU
from torchvision.ops import box_iou, nms

I'm getting the following error when the runtime is set to GPU.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-157e58f8c700> in <module>
     26 
     27 import torchvision
---> 28 from torchvision.models.mobilenet import ConvBNReLU
     29 from torchvision.ops import box_iou, nms
     30 

ImportError: cannot import name 'ConvBNReLU' from 'torchvision.models.mobilenet' (/usr/local/lib/python3.8/dist-packages/torchvision/models/mobilenet.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.
---------------------------------------------------------------------------

Expected behaviour:
It should just return imported.

What did I try:
I tried to run the code in a local environment using Jupyter and my local GPU and also connected the local runtime to Google Colab by using Jupyter to use the local GPU. In both cases, the error to import persists.

PyTorch set up in the local environment confirmed that the CUDA and usage of the local GPU are enabled and available for use.

When I set the runtime to CPU, everything gets imported and works as expected, and I can train and export the model.

Therefore, I believe the issue relates to the usage of GPU and

import 'ConvBNReLU' from 'torchvision.models.mobilenet’.

I’d be grateful for any suggestions.

Best Answer

  • mskrfttt
    mskrfttt Posts: 6
    #2 Answer ✓

    I experimented more and found a solution!

    I used this to install the packages:

    %pip install -q numpy opencv-python-headless \
        torch torchvision xarray-einstats \
        albumentations tqdm
    

    It doesn't return any compatibility errors.

    Then I used this line for Torchvision 0.14.0 & 0.15.0 :

    from torchvision.models.mobilenetv2 import Conv2dNormActivation
    
    

    Make sure that you also change the function name call to Conv2dNormActivation().

    Thanks to these I could use cuda with Google Colab and train the model.

Answers

  • Bakari Mustafa
    Bakari Mustafa Posts: 174 🔥🔥🔥

    There are a few things you could try to resolve this issue:

    • Make sure that you have the correct version of PyTorch installed on your system. The ConvBNReLU class was introduced in PyTorch 1.7, so you will need to have at least that version installed in order to use it.
    • Check for compatibility issues: It's possible that the ConvBNReLU class is not compatible with GPU acceleration, or that there is a compatibility issue between PyTorch and the GPU you are using. In this case, you may need to switch to using a CPU for training or try using a different GPU.
  • Hi, I have the same issue. I tried two things Bakari suggested but still got the same error. Any solution for this?

  • Unfortunately, I didn't find the solution. I trained the model using the CPU.

  • Thang Nguyen
    Thang Nguyen Posts: 1
    edited March 2023 #6
    clickhereclick
  • %pip install -q numpy==1.24.2 opencv-python-headless==4.7.0.72 \
    torch==1.12.0 torchvision==0.13.0\
    albumentations==0.4.5 tqdm==4.43.0

    This one worked for me, I also had the same issue.

  • Here is the solution:
    replace this line:
    from torchvision.models.mobilenet import ConvBNReLU
    with this one
    from torchvision.models.mobilenetv2 import Conv2dNormActivation as ConvBNReLU

  • mskrfttt
    mskrfttt Posts: 6
    edited May 2023 #9

    Thanks for the contribution.
    @Moule Eswarran

    I'm getting these errors when I use your packages:

    `ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
    numba 0.56.4 requires numpy<1.24,>=1.18, but you have numpy 1.24.2 which is incompatible.
    panel 0.14.4 requires tqdm>=4.48.0, but you have tqdm 4.43.0 which is incompatible.
    tensorflow 2.12.0 requires numpy<1.24,>=1.22, but you have numpy 1.24.2 which is incompatible.
    torchaudio 2.0.2+cu118 requires torch==2.0.1, but you have torch 1.12.0 which is incompatible.
    torchdata 0.6.1 requires torch==2.0.1, but you have torch 1.12.0 which is incompatible.
    torchtext 0.15.2 requires torch==2.0.1, but you have torch 1.12.0 which is incompatible.`
    

    @Ibrahim Boona
    Can you share what kind of package installation versions did you use that worked for you?

    I'm getting this now:

    `---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf . Check the section C-API incompatibility at the Troubleshooting ImportError section at https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility for indications on how to solve this problem .
    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    <ipython-input-4-157e58f8c700> in <cell line: 18>()
         16 from PIL import Image
         17 
    ---> 18 import albumentations as albu
         19 import albumentations.augmentations.functional as AF
         20 
    
    16 frames
    /usr/local/lib/python3.10/dist-packages/numpy/testing/_private/utils.py in <module>
         21 from numpy.core import(
         22      intp, float32, empty, arange, array_repr, ndarray, isnat, array)
    ---> 23 import numpy.linalg.lapack_lite
         24 
         25 from io import StringIO
    
    ImportError: numpy.core.multiarray failed to import
    
    ---------------------------------------------------------------------------
    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.
    ---------------------------------------------------------------------------`