1、fastai简介
fastai是一款由Jeremy Howard和Rachel Thomas发起基于PyTorch的深度学习框架,旨在让机器学习不再”伟大”,更加“平易近人”,将AI的学习与使用,推广到更多人群。学习fastai,你将:
-
不需要很复杂的数学知识,Just high school math is sufficient(高中数学水平足以)
-
不需要很大的数据集
-
不需要很多昂贵的机器
为能够更好的推广学习,作者基于此专门出了一套在线课程,甚至还出了一本书(《fastai与PyTorch深度学习实践指南》,实体书有售),作者也很大方的提供在线版本(https://github.com/fastai/fastbook),允许大家不花费任何资费即可学习。不过个人还是建议有条件、有兴趣的同学可以购买实体书,支持下大佬的辛勤付出。
官网文档:fastai - Vision learnerAll the functions necessary to build Learner suitable for transfer learning in computer visionhttps://docs.fast.ai/vision.learner.html#cnn_learner上面是cnn_learn的函数介绍
2、使用fastai
通过谷歌的colab来快速的上手fastai。
!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.vision.all import *
解压数据集
url = "https://course.fast.ai/datasets"
path = untar_data(URLs.PETS)/'images'
查看图像
import cv2
import matplotlib.pyplot as plt
img_path = "/root/.fastai/data/oxford-iiit-pet/images/Abyssinian_1.jpg"
img = cv2.imread(img_path)
print(img.shape)
plt.imshow(img)
plt.show()
开始训练
def is_cat(x):
return x[0].isupper()
dls = ImageDataLoaders.from_name_func(path,get_image_files(path),valid_pct=0.2,seed=42,
label_func=is_cat,item_tfms=Resize(224))
learn = cnn_learner(dls,resnet34,metrics=error_rate)
learn.fine_tune(1)
进行预测
# 上传图像
uploader = widgets.FileUpload()
uploader
img = PILImage.create(uploader.data[0])
print(img.shape)
#进行预测并打印预测结果
is_cat,a,prob = learn.predict(img)
print(is_cat)
print(a)
print(f"{prob[1].item():.2f}")
print(prob)