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!