KeremTurgutlu / self_supervised

Implementation of popular SOTA self-supervised learning algorithms as Fastai Callbacks.
Apache License 2.0
318 stars 33 forks source link

intro_tutorial.ipynb not running on google Colab (fastai v 2.7.6) #87

Open abauville opened 2 years ago

abauville commented 2 years ago

Thanks for the library, I'm really looking forward to use it, but I ran into some problems with the intro tutorial. I tried running the intro_tutorial.ipynb on google_colab. There are a few places where the code runs into bugs. This is because of fastai version fastai v2.7.6 fastcore v1.4.5

All the bugs I describe below are fixed when downgrading fastai to e.g. v2.3.0

Describe the bug

  1. Encoder bug =====================

e.g. in block 9: encoder = create_encoder("xresnet34", n_in=3, pretrained=False)

yields this error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-9-b3399242d3af>](https://localhost:8080/#) in <module>()
----> 1 encoder = create_encoder("xresnet34", n_in=3, pretrained=False)
      2 model = create_simclr_model(encoder, hidden_size=2048, projection_size=128)
      3 aug_pipelines = get_simclr_aug_pipelines(size=size, rotate=True, jitter=True, bw=True, blur=True, blur_s=(4,16), blur_p=0.25, cuda=False)
      4 learn = Learner(dls, model,loss_func=noop,cbs=[SimCLR(aug_pipelines, temp=0.07, print_augs=True),ShortEpochCallback(0.001)])

2 frames
[/usr/local/lib/python3.7/dist-packages/self_supervised/layers.py](https://localhost:8080/#) in create_encoder(arch, pretrained, n_in, pool_type)
     34 def create_encoder(arch:str, pretrained=True, n_in=3, pool_type=PoolingType.CatAvgMax):
     35     "A utility for creating encoder without specifying the package"
---> 36     if arch in globals(): return create_fastai_encoder(globals()[arch], pretrained, n_in, pool_type)
     37     else:                 return create_timm_encoder(arch, pretrained, n_in, pool_type)
     38 

[/usr/local/lib/python3.7/dist-packages/self_supervised/layers.py](https://localhost:8080/#) in create_fastai_encoder(arch, pretrained, n_in, pool_type)
     20 def create_fastai_encoder(arch:str, pretrained=True, n_in=3, pool_type=PoolingType.CatAvgMax):
     21     "Create timm encoder from a given arch backbone"
---> 22     encoder = create_body(arch, n_in, pretrained, cut=None)
     23     pool = AdaptiveConcatPool2d() if pool_type == "catavgmax" else nn.AdaptiveAvgPool2d(1)
     24     return nn.Sequential(*encoder, pool, Flatten())

[/usr/local/lib/python3.7/dist-packages/fastai/vision/learner.py](https://localhost:8080/#) in create_body(model, n_in, pretrained, cut)
     81     _update_first_layer(model, n_in, pretrained)
     82     if cut is None:
---> 83         ll = list(enumerate(model.children()))
     84         cut = next(i for i,o in reversed(ll) if has_pool_type(o))
     85     return cut_model(model, cut)

AttributeError: 'function' object has no attribute 'children'

===========

This is fixed by replacing the encoder as suggested in the README.md:

# encoder = create_encoder("xresnet34", n_in=3, pretrained=False) # a fastai encoder
encoder = create_encoder("tf_efficientnet_b4_ns", n_in=3, pretrained=False) # a timm encoder
  1. show bug ===================== Then, in block [19]: learn.sim_clr.show(n=10); yields the following error
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    [<ipython-input-19-1dbc22d0ad4a>](https://localhost:8080/#) in <module>()
    ----> 1 learn.sim_clr.show(n=10);

3 frames /usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py in decorate_context(*args, kwargs) 25 def decorate_context(*args, *kwargs): 26 with self.clone(): ---> 27 return func(args, kwargs) 28 return cast(F, decorate_context) 29

/usr/local/lib/python3.7/dist-packages/self_supervised/vision/simclr.py in show(self, n) 69 images = [] 70 for i in range(n): images += [x1[i],x2[i]] ---> 71 return show_batch(x1[0], None, images, max_n=len(images), nrows=n) 72 73 # Cell

/usr/local/lib/python3.7/dist-packages/fastcore/dispatch.py in call(self, *args, *kwargs) 121 elif self.inst is not None: f = MethodType(f, self.inst) 122 elif self.owner is not None: f = MethodType(f, self.owner) --> 123 return f(args, **kwargs) 124 125 def get(self, inst, owner):

/usr/local/lib/python3.7/dist-packages/fastai/data/core.py in show_batch(x, y, samples, ctxs, max_n, kwargs) 29 else: 30 for i in range_of(samples[0]): ---> 31 ctxs = [b.show(ctx=c, kwargs) for b,c,_ in zip(samples.itemgot(i),ctxs,range(max_n))] 32 return ctxs 33

AttributeError: 'list' object has no attribute 'itemgot'


------

A similar error is raised by the BYOL model, but the MOCO model works without error

---------------

Thanks in advance
alleniver commented 2 years ago

same problem with fastai 2.7.6,downupgrade to 2.6.3 is fine!