PyTorch deep learning (29) Pytorch code importance content

Important content of Pytorch code

1、pytorch-msssim

pip install pytorch-msssim installation uses ssim to calculate structural similarity loss

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 cannot read CUDA tensor and needs to convert it to CPU tensor

xxx.data.numpy() --> xxx.data.cpu().numpy()

4、operation

from torch.optim import Adam, lr_scheduler
lr_scheduler  
step: Learning rate adjustment step size, the learning rate is updated every step_size  
gamma: learning rate adjustment multiple  
last_epoch: last epoch number, used to indicate whether the learning rate needs to be adjusted, when The learning rate is adjusted when last_epoch meets the set interval. When set to -1, the learning rate is set to the initial value.  

5、matplotlib

matplotlib.use('agg') controls plots not displayed  

6、nn.module

m.weight.data.normal_ Set model weight and bias values  
​​m.bias.data.normal_  
m.weight.data.fill_(1) Model parameter initialization 1  
m.bias.data.zero_() Model parameter initialization to 0   

7. Encountered problems

1. Invalid shape problem in plt display grayscale image

Problem background: Dataset in tensorflow2.0.0 is converted to numpy and displayed using matplotlib. The dimension of data is [4, 256, 256, 1]

2、问题:'int' object has no attribute 'value'

It may be a tensorflow version problem.
Solution: Change in_channels = inputs.shape[-1].value to in_channels = inputs.shape.as_list()[-1]
or directly remove .value and try

3. Problem: Tensorflow loads the model and reports an error 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. Question: ''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' error analysis

This is a frequent error of tensorflow, and the reason for the error is that the memory of the graphics card is not enough.
The solution is to reduce the memory used by the graphics card. There are several ways to do this:

  1. Reduce batch size
  2. Analyze the location of the error, in which layer the graphics card is not enough, for example, if it appears in the fully connected layer, reduce the dimension of the fully connected layer, change 2048 to 1042 or something
  3. Increase the pool layer and reduce the dimension of the entire network.
  4. Modify the size of the input image

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. Command line

 (1), download anaconda, and install

In the Anaconda directory, open Anaconda Prompt

and enter python and conda respectively
to enter the anaconda installation folder, hold down shift and right button at the same time, open the Powershell window
and enter jupyter notebook, if it cannot be opened, you need to copy or copy and paste one of these URLs under URL link
new --> Python3 shift+enter key to run the code

(2), install the pytorch environment

conda create -n pytorch python=3.8 

(3) After installing the environment, switch the environment 

conda activate pytorch
to view the package list in the environment
pip list


pytorch official website
https://pytorch.org/
NV graphics card supports cuda
https://www.geforce.cn/hardware/technology/cuda/supported-gpus


cmd
nvidia-smi view driver Whether it is satisfied (requires the terminal to run as an administrator) Check the CUDA version in the graphics card driver


https://blog.csdn.net/zzq060143/article/details/88042075
1. First, add the Tsinghua mirror channel
2. The installation instructions, mainly It is to delete the -c pytorch after the official website command. -c pytorch means that the
channel for installation and download is forced to be the channel of the pytorch official website. So you need to delete it to go to the channel mirrored by Tsinghua University

清华源安装
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), and finally execute the command to install pytorch to install the pytorch command line

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge
After executing the above command, torch is installed, no need to install it again

(5) After the installation is successful, run python and enter the python command console

Enter import torch, if there is a missing package, then pip install xxx For example: pip install numpy

torch.cuda.is_available() --> output: true cude installation is successful

9. Related commands 

Python file: The code runs in blocks as a whole, and the block of the Python file is the code of all lines.
Advantages: Universal, easy to spread, suitable for large projects
Disadvantages: Need to run
Python console from scratch: Run with arbitrary behavior blocks
Advantages: Display each Lack of variable attributes: not conducive to code reading and modification
Jupyter: use arbitrary behavior blocks
Excellent: conducive to code reading and modification Lack of: the environment needs to be configured

