Short answer is that yes, it is possible to run just using venv.
I did a fresh install and decided to skip using anaconda and just use a venv.
From windows powershell (running as admin) I did the following:
# make and than cd to the dir you want to use
cd A:\.code\py\standalone\2024\fastbook
# setup the venv
python -m venv fastai-env
# activate venv
.\fastai-env\Scripts\activate
# install pytorch (custom pip command here: https://pytorch.org/get-started/locally/)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# install fastai and jupyter notebook
pip install fastai
pip install notebook
I than started my IDE (Pycharm Pro) and selected the folder I created A:\.code\py\standalone\2024\fastbook
as the base dir for my project.
I selected the venv I created as my project interpreter.
I than created a regular py file and ran the following test code:
import fastai
from fastai.vision.all import *
from pathlib import Path
# Adjusted label_func to handle both str and Path objects
def label_func(f): return Path(f).name[0].isupper()
def main():
print(f"FastAI Version: {fastai.__version__}")
print(f"PyTorch Version: {torch.__version__}")
path = untar_data(URLs.PETS)
print(f'Image Path: {path}')
files = get_image_files(path / "images")
# Sanity check: Ensure files are loaded
if not files:
print("No image files found. Check the dataset path and contents.")
return
print(f"Number of files: {len(files)}")
dls = ImageDataLoaders.from_name_func(
path, files, label_func, item_tfms=Resize(224), bs=64, num_workers=0
)
# Additional sanity checks
print(f"Train DataLoader length: {len(dls.train)}")
print(f"Valid DataLoader length: {len(dls.valid)}")
# Display a batch to ensure DataLoader is working
dls.show_batch(max_n=4)
learn = vision_learner(dls, resnet34, metrics=error_rate)
# For debugging, we'll bypass lr_find and use a hardcoded learning rate
print("Starting training with a hardcoded learning rate...")
learn.fine_tune(1, base_lr=1e-3) # Example learning rate
print("Training complete!")
if __name__ == '__main__':
main()
I kept get NaN results but solved that by adding num_workers=0
to the ImageDataLoaders
code.
Which finally worked! Giving the results below:
FastAI Version: 2.7.14
PyTorch Version: 2.2.2+cu121
Image Path: C:\Users\H1\.fastai\data\oxford-iiit-pet
Number of files: 7390
Train DataLoader length: 92
Valid DataLoader length: 24
Starting training with a hardcoded learning rate...
epoch train_loss valid_loss error_rate time
0 0.227548 0.019236 0.008119 01:06
epoch train_loss valid_loss error_rate time
0 0.044756 0.014205 0.004060 01:14
Training complete!
Once this was working, I opened up the first lesson Jupyter notebook and got it working!
Fingers crossed that I don’t run into any other major issues in the next lessons. Happy to make a better structured guide or youtube video if people are interested. Really happy to be able to work on this on my local machine!