Convert Figma logo to code with AI

fastai logofastai

The fastai deep learning library

26,904
7,590
26,904
245

Top Related Projects

188,828

An Open Source Machine Learning Framework for Everyone

88,135

Tensors and Dynamic neural networks in Python with strong GPU acceleration

scikit-learn: machine learning in Python

62,735

Deep Learning for humans

37,573

DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

Quick Overview

fastai is a deep learning library built on top of PyTorch, designed to make deep learning more accessible and efficient. It provides high-level APIs for common tasks in machine learning and computer vision, while also allowing for low-level customization when needed.

Pros

  • Easy to use, with a focus on simplicity and productivity
  • Implements many best practices and cutting-edge techniques out of the box
  • Excellent documentation and learning resources, including free courses
  • Supports a wide range of tasks, from computer vision to NLP and tabular data

Cons

  • May abstract away some lower-level details, which could be a drawback for those wanting full control
  • Requires some familiarity with PyTorch for advanced customization
  • Can be opinionated in its approach, which might not suit all use cases
  • Less extensive ecosystem compared to some other deep learning frameworks

Code Examples

  1. Loading and preparing an image dataset:
from fastai.vision.all import *

path = untar_data(URLs.PETS)
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path/"images"), valid_pct=0.2,
    seed=42, label_func=lambda x: x[0].isupper(),
    item_tfms=Resize(224), batch_tfms=aug_transforms()
)
  1. Training a vision model:
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(3)
  1. Making predictions:
img = PILImage.create('path/to/image.jpg')
pred, pred_idx, probs = learn.predict(img)
print(f"Prediction: {pred}; Probability: {probs[pred_idx]:.4f}")

Getting Started

To get started with fastai, follow these steps:

  1. Install fastai and its dependencies:
pip install fastai
  1. Import the necessary modules:
from fastai.vision.all import *
  1. Load a dataset and create a DataLoader:
path = untar_data(URLs.MNIST_TINY)
dls = ImageDataLoaders.from_folder(path)
  1. Create and train a model:
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)

This will train a ResNet18 model on the MNIST dataset for 3 epochs using fastai's fine-tuning approach.

Competitor Comparisons

188,828

An Open Source Machine Learning Framework for Everyone

Pros of TensorFlow

  • More extensive ecosystem with broader industry adoption
  • Better support for deployment across various platforms
  • More fine-grained control over model architecture and training process

Cons of TensorFlow

  • Steeper learning curve, especially for beginners
  • More verbose code, often requiring more lines to accomplish tasks
  • Can be slower to prototype and experiment with new ideas

Code Comparison

FastAI:

from fastai.vision.all import *
path = untar_data(URLs.PETS)
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=lambda x: x[0].isupper(), item_tfms=Resize(224))
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

TensorFlow:

import tensorflow as tf
from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
88,135

Tensors and Dynamic neural networks in Python with strong GPU acceleration

Pros of PyTorch

  • More flexible and customizable for advanced users
  • Larger community and ecosystem of tools/libraries
  • Better support for distributed and multi-GPU training

Cons of PyTorch

  • Steeper learning curve for beginners
  • Requires more boilerplate code for common tasks
  • Less focus on high-level APIs for quick prototyping

Code Comparison

fastai:

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fit_one_cycle(4)

PyTorch:

model = resnet34(pretrained=True)
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
for epoch in range(4):
    for inputs, labels in dataloader:
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

Summary

FastAI is built on top of PyTorch and provides a higher-level API for rapid development and prototyping. It's more beginner-friendly and requires less code for common tasks. PyTorch, on the other hand, offers more flexibility and control, making it suitable for advanced users and research. PyTorch has a larger community and ecosystem, but comes with a steeper learning curve. The choice between the two depends on the user's experience level and specific project requirements.

scikit-learn: machine learning in Python

Pros of scikit-learn

  • Broader range of traditional machine learning algorithms
  • More extensive documentation and community support
  • Better suited for small to medium-sized datasets

