agentleFS
G1JoshiG1Joshi/Agent-Skills/skills/ai-ml/fastai/SKILL.md

fastai

Skill15 starsChanged 44 days agoMITView on GitHub

Expert fastai deep learning assistance covering high-level learners, vision/tabular/text pipelines, and PyTorch integration. Use when training fast, state-of-the-art neural networks with minimal boilerplate.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use lr_find() and fine-tune with slice learning rates:

    line 109
  2. Very specific:

    Always run learn.lr_find() before training and use learn.fine_tune() for pre-trained weights.

    line 124
  3. Very specific:

    Use aug_transforms() with resize-presizing (item_tfms=Resize(460), batch_tfms=aug_transforms(size=224)) to minimize blur.

    line 125
  4. Very specific:

    Call learn.export() to package architecture, weights, and pre-processing transforms together.

    line 126

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

fastai

fastai is a layered API on top of PyTorch. It popularized Transfer Learning and good defaults (One Cycle Policy).

When to Use

  • Rapid Deep Learning Prototyping: Building vision, NLP, and tabular models with layered abstractions on top of PyTorch.
  • Transfer Learning with State-of-the-Art Defaults: Automated discriminative learning rates, 1cycle scheduling, and mixup data augmentation.
  • Computer Vision Tasks: Image classification, semantic segmentation, and object detection with minimal boilerplate.
  • Learning Rate Optimization: Finding optimal learning rates automatically using lr_find().

Quick Start

from fastai.vision.all import *

# Train state-of-the-art vision classifier in 4 lines
path = untar_data(URLs.PETS)/'images'
def is_cat(x): return x[0].isupper()

dls = ImageDataLoaders.from_name_func(path, get_image_files(path), valid_pct=0.2,
                                     seed=42, label_func=is_cat, item_tfms=Resize(224))

learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

Core Concepts

Computer Vision Classifier with DataBlock API

Building an image classification pipeline with automated transforms:

from fastai.vision.all import *

# Define flexible data pipeline
data_block = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y=parent_label,
    item_tfms=Resize(460),
    batch_tfms=aug_transforms(size=224, min_scale=0.75)
)

# Load data from directory
dls = data_block.dataloaders('/path/to/dataset', bs=64)

# Create transfer learning learner with pre-trained ResNet/ConvNeXt
learn = vision_learner(dls, resnet50, metrics=accuracy)

# Find optimal learning rate
suggested_lr = learn.lr_find()
print(f"Optimal Learning Rate: {suggested_lr.valley}")

# Fine-tune using 1cycle policy
learn.fine_tune(epochs=4, base_lr=suggested_lr.valley)

Tabular Deep Learning with Categorical Embeddings

Training neural networks on structured tabular data:

from fastai.tabular.all import *

df = pd.read_csv('customers.csv')

cat_names = ['workclass', 'education', 'marital-status', 'occupation']
cont_names = ['age', 'fnlwgt', 'education-num', 'hours-per-week']
procs = [Categorify, FillMissing, Normalize]

splits = RandomSplitter(valid_pct=0.2)(range_of(df))
to = TabularPandas(df, procs=procs, cat_names=cat_names, cont_names=cont_names, y_names='salary', splits=splits)

dls = to.dataloaders(bs=128)
learn = tabular_learner(dls, layers=[200, 100], metrics=accuracy)
learn.fit_one_cycle(5, 1e-2)

Exporting and Serving Model for Inference

Serializing the learner pipeline into a production artifact:

# Export model and all preprocessing pipelines
learn.export('classifier_model.pkl')

# In production server:
inference_learn = load_learner('classifier_model.pkl')
prediction, pred_idx, probabilities = inference_learn.predict('test_image.jpg')

print(f"Prediction: {prediction}, Confidence: {probabilities[pred_idx]:.4f}")

Common Patterns

Learning Rate Finder and Discriminative Learning Rates

Problem: Picking arbitrary learning rates leads to divergence or slow convergence.

Solution: Use lr_find() and fine-tune with slice learning rates:

# 1. Find optimal learning rate
lr_valley = learn.lr_find().valley

# 2. Unfreeze model and train with discriminative learning rates
learn.unfreeze()
learn.fit_one_cycle(4, slice(1e-5, lr_valley))

Best Practices

Do:

  • Always run learn.lr_find() before training and use learn.fine_tune() for pre-trained weights.
  • Use aug_transforms() with resize-presizing (item_tfms=Resize(460), batch_tfms=aug_transforms(size=224)) to minimize blur.
  • Call learn.export() to package architecture, weights, and pre-processing transforms together.
  • Inspect misclassified instances using ClassificationInterpretation.from_learner(learn).plot_top_losses().

Don't:

  • Train pre-trained models with standard fit(); use fine_tune() to preserve backbone weights.
  • Process single inference images in raw PyTorch without applying fastai's exported transform pipeline.
  • Ignore class imbalance; supply custom weights to CrossEntropyLossFlat.

Troubleshooting

ErrorCauseSolution
CUDA out of memory in DataLoaderBatch size too large for GPU VRAM.Reduce batch size: dls = ... bs=32 or bs=16.
Can't get attribute '...' on <module '__main__'>Custom labelling function not pickled during learn.export().Define functions in importable module or export before script exits.
RuntimeError: Expected 4D tensor but got 3DSingle image passed without batch dimension to learn.predict.Use learn.predict(img) directly; it handles batch expansion.

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.