Fixing _share_cuda_ Unsupported Operation and Out of Memory Errors with fastai lessons

As mentioned before, I am trying to setup and run the fastai notebooks locally to get some hands-on exposure to deep learning. Unfortunately, if you are running this on Windows or have a very low-end GPU environment like myself you will run into additional problems.

First, on Windows, I was hitting the error _share_cuda_ operation not supported. You can fix this error by adding num_workers=0 parameter to the ImageDataLoaders call.

The next error I hit was CUDA out of memory – You can fix this by adding the bs=16 parameter (fine tune for your environment to optimize for speed without crashing – for me 64 hit OOM, 32 crashed the GPU and 16 balanced speed vs stability). The modified cell with all the fixes is below:

#id first_training
#caption Results from the first training
# CLICK ME
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), num_workers=0, bs=16)

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

After this, I hit the _share_cuda_ operation not supported error in this block as well:

img = PILImage.create(uploader.data[0])
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}")

Unfortunately, learn.predict does not have a parameter that allows us to set num_workers=0 which is required under Windows for me. I ended up having to edit learner.py in the fastai package and explicitly pass n_workers=0 in the call to get_preds. This allowed me to complete the first cat/dog classifier successfully!

Leave a Reply

Your email address will not be published. Required fields are marked *