resnet152训练_用fastai ResNet50训练CIFAR10,85%准确度

本文介绍了如何使用fastai框架训练CIFAR10数据集,通过ResNet50模型达到了85%的验证集准确率。训练过程包括数据预处理、模型创建、训练、调整学习率和模型保存等步骤。展示了训练结果、混淆矩阵和预测错误最多的类别。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值