环境配置
官网/博客合集
清华镜像站
anaconda官网
pytorch官网
pytorch历史库
官网pytorch与cuda对应版本下载
博客torch与torchvision与python对应关系
python与pytorch对应关系
环境相关
创建环境
conda create --n unet python=3.9
激活环境
conda activate unet
退出环境
conda deactivate
删除环境
conda remove -n unet --all
检查环境冲突
pip check
安装相关
安装requirements
pip install -r requirements.txt
安装时显示进度条
pip install -r requirements.txt --progress-bar on
conda安装
指定版本加=
conda install torch=2.1.0
conda卸载
conda remove torch
pip安装
指定版本加==
pip install torch==2.1.1
如何下载老版本?pip 后面加上这一段
-f https://download.pytorch.org/whl/torch_stable.html
想换清华源
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip卸载
pip uninstall torch
镜像源相关
pip源
pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
conda源
conda config --remove-key channels
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
清除源
->恢复默认
conda config --remove-key channels
系统或conda相关
更新conda环境
conda update -all
清除缓存
conda clean --all
pip导出
导出环境为txt
pip freeze > requirements.txt
pip导入
用txt导入环境依赖
pip install -r requirements.txt
conda导出
导出为yml
conda envname export > requirements.yml
conda导入
用yml文件导入环境依赖
conda env create -n condm2 -f requirements.yml
检查与查看相关
查看指定包
conda search name
查看现有包
conda list
查看显卡环境
nvidia-smi
查看cuda版本
nvcc -V
torch是否可用
python
import torch
#查看cuda是否可用
print(torch.__version__)
#当前torch版本
print(torch.cuda.is_available())
#查看cuda设备的数量
print(torch.cuda.device_count())
#查看当前使用的cuda编号
print(torch.cuda.current_device())
#查看GPU设备名字
print(torch.cuda.get_device_name())
#查看设备容量
print(torch.cuda.get_device_capability(0))
#查看算力
cuda是否可用
nvcc -V
python
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.cuda.device_count())
print(torch.version.cuda)
TensorFlow是否可用
先python进入编辑页面
python
import tensorflow as tf
print("TensorFlow Version:", tf.__version__)
print("Available devices: ", tf.config.experimental.list_physical_devices())
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
快速安装指令
创建环境 python3.9 cu118
conda create -n new python=3.9
conda activate new
conda update --all
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
conda install tensorflow-gpu=2.6
conda install cudatoolkit=11.3
conda install cudnn=8.2
pip install numpy==1.23.4
每个模块参数量
from collections import defaultdict
def summarize_model_parameters(model):
module_param_count = defaultdict(int)
for name, param in model.named_parameters():
if not param.requires_grad:
continue
# 获取顶层模块名,比如:backbone.encoder.layer0.conv
parts = name.split('.')
if len(parts) > 1:
top_module = parts[0]
else:
top_module = 'root'
# 统计当前参数的数量
module_param_count[top_module] += param.numel()
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"\n🔍 模型总参数量:{total_params / 1e6:.2f} M\n")
print("📊 各模块参数量统计如下:")
sorted_modules = sorted(module_param_count.items(), key=lambda x: -x[1])
for mod, count in sorted_modules:
print(f" {mod.ljust(20)}: {count / 1e6:.3f} M ({count:,} params)")
然后在运行那里,
summarize_model_parameters(model)
每个模块运行时间
在train的epoch里面
with torch.autograd.profiler.profile(use_cuda=True) as prof:
mask_pred, fg_pred, bg_pred, uc_pred = model(x)
# ✅ 打印最耗时的函数
print(prof.key_averages().table(sort_by="cuda_time_total", row_limit=30))
break # ❗️为了加快测试,只运行一批(你可以先只跑 1 个 batch)
余弦退火
顶端
from torch.utils.tensorboard import SummaryWriter
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
后面
scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=2, eta_min=1e-6)
linux下的一些指令
alien -i xx.rpm
CUDA_VISIBLE_DEVICES=7 python train.py
指定gpu运行
sudo su
换root
共享路径
cd /mnt/hgfs
ls
刷新环境
source ~/.bashrc # 对于 bash 用户
查看回收站
du -sh .Trash-0
rm -rf .Trash-0/*
conda --version
下载miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sha256sum Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh