# 深度学习环境配置后的设备与软件检查指南
## 一、硬件设备检查
### 1.1 GPU配置验证
深度学习环境的核心是GPU,以下是检查GPU配置的详细步骤:
```python
import torch
import subprocess
import platform
def check_gpu_config():
"""检查GPU配置信息"""
print("=== GPU配置检查 ===")
# 检查CUDA是否可用
if torch.cuda.is_available():
print(f"✓ CUDA可用")
print(f" CUDA版本: {torch.version.cuda}")
print(f" 可用GPU数量: {torch.cuda.device_count()}")
# 检查每个GPU的详细信息
for i in range(torch.cuda.device_count()):
print(f" GPU {i}: {torch.cuda.get_device_name(i)}")
print(f" 显存总量: {torch.cuda.get_device_properties(i).total_memory / 1024**3:.2f} GB")
else:
print("✗ CUDA不可用,检查驱动安装")
# 使用nvidia-smi获取更详细信息
try:
result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
if result.returncode == 0:
print("\n✓ nvidia-smi命令可用")
print("GPU驱动和工具包安装正常")
else:
print("\n✗ nvidia-smi命令执行失败")
except FileNotFoundError:
print("\n✗ nvidia-smi未找到,请安装NVIDIA驱动")
check_gpu_config()
```
### 1.2 内存与存储检查
```python
import psutil
import os
def check_system_resources():
"""检查系统资源"""
print("\n=== 系统资源检查 ===")
# 内存检查
memory = psutil.virtual_memory()
print(f"内存总量: {memory.total / 1024**3:.2f} GB")
print(f"可用内存: {memory.available / 1024**3:.2f} GB")
# 存储检查
disk = psutil.disk_usage('/')
print(f"磁盘总量: {disk.total / 1024**3:.2f} GB")
print(f"可用磁盘: {disk.free / 1024**3:.2f} GB")
# CPU信息
print(f"CPU核心数: {psutil.cpu_count(logical=False)} 物理核心")
print(f"逻辑处理器: {psutil.cpu_count(logical=True)} 个")
check_system_resources()
```
## 二、操作系统环境检查
### 2.1 系统基本信息
```bash
# 检查操作系统版本
cat /etc/os-release # Linux
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" # Windows
# 检查系统架构
uname -m # Linux
echo %PROCESSOR_ARCHITECTURE% # Windows
```
### 2.2 环境变量检查
```python
import os
def check_environment_variables():
"""检查关键环境变量"""
print("\n=== 环境变量检查 ===")
critical_vars = {
'PATH': '系统路径',
'CUDA_PATH': 'CUDA安装路径',
'PYTHONPATH': 'Python模块路径',
'LD_LIBRARY_PATH': '动态库路径(Linux)'
}
for var, description in critical_vars.items():
value = os.getenv(var)
if value:
print(f"✓ {var}({description}): 已设置")
else:
print(f"⚠ {var}({description}): 未设置")
check_environment_variables()
```
## 三、深度学习框架检查
### 3.1 PyTorch环境验证
参考[ref_1]中提到的PyTorch环境配置,进行完整性检查:
```python
def check_pytorch_environment():
"""检查PyTorch环境完整性"""
print("\n=== PyTorch环境检查 ===")
# 检查PyTorch版本
print(f"PyTorch版本: {torch.__version__}")
# 检查基本功能
try:
# 创建测试张量
x = torch.randn(3, 3)
y = torch.randn(3, 3)
z = torch.matmul(x, y)
print("✓ 基础张量运算正常")
# GPU运算测试(如果可用)
if torch.cuda.is_available():
x_gpu = x.cuda()
y_gpu = y.cuda()
z_gpu = torch.matmul(x_gpu, y_gpu)
print("✓ GPU运算正常")
except Exception as e:
print(f"✗ PyTorch功能异常: {e}")
check_pytorch_environment()
```
### 3.2 依赖库检查
```python
def check_dependencies():
"""检查关键依赖库"""
print("\n=== 依赖库检查 ===")
dependencies = [
'numpy', 'pandas', 'matplotlib', 'opencv-python',
'PIL', 'scipy', 'scikit-learn', 'tqdm'
]
for dep in dependencies:
try:
__import__(dep)
print(f"✓ {dep}: 已安装")
except ImportError:
print(f"✗ {dep}: 未安装")
check_dependencies()
```
## 四、应用软件配置检查
### 4.1 开发环境检查
```python
def check_development_tools():
"""检查开发工具"""
print("\n=== 开发工具检查 ===")
# Python环境
import sys
print(f"Python版本: {sys.version}")
print(f"Python路径: {sys.executable}")
# 包管理器
try:
import pip
print(f"pip版本: {pip.__version__}")
except ImportError:
print("✗ pip未正确安装")
# Conda环境(如果使用)
try:
import conda
print("✓ Conda环境可用")
except ImportError:
print("⚠ Conda未安装或未激活")
check_development_tools()
```
### 4.2 特定应用配置检查
参考[ref_6]中提到的Mask R-CNN改进模型实现,检查计算机视觉相关配置:
```python
def check_cv_environment():
"""检查计算机视觉环境"""
print("\n=== 计算机视觉环境检查 ===")
cv_libraries = [
('opencv-python', 'cv2', '图像处理'),
('Pillow', 'PIL', '图像IO'),
('torchvision', 'torchvision', '视觉模型')
]
for pkg_name, import_name, description in cv_libraries:
try:
__import__(import_name)
print(f"✓ {description}({pkg_name}): 正常")
except ImportError as e:
print(f"✗ {description}({pkg_name}): 异常 - {e}")
check_cv_environment()
```
## 五、性能基准测试
### 5.1 GPU性能测试
```python
def benchmark_gpu_performance():
"""GPU性能基准测试"""
print("\n=== GPU性能测试 ===")
if not torch.cuda.is_available():
print("GPU不可用,跳过性能测试")
return
# 矩阵乘法性能测试
size = 4096
a = torch.randn(size, size, device='cuda')
b = torch.randn(size, size, device='cuda')
import time
start_time = time.time()
# 执行多次矩阵乘法
for _ in range(10):
c = torch.matmul(a, b)
torch.cuda.synchronize() # 等待GPU完成
end_time = time.time()
elapsed = end_time - start_time
print(f"GPU矩阵乘法性能: {elapsed:.2f}秒 (10次 {size}x{size}矩阵乘法)")
# 显存使用测试
memory_allocated = torch.cuda.memory_allocated() / 1024**3
print(f"当前显存使用: {memory_allocated:.2f} GB")
benchmark_gpu_performance()
```
## 六、配置检查总结表
| 检查类别 | 检查项目 | 状态 | 建议 |
|---------|---------|------|------|
| 硬件配置 | GPU可用性 | ✓/✗ | 确保NVIDIA驱动和CUDA正确安装 |
| 硬件配置 | 显存容量 | 数值 | 根据模型需求确保足够显存 |
| 硬件配置 | 系统内存 | 数值 | 建议≥16GB用于大型数据集 |
| 操作系统 | 环境变量 | ✓/✗ | 检查CUDA_PATH等关键变量 |
| 深度学习框架 | PyTorch版本 | 版本号 | 确保与CUDA版本兼容 |
| 深度学习框架 | GPU支持 | ✓/✗ | 验证torch.cuda.is_available() |
| 开发工具 | Python环境 | 版本号 | 建议Python 3.8+ |
| 依赖库 | 关键库安装 | ✓/✗ | numpy, opencv等必须库 |
| 性能 | GPU计算 | 时间 | 基准测试结果 |
## 七、常见问题排查
### 7.1 CUDA相关问题
```python
def troubleshoot_cuda_issues():
"""CUDA问题排查"""
print("\n=== CUDA问题排查 ===")
# 检查CUDA与PyTorch版本兼容性
cuda_version = torch.version.cuda
print(f"当前CUDA版本: {cuda_version}")
# 建议的版本匹配(根据实际情况调整)
version_compatibility = {
'11.7': ['1.13.0', '1.13.1'],
'11.8': ['2.0.0', '2.0.1'],
'12.1': ['2.1.0', '2.1.1']
}
if cuda_version in version_compatibility:
compatible_versions = version_compatibility[cuda_version]
print(f"建议PyTorch版本: {', '.join(compatible_versions)}")
else:
print("⚠ CUDA版本兼容性未知,建议查看官方文档")
troubleshoot_cuda_issues()
```
通过以上完整的检查流程,您可以全面了解深度学习环境的配置状态,确保硬件、操作系统和应用软件都正确配置并可用于深度学习项目的开发与实验。