After diving into the basics of deep learning and its history, I’ve been exploring the practical side of getting started with deep learning. Here are my key learnings and insights from working through the rest of fastai’s Chapter 1.
One of the most surprising insights I’ve gained is that the specific software you use for deep learning doesn’t really matter that much. While the book uses PyTorch and fastai (which are excellent choices), the authors emphasize that you can switch between libraries in just a few days. What really matters is understanding the core concepts and techniques.
That said, I found it interesting that PyTorch has become the fastest-growing deep learning library and is used in most research papers at top conferences. The fastai library builds on top of PyTorch, adding higher-level functionality that makes it easier to implement best practices.
The exciting part was building my first image classifier! With just a few lines of code, I created a model that could distinguish between dogs and cats with amazing accuracy:
from fastai.vision.all import *
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)
That’s it! Just these few lines of code gave me a model with over 99% accuracy. The most amazing part? We can use it right away:
# Load and predict on a new image
img = PILImage.create('my_cat.jpg')
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")
Here’s an example of how to create a segmentation model that can identify different objects in an image (crucial for self-driving cars):
path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func(
path, bs=8, fnames = get_image_files(path/"images"),
label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
codes = np.loadtxt(path/'codes.txt', dtype=str)
)
learn = unet_learner(dls, resnet34)
learn.fine_tune(8)
Want to analyze sentiment in text? It’s similarly straightforward:
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)
# Test it out
learn.predict("I really liked that movie!")
Even traditional tabular data can benefit from deep learning:
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)
dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
cat_names = ['workclass', 'education', 'marital-status', 'occupation',
'relationship', 'race'],
cont_names = ['age', 'fnlwgt', 'education-num'],
procs = [Categorify, FillMissing, Normalize])
learn = tabular_learner(dls, metrics=accuracy)
learn.fit_one_cycle(3)
One of the most important lessons was about validation sets. Here’s how we typically split our data:
# Always use validation_pct to hold back some data
dls = ImageDataLoaders.from_name_func(
path,
get_image_files(path),
valid_pct=0.2, # 20% for validation
seed=42, # For reproducibility
label_func=is_cat,
item_tfms=Resize(224)
)
For time series data, you might want to split by date instead:
# Example of time-based splitting (conceptual)
cutoff_date = '2023-12-31'
train_data = data[data.date <= cutoff_date]
valid_data = data[data.date > cutoff_date]
The biggest thing I’ve learned is that deep learning is much more accessible than I thought. The code is surprisingly simple - it’s the concepts and practices that matter most:
A typical deep learning workflow might look like this:
# 1. Get your data ready
dls = DataLoaders.from_folder(...) # Depends on your data type
# 2. Create a learner with a pre-trained model
learn = vision_learner(dls, resnet34, metrics=accuracy)
# 3. Fine-tune the model
learn.fine_tune(3)
# 4. Evaluate and iterate
learn.show_results()
I’m excited to continue this journey and see where deep learning takes me next. The field is moving incredibly fast, and it’s amazing to be part of it!
These are my notes and thoughts while working through Chapter 1 of the fastai book. Looking forward to sharing more as I continue learning!