【一看就懂】fastAI實戰:MNIST識別+常見函數講解

什麼是FASTAI

  • 是PyTorch的頂層框架,理念就是讓神經網絡變得非常假肚腩!!
  • FastAI類似Keras,但是對新手更加友好。FastAI的後端框架PyTorch, 而Keras的後端框架是TensorFlow。個人感覺,掌握難度排行:
    Tensorflow>PyTorch>Keras>FastAI
  • 現在Kaggle的比賽中,很多人逐漸使用FastAI了。(我太難了,剛學了PyTorch,現在又要開始FastAI,哭哭)
    這個是Fastai的官網

安裝就不講了,其實就是
pip install torch
pip install fastai
哦,安裝講完了

依賴

這個作爲擴展知識,這個是官方給出的核心模塊以來關係
在這裏插入圖片描述

實戰

實戰中的所有英文都可以不看,下面都有我的翻譯。如果有人覺得翻譯的不清晰或者有錯誤,這時候以英文原文爲主。

from fastai.vision import models, URLs, ImageDataBunch, cnn_learner, untar_data, accuracy, get_transforms
# 乾脆from fastai.vision import *

path = untar_data(URLs.MNIST_SAMPLE)  # 下載數據集,這裏只是MNIST的子集,只包含3和7的圖像,會下載並解壓(untar的命名原因)到/root/.fastai/data/mnist_sample(如果你是root用戶)下,包含訓練數據,測試數據,包含label的csv文件
tfms = get_transforms(do_flip=False)
data = ImageDataBunch.from_folder(path,ds_tfms = tfms)  # 利用ImageDataBunch讀取文件夾,返回一個ImageDataBunch對象
learn = cnn_learner(data, models.resnet18, metrics=accuracy)  # 構建cnn模型,使用resnet18預訓練模型
learn.fit(1)  # 訓練一輪

untar_data

Download url to fname if dest doesn’t exist, and un-tgz to folder dest.
In general, untar_data uses a url to download a tgz file under fname, and then un-tgz fname into a folder under dest.
就是下載並且解壓縮

URLs

目前支持這麼多的數據庫:
S3_NLP, S3_COCO, MNIST_SAMPLE, MNIST_TINY, IMDB_SAMPLE, ADULT_SAMPLE, ML_SAMPLE, PLANET_SAMPLE, CIFAR, PETS, MNIST.
但是現在好像莫名不能使用MNIST下載數據集,奇怪

get_transform()

  • 哎,現在已經不用那麼複雜的用PyTorch的randomRatation啊什麼的什麼的Compose啊構建transform,現在只用來一個get_transform()就可以了,真好啊。
  • get_transforms returns a tuple of two lists of transforms: one for the training set and one for the validation set (we don’t want to modify the pictures in the validation set, so the second list of transforms is limited to resizing the pictures).
  • 首先這個get_transform返回的是兩個元素的list,第一個是訓練集,第二個是驗證集。因爲我們一般都不會對驗證集進行圖像增強的操作,所以驗證機的tranform只會有resize這一個。
    在這裏插入圖片描述

我們來看下這個函數的參數:

get_transforms(do_flip:bool=True, flip_vert:bool=False, max_rotate:float=10.0, max_zoom:float=1.1, max_lighting:float=0.2, max_warp:float=0.2, p_affine:float=0.75, p_lighting:float=0.75, xtra_tfms:Optional[Collection[Transform]]=None) → Collection[Transform]

Utility func to easily create a list of flip, rotate, zoom, warp, lighting transforms.

  • do_flip: if True, a random flip is applied with probability 0.5
  • flip_vert: requires do_flip=True. If True, the image can be flipped vertically or rotated by 90 degrees, otherwise only an horizontal flip is applied
  • max_rotate: if not None, a random rotation between -max_rotate and max_rotate degrees is applied with probability p_affine
  • max_zoom: if not 1. or less, a random zoom between 1. and max_zoom is applied with probability p_affine
  • max_lighting: if not None, a random lightning and contrast change controlled by max_lighting is applied with probability p_lighting
  • max_warp: if not None, a random symmetric warp of magnitude between -max_warp and maw_warp is applied with probability p_affine
  • p_affine: the probability that each affine transform and symmetric warp is applied
  • p_lighting: the probability that each lighting transform is applied
  • xtra_tfms: a list of additional transforms you would like to be applied
  • do_flip: 如果是True,圖像就會隨機翻轉
  • flip_vert:如果是False,圖像只會水平翻轉,如果是True,就會水平和數值一起翻轉,就好像圖片旋轉了90一樣

zoom_crop

zoom_crop(scale:float, do_rand:bool=False, p:float=1.0)
  • scale: Decimal or range of decimals to zoom the image
  • do_rand: If true, transform is randomized, otherwise it’s a zoom of scale and a center crop
  • p: Probability to apply the zoom
    如果do_rand是False的話,scale應該是一個float;否則的話,這個scale可以是a range of floats,然後zoom會有一個隨機的值在這個range裏面。
    這有一個例子:
tfms = zoom_crop(scale=(0.75,2), do_rand=True)
plots_f(2, 4, 12, 6, size=224)

在這裏插入圖片描述

rand_resize_crop

rand_resize_crop(size:int, max_scale:float=2.0, ratios:Point=(0.75, 1.33))
  • size: Final size of the image
  • max_scale: Zooms the image to a random scale up to this
  • ratios: Range of ratios in which a new one will be randomly picked
    這個方法好,這個方法是一個主要的方法,根據近期的Imagenet優勝者的方法。
    這個的max_scale就是先把圖片隨機的放大或者縮小,然後隨機決定一個新的長和寬,再把圖片拉伸成那個長和寬,然後再隨機剪裁?
    It determines a new width and height of the image after the random scale and squish to the new ratio are applied. Those are switched with probability 0.5. Then we return the part of the image with the width and height computed, centered in row_pct, col_pct if width and height are both less than the corresponding size of the image. Otherwise we try again with new random parameters.
    案例:
tfms = [rand_resize_crop(224)]
plots_f(2, 4, 12, 6, size=224)

在這裏插入圖片描述

fastai.vision.models

視覺來說,目前支持這些pre-trained models在這裏插入圖片描述

cnn_learner()

在這裏插入圖片描述

發佈了76 篇原創文章 · 獲贊 8 · 訪問量 7356
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章