Fast.ai with Google Colab

arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True)
learn.fit(0.01, 2)
in colab this piece of code is taking about half an hour tell me how to do in less time?

I’m adding modified Google Colab compatible notebooks here: https://github.com/bhoomit/fastai-dl1-colab

2 Likes

Hi thanks but this doesnt work for it wont import fastai.transforms

thank you for this. the thing have to wait a few minutes to install the fastai + dataset.
I wonder once I set it up, can I just share the notebook and people does not have to download the fastai + dataset?

Hi Wiliam thank you for doing this. Do you see any advantage using Kaggle vs google colab for fastai?

also I wonder why do you need to modify the dataset and the way it was structure? can we just leave it as is?

Danny

I’m planning to attempt working through the course just in Colab as well. I was able to get the first lesson imports running in a colab notebook with a few minor changes, and without pip installing fastai.

I have the notebook set to Python 2 and using their GPU. I began by running the first 3 blocks of the official course nb (i.e. new cells in a fresh nb, using the first 3 cells from the official lesson1 nb)

Then in order to upload the models - I downloaded (or cloned) the course github locally, then used this block of upload code to bring in files to colab, so they can be imported in the nb (separate cells for each below, see *** note below before attempting this):

from google.colab import files
src = list(files.upload().values())[0]
open(‘vgg16.py’,‘wb’).write(src)
import vgg16

from google.colab import files
src = list(files.upload().values())[0]
open(‘vgg16bn.py’,‘wb’).write(src)
import vgg16bn

from google.colab import files
src = list(files.upload().values())[0]
open(‘utils.py’,‘wb’).write(src)
import utils

***Note - before doing this, Google colab uses the v2 of Keras, so I made some minor adjustments to the files prior to uploading.

In utils.py (on your machine) change line 42 to this: from keras.regularizers import l2, l1
and change line 45 to this: from keras.layers import deserialize as layer_from_config

in vgg16.py (on your machine) change line 177 in the fine tune function to this: self.ft(batches.num_classes)
and change line 213 in fit function to this:

self.model.fit_generator(batches, samples_per_epoch=batches.samples, nb_epoch=nb_epoch,
validation_data=val_batches, nb_val_samples=val_batches.samples)

OK, now you can go use those little upload script cells to bring the altered files in the notebook. Finally run this in a cell:

reload(utils)
from utils import plots

Doing that got the next section of the official notebook code to work for me (also change batch size to 4 instead of 64 when you first run it). I realize I could probably just import fastai with pip but I kinda like having it by hand with manually uploaded files so I know it will be reproducible, and then I know where the dependencies are causing issues when things like Keras change versions.

edit! also important to actually bring the zip data into your colab environment if you do it this way. can be achieved like this:

!wget http://files.fast.ai/data/dogscats.zip && unzip -qq dogscats.zip -d data/

you’ll then see the data in the colab file browser; also make sure to adjust path (from the official code) by running a cell with: path = “data/dogscats/”

Sorry for hijacking this thread as I am not able to find button to create new post.
I have been trying to use crestle to create an instance but its stuck for over an hour. I refreshed couple of times but it doesn’t move forward. Any idea how to solve this issue? crestle

as abdullah said this failed in importing fastai.transforms. (I ran !pip3 install “fastai<1”) and then the usual importing.

NameError                                 Traceback (most recent call last)
<ipython-input-8-94ce375e95cb> in <module>()
      1 from fastai.imports import *
----> 2 from fastai.transforms import *
      3 from fastai.conv_learner import *
      4 from fastai.model import *
      5 from fastai.dataset import *

/usr/local/lib/python3.6/dist-packages/fastai/transforms.py in <module>()
      3 from enum import IntEnum
      4 
----> 5 def scale_min(im, targ, interpolation=cv2.INTER_AREA):
      6     """ Scales the image so that the smallest axis is of size targ.

NameError: name 'cv2' is not defined

Hi @bhoomit,

Using your notebook, I am getting “module ‘torch’ has no attribute ‘float32’” error at
from fastai.transforms import *.

Any suggestions? Thanks.

I dont think you can really get it run faster on Colab. I guess Colab only give us access to the least amount of GPU. Maybe you can try with more GPU on Google Cloud Platform.

i have gotten part of lesson1 working on colab:

https://colab.research.google.com/drive/1wmMFcfvEQlu31YBQ3IoEQn-Rsylscrkj

as of 10-12-18 :slight_smile:

used alot of this code:
https://colab.research.google.com/github/corykendrick/fastai_in_colab/blob/master/Using_Google_Colab_for_Fastai.ipynb#scrollTo=aBlnbQ-wJNCY

i do have some type-errors being thrown, if anyone can suggest a fix. tia

update: found this linked on wiki and wish i has seen it two weeks ago. first heard of fast.ai from announcement of new version-1 … on twitter… my mistake for not reading up first


only use conda for courses and not pip …

really enjoying the videos… thanks

2 Likes

Can we run the ML courses on Kaggle?

Reset your runtime and try again. It happened to me too and reseting runtime solved it.

its not working now getting error:
**
ImportError: cannot import name ‘as_tensor’**
Looks like because of fast.ai v1 there is some issue with fast.ai 0.7.

Any one solved this?

1 Like

yes,it not wok.
have you fixed it now?

Use this link and save yourself some time on setting up fast.ai on Google Collab!

1 Like

TypeError Traceback (most recent call last)
in ()
1
----> 2 download_images(path/file, dest, max_pics=20, max_workers=0)

/usr/lib/python3.6/pathlib.py in truediv(self, key)
895
896 def truediv(self, key):
–> 897 return self._make_child((key,))
898
899 def rtruediv(self, key):

/usr/lib/python3.6/pathlib.py in _make_child(self, args)
684
685 def _make_child(self, args):
–> 686 drv, root, parts = self._parse_args(args)
687 drv, root, parts = self._flavour.join_parsed_parts(
688 self._drv, self._root, self._parts, drv, root, parts)

/usr/lib/python3.6/pathlib.py in _parse_args(cls, args)
638 parts += a._parts
639 else:
–> 640 a = os.fspath(a)
641 if isinstance(a, str):
642 # Force-cast str subclasses to str (issue #21127)

TypeError: expected str, bytes or os.PathLike object, not dict

I received the following error while running lecture 2 notebook. Can someone tell me how can I get past this?

Can u post screenshot of code where u are getting error

/usr/local/lib/python3.6/dist-packages/fastai/imports/torch.py in ()
1 import torch, torch.nn.functional as F
2 from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, LongTensor, ShortTensor, Tensor
----> 3 from torch import nn, optim, as_tensor
4 from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset

ImportError: cannot import name ‘as_tensor’

doesn’t work for me.

Thanks for posting this. As of today, this version of pytorch is available. I will try and see if there’s any issue with fastai module.

!pip3 install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision