A walk with fastai2 - Vision - Study Group and Online Lectures Megathread

I won’t be able to look at it for a few hours @akschougule but try running the examples for multi label here: https://github.com/fastai/fastai2/blob/master/nbs/50_datablock_examples.ipynb

1 Like

Well this works

import fastai2
fastai2.__version__

but I get the same error at dls = planet.dataloaders(df)

Code with reproduced error here

fyi

  • I am creating a slink (ln -s) to fastai2 in my project dir.
  • local machine Ubuntu 18.04
  • upgraded to fastai2 yesterday evening

update: Now we know something new. Ran the datablock notebook and found CategoryBlock, ImageBlock(cls=PILMask), PointBlock works fine but MultiCategoryBlock breaks.

!pip show fastai2 works too. you can see the version below in the output cell. you are 0.0.10 too.
Very strange that it works for me.
Are you using collab? (i am on collab)

A very basic question on the style xfr nb.

What is/are vgg19.features - are they activations? (I assume but is that correct?).
Also typically for attributes/methods etc I can typically do vgg. and the nb provides options for completion.
In this case features does not come up - so what is features in a python sense - attribute of the model, method of it?
Do all models have .features or is this true only of vgg models?

i think those are the layer group names.
We usually have a feature extractor(the body) and a classifier(the head).
Here if you print vgg out you’ll see : feature, avgpool and classification.
I don’t think it is a python thing. i think they jsut named it like that.
vgg19.features - are the layers in the feature extractor, since we got a pretrained network it has the pretrained weights. Activations are the output ie when the input is multiplied with these pretrained weights.
All models don’t have them but we do something similar by indexing into it ie m[0] :slight_smile:

We seem to be grabbing only specific ReLU layers from the Vgg16 and Vgg19 models. I have not yet read the relevant paper but is there a reason why these specific ReLU layers are being looked at. There are other ReLU layers in the model. If it is just go read the paper and u will understand that is fine too.

the video lecture will be a good starting point too. :slight_smile:
style transfer (starts ~59:14)

not yet found out why the last numbers are there in the config ‘20’ and ‘22’ for vgg16 and vgg19 respectively.
def get_stl_fs(fs): return fs[:-1] seems to remove them. Let us know if you figure out why

Thanks. I think features is the name of the layer group and by doing vgg19(pretrained=True).features.cuda().eval() you transfer all the layers in the feature group on to cuda and eval() them. That is what makes sense to me. Yes, we can grab a particular layer by indexing into the model like m[0] but that is grabbing one layer - here we seem to grab all the layers in the layer group called features - So by grabbing layer group name it seems you can grab all the layers in the layer group…hmm…interesting.

i agree since it is the feature extractor part of the network.

.eval() is turning the model from default ‘train’ mode to eval mode. There is a good discussion above regarding this.
Depending how the structure is - m[0] is usually a layer group, m[0][0] - is a layer. This depends on how things are wrapped in a list.
Layer group is what i think it is called i could be wrong about that. Layer groups is what we use while we do discriminative lrs, how we split the model into three parts.

You could do :
vgg_=list(vgg.children())
vgg_[0][:5]

On a very basic level, a layer group is any set of layers wrapped in a nn.Sequential(). So we could have one big layer group (one nn.Sequential model), two like we make with cnn_learner (body and head), or multiple (for instance a regular PyTorch model). You can check how many groups any model has by doing len(net) (where net is your model). If it gives an error, there is no ‘groups’ and instead they’re split into properties (the layer groups have names associated with them), and so we can also split via these names (IIRC there’s an example in the multi-output notebook)

To add a bit more to this, if they’re stored as properties they also can be individual layers themselves that we want to treat as a layer group. So when we split our layers via a splitter, it’ll do the wrapping.

3 Likes

Turns out that features is a method in vgg19. If you do dir(vgg19) you get features and a bunch of other things…

@Srinivas if you check the PyTorch documentation, you can see what that actually is doing: https://pytorch.org/docs/stable/_modules/torchvision/models/vgg.html

thanks - that is helpful in understanding more about layers and layer groups.
Also I noticed that if you do vgg19.summary you get that features is
<bound method Module.summary of VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace=True) and so on

while if you do feature_net.summary you get that it is
<bound method Module.summary of Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace=True)

So notice that features is a Sequential of layers while vgg is not

Summary needs to be called as a function. (Any sort of bound method error means this). IE summary()

But…when I call vgg19(pretrained=True).summary() I get an error while if I
do not then I do get a summary.


TypeError Traceback (most recent call last)
in ()
----> 1 vgg19(pretrained=True).summary()

2 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
–> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

TypeError: forward() missing 1 required positional argument: ‘x’

I’d go read the documentation to learn what that’s actually doing and what it requires, as this is a PyTorch method not a fastai method :wink: (we call learn.summary() after a model is made and a Learner)

thx. will do.

No this is a fastai method. It’s just that if you call model.summary, you have to give an input of the model cause we can’t magically guess what kind of inputs the model expects.
learn.summary can magically guess since it has the data, which is why you don’t need to pass it here.

4 Likes

Got it. Thanks! (Did not know this!)

@Srinivas so you’d want to grab a batch of your data by doing

batch = dls.one_batch()[0]
and then do model.summary(batch)

yup - got that. Thanks @sgugger - that is why the error was asking for an x input to the model when I tried doing model.summary()