Workaround to using widgets in Colab

Hi.

I’m trying to use the widgets from fastai.widgets import *in Colab but as some users have pointed out, colab does not permit the use of widgets. A user suggested setting up a local Jupyter environment, connecting it to Drive and running the widget. My question is: How can I do this? I’ve never used widgets before, let alone running them on a different platform. Could someone guide me?

Thank you.

Unfortunately as you mentioned Colab does not cater for widgets that open new windows hence cannot use widgets for uploading files. For testing you can upload your images into colab and then by right clicking on the image name you can copy the path of the image.

Here is some code you can copy into colab and then just paste the copied path to get your result. Hope this helps somewhat. Not quite the same as a widget but you can easily choose image files within Colab.

def colab_img():
  from ipywidgets import widgets
  button_choice = widgets.Button(description="Predict", button_style='success')
  colab_img.output_variable = (widgets.Text())
  display(colab_img.output_variable)
  display(button_choice)
  def on_choice(b):
    j = Path(colab_img.output_variable.value)
    is_cat,_,probs = learn.predict(j)
    print(f"Is this a cat?: {is_cat}.") 
    print(f"Probability it's a cat: {probs[1].item():.6f}"
  button_choice.on_click(on_choice)

So what does a widget do exactly?

It can provide some extra functionalities and interactions for the user. For instance I have my ClassConfusion widget here: https://docs.fast.ai/widgets.class_confusion.html

It uses basic fastai functions in the background to tell the user a bit more about what’s happening in their models. Now I did manage to port it over into Colab, so it can be done, just widgets need to be written a little differently :slight_smile:

If you want a fair comparison it’s source code is here: https://github.com/muellerzr/ClassConfusion/blob/master/classConfusion.py

1 Like

Hello Zach.

Great work!

To utilize this function, I see that this is the import from ClassLosses import *. However, how do I import this to Google Colab? What is the pip install or its fastai parent?

You should clone the repositoryand %cd into it. (Also I need to update that readme, it should say from ClassConfusion import *)

Hi there,

I am new to colab and am trying to upload my own picture for 01_intro, uploading an image to the dog/cat classifier.

Can you explain the steps for uploading a photo with a bit more detail? I tried working through what you said above, but I don’t follow. It doesn’t matter if it uses the upload widget or if its copy and pasting a path to a photo - either is good with me.

Thank you so much!

It’s my first day with the Book. I’m just starting my AI journey.
With this code in a Colab cell, I’m able to upload a file from my Mac desktop:

from google.colab import files
from fastai.vision.all import *

#upload the image with the button that shows
uploaded = files.upload()

#Extract the first file’s content from the uploaded dictionary
file_name = next(iter(uploaded))
file_content = uploaded[file_name]

#Convert the file content into a BytesIO object and create a PIL image
from io import BytesIO
img = PILImage.create(BytesIO(file_content))

#Get the learning model
learn = vision_learner(dls, resnet34, metrics=error_rate)

#make the prediction
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}”)

I am seeing instances of false being returned with certain images that are clearly cats. Hopefully everything above is correct.