PyTorch深度学习(29)Pytorch code importance content
Pytorch 代码重要内容
1、pytorch-msssim
pip install pytorch-msssim 安装使用ssim计算结构相似性损失
2、torch
torch.seed() 设置随机种子后,每次运行文件输出结果都一样,而不是每次随机函数生成的结果一样
torch.manual_seed() 设置CPU生成随机数的种子,方便下次复现实验结果 torch.squeeze() 对数据的维度进行压缩,去掉维数为1的维度,参数为在哪个地方加维度 -1是在最后一个维度处加维度,0为在第一个维度处加维度
torch.unsqueeze() 对数据维度进行扩充 torch.stack 在指定维度对元素叠加 等待当前设备上所有流中的所有核心完成
pytorch中,程序的执行是异步的 等待GPU上所有任务都完成
torch.cuda.synchronize()
3、numpy
numpy不能读取CUDA tensor 需要将它转化为 CPU tensor
xxx.data.numpy() --> xxx.data.cpu().numpy()
4、operation
from torch.optim import Adam, lr_scheduler
lr_scheduler
· step:学习率调整步长,每经过step_size学习率更新一次
· gamma:学习率调整倍数
· last_epoch:上一个epoch数,用于指示学习率是否需要调整,当last_epoch符合设定的间隔时就会调整学习率。当设置为-1时,学习率设置为初始值。
5、matplotlib
matplotlib.use('agg') 控制绘图不显示
6、nn.module
m.weight.data.normal_ 设置模型权重值和偏置值
m.bias.data.normal_
m.weight.data.fill_(1) 模型参数初始化1
m.bias.data.zero_() 模型参数初始化为0
7、遇到问题
1、plt显示灰度图出现Invalid shape问题
问题背景:tensorflow2.0.0中的Dataset转换为numpy,利用matplotlib进行显示。data的维度为[4, 256, 256, 1]
2、问题:'int' object has no attribute 'value'
可能是tensorflow版本的问题
解决方法: 将in_channels = inputs.shape[-1].value改为 in_channels = inputs.shape.as_list()[-1]
或者直接将 .value 去掉试试
3、问题:Tensorflow加载模型报错 Cannot assign a device for operation...
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config=config) as sess:
saver.restore(sess, "model-xxxx")
4、问 题:''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' 错误分析
这是tensorflow 一个经常性错误,错误的原因在于:显卡内存不够。
解决方法就是降低显卡的使用内存,途径有以下几种措施:
- 减少Batch 的大小
- 分析错误的位置,在哪一层出现显卡不够,比如在全连接层出现的,则降低全连接层的维度,把2048改成1042啥的
- 增加pool 层,降低整个网络的维度。
- 修改输入图片的大小
imshow cmap
'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'
8、命令行
(1)、下载anaconda,并安装
在Anaconda目录中,打开Anaconda Prompt
分别输入python 及 conda
进入anaconda安装文件夹,同时按住shift和右键,打开Powershell窗口
输入jupyter notebook,如打不开,则需要复制 or copy and paste one of these URLs下的网址链接
new——> Python3 shift+enter键可运行代码
(2)、安装pytorch环境
conda create -n pytorch python=3.8
(3)、安装环境后,切换环境
conda activate pytorch
查看环境中的包列表
pip listpytorch官网
https://pytorch.org/
NV显卡支持cuda
https://www.geforce.cn/hardware/technology/cuda/supported-gpuscmd
nvidia-smi 查看驱动是否满足(需要终端以 管理员身份运行) 查看显卡驱动中的CUDA版本https://blog.csdn.net/zzq060143/article/details/88042075
1、首先是添加清华镜像channel
2、安装的指令,主要是把官网指令后面的-c pytorch删掉,-c pytorch的意思是,
安装下载的channel强制为pytorch官网的channel。所以需要删除才能走清华镜像的channel清华源安装
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# reference
# https://mirror.tuna.tsinghua.edu.cn/help/anaconda/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
(4)、最后执行安装pytorch的命令即可 安装pytorch命令行
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge
执行上方命令后即安装了torch,不需要再次安装
(5)、安装成功后运行python,进入python命令台
输入import torch,如果出现缺少包的情况,则pip install xxx 例如:pip install numpy
torch.cuda.is_available()——>输出:true cude安装成功
9、相关命令
Python文件:代码以块为一个整体运行,Python文件的块是所有行的代码
优点:通用,传播方便,适用于大型项目
缺点:需要从头运行
Python控制台:以任意行为块运行
优:显示每个变量属性 缺:不利于代码阅读及修改
Jupyter:以任意行为块进行
优:利于代码阅读及修改 缺:环境需要配置
(1)安装、创建命令
pip install opencv-python 安装cv2包
pip install tensorboard 安装tensorboard (x轴步长,y轴值)
tensorboard --logdir=logs 事件文件所在文件夹名称
tensorboard --logdir=logs --port=6007 修改端口为6007Settings——Editor——General——Code Completion代码补全——Match case匹配大小写
Ctrl + O 重写方法 ctrl+l 实现方法
conda create -n pytorch python=3.8 创建环境
conda update -n base -c defaults conda 更新conda
(2)查看conda中环境
conda env list conda环境列表
conda info --envs 查看环境
conda remove --name env_name --all 删除环境
conda remove --name env_name package_name 删除环境中某个包
conda deactivate 环境名 关闭环境python -m pip install --upgrade pip 更新pip
python --version 查看python版本
pip install -U numpy 升级numpy
安装tensorflow GPU版本(使用源下载速度更快)
pip install tensorflow-gpu -i https://pypi.doubanio.com/simple 默认安装的是2.6.0版本pip uninstall tensorflow tensorflow-gpu 卸载tensorflow
pip install tensorflow-gpu==2.4.0 -i https://pypi.doubanio.com/simple
(3)使用 PyTorch 查看 CUDA 和 cuDNN 版本
import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.backends.cudnn.version())conda config --remove-key channels 恢复Anaconda源为默认
(4)查看tensorflow版本及是否是gpu版本
import tensorflow as tf
tf.__version__ # 此命令为获取安装的tensorflow版本
print(tf.__version__) # 输出版本
tf.test.is_gpu_available()from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())缺失cudnn64_7.dll文件
安装了cudnn8.0以上版本以后,有时会出现报错Could not load dynamic library ‘cudnn64_7.dll’; dlerror: cudnn64_7.dll not found。这是因为cudnn8.0以上缺失cudnn64_7.dll文件。
解决方法:把C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin文件夹下的‘cudnn64_8.dll’复制一份并命名为为‘cudnn64_7.dll。’
(5)cmd中
where python python的存放目录
where ipython python交互工具
where pip pip的位置目录pip install cupy-cuda110
pip install cupy-cuda110==7.8.0
conda create -n chainer python==3.7
conda install -c conda-forge cupy cudatoolkit=10.1
python import cupy
pip install chainer
python print(chainer.backends.cuda.available) print(chainer.backends.cuda.cudnn_enabled)import chainer
chainer.backends.cuda.available
10、将py文件打包为exe文件
pip install pyinstaller
如果比较慢可以换清华源pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller执行打包命令:pyinstaller -F xx/xx/xx.py
备注:如果是根目录下的.py文件,无需设置目录最后,生成的exe文件会在dist文件夹中,同时生成有build文件夹
想打包为独立单一的.exe文件,使用-F参数
pyinstaller -F myscript.py默认生成的执行文件执行时,会弹出一个命令行的窗口,如果不需要弹出,可以再加个-w参数
pyinstaller -F -w myscript.pypyInstaller -F -w -p /dir1/;/dir2/ -i /dir3/icon.ico ***.py
**遇到问题**
ModuleNotFoundError
**重点:需要打包引入自定义库文件夹的父类文件夹**
**主程序所在文件目录下进行打包**默认模块:会在site-packages
自己指定的目录:通过-P DIR DIR为需要的模块路径 如pyinstaller -F -w -p DIR myscript.py
多个模块路径:pyinstaller -p DIR1:DIR2:DIR3 myscript.py或pyinstaller -p DIR1 -p DIR2 -p DIR3 myscripy.py
就用分号隔开如:pyinstaller -F -p E:\python\clpicdownload\venv\Lib\site-packages;E:\python\clpicdownload\venv\Lib\site-packages mypython.py有时候并不是找不到模块,而是代码中有些模块是隐含导入的,这样的话就需要指出这些模块,才能正确的打包,可以在命令行打包时使用参数--hidden-import MODULENAME指定模块名,或者可以在打包后生成的myscript.spec文件中修改,该文件中有个参数hiddenimports=[]
配置为:
hiddenimports=['cython','sklearn','sklearn.ensemble','sklearn.tree._utils','scipy._lib.messagestream']
然后再运行以下命令
pyinstaller myscript.spec
PyTorch深度学习(29)Pytorch code importance content相关推荐
- [PyTorch] 深度学习框架PyTorch中的概念和函数
Pytorch的概念 Pytorch最重要的概念是tensor,意为"张量". Variable是能够构建计算图的 tensor(对 tensor 的封装).借用Variable才 ...
- 【项目实战】vue-springboot-pytorch前后端结合pytorch深度学习 html打开本地摄像头 监控人脸和记录时间
是一个项目的一个功能之一,调试了两小时,终于能够 javascript设置开始计和暂停计时 监控人脸 记录时间了 效果图: 离开页面之后回到页面会从0计时(不是关闭页面,而是页面失去焦点) 离开摄像头 ...
- pytorch深度学习_用于数据科学家的深度学习的最小pytorch子集
pytorch深度学习 PyTorch has sort of became one of the de facto standards for creating Neural Networks no ...
- pytorch深度学习_了解如何使用PyTorch进行深度学习
pytorch深度学习 PyTorch is an open source machine learning library for Python that facilitates building ...
- 深度学习准「研究僧」预习资料:图灵奖得主Yann LeCun《深度学习(Pytorch)》春季课程...
视学算法报道 编辑:蛋酱 转载自公众号:机器之心 开学进入倒计时,深度学习方向的准「研究僧」们,你们准备好了吗? 转眼 2020 年已经过半,又一届深度学习方向的准研究生即将踏上「炼丹」之路.对于这一 ...
- pytorch | 深度学习分割网络U-net的pytorch模型实现
原文:https://blog.csdn.net/u014722627/article/details/60883185 pytorch | 深度学习分割网络U-net的pytorch模型实现 这个是 ...
- 一篇文章入门深度学习框架PyTorch
一篇文章入门深度学习框架PyTorch 1 Tensor(张量) 2 Variable(变量) 3 Dataset(数据集) 4 nn.Module(模组) 5 torch.optim(优化) 一阶优 ...
- pytorch深度学习_深度学习和PyTorch的推荐系统实施
pytorch深度学习 The recommendation is a simple algorithm that works on the principle of data filtering. ...
- 深度学习三(PyTorch物体检测实战)
深度学习三(PyTorch物体检测实战) 文章目录 深度学习三(PyTorch物体检测实战) 1.网络骨架:Backbone 1.1.神经网络基本组成 1.1.1.卷积层 1.1.2.激活函数层 1. ...
- 深度学习二(Pytorch物体检测实战)
深度学习二(Pytorch物体检测实战) 文章目录 深度学习二(Pytorch物体检测实战) 1.PyTorch基础 1.1.基本数据结构:Tensor 1.1.1.Tensor数据类型 1.1.2. ...
最新文章
- IOS中的JSON数据的解析
- A02_Python(基本数据类型,容器,函数,类),Numpy(数组array,数组索引,数据类型,数组中的数学,广播)
- shell 函数的高级用法
- 技术人员兼职招聘平台HourlyHerd融资2200万美元C轮融资
- 几个可能会用到的前端库
- 执行php程序的时候,报错Allowed memory size of 134217728 bytes exhausted (tried to allocate 83 bytes)...
- 基于python的测试报告自动化生成
- python中的多线程(一)
- Linux下nand flash读写测试
- 万能地图下载器的不同授权在矢量地图下载功能上有哪些区别?
- 1.7 COMS边沿触发器
- C 类IP 地址,连接6 个子公司
- cad的计算机功能,CAD的基本功能有哪些
- 薅羊毛!企业微信自动加好友工具免费使用机会!
- 探讨!自媒体的推荐机制提高百家号阅读收益方法!
- 移动硬盘3.5寸和2.5寸的区别
- Dice Loss与mIoU
- linux系统下修改hosts文件的权限
- Webshell文件上传漏洞
- 计算机管理员 职称业绩登记,任现职前主要专业技术职务工作业绩登记完整版(30页)-原创力文档...