版权声明:本文为博主原创文章,欢迎转载,并请注明出处。联系方式: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',
'do