模型部署的一些问题及其解决方案

# 1. 显示<PIL.Image.Image image mode=RGB size=512x512 at 0x7A12021134C0>图片 并保存

得到一个<PIL.Image.Image image mode=RGB size=512x512 at 0x7A12021134C0>的Image对象

import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()

image.save("./duck.png") #保存

# 2.连接Drive云盘

#@title 加载Google云端硬盘
from google.colab import drive
drive.mount('/content/drive')

# 3.杀掉colab进程

#@title 杀掉colab进程,会使页面报错,但是不用担心,继续运行即可,请勿跳过
import os
os.kill(os.getpid(), 9)

# 4.复制文件夹(从A位置移到B位置)

import shutil
shutil.copytree('APATH','BPATH')

# 5.查看Pytorch 版本

!pip show torch #查看torch版本
或
!python -c "import torch; print(torch.__version__)"

# 6.更新torchvision版本

!pip install torchvision==0.13.0
!pip install tomesd torchaudio torchdata torchtext xformers

# 7.查看CUDA版本(输出的未必是当前的CUDA,例如在Colab中有多个CUDA版本,真正对应应该查看Pytorch版本后面接的cu版本,例如 torch版本为12.1+cu121 这里的cu121就指定当前cuda版本需要12.1的)

!nvcc --version

# 8.解决torch版本错乱问题

https://pytorch.org/get-started/previous-versions/

# 9.使用nogrk

!wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
!tar -zxvf ./ngrok-v3-stable-linux-amd64.tgz
!./ngrok config add-authtoken 2W15Ge3mjHesAgooklex7YQmDu6_14X6DY_3GNJYMMzDEqvSSN22sPm9

!nohup ./ngrok http --domain=privately-exact-gopher.ngrok-free.app 7860 &

# 10.查看文件夹大小

!du -sh 文件夹路径

# 11.快速复制文件夹

cp -R 原位置 目的位置
cp -R /content/facefusion-unlocked /content/drive/MyDrive/facefusion

# 12. colab安装MiniConda


#@title 加载Google云端硬盘
from google.colab import drive
drive.mount('/content/drive')

import sys
sys.path.insert(0,'/content/drive/MyDrive/Colab Notebooks')

!wget -c https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
!chmod +x Miniconda3-latest-Linux-x86_64.sh
!bash ./Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local #-p indicates where you want to install

# 13.测试自己的torch是否可以用GPU

import torch

# Check if CUDA (GPU support) is available
if torch.cuda.is_available():
    print("GPU is available.")
    # Print the current GPU device
    print("Current GPU device:", torch.cuda.get_device_name(0))
else:
    print("GPU is not available. Running on CPU.")

如果不可用,先执行nvcc --version查看CUDA及其tools的版本,之后打开https://pytorch.org/get-started/previous-versions/,找到对应版本的torch安装命令

安装前要查看你的环境是conda还是非conda
conda就用里面Conda OSX的conda install ...的命令
非conda就用里面Wheel OSX的pip install ...的命令

如果安装之后torch版本没有改变,或者pip list查询的torch版本和pip show torch版本不一致,那可能是缓存的问题,执行pip cache purge代码清空缓存后在执行pip list和pip show torch查看

例如:
 1.我原本装的torch版本为2.1.0(通过pip list查询),运行上面代码后提示GPU is not available. Running on CPU.
 2.之后运行nvcc --version后查询得知我的CUDA版本为cuda_11.7。
 3.打开https://pytorch.org/get-started/previous-versions/网站,发现只有torch2.0.1版本及其以下才支持CUDA_11.7版本,且我用的是pip安装命令,故选择Wheel OSX下的安装命令
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2
 4.安装成功后重新运行上面代码即可
 

posted @ 2023-12-02 12:16  黄伟鸿  阅读(32)  评论(0编辑  收藏  举报