Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

之前一篇文章介绍了FastAI v0.7中处理dogscats数据集的过程。
这一篇则介绍一下FastAI v1.0版本变化后的dogscats图像分类的使用方法。相比较v0.7,v1.0整体来说api更加简洁清晰一些,特别是预测和评估的部分。

让我们开始吧

1.配置colab中的FastAI环境

!curl https://course.fast.ai/setup/colab | bash

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

2.导入包

from fastai import *
from fastai.vision import *

3.下载数据集文件

!wget http://files.fast.ai/data/dogscats.zip

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

4.解压数据集

!unzip dogscats.zip

5.加载数据集

path = "/content/dogscats/"
data = ImageDataBunch.from_folder(path,ds_tfms=get_transforms(), size=224,bs=32).normalize(imagenet_stats)

6.检查数据文件

data.show_batch(3,figsize=(8,8))

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

7.设置深度学习中的神经网络,选择预置的resnet50

learner = cnn_learner(data,models.resnet50,metrics=[accuracy])

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

8.显示训练结果

learner.fit(1)

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

这里需要稍等片刻,花了差不多8分半钟。

9.显示网络结构

print(learner.summary())

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

10.预测单张图片的结果

img = data.train_ds[0][0]
img.show()
learner.predict(img)

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

这里也可以使用upload方法上传本地图片来测试
v1.0中预测单张图片的方法似乎更加简单方便了。可以直接把图片传入predict方法中。

11.制作混淆矩阵

interp = ClassificationInterpretation.from_learner(learner)
interp.plot_confusion_matrix()

《Image classification with FastAI1.0.x, Colab and Python3(Dogs&Cats)》

^^

参考:
https://github.com/adi0229/ML-DL/blob/master/dogcat_fastaiv3.ipynb

    原文作者:一杯奶茶的功夫
    原文地址: https://www.jianshu.com/p/7584a4582f94
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