Cons of scikit-learn

  • Less focus on deep learning and neural networks
  • Slower adoption of cutting-edge techniques
  • More verbose code for complex tasks

Code Comparison

scikit-learn:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)

fastai:

from fastai.tabular.all import *

data = TabularDataLoaders.from_df(df, y_names='target', procs=[Categorify, FillMissing, Normalize])
learn = tabular_learner(data, metrics=accuracy)
learn.fit(5)
predictions = learn.predict(test_df)

fastai focuses on simplifying complex deep learning tasks with fewer lines of code, while scikit-learn provides a more explicit approach to traditional machine learning algorithms. fastai excels in areas like computer vision and natural language processing, whereas scikit-learn offers a wider range of classical algorithms and is more suitable for smaller datasets and traditional machine learning tasks.

62,735

Deep Learning for humans

Pros of Keras

  • More extensive ecosystem with wider adoption and community support
  • Greater flexibility in backend choices (TensorFlow, Theano, CNTK)
  • Easier integration with TensorFlow for production deployment

Cons of Keras

  • Steeper learning curve for beginners
  • Less opinionated, requiring more configuration decisions
  • Slower development of cutting-edge features compared to fastai

Code Comparison

Keras:

from keras.models import Sequential
from keras.layers import Dense

model = Sequential([
    Dense(64, activation='relu', input_shape=(784,)),
    Dense(10, activation='softmax')
])

fastai:

from fastai.vision.all import *

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fit_one_cycle(4)

The Keras example shows a basic neural network setup, while the fastai example demonstrates its high-level API for quickly creating and training a CNN model. fastai's approach is more concise and abstracts away many implementation details, making it easier for beginners to get started with deep learning. However, Keras offers more granular control over model architecture and training process, which can be beneficial for more advanced users or specific use cases.

37,573

DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.

Pros of DeepSpeed

  • Focuses on large-scale distributed training and optimization
  • Offers advanced memory efficiency techniques like ZeRO
  • Provides seamless integration with popular frameworks like PyTorch

Cons of DeepSpeed

  • Steeper learning curve for beginners
  • Less emphasis on high-level APIs and rapid prototyping
  • Requires more manual configuration for optimal performance

Code Comparison

DeepSpeed:

model_engine, optimizer, _, _ = deepspeed.initialize(
    args=args,
    model=model,
    model_parameters=params
)

fastai:

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fit_one_cycle(4)

DeepSpeed focuses on distributed training and optimization, offering advanced techniques for large-scale models. It provides fine-grained control but requires more setup. fastai, on the other hand, emphasizes ease of use and rapid prototyping with high-level APIs.

DeepSpeed excels in scenarios involving massive models and distributed environments, while fastai shines in quick experimentation and educational settings. The choice between them depends on the specific requirements of the project and the user's expertise level.

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

Pros of Transformers

  • Broader range of pre-trained models and tasks
  • More extensive documentation and community support
  • Seamless integration with popular deep learning frameworks

Cons of Transformers

  • Steeper learning curve for beginners
  • Less focus on high-level abstractions for quick prototyping
  • Potentially more complex setup and configuration

Code Comparison

Transformers example:

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello, world!", return_tensors="pt")
outputs = model(**inputs)

Fastai example:

from fastai.text.all import *

dls = TextDataLoaders.from_folder(path, valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5)
learn.fit_one_cycle(1, 2e-2)

Both libraries offer powerful tools for deep learning tasks, but Transformers provides a wider range of pre-trained models and more extensive documentation. Fastai, on the other hand, focuses on simplifying the learning process and offers high-level abstractions for quick prototyping. The choice between the two depends on the specific project requirements and the user's experience level.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Welcome to fastai

CI PyPI Conda (channel
only) docs

Installing

You can use fastai without any installation by using Google Colab. In fact, every page of this documentation is also available as an interactive notebook - click “Open in colab” at the top of any page to open it (be sure to change the Colab runtime to “GPU” to have it run fast!) See the fast.ai documentation on Using Colab for more information.

