0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

PyTorch教程-14.1. 圖像增強

jf_pJlTbmA9 ? 來源:PyTorch ? 作者:PyTorch ? 2023-06-05 15:44 ? 次閱讀

在8.1 節(jié)中,我們提到大型數(shù)據(jù)集是深度神經(jīng)網(wǎng)絡在各種應用中取得成功的先決條件。圖像增強在對訓練圖像進行一系列隨機變化后生成相似但不同的訓練示例,從而擴大了訓練集的大小?;蛘撸瑘D像增強的動機可能是訓練示例的隨機調(diào)整允許模型減少對某些屬性的依賴,從而提高它們的泛化能力。例如,我們可以通過不同的方式裁剪圖像,使感興趣的對象出現(xiàn)在不同的位置,從而減少模型對對象位置的依賴。我們還可以調(diào)整亮度和顏色等因素,以降低模型對顏色的敏感度。圖像增強對于當時 AlexNet 的成功來說可能是不可或缺的。在本節(jié)中,我們將討論這種在計算機視覺中廣泛使用的技術。

%matplotlib inline
import torch
import torchvision
from torch import nn
from d2l import torch as d2l

%matplotlib inline
from mxnet import autograd, gluon, image, init, np, npx
from mxnet.gluon import nn
from d2l import mxnet as d2l

npx.set_np()

14.1.1。常見的圖像增強方法

在我們對常見圖像增強方法的研究中,我們將使用以下方法400×500形象一個例子。

d2l.set_figsize()
img = d2l.Image.open('../img/cat1.jpg')
d2l.plt.imshow(img);

d2l.set_figsize()
img = image.imread('../img/cat1.jpg')
d2l.plt.imshow(img.asnumpy());

大多數(shù)圖像增強方法都具有一定的隨機性。為了方便我們觀察圖像增強的效果,接下來我們定義一個輔助函數(shù)apply。aug此函數(shù)在輸入圖像上多次運行圖像增強方法img 并顯示所有結果。

def apply(img, aug, num_rows=2, num_cols=4, scale=1.5):
  Y = [aug(img) for _ in range(num_rows * num_cols)]
  d2l.show_images(Y, num_rows, num_cols, scale=scale)

def apply(img, aug, num_rows=2, num_cols=4, scale=1.5):
  Y = [aug(img) for _ in range(num_rows * num_cols)]
  d2l.show_images(Y, num_rows, num_cols, scale=scale)

14.1.1.1。翻轉和裁剪

左右翻轉圖像通常不會改變對象的類別。這是最早和最廣泛使用的圖像增強方法之一。接下來,我們使用該transforms模塊創(chuàng)建實例RandomHorizontalFlip,它以 50% 的幾率左右翻轉圖像。

apply(img, torchvision.transforms.RandomHorizontalFlip())

上下翻轉不像左右翻轉那樣常見。但至少對于這個示例圖像,上下翻轉并不妨礙識別。接下來,我們創(chuàng)建一個RandomVerticalFlip實例,以 50% 的幾率上下翻轉圖像。

apply(img, torchvision.transforms.RandomVerticalFlip())

Flipping the image left and right usually does not change the category of the object. This is one of the earliest and most widely used methods of image augmentation. Next, we use the transforms module to create the RandomFlipLeftRight instance, which flips an image left and right with a 50% chance.

apply(img, gluon.data.vision.transforms.RandomFlipLeftRight())

Flipping up and down is not as common as flipping left and right. But at least for this example image, flipping up and down does not hinder recognition. Next, we create a RandomFlipTopBottom instance to flip an image up and down with a 50% chance.

apply(img, gluon.data.vision.transforms.RandomFlipTopBottom())

在我們使用的示例圖像中,貓位于圖像的中間,但一般情況下可能并非如此。在7.5 節(jié)中,我們解釋了池化層可以降低卷積層對目標位置的敏感性。此外,我們還可以隨機裁剪圖像,讓物體以不同的尺度出現(xiàn)在圖像中的不同位置,這樣也可以降低模型對目標位置的敏感度。

在下面的代碼中,我們隨機裁剪一個面積為 10%~100%每次都是原始區(qū)域的大小,這個區(qū)域的寬高比是隨機選擇的 0.5~2. 然后,該區(qū)域的寬度和高度都縮放為 200 像素。除非另有說明,之間的隨機數(shù)a和b本節(jié)中指的是從區(qū)間中隨機均勻采樣得到的連續(xù)值 [a,b].

shape_aug = torchvision.transforms.RandomResizedCrop(
  (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
apply(img, shape_aug)

shape_aug = gluon.data.vision.transforms.RandomResizedCrop(
  (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
apply(img, shape_aug)

14.1.1.2。改變顏色

另一種增強方法是改變顏色。我們可以改變圖像顏色的四個方面:亮度、對比度、飽和度和色調(diào)。在下面的示例中,我們將圖像的亮度隨機更改為 50% (1?0.5) 和 150% (1+0.5) 的原始圖像。

apply(img, torchvision.transforms.ColorJitter(
  brightness=0.5, contrast=0, saturation=0, hue=0))

apply(img, gluon.data.vision.transforms.RandomBrightness(0.5))

同樣,我們可以隨機改變圖像的色調(diào)。

apply(img, torchvision.transforms.ColorJitter(
  brightness=0, contrast=0, saturation=0, hue=0.5))

pYYBAGR9OyiAKZ5XAAKgBf48WXw811.svg

apply(img, gluon.data.vision.transforms.RandomHue(0.5))

pYYBAGR9OyuAPyxyAAKjNZd5bQw461.svg

我們也可以創(chuàng)建一個RandomColorJitter實例,同時設置如何隨機改變圖片的brightness, contrast, saturation, 。hue

color_aug = torchvision.transforms.ColorJitter(
  brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
apply(img, color_aug)

pYYBAGR9Oy2AE_XgAAJd2aug_jE886.svg

color_aug = gluon.data.vision.transforms.RandomColorJitter(
  brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
apply(img, color_aug)

poYBAGR9Oy-AVr8NAAJhlfJqe4U114.svg

14.1.1.3。結合多種圖像增強方法

在實踐中,我們將結合多種圖像增強方法。例如,我們可以組合上面定義的不同圖像增強方法,并通過實例將它們應用于每個圖像Compose。

augs = torchvision.transforms.Compose([
  torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])
apply(img, augs)

pYYBAGR9OzKAQ7GaAAKsrZQVOms690.svg

augs = gluon.data.vision.transforms.Compose([
  gluon.data.vision.transforms.RandomFlipLeftRight(), color_aug, shape_aug])
apply(img, augs)

poYBAGR9OzWAQEfaAAK3SaxJQmU062.svg

14.1.2。圖像增強訓練

讓我們訓練一個具有圖像增強功能的模型。這里我們使用 CIFAR-10 數(shù)據(jù)集而不是我們之前使用的 Fashion-MNIST 數(shù)據(jù)集。這是因為 Fashion-MNIST 數(shù)據(jù)集中物體的位置和大小已經(jīng)歸一化,而 CIFAR-10 數(shù)據(jù)集中物體的顏色和大小有更顯著的差異。CIFAR-10 數(shù)據(jù)集中的前 32 個訓練圖像如下所示。

all_images = torchvision.datasets.CIFAR10(train=True, root="../data",
                     download=True)
d2l.show_images([all_images[i][0] for i in range(32)], 4, 8, scale=0.8);

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data/cifar-10-python.tar.gz

 0%|     | 0/170498071 [00:00 

Extracting ../data/cifar-10-python.tar.gz to ../data

pYYBAGR9OzuAEctXAAPkYlTx74E987.svg

d2l.show_images(gluon.data.vision.CIFAR10(
  train=True)[:32][0], 4, 8, scale=0.8);

pYYBAGR9OzuAEctXAAPkYlTx74E987.svg

為了在預測過程中獲得確定的結果,我們通常只對訓練樣例進行圖像增強,而不會在預測過程中使用帶有隨機操作的圖像增強。這里我們只使用最簡單的隨機左右翻轉的方法。此外,我們使用 ToTensor實例將一小批圖像轉換為深度學習框架所需的格式,即 0 到 1 之間的 32 位浮點數(shù),形狀為(批量大小,通道數(shù),高度,寬度).

train_augs = torchvision.transforms.Compose([
   torchvision.transforms.RandomHorizontalFlip(),
   torchvision.transforms.ToTensor()])

test_augs = torchvision.transforms.Compose([
   torchvision.transforms.ToTensor()])

接下來,我們定義一個輔助函數(shù)以方便讀取圖像和應用圖像增強。PyTorch 數(shù)據(jù)集提供的參數(shù)transform應用增強來轉換圖像。詳細介紹DataLoader請參考 4.2節(jié)。

def load_cifar10(is_train, augs, batch_size):
  dataset = torchvision.datasets.CIFAR10(root="../data", train=is_train,
                      transform=augs, download=True)
  dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
          shuffle=is_train, num_workers=d2l.get_dataloader_workers())
  return dataloader

train_augs = gluon.data.vision.transforms.Compose([
  gluon.data.vision.transforms.RandomFlipLeftRight(),
  gluon.data.vision.transforms.ToTensor()])

test_augs = gluon.data.vision.transforms.Compose([
  gluon.data.vision.transforms.ToTensor()])

Next, we define an auxiliary function to facilitate reading the image and applying image augmentation. The transform_first function provided by Gluon’s datasets applies image augmentation to the first element of each training example (image and label), i.e., the image. For a detailed introduction to DataLoader, please refer to Section 4.2.

def load_cifar10(is_train, augs, batch_size):
  return gluon.data.DataLoader(
    gluon.data.vision.CIFAR10(train=is_train).transform_first(augs),
    batch_size=batch_size, shuffle=is_train,
    num_workers=d2l.get_dataloader_workers())

14.1.2.1。多 GPU 訓練

我們在 CIFAR-10 數(shù)據(jù)集上訓練第 8.6 節(jié)中的 ResNet-18 模型?;叵胍幌?3.6 節(jié)中對多 GPU 訓練的介紹 。在下文中,我們定義了一個函數(shù)來使用多個 GPU 訓練和評估模型。

#@save
def train_batch_ch13(net, X, y, loss, trainer, devices):
  """Train for a minibatch with multiple GPUs (defined in Chapter 13)."""
  if isinstance(X, list):
    # Required for BERT fine-tuning (to be covered later)
    X = [x.to(devices[0]) for x in X]
  else:
    X = X.to(devices[0])
  y = y.to(devices[0])
  net.train()
  trainer.zero_grad()
  pred = net(X)
  l = loss(pred, y)
  l.sum().backward()
  trainer.step()
  train_loss_sum = l.sum()
  train_acc_sum = d2l.accuracy(pred, y)
  return train_loss_sum, train_acc_sum

#@save
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
        devices=d2l.try_all_gpus()):
  """Train a model with multiple GPUs (defined in Chapter 13)."""
  timer, num_batches = d2l.Timer(), len(train_iter)
  animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
              legend=['train loss', 'train acc', 'test acc'])
  net = nn.DataParallel(net, device_ids=devices).to(devices[0])
  for epoch in range(num_epochs):
    # Sum of training loss, sum of training accuracy, no. of examples,
    # no. of predictions
    metric = d2l.Accumulator(4)
    for i, (features, labels) in enumerate(train_iter):
      timer.start()
      l, acc = train_batch_ch13(
        net, features, labels, loss, trainer, devices)
      metric.add(l, acc, labels.shape[0], labels.numel())
      timer.stop()
      if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
        animator.add(epoch + (i + 1) / num_batches,
               (metric[0] / metric[2], metric[1] / metric[3],
               None))
    test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
    animator.add(epoch + 1, (None, None, test_acc))
  print(f'loss {metric[0] / metric[2]:.3f}, train acc '
     f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
  print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
     f'{str(devices)}')

#@save
def train_batch_ch13(net, features, labels, loss, trainer, devices,
           split_f=d2l.split_batch):
  """Train for a minibatch with multiple GPUs (defined in Chapter 13)."""
  X_shards, y_shards = split_f(features, labels, devices)
  with autograd.record():
    pred_shards = [net(X_shard) for X_shard in X_shards]
    ls = [loss(pred_shard, y_shard) for pred_shard, y_shard
       in zip(pred_shards, y_shards)]
  for l in ls:
    l.backward()
  # The `True` flag allows parameters with stale gradients, which is useful
  # later (e.g., in fine-tuning BERT)
  trainer.step(labels.shape[0], ignore_stale_grad=True)
  train_loss_sum = sum([float(l.sum()) for l in ls])
  train_acc_sum = sum(d2l.accuracy(pred_shard, y_shard)
            for pred_shard, y_shard in zip(pred_shards, y_shards))
  return train_loss_sum, train_acc_sum

#@save
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
        devices=d2l.try_all_gpus(), split_f=d2l.split_batch):
  """Train a model with multiple GPUs (defined in Chapter 13)."""
  timer, num_batches = d2l.Timer(), len(train_iter)
  animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
              legend=['train loss', 'train acc', 'test acc'])
  for epoch in range(num_epochs):
    # Sum of training loss, sum of training accuracy, no. of examples,
    # no. of predictions
    metric = d2l.Accumulator(4)
    for i, (features, labels) in enumerate(train_iter):
      timer.start()
      l, acc = train_batch_ch13(
        net, features, labels, loss, trainer, devices, split_f)
      metric.add(l, acc, labels.shape[0], labels.size)
      timer.stop()
      if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
        animator.add(epoch + (i + 1) / num_batches,
               (metric[0] / metric[2], metric[1] / metric[3],
               None))
    test_acc = d2l.evaluate_accuracy_gpus(net, test_iter, split_f)
    animator.add(epoch + 1, (None, None, test_acc))
  print(f'loss {metric[0] / metric[2]:.3f}, train acc '
     f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
  print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
     f'{str(devices)}')

現(xiàn)在我們可以定義train_with_data_aug函數(shù)來訓練帶有圖像增強的模型。該函數(shù)獲取所有可用的 GPU,使用 Adam 作為優(yōu)化算法,對訓練數(shù)據(jù)集應用圖像增強,最后調(diào)用train_ch13剛剛定義的函數(shù)來訓練和評估模型。

batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3)
net.apply(d2l.init_cnn)

def train_with_data_aug(train_augs, test_augs, net, lr=0.001):
  train_iter = load_cifar10(True, train_augs, batch_size)
  test_iter = load_cifar10(False, test_augs, batch_size)
  loss = nn.CrossEntropyLoss(reduction="none")
  trainer = torch.optim.Adam(net.parameters(), lr=lr)
  net(next(iter(train_iter))[0])
  train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)

batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10)
net.initialize(init=init.Xavier(), ctx=devices)

def train_with_data_aug(train_augs, test_augs, net, lr=0.001):
  train_iter = load_cifar10(True, train_augs, batch_size)
  test_iter = load_cifar10(False, test_augs, batch_size)
  loss = gluon.loss.SoftmaxCrossEntropyLoss()
  trainer = gluon.Trainer(net.collect_params(), 'adam',
              {'learning_rate': lr})
  train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)

讓我們使用基于隨機左右翻轉的圖像增強來訓練模型。

train_with_data_aug(train_augs, test_augs, net)

loss 0.224, train acc 0.922, test acc 0.820
3237.5 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]

pYYBAGR9O0CARiWPAAEPdYt1u04765.svg

train_with_data_aug(train_augs, test_augs, net)

loss 0.168, train acc 0.942, test acc 0.837
1933.3 examples/sec on [gpu(0), gpu(1)]

poYBAGR9O0OAK33CAAEP4uKX3Ew806.svg

14.1.3。概括

圖像增強是在現(xiàn)有訓練數(shù)據(jù)的基礎上生成隨機圖像,以提高模型的泛化能力。

為了在預測過程中獲得確定的結果,我們通常只對訓練樣例進行圖像增強,而不會在預測過程中使用帶有隨機操作的圖像增強。

