用fastai ResNet50训练CIFAR10,85%准确度
版权声明:本文为博主原创文章,欢迎转载,并请注明出处。联系方式: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))