版權聲明:本文為博主原創文章,歡迎轉載,並請注明出處。聯系方式:460356155@qq.com
Fastai是在pytorch上封裝的深度學習框架,效果出眾,以下是訓練CIFAR10的過程。
導入庫
from fastai import * from fastai.vision import * from fastai.callbacks import CSVLogger, SaveModelCallback
驗證集上訓練結果計算和顯示
def show_result(learn): # 得到驗證集上的准確度 probs, val_labels = learn.get_preds(ds_type=DatasetType.Valid) print('Accuracy', accuracy(probs, val_labels)), print('Error Rate', error_rate(probs, val_labels))
訓練結果混淆矩陣及預測錯誤最多的類型顯示
def show_matrix(learn):
# 畫訓練結果的混合矩陣
interp = ClassificationInterpretation.from_learner(learn)
interp.confusion_matrix()
interp.plot_confusion_matrix(dpi=120)
# 顯示判斷錯誤最多的類型,min_val指定錯誤次數,默認1
# 打印順序為actual, predicted, number of occurrences.
interp.most_confused(min_val=5)
# 模型預測最困難的9個樣本顯示
# 顯示順序為預測值、實際值、損失值、預測對的概率
interp.plot_top_losses(9, figsize=(10, 10))
下載數據集,因調用linux的tar進行解壓,在windows下會出錯,可手動解壓,解壓后的目錄:
# 下載數據集 untar_data(URLs.CIFAR)
# 訓練數據目錄
path = Path(r'C:\Users\Administrator\.fastai\data\cifar10')
定義數據及數據在線增強方式
# 數據在線增強方式定義 tfms = get_transforms(do_flip=False) data = (ImageList.from_folder(path) # Where to find the data? -> in path and its subfolders .split_by_rand_pct() # How to split in train/valid? -> use the folders .label_from_folder() # How to label? -> depending on the folder of the filenames .add_test_folder() # Optionally add a test set (here default name is test) .transform(tfms, size=(32, 32)) # Data augmentation? -> use tfms with a size of 164 .databunch(bs=128) # Finally? -> use the defaults for conversion to ImageDataBunch .normalize(imagenet_stats))
查看數據
# 查看數據信息 data.classes, data.c, data
(['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'], 10, ImageDataBunch; Train: LabelList (39072 items) x: ImageList Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32) y: CategoryList airplane,airplane,airplane,airplane,airplane Path: C:\Users\Administrator\.fastai\data\cifar10; Valid: LabelList (9767 items) x: ImageList Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32) y: CategoryList airplane,deer,deer,deer,automobile Path: C:\Users\Administrator\.fastai\data\cifar10; Test: LabelList (10000 items) x: ImageList Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32),Image (3, 32, 32) y: EmptyLabelList ,,,, Path: C:\Users\Administrator\.fastai\data\cifar10)
創建訓練器
# 創建learn learn = cnn_learner(data, models.resnet50, metrics=[accuracy, error_rate], callback_fns=[ShowGraph, SaveModelCallback])
第一階段訓練
# 最佳學習率尋找 learn.lr_find(end_lr=1)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
# 畫出學習率尋找曲線,給出建議學習率 learn.recorder.plot(suggestion=True)
# 根據學習率曲線得到max_lr,開始訓練 learn.fit_one_cycle(cyc_len=15, max_lr=1.78e-2)
訓練結果
# 計算和顯示訓練結果 show_result(learn)
Accuracy tensor(0.8407)
Error Rate tensor(0.1593)
# 保存訓練模型
learn.save('stg1')
第二階段訓練
learn.load('stg1') learn.unfreeze() learn.lr_find(end_lr=1)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
learn.recorder.plot(suggestion=True)
learn.fit_one_cycle(15, slice(1e-6, 5e-5))