Extracting weights from EfficientNet Fastai

Hi Team,
I have trained an image regression model using EfficientNetB5.
I wanted to extract the weights from it’s previous layers because i want to use those weights to further train a boosting model.

Previously I used Densenet and I was able to extract it’s weights using below function and code:

class SaveFeatures():

features=None
def __init__(self, m): 
    self.hook = m.register_forward_hook(self.hook_fn)
    self.features = None
def hook_fn(self, module, input, output): 
    out = output.detach().cpu().numpy()
    if isinstance(self.features, type(None)):
        self.features = out
    else:
        self.features = np.row_stack((self.features, out))
def remove(self): 
    self.hook.remove()

sf = SaveFeatures(learn.model[1][4]) # using 4 because i wanted to extract the fourth layer from the bottom

Now since EfficientNet models doesn’t support indexing.
Is their any way to extract the weights from the previous layers?

Any help would be really appreciated :slight_smile:

Regards,
Akshat

2 Likes

I have the same problem, haven’t you solved it? Can anyone share how best to do this?