こすたろーんエンジニアの試行錯誤部屋

作成物の備忘録を書いていきますー

google colab上でdiffusersとRealESRGANを使う際に発生したDetected that PyTorch and torchvision were compiled with different CUDA major versionsの対処法

スポンサーリンク

google colab上iffusersとRealESRGANを使うためにモジュールをインストールして実行した際に
Detected that PyTorch and torchvision were compiled with different CUDA major versionsが発生しました
本記事は上記エラーの対処法備忘録です

目次

スポンサーリンク

この記事でわかること

google colab上でdiffusersとRealESRGANを使う際に発生したDetected that PyTorch and torchvision were compiled with different CUDA major versionsの対処法

1.実行環境

Google Colab
git:clone https://github.com/xinntao/Real-ESRGAN.git
diffusers[torch]==0.14.0
transformers==4.26.0

2. モジュールインストールコード

本エラー発生時のインストールおよびインポートのコードは以下になります

!pip install diffusers[torch]==0.14.0 transformers==4.26.0 ftfy accelerate
!pip install safetensors
!pip install xformers==0.0.16

!git clone https://github.com/xinntao/Real-ESRGAN.git
%cd Real-ESRGAN
!apt-get install ffmpeg
!pip install pydub
!pip install basicsr
!pip install facexlib
!pip install gfpgan
!pip install numpy
!pip install opencv-python
!pip install Pillow
!pip install torchvision
!pip install tqdm

!python setup.py develop
!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P experiments/pretrained_models
!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -P experiments/pretrained_model

%cd ..

import torch
from torch import autocast
import os
from diffusers import StableDiffusionUpscalePipeline
from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNet2DConditionModel, DPMSolverMultistepScheduler
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import gc
import datetime
import configparser
import numpy as np
from PIL import Image
import pandas as pd
import sys
import math
from transformers import CLIPTextModel, CLIPTokenizer, CLIPTextConfig, logging
from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNet2DConditionModel
from safetensors.torch import load_file, save_file
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
import subprocess
import cv2

上記コードを実行すると以下のエラーが発生します

Detected that PyTorch and torchvision were compiled with different CUDA major versions...

3. 原因

PyTorch と torchvision が異なる CUDA バージョンでコンパイルされてため発生しています

4.対処法

使用するモジュールのバージョンを合わせるため2で記述したコードの最後に以下を追加します

!pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchtext torchaudio torchdata --extra-index-url https://download.pytorch.org/whl/cu118

変更後のコードは以下になります

!pip install diffusers[torch]==0.14.0 transformers==4.26.0 ftfy accelerate
!pip install safetensors
!pip install xformers==0.0.16

!git clone https://github.com/xinntao/Real-ESRGAN.git
%cd Real-ESRGAN
!apt-get install ffmpeg
!pip install pydub
!pip install basicsr
!pip install facexlib
!pip install gfpgan
!pip install numpy
!pip install opencv-python
!pip install Pillow
!pip install torchvision
!pip install tqdm

!python setup.py develop
!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P experiments/pretrained_models
!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -P experiments/pretrained_model

%cd ..

import torch
from torch import autocast
import os
from diffusers import StableDiffusionUpscalePipeline
from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNet2DConditionModel, DPMSolverMultistepScheduler
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import gc
import datetime
import configparser
import numpy as np
from PIL import Image
import pandas as pd
import sys
import math
from transformers import CLIPTextModel, CLIPTokenizer, CLIPTextConfig, logging
from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNet2DConditionModel
from safetensors.torch import load_file, save_file
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
import subprocess
import cv2

# 追加したコード
!pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchtext torchaudio torchdata --extra-index-url https://download.pytorch.org/whl/cu118

これでエラーを対処できます

スポンサーリンク

参考

github.com