天天看點

【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識

目錄

  • 環境搭建
  • 代碼解釋
  • 其他知識
    • 1. torchvision
    • 2. PIL:Python Image Library
    • 3. torch

環境搭建

個人記錄文檔,電腦系統為win11
  1. 安裝conda和pycharm,cuda,cuDNN以及pytorch

    conda和pychram安裝不再贅述

    cuDNN:

    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識
    >複制解壓後的cdDNN檔案到cuda的相關安裝目錄即可,之後檢視是否安裝成功cuDNN
    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識
    conda建立好環境之後安裝pytorch
conda create -n pytorch_gpu python=3.9 # 利用conda建立環境
# conda create --clone foo_env --name bar_env # 将foo_env的環境複制到新環境bar_env中
# 此時需檢視自己電腦安裝的cuda版本(2中有示例)
pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu102/torch_stable.html
           
  1. 檢視cuda的安裝版本和位置

    nvcc -V

    set cuda

    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識
    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識
  1. 顯示卡為NVIDIA GTX 1660 super
    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識
  2. 其他電腦設定如下:
    【算法01】pycharm + win11 + conda中利用pytorch實作CNN環境搭建代碼解釋其他知識

代碼解釋

其他知識

1. torchvision

構成:

  • torchvision.datasets:常用的資料集接口
  • torchvision.models:常用的模型結構,譬如AlexNet,VGG, ResNet
  • torchvision.transforms:圖檔變換
    • transforms.CenterCrop 對圖檔中心進行裁剪

      transforms.ColorJitter 對圖像顔色的對比度、飽和度和零度進行變換

      transforms.FiveCrop 對圖像四個角和中心進行裁剪得到五分圖像

      transforms.Grayscale 對圖像進行灰階變換

      transforms.Pad 使用固定值進行像素填充

      transforms.RandomAffine 随機仿射變換

      transforms.RandomCrop 随機區域裁剪

      transforms.RandomHorizontalFlip 随機水準翻轉

      transforms.RandomRotation 随機旋轉

      transforms.RandomVerticalFlip 随機垂直翻轉

  • torchvision.utils:其他操作

2. PIL:Python Image Library

是Python 語言的一個第三方庫,支援圖像存儲、顯示和處理,能夠處理幾乎所有格式的圖檔。

3. torch

  • 3.1 torch:生成随機數字的tensor,滿足标準正态分布(0~1)
print(torch.randn(3),'\n',
      torch.randn(3, 4),'\n',
      torch.randn(3, 4, 5))
###################################
tensor([ 0.2965, -1.9862,  0.6845]) 
 tensor([[ 0.0102, -0.7910,  0.2807,  0.2399],
        [ 0.0896, -0.4920,  1.1830,  1.0532],
        [-0.3168, -0.5191, -0.2676,  0.0547]]) 
 tensor([[[ 0.3623, -0.0488, -0.9397,  2.0493, -0.4924],
         [-0.8506,  0.0435,  0.1077,  0.5010, -0.6206],
         [-0.0964,  1.2366,  1.2035,  1.1832, -1.0772],
         [ 0.7585, -2.2725, -0.7748, -0.6995,  1.4313]],

        [[-0.8132,  0.3222, -2.0298, -0.8955, -1.5885],
         [-1.9234, -0.2824, -2.0615,  2.4361,  1.0798],
         [-0.7023,  0.8195, -0.5983,  1.6142, -0.5253],
         [-0.3071, -1.3893,  0.7453,  0.0168, -0.8236]],

        [[-0.5623,  0.5198, -0.1727,  0.0726,  0.9915],
         [ 1.7734,  0.4547, -0.1993,  0.3765, -0.4798],
         [ 0.0041,  0.0529,  1.1025,  1.3861, -0.1418],
         [-0.4634,  1.1942,  0.6051,  0.3423,  0.2027]]])

           
  • 3.2 torch.nn
  • 3.3 torch.cuda
  • 3.4 torch.sparse
torch.sparse_coo_tensor(indices, values,
					    size=None, *,
					    dtype=None,
					    device=None,
					    requires_grad=False)

***
待更新

           

繼續閱讀