You can install fastai on your own machines with conda (highly recommended), as long as you’re running Linux or Windows (NB: Mac is not supported). For Windows, please see the “Running on Windows” for important notes.

We recommend using miniconda (or miniforge). First install PyTorch using the conda line shown here, and then run:

conda install fastai::fastai

To install with pip, use: pip install fastai.

If you plan to develop fastai yourself, or want to be on the cutting edge, you can use an editable install (if you do this, you should also use an editable install of fastcore to go with it.) First install PyTorch, and then:

git clone https://github.com/fastai/fastai
pip install -e "fastai[dev]"

Learning fastai

The best way to get started with fastai (and deep learning) is to read the book, and complete the free course.

To see what’s possible with fastai, take a look at the Quick Start, which shows how to use around 5 lines of code to build an image classifier, an image segmentation model, a text sentiment model, a recommendation system, and a tabular model. For each of the applications, the code is much the same.

Read through the Tutorials to learn how to train your own models on your own datasets. Use the navigation sidebar to look through the fastai documentation. Every class, function, and method is documented here.

To learn about the design and motivation of the library, read the peer reviewed paper.

About fastai

fastai is a deep learning library which provides practitioners with high-level components that can quickly and easily provide state-of-the-art results in standard deep learning domains, and provides researchers with low-level components that can be mixed and matched to build new approaches. It aims to do both things without substantial compromises in ease of use, flexibility, or performance. This is possible thanks to a carefully layered architecture, which expresses common underlying patterns of many deep learning and data processing techniques in terms of decoupled abstractions. These abstractions can be expressed concisely and clearly by leveraging the dynamism of the underlying Python language and the flexibility of the PyTorch library. fastai includes:

  • A new type dispatch system for Python along with a semantic type hierarchy for tensors
  • A GPU-optimized computer vision library which can be extended in pure Python
  • An optimizer which refactors out the common functionality of modern optimizers into two basic pieces, allowing optimization algorithms to be implemented in 4–5 lines of code
  • A novel 2-way callback system that can access any part of the data, model, or optimizer and change it at any point during training
  • A new data block API
  • And much more…

fastai is organized around two main design goals: to be approachable and rapidly productive, while also being deeply hackable and configurable. It is built on top of a hierarchy of lower-level APIs which provide composable building blocks. This way, a user wanting to rewrite part of the high-level API or add particular behavior to suit their needs does not have to learn how to use the lowest level.

Layered API

Migrating from other libraries

It’s very easy to migrate from plain PyTorch, Ignite, or any other PyTorch-based library, or even to use fastai in conjunction with other libraries. Generally, you’ll be able to use all your existing data processing code, but will be able to reduce the amount of code you require for training, and more easily take advantage of modern best practices. Here are migration guides from some popular libraries to help you on your way:

Windows Support

Due to python multiprocessing issues on Jupyter and Windows, num_workers of Dataloader is reset to 0 automatically to avoid Jupyter hanging. This makes tasks such as computer vision in Jupyter on Windows many times slower than on Linux. This limitation doesn’t exist if you use fastai from a script.

See this example to fully leverage the fastai API on Windows.

We recommend using Windows Subsystem for Linux (WSL) instead – if you do that, you can use the regular Linux installation approach, and you won’t have any issues with num_workers.

Tests

To run the tests in parallel, launch:

nbdev_test

For all the tests to pass, you’ll need to install the dependencies specified as part of dev_requirements in settings.ini

pip install -e .[dev]

Tests are written using nbdev, for example see the documentation for test_eq.

Contributing

After you clone this repository, make sure you have run nbdev_install_hooks in your terminal. This install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks.

After making changes in the repo, you should run nbdev_prepare and make additional and necessary changes in order to pass all the tests.

Docker Containers

For those interested in official docker containers for this project, they can be found here.