<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          重磅!Pytorch寶典來(lái)了!

          共 45532字,需瀏覽 92分鐘

           ·

          2021-03-13 15:30

          來(lái)源:Jack Cui        作者:科技猛獸

          一、基本配置

          導(dǎo)入包和版本查詢:

          import torch
          import torch.nn as nn
          import torchvision
          print(torch.__version__)
          print(torch.version.cuda)#cuda版本查詢
          print(torch.backends.cudnn.version())#cudnn版本查詢
          print(torch.cuda.get_device_name(0))#設(shè)備名

          可復(fù)現(xiàn)性

          在硬件設(shè)備(CPU、GPU)不同時(shí),完全的可復(fù)現(xiàn)性無(wú)法保證,即使隨機(jī)種子相同。但是,在同一個(gè)設(shè)備上,應(yīng)該保證可復(fù)現(xiàn)性。具體做法是,在程序開始的時(shí)候固定torch的隨機(jī)種子,同時(shí)也把numpy的隨機(jī)種子固定。

          np.random.seed(0)
          torch.manual_seed(0)#為CPU設(shè)置種子用于生成隨機(jī)數(shù),以使得結(jié)果是確定的
          torch.cuda.manual_seed_all(0)#為所有的GPU設(shè)置種子,以使得結(jié)果是確定的

          torch.backends.cudnn.deterministic = True
          torch.backends.cudnn.benchmark = False

          解釋一下:torch.backends.cudnn.benchmark = true

          總的來(lái)說,大部分情況下,設(shè)置這個(gè) flag 可以讓內(nèi)置的 cuDNN 的 auto-tuner 自動(dòng)尋找最適合當(dāng)前配置的高效算法,來(lái)達(dá)到優(yōu)化運(yùn)行效率的問題。

          一般來(lái)講,應(yīng)該遵循以下準(zhǔn)則:

          • 如果網(wǎng)絡(luò)的輸入數(shù)據(jù)維度或類型上變化不大,設(shè)置 torch.backends.cudnn.benchmark = true 可以增加運(yùn)行效率;
          • 如果網(wǎng)絡(luò)的輸入數(shù)據(jù)在每次 iteration 都變化的話,會(huì)導(dǎo)致 cnDNN 每次都會(huì)去尋找一遍最優(yōu)配置,這樣反而會(huì)降低運(yùn)行效率。

          torch.backends.cudnn.deterministic是啥?顧名思義,將這個(gè) flag 置為True的話,每次返回的卷積算法將是確定的,即默認(rèn)算法。如果配合上設(shè)置 Torch 的隨機(jī)種子為固定值的話,應(yīng)該可以保證每次運(yùn)行網(wǎng)絡(luò)的時(shí)候相同輸入的輸出是固定的。

          顯卡設(shè)置

          如果只需要一張顯卡:

          # Device configuration
          device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

          如果需要指定多張顯卡,比如0,1號(hào)顯卡:

          import os
          os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'

          也可以在命令行運(yùn)行代碼時(shí)設(shè)置顯卡:

          CUDA_VISIBLE_DEVICES=0,1 python train.py

          清除顯存

          torch.cuda.empty_cache()

          也可以使用在命令行重置GPU的指令

          nvidia-smi --gpu-reset -i [gpu_id]

          二、張量(Tensor)處理

          張量的數(shù)據(jù)類型

          PyTorch有9種CPU張量類型和9種GPU張量類型。

          張量基本信息

          tensor = torch.randn(3,4,5)
          print(tensor.type())  # 數(shù)據(jù)類型
          print(tensor.size())  # 張量的shape,是個(gè)元組
          print(tensor.dim())   # 維度的數(shù)量

          命名張量

          張量命名是一個(gè)非常有用的方法,這樣可以方便地使用維度的名字來(lái)做索引或其他操作,大大提高了可讀性、易用性,防止出錯(cuò)。

          # 在PyTorch 1.3之前,需要使用注釋
          # Tensor[N, C, H, W]
          images = torch.randn(3235656)
          images.sum(dim=1)
          images.select(dim=1, index=0)

          # PyTorch 1.3之后
          NCHW = [‘N’, ‘C’, ‘H’, ‘W’]
          images = torch.randn(3235656, names=NCHW)
          images.sum('C')
          images.select('C', index=0)
          # 也可以這么設(shè)置
          tensor = torch.rand(3,4,1,2,names=('C''N''H''W'))
          # 使用align_to可以對(duì)維度方便地排序
          tensor = tensor.align_to('N''C''H''W')

          數(shù)據(jù)類型轉(zhuǎn)換

          # 設(shè)置默認(rèn)類型,pytorch中的FloatTensor遠(yuǎn)遠(yuǎn)快于DoubleTensor
          torch.set_default_tensor_type(torch.FloatTensor)

          # 類型轉(zhuǎn)換
          tensor = tensor.cuda()
          tensor = tensor.cpu()
          tensor = tensor.float()
          tensor = tensor.long()

          torch.Tensor與np.ndarray轉(zhuǎn)換

          除了CharTensor,其他所有CPU上的張量都支持轉(zhuǎn)換為numpy格式然后再轉(zhuǎn)換回來(lái)。

          ndarray = tensor.cpu().numpy()
          tensor = torch.from_numpy(ndarray).float()
          tensor = torch.from_numpy(ndarray.copy()).float() # If ndarray has negative stride.

          Torch.tensor與PIL.Image轉(zhuǎn)換

          # pytorch中的張量默認(rèn)采用[N, C, H, W]的順序,并且數(shù)據(jù)范圍在[0,1],需要進(jìn)行轉(zhuǎn)置和規(guī)范化
          # torch.Tensor -> PIL.Image
          image = PIL.Image.fromarray(torch.clamp(tensor*255, min=0, max=255).byte().permute(1,2,0).cpu().numpy())
          image = torchvision.transforms.functional.to_pil_image(tensor)  # Equivalently way

          # PIL.Image -> torch.Tensor
          path = r'./figure.jpg'
          tensor = torch.from_numpy(np.asarray(PIL.Image.open(path))).permute(2,0,1).float() / 255
          tensor = torchvision.transforms.functional.to_tensor(PIL.Image.open(path)) # Equivalently way

          對(duì)應(yīng)的2種方法的示意圖分別為:

          torch.clamp把每個(gè)元素夾緊到區(qū)間[min, max]中。第1種方法用到的2個(gè)函數(shù)是:PIL.Image.fromarray() torch.from_numpy(np.asarray())

          np.ndarray與PIL.Image的轉(zhuǎn)換

          image = PIL.Image.fromarray(ndarray.astype(np.uint8))

          ndarray = np.asarray(PIL.Image.open(path))

          從只包含一個(gè)元素的張量中提取值

          value = torch.rand(1).item()

          張量形變

          # 在將卷積層輸入全連接層的情況下通常需要對(duì)張量做形變處理,
          # 相比torch.view,torch.reshape可以自動(dòng)處理輸入張量不連續(xù)的情況。
          tensor = torch.rand(2,3,4)
          shape = (64)
          tensor = torch.reshape(tensor, shape)

          打亂順序

          tensor = tensor[torch.randperm(tensor.size(0))]  # 打亂第一個(gè)維度

          水平翻轉(zhuǎn)

          # pytorch不支持tensor[::-1]這樣的負(fù)步長(zhǎng)操作,水平翻轉(zhuǎn)可以通過張量索引實(shí)現(xiàn)
          # 假設(shè)張量的維度為[N, D, H, W].
          tensor = tensor[:,:,:,torch.arange(tensor.size(3) - 1-1-1).long()]

          復(fù)制張量

          # Operation                 |  New/Shared memory | Still in computation graph |
          tensor.clone()            # |        New         |          Yes               |
          tensor.detach()           # |      Shared        |          No                |
          tensor.detach.clone()()   # |        New         |          No                |

          張量拼接

          '''
          注意torch.cat和torch.stack的區(qū)別在于torch.cat沿著給定的維度拼接,
          而torch.stack會(huì)新增一維。例如當(dāng)參數(shù)是3個(gè)10x5的張量,torch.cat的結(jié)果是30x5的張量,
          而torch.stack的結(jié)果是3x10x5的張量。
          '''

          tensor = torch.cat(list_of_tensors, dim=0)
          tensor = torch.stack(list_of_tensors, dim=0)

          將整數(shù)標(biāo)簽轉(zhuǎn)為one-hot編碼

          # pytorch的標(biāo)記默認(rèn)從0開始
          tensor = torch.tensor([0213])
          N = tensor.size(0)
          num_classes = 4
          one_hot = torch.zeros(N, num_classes).long()
          one_hot.scatter_(dim=1, index=torch.unsqueeze(tensor, dim=1), src=torch.ones(N, num_classes).long())

          得到非零元素

          torch.nonzero(tensor)               # index of non-zero elements
          torch.nonzero(tensor==0)            # index of zero elements
          torch.nonzero(tensor).size(0)       # number of non-zero elements
          torch.nonzero(tensor == 0).size(0)  # number of zero elements

          判斷兩個(gè)張量相等

          torch.allclose(tensor1, tensor2)  # float tensor
          torch.equal(tensor1, tensor2)     # int tensor

          張量擴(kuò)展

          # Expand tensor of shape 64*512 to shape 64*512*7*7.
          tensor = torch.rand(64,512)
          torch.reshape(tensor, (6451211)).expand(6451277)

          矩陣乘法

          # Matrix multiplcation: (m*n) * (n*p) * -> (m*p).
          result = torch.mm(tensor1, tensor2)

          # Batch matrix multiplication: (b*m*n) * (b*n*p) -> (b*m*p)
          result = torch.bmm(tensor1, tensor2)

          # Element-wise multiplication.
          result = tensor1 * tensor2

          計(jì)算兩組數(shù)據(jù)之間的兩兩歐式距離

          利用broadcast機(jī)制

          dist = torch.sqrt(torch.sum((X1[:,None,:] - X2) ** 2, dim=2))

          三、模型定義和操作

          一個(gè)簡(jiǎn)單兩層卷積網(wǎng)絡(luò)的示例

          # convolutional neural network (2 convolutional layers)
          class ConvNet(nn.Module):
              def __init__(self, num_classes=10):
                  super(ConvNet, self).__init__()
                  self.layer1 = nn.Sequential(
                      nn.Conv2d(116, kernel_size=5, stride=1, padding=2),
                      nn.BatchNorm2d(16),
                      nn.ReLU(),
                      nn.MaxPool2d(kernel_size=2, stride=2))
                  self.layer2 = nn.Sequential(
                      nn.Conv2d(1632, kernel_size=5, stride=1, padding=2),
                      nn.BatchNorm2d(32),
                      nn.ReLU(),
                      nn.MaxPool2d(kernel_size=2, stride=2))
                  self.fc = nn.Linear(7*7*32, num_classes)
              
              def forward(self, x):
                  out = self.layer1(x)
                  out = self.layer2(out)
                  out = out.reshape(out.size(0), -1)
                  out = self.fc(out)
                  return out

          model = ConvNet(num_classes).to(device)

          雙線性匯合(bilinear pooling)

          X = torch.reshape(N, D, H * W)                        # Assume X has shape N*D*H*W
          X = torch.bmm(X, torch.transpose(X, 12)) / (H * W)  # Bilinear pooling
          assert X.size() == (N, D, D)
          X = torch.reshape(X, (N, D * D))
          X = torch.sign(X) * torch.sqrt(torch.abs(X) + 1e-5)   # Signed-sqrt normalization
          X = torch.nn.functional.normalize(X)                  # L2 normalization

          多卡同步 BN(Batch normalization)

          當(dāng)使用 torch.nn.DataParallel 將代碼運(yùn)行在多張 GPU 卡上時(shí),PyTorch 的 BN 層默認(rèn)操作是各卡上數(shù)據(jù)獨(dú)立地計(jì)算均值和標(biāo)準(zhǔn)差,同步 BN 使用所有卡上的數(shù)據(jù)一起計(jì)算 BN 層的均值和標(biāo)準(zhǔn)差,緩解了當(dāng)批量大?。╞atch size)比較小時(shí)對(duì)均值和標(biāo)準(zhǔn)差估計(jì)不準(zhǔn)的情況,是在目標(biāo)檢測(cè)等任務(wù)中一個(gè)有效的提升性能的技巧。

          sync_bn = torch.nn.SyncBatchNorm(num_features, eps=1e-05, momentum=0.1, affine=True
                                           track_running_stats=True)

          將已有網(wǎng)絡(luò)的所有BN層改為同步BN層

          def convertBNtoSyncBN(module, process_group=None):
              '''Recursively replace all BN layers to SyncBN layer.

              Args:
                  module[torch.nn.Module]. Network
              '''

              if isinstance(module, torch.nn.modules.batchnorm._BatchNorm):
                  sync_bn = torch.nn.SyncBatchNorm(module.num_features, module.eps, module.momentum, 
                                                   module.affine, module.track_running_stats, process_group)
                  sync_bn.running_mean = module.running_mean
                  sync_bn.running_var = module.running_var
                  if module.affine:
                      sync_bn.weight = module.weight.clone().detach()
                      sync_bn.bias = module.bias.clone().detach()
                  return sync_bn
              else:
                  for name, child_module in module.named_children():
                      setattr(module, name) = convert_syncbn_model(child_module, process_group=process_group))
                  return module

          affine定義了BN層的參數(shù)γ和β是否是可學(xué)習(xí)的(不可學(xué)習(xí)默認(rèn)是常數(shù)1和0)。

          類似 BN 滑動(dòng)平均

          如果要實(shí)現(xiàn)類似 BN 滑動(dòng)平均的操作,在 forward 函數(shù)中要使用原地(inplace)操作給滑動(dòng)平均賦值。

          class BN(torch.nn.Module)
              def __init__(self):

                  ...
                  self.register_buffer('running_mean', torch.zeros(num_features))

              def forward(self, X):
                  ...
                  self.running_mean += momentum * (current - self.running_mean)

          計(jì)算模型整體參數(shù)量

          num_parameters = sum(torch.numel(parameter) for parameter in model.parameters())

          查看網(wǎng)絡(luò)中的參數(shù)

          可以通過model.state_dict()或者model.named_parameters()函數(shù)查看現(xiàn)在的全部可訓(xùn)練參數(shù)(包括通過繼承得到的父類中的參數(shù))

          params = list(model.named_parameters())
          (name, param) = params[28]
          print(name)
          print(param.grad)
          print('-------------------------------------------------')
          (name2, param2) = params[29]
          print(name2)
          print(param2.grad)
          print('----------------------------------------------------')
          (name1, param1) = params[30]
          print(name1)
          print(param1.grad)

          模型權(quán)重初始化

          注意 model.modules() 和 model.children() 的區(qū)別:model.modules() 會(huì)迭代地遍歷模型的所有子層,而 model.children() 只會(huì)遍歷模型下的一層。

          # Common practise for initialization.
          for layer in model.modules():
              if isinstance(layer, torch.nn.Conv2d):
                  torch.nn.init.kaiming_normal_(layer.weight, mode='fan_out',
                                                nonlinearity='relu')
                  if layer.bias is not None:
                      torch.nn.init.constant_(layer.bias, val=0.0)
              elif isinstance(layer, torch.nn.BatchNorm2d):
                  torch.nn.init.constant_(layer.weight, val=1.0)
                  torch.nn.init.constant_(layer.bias, val=0.0)
              elif isinstance(layer, torch.nn.Linear):
                  torch.nn.init.xavier_normal_(layer.weight)
                  if layer.bias is not None:
                      torch.nn.init.constant_(layer.bias, val=0.0)

          # Initialization with given tensor.
          layer.weight = torch.nn.Parameter(tensor)

          提取模型中的某一層

          modules()會(huì)返回模型中所有模塊的迭代器,它能夠訪問到最內(nèi)層,比如self.layer1.conv1這個(gè)模塊,還有一個(gè)與它們相對(duì)應(yīng)的是name_children()屬性以及named_modules(),這兩個(gè)不僅會(huì)返回模塊的迭代器,還會(huì)返回網(wǎng)絡(luò)層的名字。

          # 取模型中的前兩層
          new_model = nn.Sequential(*list(model.children())[:2
          # 如果希望提取出模型中的所有卷積層,可以像下面這樣操作:
          for layer in model.named_modules():
              if isinstance(layer[1],nn.Conv2d):
                   conv_model.add_module(layer[0],layer[1])

          部分層使用預(yù)訓(xùn)練模型

          注意如果保存的模型是 torch.nn.DataParallel,則當(dāng)前的模型也需要是

          model.load_state_dict(torch.load('model.pth'), strict=False)

          將在 GPU 保存的模型加載到 CPU

          model.load_state_dict(torch.load('model.pth', map_location='cpu'))

          導(dǎo)入另一個(gè)模型的相同部分到新的模型

          模型導(dǎo)入?yún)?shù)時(shí),如果兩個(gè)模型結(jié)構(gòu)不一致,則直接導(dǎo)入?yún)?shù)會(huì)報(bào)錯(cuò)。用下面方法可以把另一個(gè)模型的相同的部分導(dǎo)入到新的模型中。

          # model_new代表新的模型
          # model_saved代表其他模型,比如用torch.load導(dǎo)入的已保存的模型
          model_new_dict = model_new.state_dict()
          model_common_dict = {k:v for k, v in model_saved.items() if k in model_new_dict.keys()}
          model_new_dict.update(model_common_dict)
          model_new.load_state_dict(model_new_dict)

          四、數(shù)據(jù)處理

          計(jì)算數(shù)據(jù)集的均值和標(biāo)準(zhǔn)差

          import os
          import cv2
          import numpy as np
          from torch.utils.data import Dataset
          from PIL import Image


          def compute_mean_and_std(dataset):
              # 輸入PyTorch的dataset,輸出均值和標(biāo)準(zhǔn)差
              mean_r = 0
              mean_g = 0
              mean_b = 0

              for img, _ in dataset:
                  img = np.asarray(img) # change PIL Image to numpy array
                  mean_b += np.mean(img[:, :, 0])
                  mean_g += np.mean(img[:, :, 1])
                  mean_r += np.mean(img[:, :, 2])

              mean_b /= len(dataset)
              mean_g /= len(dataset)
              mean_r /= len(dataset)

              diff_r = 0
              diff_g = 0
              diff_b = 0

              N = 0

              for img, _ in dataset:
                  img = np.asarray(img)

                  diff_b += np.sum(np.power(img[:, :, 0] - mean_b, 2))
                  diff_g += np.sum(np.power(img[:, :, 1] - mean_g, 2))
                  diff_r += np.sum(np.power(img[:, :, 2] - mean_r, 2))

                  N += np.prod(img[:, :, 0].shape)

              std_b = np.sqrt(diff_b / N)
              std_g = np.sqrt(diff_g / N)
              std_r = np.sqrt(diff_r / N)

              mean = (mean_b.item() / 255.0, mean_g.item() / 255.0, mean_r.item() / 255.0)
              std = (std_b.item() / 255.0, std_g.item() / 255.0, std_r.item() / 255.0)
              return mean, std

          常用訓(xùn)練和驗(yàn)證數(shù)據(jù)預(yù)處理

          其中 ToTensor 操作會(huì)將 PIL.Image 或形狀為 H×W×D,數(shù)值范圍為 [0, 255] 的 np.ndarray 轉(zhuǎn)換為形狀為 D×H×W,數(shù)值范圍為 [0.0, 1.0] 的 torch.Tensor。

          train_transform = torchvision.transforms.Compose([
              torchvision.transforms.RandomResizedCrop(size=224,
                                                       scale=(0.081.0)),
              torchvision.transforms.RandomHorizontalFlip(),
              torchvision.transforms.ToTensor(),
              torchvision.transforms.Normalize(mean=(0.4850.4560.406),
                                               std=(0.2290.2240.225)),
           ])
           val_transform = torchvision.transforms.Compose([
              torchvision.transforms.Resize(256),
              torchvision.transforms.CenterCrop(224),
              torchvision.transforms.ToTensor(),
              torchvision.transforms.Normalize(mean=(0.4850.4560.406),
                                               std=(0.2290.2240.225)),
          ])

          五、模型訓(xùn)練和測(cè)試

          分類模型訓(xùn)練代碼

          # Loss and optimizer
          criterion = nn.CrossEntropyLoss()
          optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

          # Train the model
          total_step = len(train_loader)
          for epoch in range(num_epochs):
              for i ,(images, labels) in enumerate(train_loader):
                  images = images.to(device)
                  labels = labels.to(device)
                  
                  # Forward pass
                  outputs = model(images)
                  loss = criterion(outputs, labels)
                  
                  # Backward and optimizer
                  optimizer.zero_grad()
                  loss.backward()
                  optimizer.step()
                  
                  if (i+1) % 100 == 0:
                      print('Epoch: [{}/{}], Step: [{}/{}], Loss: {}'
                            .format(epoch+1, num_epochs, i+1, total_step, loss.item()))

          分類模型測(cè)試代碼

          # Test the model
          model.eval()  # eval mode(batch norm uses moving mean/variance 
                        #instead of mini-batch mean/variance)
          with torch.no_grad():
              correct = 0
              total = 0
              for images, labels in test_loader:
                  images = images.to(device)
                  labels = labels.to(device)
                  outputs = model(images)
                  _, predicted = torch.max(outputs.data, 1)
                  total += labels.size(0)
                  correct += (predicted == labels).sum().item()
                  
              print('Test accuracy of the model on the 10000 test images: {} %'
                    .format(100 * correct / total))

          自定義loss

          繼承torch.nn.Module類寫自己的loss。

          class MyLoss(torch.nn.Module):
              def __init__(self):
                  super(MyLoss, self).__init__()
                  
              def forward(self, x, y):
                  loss = torch.mean((x - y) ** 2)
                  return loss

          標(biāo)簽平滑(label smoothing)

          寫一個(gè)label_smoothing.py的文件,然后在訓(xùn)練代碼里引用,用LSR代替交叉熵?fù)p失即可。label_smoothing.py內(nèi)容如下:

          import torch
          import torch.nn as nn


          class LSR(nn.Module):

              def __init__(self, e=0.1, reduction='mean'):
                  super().__init__()

                  self.log_softmax = nn.LogSoftmax(dim=1)
                  self.e = e
                  self.reduction = reduction
              
              def _one_hot(self, labels, classes, value=1):
                  """
                      Convert labels to one hot vectors
                  
                  Args:
                      labels: torch tensor in format [label1, label2, label3, ...]
                      classes: int, number of classes
                      value: label value in one hot vector, default to 1
                  
                  Returns:
                      return one hot format labels in shape [batchsize, classes]
                  """


                  one_hot = torch.zeros(labels.size(0), classes)

                  #labels and value_added  size must match
                  labels = labels.view(labels.size(0), -1)
                  value_added = torch.Tensor(labels.size(0), 1).fill_(value)

                  value_added = value_added.to(labels.device)
                  one_hot = one_hot.to(labels.device)

                  one_hot.scatter_add_(1, labels, value_added)

                  return one_hot

              def _smooth_label(self, target, length, smooth_factor):
                  """convert targets to one-hot format, and smooth
                  them.
                  Args:
                      target: target in form with [label1, label2, label_batchsize]
                      length: length of one-hot format(number of classes)
                      smooth_factor: smooth factor for label smooth
                  
                  Returns:
                      smoothed labels in one hot format
                  """

                  one_hot = self._one_hot(target, length, value=1 - smooth_factor)
                  one_hot += smooth_factor / (length - 1)

                  return one_hot.to(target.device)

              def forward(self, x, target):

                  if x.size(0) != target.size(0):
                      raise ValueError('Expected input batchsize ({}) to match target batch_size({})'
                              .format(x.size(0), target.size(0)))

                  if x.dim() < 2:
                      raise ValueError('Expected input tensor to have least 2 dimensions(got {})'
                              .format(x.size(0)))

                  if x.dim() != 2:
                      raise ValueError('Only 2 dimension tensor are implemented, (got {})'
                              .format(x.size()))


                  smoothed_target = self._smooth_label(target, x.size(1), self.e)
                  x = self.log_softmax(x)
                  loss = torch.sum(- x * smoothed_target, dim=1)

                  if self.reduction == 'none':
                      return loss
                  
                  elif self.reduction == 'sum':
                      return torch.sum(loss)
                  
                  elif self.reduction == 'mean':
                      return torch.mean(loss)
                  
                  else:
                      raise ValueError('unrecognized option, expect reduction to be one of none, mean, sum')

          或者直接在訓(xùn)練文件里做label smoothing

          for images, labels in train_loader:
              images, labels = images.cuda(), labels.cuda()
              N = labels.size(0)
              # C is the number of classes.
              smoothed_labels = torch.full(size=(N, C), fill_value=0.1 / (C - 1)).cuda()
              smoothed_labels.scatter_(dim=1, index=torch.unsqueeze(labels, dim=1), value=0.9)

              score = model(images)
              log_prob = torch.nn.functional.log_softmax(score, dim=1)
              loss = -torch.sum(log_prob * smoothed_labels) / N
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()

          Mixup訓(xùn)練

          beta_distribution = torch.distributions.beta.Beta(alpha, alpha)
          for images, labels in train_loader:
              images, labels = images.cuda(), labels.cuda()

              # Mixup images and labels.
              lambda_ = beta_distribution.sample([]).item()
              index = torch.randperm(images.size(0)).cuda()
              mixed_images = lambda_ * images + (1 - lambda_) * images[index, :]
              label_a, label_b = labels, labels[index]

              # Mixup loss.
              scores = model(mixed_images)
              loss = (lambda_ * loss_function(scores, label_a)
                      + (1 - lambda_) * loss_function(scores, label_b))
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()

          L1 正則化

          l1_regularization = torch.nn.L1Loss(reduction='sum')
          loss = ...  # Standard cross-entropy loss
          for param in model.parameters():
              loss += torch.sum(torch.abs(param))
          loss.backward()

          不對(duì)偏置項(xiàng)進(jìn)行權(quán)重衰減(weight decay)

          pytorch里的weight decay相當(dāng)于l2正則

          bias_list = (param for name, param in model.named_parameters() if name[-4:] == 'bias')
          others_list = (param for name, param in model.named_parameters() if name[-4:] != 'bias')
          parameters = [{'parameters': bias_list, 'weight_decay'0},                
                        {'parameters': others_list}]
          optimizer = torch.optim.SGD(parameters, lr=1e-2, momentum=0.9, weight_decay=1e-4)

          得到當(dāng)前學(xué)習(xí)率

          # If there is one global learning rate (which is the common case).
          lr = next(iter(optimizer.param_groups))['lr']

          # If there are multiple learning rates for different layers.
          all_lr = []
          for param_group in optimizer.param_groups:
              all_lr.append(param_group['lr'])

          另一種方法,在一個(gè)batch訓(xùn)練代碼里,當(dāng)前的lr是optimizer.param_groups[0]['lr']

          學(xué)習(xí)率衰減

          # Reduce learning rate when validation accuarcy plateau.
          scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=5, verbose=True)
          for t in range(080):
              train(...)
              val(...)
              scheduler.step(val_acc)

          # Cosine annealing learning rate.
          scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=80)
          # Reduce learning rate by 10 at given epochs.
          scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5070], gamma=0.1)
          for t in range(080):
              scheduler.step()    
              train(...)
              val(...)

          # Learning rate warmup by 10 epochs.
          scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda t: t / 10)
          for t in range(010):
              scheduler.step()
              train(...)
              val(...)

          優(yōu)化器鏈?zhǔn)礁?/span>

          從1.4版本開始,torch.optim.lr_scheduler 支持鏈?zhǔn)礁拢╟haining),即用戶可以定義兩個(gè) schedulers,并交替在訓(xùn)練中使用。

          import torch
          from torch.optim import SGD
          from torch.optim.lr_scheduler import ExponentialLR, StepLR
          model = [torch.nn.Parameter(torch.randn(22, requires_grad=True))]
          optimizer = SGD(model, 0.1)
          scheduler1 = ExponentialLR(optimizer, gamma=0.9)
          scheduler2 = StepLR(optimizer, step_size=3, gamma=0.1)
          for epoch in range(4):
              print(epoch, scheduler2.get_lr()[0])
              optimizer.step()
              scheduler1.step()
              scheduler2.step()

          模型訓(xùn)練可視化

          PyTorch可以使用tensorboard來(lái)可視化訓(xùn)練過程。

          安裝和運(yùn)行TensorBoard。

          pip install tensorboard
          tensorboard --logdir=runs

          使用SummaryWriter類來(lái)收集和可視化相應(yīng)的數(shù)據(jù),放了方便查看,可以使用不同的文件夾,比如'Loss/train'和'Loss/test'。

          from torch.utils.tensorboard import SummaryWriter
          import numpy as np

          writer = SummaryWriter()

          for n_iter in range(100):
              writer.add_scalar('Loss/train', np.random.random(), n_iter)
              writer.add_scalar('Loss/test', np.random.random(), n_iter)
              writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
              writer.add_scalar('Accuracy/test', np.random.random(), n_iter)

          保存與加載斷點(diǎn)

          注意為了能夠恢復(fù)訓(xùn)練,我們需要同時(shí)保存模型和優(yōu)化器的狀態(tài),以及當(dāng)前的訓(xùn)練輪數(shù)。

          start_epoch = 0
          # Load checkpoint.
          if resume: # resume為參數(shù),第一次訓(xùn)練時(shí)設(shè)為0,中斷再訓(xùn)練時(shí)設(shè)為1
              model_path = os.path.join('model''best_checkpoint.pth.tar')
              assert os.path.isfile(model_path)
              checkpoint = torch.load(model_path)
              best_acc = checkpoint['best_acc']
              start_epoch = checkpoint['epoch']
              model.load_state_dict(checkpoint['model'])
              optimizer.load_state_dict(checkpoint['optimizer'])
              print('Load checkpoint at epoch {}.'.format(start_epoch))
              print('Best accuracy so far {}.'.format(best_acc))

          # Train the model
          for epoch in range(start_epoch, num_epochs): 
              ... 

              # Test the model
              ...
                  
              # save checkpoint
              is_best = current_acc > best_acc
              best_acc = max(current_acc, best_acc)
              checkpoint = {
                  'best_acc': best_acc,
                  'epoch': epoch + 1,
                  'model': model.state_dict(),
                  'optimizer': optimizer.state_dict(),
              }
              model_path = os.path.join('model''checkpoint.pth.tar')
              best_model_path = os.path.join('model''best_checkpoint.pth.tar')
              torch.save(checkpoint, model_path)
              if is_best:
                  shutil.copy(model_path, best_model_path)

          提取 ImageNet 預(yù)訓(xùn)練模型某層的卷積特征

          # VGG-16 relu5-3 feature.
          model = torchvision.models.vgg16(pretrained=True).features[:-1]
          # VGG-16 pool5 feature.
          model = torchvision.models.vgg16(pretrained=True).features
          # VGG-16 fc7 feature.
          model = torchvision.models.vgg16(pretrained=True)
          model.classifier = torch.nn.Sequential(*list(model.classifier.children())[:-3])
          # ResNet GAP feature.
          model = torchvision.models.resnet18(pretrained=True)
          model = torch.nn.Sequential(collections.OrderedDict(
              list(model.named_children())[:-1]))

          with torch.no_grad():
              model.eval()
              conv_representation = model(image)

          提取 ImageNet 預(yù)訓(xùn)練模型多層的卷積特征

          class FeatureExtractor(torch.nn.Module):
              """Helper class to extract several convolution features from the given
              pre-trained model.

              Attributes:
                  _model, torch.nn.Module.
                  _layers_to_extract, list or set

              Example:
                  >>> model = torchvision.models.resnet152(pretrained=True)
                  >>> model = torch.nn.Sequential(collections.OrderedDict(
                          list(model.named_children())[:-1]))
                  >>> conv_representation = FeatureExtractor(
                          pretrained_model=model,
                          layers_to_extract={'layer1', 'layer2', 'layer3', 'layer4'})(image)
              """

              def __init__(self, pretrained_model, layers_to_extract):
                  torch.nn.Module.__init__(self)
                  self._model = pretrained_model
                  self._model.eval()
                  self._layers_to_extract = set(layers_to_extract)

              def forward(self, x):
                  with torch.no_grad():
                      conv_representation = []
                      for name, layer in self._model.named_children():
                          x = layer(x)
                          if name in self._layers_to_extract:
                              conv_representation.append(x)
                      return conv_representation

          微調(diào)全連接層

          model = torchvision.models.resnet18(pretrained=True)
          for param in model.parameters():
              param.requires_grad = False
          model.fc = nn.Linear(512100)  # Replace the last fc layer
          optimizer = torch.optim.SGD(model.fc.parameters(), lr=1e-2, momentum=0.9, weight_decay=1e-4)

          以較大學(xué)習(xí)率微調(diào)全連接層,較小學(xué)習(xí)率微調(diào)卷積層

          model = torchvision.models.resnet18(pretrained=True)
          finetuned_parameters = list(map(id, model.fc.parameters()))
          conv_parameters = (p for p in model.parameters() if id(p) not in finetuned_parameters)
          parameters = [{'params': conv_parameters, 'lr'1e-3}, 
                        {'params': model.fc.parameters()}]
          optimizer = torch.optim.SGD(parameters, lr=1e-2, momentum=0.9, weight_decay=1e-4)

          六、其他注意事項(xiàng)

          • 不要使用太大的線性層。因?yàn)閚n.Linear(m,n)使用的是O(m,n)的內(nèi)存,線性層太大很容易超出現(xiàn)有顯存。
          • 不要在太長(zhǎng)的序列上使用RNN。因?yàn)镽NN反向傳播使用的是BPTT算法,其需要的內(nèi)存和輸入序列的長(zhǎng)度呈線性關(guān)系。
          • model(x) 前用 model.train()model.eval() 切換網(wǎng)絡(luò)狀態(tài)。
          • 不需要計(jì)算梯度的代碼塊用 with torch.no_grad() 包含起來(lái)。
          • model.eval() 和 torch.no_grad() 的區(qū)別在于,model.eval() 是將網(wǎng)絡(luò)切換為測(cè)試狀態(tài),例如 BN 和dropout在訓(xùn)練和測(cè)試階段使用不同的計(jì)算方法。torch.no_grad() 是關(guān)閉 PyTorch 張量的自動(dòng)求導(dǎo)機(jī)制,以減少存儲(chǔ)使用和加速計(jì)算,得到的結(jié)果無(wú)法進(jìn)行 loss.backward()。
          • model.zero_grad()會(huì)把整個(gè)模型的參數(shù)的梯度都?xì)w零, 而optimizer.zero_grad()只會(huì)把傳入其中的參數(shù)的梯度歸零。
          • torch.nn.CrossEntropyLoss 的輸入不需要經(jīng)過 Softmax。torch.nn.CrossEntropyLoss 等價(jià)于 torch.nn.functional.log_softmax + torch.nn.NLLLoss。
          • loss.backward() 前用 optimizer.zero_grad() 清除累積梯度。
          • torch.utils.data.DataLoader 中盡量設(shè)置 pin_memory=True,對(duì)特別小的數(shù)據(jù)集如 MNIST 設(shè)置 pin_memory=False 反而更快一些。num_workers 的設(shè)置需要在實(shí)驗(yàn)中找到最快的取值。
          • 用 del 及時(shí)刪除不用的中間變量,節(jié)約 GPU 存儲(chǔ)。
          • 使用 inplace 操作可節(jié)約 GPU 存儲(chǔ),如
          x = torch.nn.functional.relu(x, inplace=True)
          • 減少 CPU 和 GPU 之間的數(shù)據(jù)傳輸。 例如如果你想知道一個(gè) epoch 中每個(gè) mini-batch 的 loss 和準(zhǔn)確率,先將它們累積在 GPU 中等一個(gè) epoch 結(jié)束之后一起傳輸回 CPU 會(huì)比每個(gè) mini-batch 都進(jìn)行一次 GPU 到 CPU 的傳輸更快。
          • 使用半精度浮點(diǎn)數(shù) half() 會(huì)有一定的速度提升,具體效率依賴于 GPU 型號(hào)。需要小心數(shù)值精度過低帶來(lái)的穩(wěn)定性問題。
          • 時(shí)常使用 assert tensor.size() == (N, D, H, W) 作為調(diào)試手段,確保張量維度和你設(shè)想中一致。
          • 除了標(biāo)記 y 外,盡量少使用一維張量,使用 n*1 的二維張量代替,可以避免一些意想不到的一維張量計(jì)算結(jié)果。
          • 統(tǒng)計(jì)代碼各部分耗時(shí)
          with torch.autograd.profiler.profile(enabled=True, use_cuda=Falseas profile:
              ...
          print(profile)

          # 或者在命令行運(yùn)行
          python -m torch.utils.bottleneck main.py
          • 使用TorchSnooper來(lái)調(diào)試PyTorch代碼,程序在執(zhí)行的時(shí)候,就會(huì)自動(dòng) print 出來(lái)每一行的執(zhí)行結(jié)果的 tensor 的形狀、數(shù)據(jù)類型、設(shè)備、是否需要梯度的信息。
          # pip install torchsnooper
          import torchsnooper

          # 對(duì)于函數(shù),使用修飾器
          @torchsnooper.snoop()

          # 如果不是函數(shù),使用 with 語(yǔ)句來(lái)激活 TorchSnooper,把訓(xùn)練的那個(gè)循環(huán)裝進(jìn) with 語(yǔ)句中去。
          with torchsnooper.snoop():
              原本的代碼
          ·················END·················
          瀏覽 37
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  97人妻人人澡 | 欧美V亚洲V综合V国产 | 日本黄色视频在线免费观看 | 成人夜色激情福利av | xjgggyxgs.com高价收liang,请涟系@qdd2000 |