Using databricks for fastai

Has anyone had success using Databricks? I’m running into the curious problem of how it handles html output. Things like show_batch don’t work, and neither its display nor displayHTML functions do anything with them.

1 Like

If you get this figured out, I’d be interested to hear about it. How did you get fast.ai installed in databricks? I’m pretty new to that platform.

No progress on the display problem, sadly! Installing it in the first place is easy, from the Cluster management page go to Libraries and have it install fastai from PyPI.

1 Like

I’m getting some really weird behavior from databricks even just trying to run some of the basic demos from the fastbook. When I executed the following cell from Chapter 2:

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))

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

I got this extended error:

Downloading: "https://download.pytorch.org/models/resnet34-333f7ec4.pth" to /root/.cache/torch/hub/checkpoints/resnet34-333f7ec4.pth

  0%|          | 0.00/83.3M [00:00<?, ?B/s]
 11%|█         | 8.88M/83.3M [00:00<00:00, 93.1MB/s]
 29%|██▊       | 23.9M/83.3M [00:00<00:00, 106MB/s] 
 47%|████▋     | 39.1M/83.3M [00:00<00:00, 118MB/s]
 65%|██████▌   | 54.3M/83.3M [00:00<00:00, 128MB/s]
 84%|████████▍ | 70.0M/83.3M [00:00<00:00, 137MB/s]
100%|██████████| 83.3M/83.3M [00:00<00:00, 150MB/s]
█
epoch     train_loss  valid_loss  error_rate  time    
█
0         0.184346    00:40       


TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not list

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-1250049137733405> in <module>
      8 
      9 learn = cnn_learner(dls, resnet34, metrics=error_rate)
---> 10 learn.fine_tune(1)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/callback/schedule.py in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
    159     "Fine tune with `freeze` for `freeze_epochs` then with `unfreeze` from `epochs` using discriminative LR"
    160     self.freeze()
--> 161     self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
    162     base_lr /= 2
    163     self.unfreeze()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    205             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    206             self.n_epoch = n_epoch
--> 207             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    208 
    209     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch
--> 197             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    198 
    199     @log_args(but='cbs')

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    189 
    190     def _do_epoch(self):
--> 191         self._do_epoch_train()
    192         self._do_epoch_validate()
    193 

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
    181     def _do_epoch_train(self):
    182         self.dl = self.dls.train
--> 183         self._with_events(self.all_batches, 'train', CancelTrainException)
    184 
    185     def _do_epoch_validate(self, ds_idx=1, dl=None):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    159     def all_batches(self):
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
    163     def _do_one_batch(self):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    177         self.iter = i
    178         self._split(b)
--> 179         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    180 
    181     def _do_epoch_train(self):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
    162 
    163     def _do_one_batch(self):
--> 164         self.pred = self.model(*self.xb)
    165         self('after_pred')
    166         if len(self.yb): self.loss = self.loss_func(self.pred, *self.yb)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    417 
    418     def forward(self, input: Tensor) -> Tensor:
--> 419         return self._conv_forward(input, self.weight)
    420 
    421 class Conv3d(_ConvNd):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-816c357b-8c7f-421c-bea5-a5c68ffa335a/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    414                             _pair(0), self.dilation, self.groups)
    415         return F.conv2d(input, weight, self.bias, self.stride,
--> 416                         self.padding, self.dilation, self.groups)
    417 
    418     def forward(self, input: Tensor) -> Tensor:

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not list

If anyone has any idea what could be going on here, I’d be very grateful. It looks like my organization is committing to Databricks as infrastructure and this could be problematic.

Here is a fix for errors displaying show_batch and show_result in Databricks notebooks.

Acknowledgement: Fabrizio Castrotorres in the engineering team at CZI.

Add this code to your notebook before you run any imports:

# Always execute this cell before the rest of the imports
import fastai.torch_core

def patched_display_df(df):
    """
    Have to use the databricks displayHTML function instead of iPython functionality:
    https://docs.databricks.com/notebooks/visualizations/index.html#displayhtml-function
    """
    print("calling patched_display_df ...")
    displayHTML(df.to_html())

original_display_df = fastai.torch_core.display_df

# This replaces the PyTorch display_df function
fastai.torch_core.display_df = patched_display_df

from fastai.text.all import *
from fastai.vision.all import *

This replaces the display_df the function in fastai’s core libraries with databricks’ displayHTML function. This works pretty well.

1 Like