Author | [email protected] compile | Flin source | analyticsvidhya
introduce
Fastai is a popular open source library for learning and practicing machine learning as well as deep learning. Jeremy Howard and Rachel Thomas founded Fast. Ai with the goal of making deep learning resources more accessible. All the detailed resources provided in fast. Ai, such as courses, software and research papers, are completely free.
In August 2020, FastAI_V2 was released, which promises faster and more flexible implementation of deep learning frameworks. The faSTAI course in 2020 combines the core concepts of machine learning and deep learning. It also introduces users to important aspects of model production and deployment.
In this article, I will discuss the techniques for building fast and simple image classification models introduced in the first three lessons of the Fast. Ai beginner’s course. As you build the model, you’ll learn how to easily develop a Web application for the model and deploy it to production.
This article will follow Jeremy’s top-down approach to teaching in his course. You will first learn about training image classifiers. Details about the models used for classification will be explained later. To understand this article, you must have a knowledge of Python, as Fastai is written in Python and built on PyTorch. We recommend that you run this code in Google Colab or Gradient, as we need GPU access and Fastai can be easily installed on both platforms.
Install, import, and load datasets
! pip install -Uqq fastbookCopy the code
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.vision.widgets import *
Copy the code
Install Fastai and import the necessary libraries. If you are using Colab, you must provide access to Google’s cloud drive to save files and images. You can download any image dataset from sources like Kaggle and Bing Image Search. Fast. Ai also has a large collection of images. I used information from github.com/ieee8023/co… A series of chest X-ray images of
path = Path ('/content/gdrive/My Drive/Covid19images')
Copy the code
Save the Path to the location of the data set in the Path() object. If you use the fast. Ai data set, you can use the following code:
path = untar_data(URLs.PETS)/'images'
Copy the code
This will download and extract images from the Fastai PETS dataset collection.
Examine the image path and display some sample images from the dataset. I have used the Python Imaging Library (PIL) for this purpose.
path.ls
from PIL import Image
img = Image.open(path'/ train/covid / 1 - s2.0 - S1684118220300682 - main. PDF - 002 - a2. PNG')
print(img.shape)
img.to_thumb(128.128)
Copy the code
In this image classification problem, I will train the model to classify X-ray images as COVID or No COVID. The pre-processed data set has been placed in separate COVID and No COVID folders (source: ChristianTutivenGalvez).
If you are using a fast. Ai dataset, use the following function to group images by pet name:
def is_cat(x) : return x[0].isupper()
Copy the code
PETS is a collection of cat and dog images. Cat pictures are marked with the first letter of a capital letter, so they are easy to classify.
Image transformation
Image transformation is the key step of training image model. It is also known as data augmentation. In order to avoid model overfitting, image transformation must be carried out. You can use a variety of methods to transform images, such as resizing, cropping, compressing, and filling. However, compression and padding grab the original information in the image and add other pixels, respectively. Therefore, random resizing of images can produce good results.
In this method, random regions of each image are sampled at each period, as shown in the following example. This allows the model to learn more detail about each image and thus achieve greater accuracy.
Another important point to keep in mind is to always transform only the training image and never modify the validation image. In the Fastai library, this problem is handled by default.
item_tfms=Resize(128, ResizeMethod.Squish))
item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros')
item_tfms=RandomResizedCrop(128, min_scale=0.3) - 30% of the image area is zoomed by specifying 0.3
Copy the code
The Fastai library provides a set of standard extensions via aug_transforms functions. If the image size is uniform, it can be applied in batches to save a lot of training time.
tfms = aug_transforms(do_flip = True, flip_vert = False, mult=2.0)
Copy the code
The DataLoaders class in Fastai makes it easy to store the various objects used to train and validate models. If you want to customize the objects to be used during training, you can use the DataBlock class with DataLoaders.
data= ImageDataLoaders.from_folder(path,train = "train", valid_pct=0.2, item_tfms=Resize(128), batch_tfms=tfms, bs = 30, num_workers = 4)
Copy the code
If you define an image label in a metafile, you can use DataBlock to split the image and label into two different blocks, as shown in the code snippet below. The defined data block is used with the data loader function to access the image.
Data = DataBlock( blocks=(ImageBlock, CategoryBlock), get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42), get_y=parent_label, item_tfms=Resize(128))
dls = Data.dataloaders(path)
Copy the code
Model training
To train the image data set, a pre-trained CNN model is used. This method is called transfer learning. Jeremy suggests using pre-trained models to speed up training and improve accuracy. This is especially true for computer vision problems.
learn = cnn_learner(data, resnet34, metrics=error_rate)
learn.fine_tune(4)
Copy the code
The ResNet34 architecture was used and the results were validated against error rates. Since pre-trained models are used for training, fine-tuning methods are used instead of fitting models.
You can run more periods and see the performance of the model. Choose the right period to avoid overfitting.
Instead of using error_rate, try using accuracy (accuracy = 1- error rate) to verify model performance. Both are used to validate the output of the model. In this example, 20% of the data is reserved for validation. Therefore, the model will train only 80% of the data. This is a critical step in checking the performance of any machine learning model. You can also run this model by changing the ResNet layer (options 18, 50, 101, and 152). Again, this can lead to overfitting unless you have a large data set that will produce accurate results.
Validate model performance
Model performance can be verified in different ways. One popular approach is to use obfuscation matrices. The diagonal values of the matrix indicate correct predictions for each category, while the other cell values indicate many incorrect predictions.
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
Copy the code
Fastai provides a useful feature to view incorrect predictions based on the highest miss rate. The output of this function indicates the prediction label, target label, loss rate, and probability values for each image. High probability means that the model has high confidence. It varies between zero and one. A high loss rate indicates how poor the model performance is.
interp.plot_top_losses(5, nrows=1, figsize = (25.5))
Copy the code
Another great Fastai feature, ImageClassifierCleaner (GUI), can remove faulty images by removing them or renaming their labels. This is very helpful for data preprocessing, thus improving the accuracy of the model.
Jeremy recommends not running this feature until you have done basic training on the image, as this will give you an idea of the kinds of anomalies in the dataset.
from fastai.vision.widgets import *
cleaner = ImageClassifierCleaner(learn)
cleaner
Copy the code
Save and deploy the model
Once you have trained the model and are satisfied with the results, you can deploy the model. To deploy the model into production, you need to save the model architecture and the parameters to train it. To do this, an export method is used. The exported model is saved as a PKL file, which is a file created by pickle (Python modules).
learn.export()
Copy the code
Create an inference learner from the exported file that can be used to deploy the model as an application. The inference learner predicts the output of a new image one at a time. The prediction returns three parameters: the prediction category, the index of the prediction category, and the probability of each category.
learn_inf = load_learner(path/'export.pkl')
learn_inf.predict("img")
Copy the code
(‘ noCovid ‘, tensor(1), tensor([5.4443E-05, 9.9995E-01]) — Prediction
There are several ways to create Web applications for deploying models. One of the simplest ways to do this is to use the IPython widget as a GUI component to create the required objects for the application in Jupyter Notebook.
from fastai.vision.widgets import *
btn_upload = widgets.FileUpload()
out_pl = widgets.Output()
lbl_pred = widgets.Label()
Copy the code
After designing the application elements, deploy the model using Voila that runs Jupyter Notebook as a Web application. It removes all cell input and displays only the model output. To view notebook as a Voila Web application, replace the word “notebook” with “voila/render” in your browser URL. Voila must be installed and executed in the same notebook that contains the trained model and IPython widgets.
! pip install voila ! jupyter serverextension enable voila --sys-prefixCopy the code
conclusion
Just like that, you’ve built and deployed a cool image classifier application using the Fastai library in just eight steps! This is just the tip of the iceberg that I’ve shown in this article. There are more FASTAi components available for a variety of deep learning use cases related to NLP and computer vision that you can explore.
Here are the Fastai learning resources, along with my Git repo, which contains the code and images for the image classifier explained in this article.
-
COVID-19 X-ray Image Classifier: Contains the complete code and data set discussed in this article
- Github.com/RajiRai/Fas…
-
Covers all courses taught in the FAST. Ai course
- Github.com/fastai/fast…
-
Full fastai API documentation is covered
- docs.fast.ai/
-
Fast. Ai Community Forum
- forums.fast.ai/
The original link: www.analyticsvidhya.com/blog/2020/1…
Welcome to panchuangai blog: panchuang.net/
Sklearn123.com/
Welcome to docs.panchuang.net/