深度學習框架提供了許多不同的圖像增強方法,可以同時應用。

14.1.4。練習

在不使用圖像增強的情況下訓練模型: 。比較使用和不使用圖像增強時的訓練和測試準確性。這個對比實驗能否支持圖像增強可以減輕過擬合的論點?為什么?train_with_data_aug(test_augs, test_augs)

在 CIFAR-10 數(shù)據(jù)集的模型訓練中結合多種不同的圖像增強方法。它會提高測試準確性嗎?

參考深度學習框架的在線文檔。它還提供了哪些其他圖像增強方法?

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 圖像增強
    +關注

    關注

    0

    文章

    53

    瀏覽量

    10015
  • pytorch
    +關注

    關注

    2

    文章

    795

    瀏覽量

    13088
收藏 人收藏

    評論

    相關推薦

    基于labview的紅外圖像增強

    基于labview的紅外圖像增強,如直方圖均衡、鄰域平均法都可以,希望大神們可以給出詳細的程序框圖,拜托~~~~~~~
    發(fā)表于 04-27 22:56

    圖像的對數(shù)增強問題

    如圖前兩張為一張圖片的R通道進行增強的小程序,可以實現(xiàn)讀片的增強,我想問各路大神,如果單純地從數(shù)組方面進行圖像增強,如對數(shù)增強,那該怎么操作
    發(fā)表于 09-26 17:13

    Pytorch模型訓練實用PDF教程【中文】

    PyTorch 提供的數(shù)據(jù)增強方法(22 個)、權值初始化方法(10 個)、損失函數(shù)(17 個)、優(yōu)化器(6 個)及 tensorboardX 的方法(13 個)進行了詳細介紹。本教程分為四章
    發(fā)表于 12-21 09:18

    如何設計基于FPGA的彩色圖像增強系統(tǒng)?

    在從圖像源到終端顯示的過程中,電路噪聲、傳輸損耗等會造成圖像質(zhì)量下降,為了改善顯示器的視覺效果,常常需要進行圖像增強處理。圖像
    發(fā)表于 10-21 07:52

    基于GFO算子的圖像增強算法如何去實現(xiàn)?

    基于GFO算子(廣義模糊算子)的圖像增強算法如何去實現(xiàn)?怎樣對圖像增強算法進行分析?
    發(fā)表于 06-04 06:24

    在Ubuntu 18.04 for Arm上運行的TensorFlow和PyTorch的Docker映像

    基于 Ubuntu 18.04 的 Docker 映像的腳本可從GitHub 上的 Arm 工具解決方案存儲庫獲得。完成的 TensorFlow 和 PyTorch 圖像包含:AArch64 的 Ubuntu
    發(fā)表于 10-14 14:25

    詳解各種圖像數(shù)據(jù)增強技術

    使用 PyTorch 動手實踐并實現(xiàn)圖像數(shù)據(jù)或計算機視覺中主要使用的數(shù)據(jù)增強技術。因為介紹的是數(shù)據(jù)增強技術。所以只使用一張圖片就可以了,我們先看看可視話的代碼import PIL.Im
    發(fā)表于 10-26 16:29

    基于Retinex圖像增強

    為了提高低照度圖像的亮度和對比度,提出了一種新的基于Retinex理論的彩色圖像增強方法。首先,基于Retinex理論,提出對HSV空間V分量進行域濾波估計圖像光照分量,然后將V分量與
    發(fā)表于 11-25 10:59 ?7次下載

    使用PyTorch提取CNNs圖像特征

    讓我們快速回顧一下第一篇文章中涉及的內(nèi)容。我們討論了PyTorch和張量的基礎知識,還討論了PyTorch與NumPy的相似之處。
    的頭像 發(fā)表于 05-05 08:52 ?7713次閱讀

    13種圖像增強技術的pytorch實現(xiàn)方法

    使用數(shù)據(jù)增強技術可以增加數(shù)據(jù)集中圖像的多樣性,從而提高模型的性能和泛化能力。
    發(fā)表于 11-04 10:28 ?791次閱讀

    基于PyTorch圖像數(shù)據(jù)增強技術

    因為介紹的是數(shù)據(jù)增強技術。所以只使用一張圖片就可以了,我們先看看可視話的代碼
    發(fā)表于 11-22 21:56 ?916次閱讀

    PyTorch教程4.2之圖像分類數(shù)據(jù)集

    電子發(fā)燒友網(wǎng)站提供《PyTorch教程4.2之圖像分類數(shù)據(jù)集.pdf》資料免費下載
    發(fā)表于 06-05 15:41 ?0次下載
    <b class='flag-5'>PyTorch</b>教程4.2之<b class='flag-5'>圖像</b>分類數(shù)據(jù)集

    PyTorch教程7.2之圖像卷積

    電子發(fā)燒友網(wǎng)站提供《PyTorch教程7.2之圖像卷積.pdf》資料免費下載
    發(fā)表于 06-05 10:13 ?0次下載
    <b class='flag-5'>PyTorch</b>教程7.2之<b class='flag-5'>圖像</b>卷積

    PyTorch教程14.1圖像增強

    電子發(fā)燒友網(wǎng)站提供《PyTorch教程14.1圖像增強.pdf》資料免費下載
    發(fā)表于 06-05 14:24 ?0次下載
    <b class='flag-5'>PyTorch</b>教程<b class='flag-5'>14.1</b>之<b class='flag-5'>圖像</b><b class='flag-5'>增強</b>

    使用PyTorch加速圖像分割

    使用PyTorch加速圖像分割
    的頭像 發(fā)表于 08-31 14:27 ?770次閱讀
    使用<b class='flag-5'>PyTorch</b>加速<b class='flag-5'>圖像</b>分割