py3->py2,GPU->CPU,tensorboardX,环境搭建。

  1. py3和py2基础语法不同,首先加入下面future模块
from __future__ import print_function
from __future__ import division 
from __future__ import absolute_import 
from __future__ import with_statement

或者更懒一点加入

import  __future__ 
  1. 关于训练好的显卡模型迁移到cpu上,
    可能的报错
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location='cpu' to map your storages to the CPU.
torch.load(opt.model,map_location='cpu')

更多关于模型迁移cpu,gpu环境的请参考

  1. 离线安装requirement.txt中的依赖
 pip install --no-index --find-links modules_download -r requirements.txt
  1. pip批量导出包含环境中所有组件的requirements.txt文件
pip freeze > requirements.txt
  1. pip批量安装requirements.txt文件中包含的组件依赖
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
  1. conda批量导出包含环境中所有组件的requirements.txt文件
conda list -e > requirements.txt
  1. pip批量安装requirements.txt文件中包含的组件依赖
conda install --yes --file requirements.txt

参考

  1. 删除默认源地址
conda config --remove-key channels
  1. 跨平台下载可能用到
macos版: --platform macosx-10_10_x86_64

Linux版:--platform linux_x86_64

Windows版: --platform windows_x86_64
  1. tensorflowboardX用法
tensorboard --logdir=./log --port=6006
# 在浏览器输入:localhost: port 访问
  1. pip离线安装依赖
pip install  --no-index --find-links=./py2 -r requirements.txt
  1. 同时有py2和py3安装依赖到指定的环境中
# 先安装pip
curl https://bootstrap.pypa.io//get-pip.py -o get-pip.py   #先下载pip脚本
sudo python  get-pip.py
sudo python3 get-pip.py

python2 -m  pip install pkg_name
python3 -m  pip install pkg_name
  1. GPU迁移CPU环境时候model.load_state_dict()报错
# 原因是静态字典导入不进去,字典的键在GPU下带module.开头
# 解决方法:
# original saved file with DataParallel
state_dict = torch.load('myfile.pth.tar')
# create new OrderedDict that does not contain `module.`
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
    name = k[7:] # remove `module.`
    new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)

# 或者
model.load_state_dict({k.replace('module.',''):v for k,v in torch.load('checkpoint.pt').items()})

参考
14. 服务器双显卡,有一卡被tensorflow任务占用
,pytorch中指定GPU 可能会失效,可以用以下命令强制。

CUDA_VISIBLE_DEVICES=0 python3 main.py  1>log/3labels/adam_bs4_lr0.01_20191121.txt 2>&1 &
  1. pytorch常见 .cpu().numpy()原因。
1. CPU tensor转GPU tensor:
cpu_imgs.cuda()

2. GPU tensor 转CPU tensor:
gpu_imgs.cpu()

3. numpy转为CPU tensor:
torch.from_numpy( imgs )

4.CPU tensor转为numpy数据:
cpu_imgs.numpy()

5. note:GPU tensor不能直接转为numpy数组,必须先转到CPU tensor。
6. 如果tensor是标量的话,可以直接使用 item() 函数(只能是标量)将值取出来:
  1. 禁用GPU
os.environ["CUDA_VISIBLE_DEVICES"]="-1"  
#若只禁用第二块
Setting CUDA_VISIBLE_DEVICES to 0,2,-1,1 causes devices 0 and 2 to be visible and device 1 to be invisible.
  1. 显示设备信息
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
[name: "/cpu:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 2215045474989189346
, name: "/gpu:0"
device_type: "GPU"
memory_limit: 6787871540
locality {
  bus_id: 1
}
incarnation: 13663872143510826785
physical_device_desc: "device: 0, name: GeForce GTX 1080, pci bus id: 0000:02:00.0"
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值