Introduction to Fastai
Fastai
is an open-source Python library built on top of PyTorch to make deep learning more accessible and user-friendly while being powerful enough for professionals. Developed by Jeremy Howard and Rachel Thomas, Fastai simplifies the complexities of training neural networks, enabling rapid prototyping, experimentation, and deployment of state-of-the-art machine learning models.
A key feature of Fastai is its layered API design, allowing both beginners and experts to benefit from the library. Beginners can easily use high-level abstractions for transfer learning, image classification, text classification, and tabular analysis, while advanced users have the flexibility to dig deeper and customize the lower-level components.
Fastai supports:
- Image processing (classification, segmentation, super-resolution, etc.)
- NLP tasks (text classification, language modeling, etc.)
- Tabular data (predictive modeling)
- Collaborative filtering (recommendation systems)
- Widget integration for easy deployment (using Fastai+Gradio or Fastai+Streamlit).
By prioritizing ease of use, speed of experimentation, and modularity, Fastai accelerates learning and empowers developers and data scientists to tackle diverse machine learning challenges with minimal effort.
20+ Useful Fastai
APIs with Examples
Below, we explore the most commonly used and helpful APIs provided by the Fastai
library. Each API is accompanied by an explanation and code snippet.
1. ImageDataLoaders.from_folder
Creates a DataLoader
from folders where images are organized into subdirectories for classification.
from fastai.vision.all import * # Create ImageDataLoaders path = untar_data(URLs.PETS)/'images' dls = ImageDataLoaders.from_folder(path, valid_pct=0.2, item_tfms=Resize(224)) dls.show_batch()
2. cnn_learner
Creates a Convolutional Neural Network (CNN) learner using a pretrained model.
learn = cnn_learner(dls, resnet34, metrics=error_rate) learn.fine_tune(3)
3. fit_one_cycle
Trains the model using the 1-cycle policy.
learn.fit_one_cycle(5)
4. lr_find
Helps identify the optimal learning rate.
learn.lr_find()
5. show_results
Displays predictions from the model.
learn.show_results()
6. TextDataLoaders.from_csv
Prepares a DataLoader
for text data from a CSV file.
from fastai.text.all import * path = untar_data(URLs.IMDB) dls_text = TextDataLoaders.from_csv(path, 'texts.csv', text_col='text', label_col='label')
7. text_classifier_learner
Builds a text classification learner.
learn = text_classifier_learner(dls_text, AWD_LSTM, drop_mult=0.5) learn.fine_tune(4)
8. TabularDataLoaders
Handles tabular data with categorical and continuous columns.
from fastai.tabular.all import * path = untar_data(URLs.ADULT_SAMPLE) dls_tab = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary", cat_names=["workclass", "education"], cont_names=["age", "hours_per_week"], procs=[Categorify, FillMissing, Normalize])
9. tabular_learner
Creates a learner for tabular data.
learn = tabular_learner(dls_tab, metrics=accuracy) learn.fit_one_cycle(3)
10. CollabDataLoaders.from_df
Creates a collaborative filtering DataLoader
for recommendation tasks.
from fastai.collab import * path = untar_data(URLs.ML_100k) dls_collab = CollabDataLoaders.from_csv(path/'u.data', item_name="item", user_name="user", rating_name="rating")
11. collab_learner
Builds a collaborative filtering learner.
learn = collab_learner(dls_collab, y_range=(0, 5)) learn.fit_one_cycle(5, 5e-3)
12. DataBlock
Provides a powerful API for creating custom data pipelines.
blocks = DataBlock(blocks=(ImageBlock, CategoryBlock), get_items=get_image_files, splitter=RandomSplitter(valid_pct=0.2), get_y=parent_label, item_tfms=Resize(224)) dls = blocks.dataloaders(path)
13. untar_data
Downloads and extracts pre-built datasets.
path = untar_data(URLs.MNIST_TINY) files = get_image_files(path)
Generic Application Using Fastai
APIs
Here’s an example of an end-to-end deep learning application using Fastai to classify the Pet dataset:
from fastai.vision.all import * # 1. Load data (Pets dataset) path = untar_data(URLs.PETS)/'images' dls = ImageDataLoaders.from_folder(path, valid_pct=0.2, item_tfms=Resize(224)) # 2. Create a model learn = cnn_learner(dls, resnet34, metrics=error_rate) # 3. Train the model learn.fine_tune(4) # 4. Evaluate results interp = ClassificationInterpretation.from_learner(learn) interp.plot_confusion_matrix() # 5. Save the model for future inference learn.export("pet_classifier.pkl") # 6. Load model (for new inference example) inference_model = load_learner("pet_classifier.pkl") prediction = inference_model.predict("some_image.jpg") print("Predicted class:", prediction)
This application demonstrates image classification using the Fastai library with a simple workflow: load data, train a model, evaluate it, and export it for inference. The Fastai
library significantly reduces the development time of such projects with its high-level APIs.