Behind_three_codes_high_performance_classifier三行代码构建一个深度学习AI图像分类识别器背后的内容_wizard_新浪博客

With the help of fastai library, you can build a high performance image classifier with just three codes like this:

tfms=tfms_from_model(arch,sz,aug_tfms=trnsforms_side_on, max_zoom=1.2)
md=ImageClassifierData.form_csv(path,'train',labels_csv,tfms=tfms,bs=bs)
learn=ConvLearner.pretrained(arch,md,precompute=True,ps=0.5)

This classifier can get a accuracy no lower than 97% on  the cats/dogs images set. It looks so magical! What's behind it?
1. First of all, it is built on pytorch calculation platform.
2. Second, the fastai has done a lots of things to simplify the work of data preparing, data loading. 
Pytorch development group have provided a plenty of tutorial resources .You can get into pytorch on its website. In this page, I just want to explain the detail of data processing procedure of fastai library. 

To understand the three codes, you must follow the data loading steps. Let's start with a line of data retrieving codes:

x,y=next(iter(md.val_dl))

executing the above code line, you can retrieve a batch of image data tensors into x with corresponding labels into y. What's a simple statement! just like a magic. How it complete the complicate data preparing, data augmentation, and data loading. To look into it clearly, you just need use a debugger like visual studio code or pycharm to debug it. Set a breakpoint at dataset.py->open_imgae(fn), start debugging you program. when the program pause at the breakpoint, you can get a clear view of call stack like following:

next(iter(md.val_dl))

-->Dataloader.__iter__(self)
---->Dataloader.get_batch()
------>self.dataset[i]
-------->BaseDataset.__getitem__(self,idex)
---------->self.getitem(idx)
-------------->FileDataset.get_x(self,i)
------------------>open_image(os.path.join(self.path,sefl.fnames[I]))
-------------->FileDataset.get(self.transform,x,y)
----------------->Transforms(x,y).   # self.transform is a instance of Transforms with __call__(self,im,y=None)
------------------->Transforms.compose(im,y,self.tfms)
--------------------->for fn in tfms im,y=fn(im,y)

Thanks to the developer of the fastai library, the symbols in the above codes is self-explanatory. You can understand the purpose of each Class and each member function clearly. Your calling to next(iter(md.val_dl)) activates a chain of member functions of Dataloader, Dataset and Transforms. These member functions complete different parts of data shuffling, reading, and transformation as you specified in ImageClassifierData.from_csv() function calling. Look into the details of these Classes and functions can make you get into the AI data preparing deeply. So, don't wait. Take a debugger, run it step by step!



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值