Efficientnet: output deep feature vector in addition to logit

Currently, the network ouput is logit, which is self._fc(x), I want to access deep feature vector.

The deep feature is input of last dropout (Dropout-444), i.e., output of _avg_pooling(x), which has [-1, 1536] dimension.

We can change the source code to

  def forward(self, inputs):
      """ Calls extract_features to extract features, applies final linear layer, and returns logits. """
      bs = inputs.size(0)
      # Convolution layers
      x = self.extract_features(inputs)
 
      # Pooling and final linear layer
      x = self._avg_pooling(x)
      FV = x.view(bs, -1)
      h = self._dropout(FV)
      Logit = self._fc(h)
      return (FV, logit)

How can I get both feature vector and logit without changing the source code?

I tried following, but it show an error. I did similar thing with resnet-50 without error:

from efficientnet_pytorch import EfficientNet

class new_efficientnet(nn.Module):
  def __init__(self,origin_model):
    super(new_efficientnet, self).__init__()
    modules = list(origin_model.children())[:-2]
    self.new_model = nn.Sequential(*modules)
    self.FC = nn.Linear(1536, 300) # changing output size
         
    def forward(self, x):
    FV = self.new_model(x)
    FV = torch.flatten(FV, 1)
    logit = self.FC(FV)
    return (FV, logit)

efficientnet_original = EfficientNet.from_name('efficientnet-b3')
efficientnet = new_efficientnet(efficientnet_original)
efficientnet.cuda()
summary(efficientnet, ( 3, 300, 300), batch_size=-1, device='cuda')

I do not know how change the efficientnet-b3, to output deep feature vector without changing the source code.

You can override the function without actually modifying the source code like the following.

import efficientnet_pytorch
def my_efficientnet_forward(self, inputs):
	""" Calls extract_features to extract features, applies final linear layer, and returns logits. """
	bs = inputs.size(0)
	# Convolution layers
	x = self.extract_features(inputs)

	# Pooling and final linear layer
	x = self._avg_pooling(x)
	FV = x.view(bs, -1)
	h = self._dropout(FV)
	Logit = self._fc(h)
	return (FV, logit)

efficientnet_pytorch.EfficientNet.forward.__code__ = my_efficientnet_forward.__code__