Fastai v2 chat

Hi,
is there a way to rename the first and the last layer of my model? or do i have to add a custom layer for that purpose?

I assume you mean when working with a pre-designed model (say Resnet50), since otherwise you can define your own names, right?

Of course one option is to copy/edit your own version. Another (perhaps better) idea is to think of your model as a Python object where your layers are attributes. Then you can rename your attributes, but it feels a bit weird.

I donā€™t know if this makes sense or if it will work, but you could try and let us know :sweat_smile:

Quick question: is it possible to load a learner (not just weights) from V1 using V2?

No, as they utilize two completely different frameworks. (You canā€™t even load a learner from fastai2 v 0.0.4 in 0.0.5 for instance because thereā€™s differences)

1 Like

I assumed this, but I wanted to dream for a second there. Do you know if there are pretrained language models for V2 somewhere? (Not only in English ideally!) I have looked for this for a bit, but my results are swarmed by V1ā€¦

I mean itā€™s just a PyTorch model, just use those weights :slight_smile: the architectures never changed :wink: (my Walk with fastai2 lectures went over using EfficientNet for instance and itā€™s exactly the same as v1)

1 Like

In this case I was trying to use a pickled language model, so I canā€™t very easily get access to the weights alone. But I should probably find another option for weights alone!

Why not load it first in a different version, save just the weights via learn.save(), then bring them in in v2? :slight_smile:

Yes, I was thinking about that now! It might be the easiest thing to do. Thanks for your help :slight_smile:

1 Like

Thanks for your answer, yes, i am using resnet34 at the moment. I need to export it to onnx format, which i did and after viewing it with netron i noticed the last layer is named: 368.
But can i find the layer name inside the learn.model somehow? I can only find what types of layers there are, but not their names.
Sorry, if this is a stupid question am quite new to fastai :slight_smile:

Iā€™m guessing a bit but I seem to remember that simply print(model) will give you this information.

And of course, no worries about ā€œstupidā€ questions. As you can see, I have been around for a while and I also ask my fair share of ā€œstupidā€ questions even today. Luckily, this is a rather nice community :slight_smile:

1 Like

with print(learn.model) i only get:

Sequential(
(0): Sequential(
(0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
ā€¦

but no names for the different layers.
in Netron i can see that for example the names of the first and last layer:
image
and ther mainly the name 368 is the problem, because i need that name as a property for a c# class of mine :sweat_smile:

Arenā€™t those the names of the layers, though? (Try print(model.summary) as well)

We needed to access one specific layer and we got the name this way. Note that when you have several of the same type you get names like Conv2d-3.

Hope this helps

Not quite, those are the types of the layes, as they do not get incresed names like you mentioned with the Conv2d-3
But it seems like (at least thats my understanding so far) that those numbers in the brackets to have something to do with the names as they get actual increasing names inside the basic block (conv1, conv2ā€¦)


but if i change those i can no longer export to onnx

:thinking: I was seeing a slightly different outputā€¦

Iā€™ve been trying to do this, but there is a problem that may be either trivial or very hard to solve.

The model I am trying to use is a Transformer XL, which is not currently implemented in Fastai V2 as far as I can see (itā€™s only mentioned once in the repo, and itā€™s commented out: fastai2/fastai2/text/models/ core.py).

I tried adding some imports there from Fastai V1, and it actually lets me get the model, but then it wonā€™t train ('DecoderLayer' object has no attribute 'requires_grad' and 'LMLearner' object has no attribute 'master_pgs', which is probably to be expected).

Any quick ideas? Otherwise I will look for a trained AWD_QRNN model, which should work fine.

Can you provide the code you used to build the model? And the imports? Iā€™d recommend manually porting over everything it requires and then using Module to build it up (this is what Iā€™ll eventually do)

1 Like

Iā€™m not proud of this, please donā€™t judge :sweat_smile:. This is what I tried, which was obviously too optimistic, but it was easy to do, soā€¦

from fastai.text.models.transformer import (TransformerXL, tfmerXL_lm_config,
                                            tfmerXL_lm_split, tfmerXL_clas_config,
                                            tfmerXL_clas_split)

# Cell
_model_meta = {AWD_LSTM: {'hid_name':'emb_sz', 'url':URLs.WT103_FWD, 'url_bwd':URLs.WT103_BWD,
                          'config_lm':awd_lstm_lm_config, 'split_lm': awd_lstm_lm_split,
                          'config_clas':awd_lstm_clas_config, 'split_clas': awd_lstm_clas_split},
           AWD_QRNN: {'hid_name':'emb_sz',
                      'config_lm':awd_qrnn_lm_config, 'split_lm': awd_lstm_lm_split,
                      'config_clas':awd_qrnn_clas_config, 'split_clas': awd_lstm_clas_split},
           # Transformer: {'hid_name':'d_model', 'url':URLs.OPENAI_TRANSFORMER,
           #               'config_lm':tfmer_lm_config, 'split_lm': tfmer_lm_split,
           #               'config_clas':tfmer_clas_config, 'split_clas': tfmer_clas_split},
           TransformerXL: {'hid_name':'d_model',
                          'config_lm':tfmerXL_lm_config, 'split_lm': tfmerXL_lm_split,
                          'config_clas':tfmerXL_clas_config, 'split_clas': tfmerXL_clas_split}}

Ok, i fixed my problem now. But with a workaround :sweat_smile:
I rename the layers from my onnx model (Rename OnnxLayer)
Still thanks for the help, its good to know that i can even ask some ā€œstupitā€ questions :slight_smile:

1 Like