Migrating from PyTorch


I am trying to use the Migrating notebook from fastai2 to try to use some PyTorch code in fastai2.

It seems that everything work until I tried to fit_one_cycle when I got an error about pbar . Disable it does not seem to solve the issue. Here is the code with the error.

I did a fresh install of fastai2, openslide-python (1.1.1) and fastprogress (0.2.2)

Any ideas how to solve the problem? Thanks

P.S.: This question was first here but I opened a new issue bc is kind of unrelated

It looks like the DataLoader could not pull the len of your dataset. Can you try len(ds) (where ds is what you give the DataLoader)?

Thanks @sgugger for the answer. It seems that the custom dataset build has an argument (mode) that, if not specified, does not generate the ds. However, things seems a little bit more complex than default migrating. I made a summary of what I think it does:

BACKGROUND: This pipeline uses openslide library to tile a very big image (WSI - a tissue histology image) and make a prediction in a weakly supervised way. The authors mean with this that they only have an overall label rather than a per tile label. The workflow goes more or less like:

  1. Perform predictions directly on all the tiles generated and got probabilities
  2. Reorder the tiles with the greatest probability
  3. Keep only the tiles with the greatest probability and generate a subset of images with the overall label (converting the problem to a fully supervised fashion)
  4. Perfrom training on this subset and update weights and optimizer

So, if I understood the code correctly, this pipeline perform a first prediciton step that is not suitable for fastai2 Dataloaders to work out of the box. Am I right? Do you think is there any possibility to implement this kind of pipeline in fastai2? Maybe is just enough to give the dataloader a first prediction and then the subsequent steps in the fastai2 pipeline will work?
Thanks :slight_smile:

I am trying to use a PyTorch model with fastai2 in Google Colab using the Migration tool from

from migrating_pytorch import *

raises the error: ModuleNotFoundError: No module named ‘migrating_pytorch’

Which also happened when running the Tutorial on Colab

Thank you for any help

1 Like

I guess the migrating_pytorch.py is missing. you can find them here:

Thank you,
beginner’s question: how do I load / download / integrate such a code from GitHub to Google Colab?
I couldn’t work it out myself when I found this page before posting my question.

Just add the the following line at the top of the notebook in Colab:

!wget https://raw.githubusercontent.com/fastai/fastai/master/nbs/examples/migrating_pytorch.py

you can run os commands in Jupiter notebooks with !<oscommand>

you should read the code in the .py file … the actual code is in there instead of the notebook imho.

Thank you
The migration part worked, but the learning / fit one cycle with the PyTorch model not yet.
Will have to figure that out.
Best regards