How to user pretrained weights with classes != 1000 ?
veryviolet opened this issue · comments
Kirill Groshenkov commented
Suppose I want to get EfficientNet B0 with classes=2 and pretrained weights (excluding classifying layer):
from tf2cv.model_provider import get_model
net = get_model(name="efficientnet_b0", pretrained=True, classes=2)
Code above failes with:
ValueError: Shapes (1280, 2) and (1280, 1000) are incompatible
Obviously, weights are loaded for classification layer also, and that leads to an error. How can I avoid loading weights for a classification layer?
Oleg Sémery commented
There are at least two ways:
- After your unsuccessful attempt to load the pre-trained weights, the corresponding weight file was cached in
.tensorflow/models.
You can load these weighs by using methodnet.load_weights()
with appropriate parameters. - You can load the feature extractor
efficientnet_b0(pretrained=True).features
and use it in your own model with any classifier.