rwightman/gen-efficientnet-pytorch

some question about transform-learning with this method

mk123qwe opened this issue · 1 comments

from geffnet.mobilenetv3 import mobilenetv3_rw, mobilenetv3_small_075
net = mobilenetv3_small_075()

########################modify###################
# self.global_pool = nn.AdaptiveAvgPool2d(1)
# self.conv_head = select_conv2d(in_chs, num_features, 1, padding=pad_type, bias=head_bias)
# self.act2 = act_layer(inplace=True)
# self.classifier = nn.Linear(num_features, num_classes)
def as_sequential(self):
layers = [self.conv_stem, self.bn1, self.act1]
layers.extend(self.blocks)
# layers.extend([
# self.global_pool, self.conv_head, self.act2,
# nn.Flatten(), nn.Dropout(self.drop_rate), self.classifier])
return nn.Sequential(*layers)

def features(self, x):
    x = self.conv_stem(x)
    x = self.bn1(x)
    x = self.act1(x)
    x = self.blocks(x)
    # x = self.global_pool(x)
    # x = self.conv_head(x)
    # x = self.act2(x)
    return x
def forward(self, x):
    x = self.features(x)
    # x = x.flatten(1)
    if self.drop_rate > 0.:
        x = F.dropout(x, p=self.drop_rate, training=self.training)
    return x

I'm not sure if this model modification method is correct.
For complex models, I'm falling into the quagmire , and I hope to get some suggestions.

@mk123qwe these models are really no different than other PyTorch models, if you need help knowing how to work with models, this isn't the correct place. Perhaps FastAI courses, etc would help.

For classification transfer learning, just set num_classes=x where x is the number of classes in your dataset and create the model with pretrained=True, it will create a new classifier for you. To use the features in other tasks like segmentation, that requires new heads and is beyond the scope of discussion here. Please keep issues here to bugs and problems. Thanks