(1) Install and create commands 

pip install opencv-python install cv2 package

pip install tensorboard install tensorboard (x-axis step size, y-axis value)
tensorboard --logdir=logs event file folder name
tensorboard --logdir=logs --port=6007 modify port to 6007


Settings——Editor——General——Code Completion code completion——Match case matching case

Ctrl + O Rewrite method ctrl+l Implementation method

conda create -n pytorch python=3.8 Create environment

conda update -n base -c defaults conda update conda

(2) View the environment in conda

conda env list conda environment list
conda info --envs view environment
conda remove --name env_name --all delete environment
conda remove --name env_name package_name delete a package in environment
conda deactivate environment name close environment

python -m pip install -- upgrade pip update pip

python --version view python version

pip install -U numpy upgrade numpy

install tensorflow GPU version (use source to download faster)
pip install tensorflow-gpu -i https://pypi.doubanio.com/simple default installation The version is 2.6.0

pip uninstall tensorflow tensorflow-gpu uninstall tensorflow
pip install tensorflow-gpu==2.4.0 -i https://pypi.doubanio.com/simple 

(3) Use PyTorch to view CUDA and cuDNN versions

import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.backends.cudnn.version())

conda config --remove-key channels restore Anaconda source to default

(4) Check tensorflow version and whether it is gpu version

import tensorflow as tf
tf.__version__ # This command is to get the installed tensorflow version
print(tf.__version__) # Output version
tf.test.is_gpu_available()

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

missing cudnn64_7 .dll file
After installing cudnn8.0 or above, sometimes an error message "Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found will appear. This is because the cudnn64_7.dll file is missing above cudnn8.0.
Solution: Copy the 'cudnn64_8.dll' under the C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin folder and name it as 'cudnn64_7.dll. '

(5) in cmd

where python python storage directory
where ipython python interactive tool
where pip pip location directory

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. Package the py file into an exe file

If pip install pyinstaller  
is slow, you can change to Tsinghua source pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller  

executes the packaging command: pyinstaller -F xx/xx/xx.py  
Note: If it is the root directory The .py file below does not need to set the directory.  

Finally, the generated exe file will be in the dist folder. At the same time, there is a build folder  

to be packaged as an independent single .exe file. Use the -F parameter  
pyinstaller -F myscript.py  

to generate by default When the executable file is executed, a command line window will pop up. If you don’t need to pop up, you can add the -w parameter  
pyinstaller -F -w myscript.py  

pyInstaller -F -w -p /dir1/;/dir2/ -i /dir3/icon.ico ***.py

**Encountered a problem**  
ModuleNotFoundError  
**Important: Need to package and import the parent class folder of the custom library folder**  
**Package in the file directory where the main program is located**


Default module: the directory specified in site-packages  
: pass -P DIR DIR to the required module path, such as pyinstaller -F -w -p DIR myscript.py  
Multiple module paths: pyinstaller -p DIR1:DIR2:DIR3 myscript.py or pyinstaller -p DIR1 -p DIR2 -p DIR3 myscripy.py  
are separated by semicolons such as: pyinstaller -F -p E:\python\clpicdownload\ venv\Lib\site-packages;E:\python\clpicdownload\venv\Lib\site-packages mypython.py

Sometimes it is not that the module cannot be found, but some modules in the code are implicitly imported, so you need Only by pointing out these modules can they be packaged correctly. You can use the parameter --hidden-import MODULENAME to specify the module name when packaging on the command line, or you can modify it in the myscript.spec file generated after packaging. There is a parameter hiddenimports=[ ]

configured as:
hiddenimports=['cython','sklearn','sklearn.ensemble','sklearn.tree._utils','scipy._lib.messagestream']
and then run the following command
pyinstaller myscript.spec

Guess you like

Origin blog.csdn.net/jiangyangll/article/details/127601586