EfficientNet-PyTorch icon indicating copy to clipboard operation
EfficientNet-PyTorch copied to clipboard

from_name and num_classes

Open jacekpoplawski opened this issue 5 years ago • 4 comments

hello, I am little confused with using num_classes,

according to documentaton "num_classes" is optional argument for both from_pretrained and from_name but in the code it is like that:

def from_pretrained(cls, model_name, weights_path=None, advprop=False, 
                        in_channels=3, num_classes=1000, **override_params):

def from_name(cls, model_name, in_channels=3, **override_params):

and when I tried to use num_classes as parameter of from_name it doesn't work, any tips?

jacekpoplawski avatar Jul 27 '20 11:07 jacekpoplawski

in 0.6.3 (which is currently the latest version in the pypi index) the signature of from_name is

def from_name(model_name, override_params=None)

so you have to use it like this:

from_name('efficientnet-b0', {'num_classes': 1000})

louis-she avatar Oct 07 '20 09:10 louis-she

@louis-she Hi, thanks for your reply. I tried your method and I got the following error message

>>> model = EfficientNet.from_name('efficientnet-b3', {'num_classes': 500})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/service4card/.local/lib/python3.8/site-packages/efficientnet_pytorch/model.py", line 342, in from_name
    model._change_in_channels(in_channels)
  File "/home/service4card/.local/lib/python3.8/site-packages/efficientnet_pytorch/model.py", line 415, in _change_in_channels
    self._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False)
  File "/home/service4card/.local/lib/python3.8/site-packages/efficientnet_pytorch/utils.py", line 252, in __init__
    super().__init__(in_channels, out_channels, kernel_size, stride, **kwargs)
  File "/home/service4card/.local/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 406, in __init__
    super(Conv2d, self).__init__(
  File "/home/service4card/.local/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 50, in __init__
    if in_channels % groups != 0:
TypeError: unsupported operand type(s) for %: 'dict' and 'int'

Do you know the reason?

Thanks!

ElegantLin avatar Oct 13 '20 05:10 ElegantLin

@ElegantLin Make sure you are using the right version (0.6.3).

louis-she avatar Oct 15 '20 07:10 louis-she

@ElegantLin Make sure you are using the right version (0.6.3).

Hi, I installed version 0.6.3, and I got the same error message, do you know why?

  Building wheel for efficientnet-pytorch (setup.py) ... -
done
  Created wheel for efficientnet-pytorch: filename=efficientnet_pytorch-0.6.3-py3-none-any.whl size=12419 sha256=64e3a3dc760cc0048899dc1578e1d5f97b8e5130c6a00c96f262c97658dcf708
  Stored in directory: /root/.cache/pip/wheels/d9/f9/d1/9b0d379014c521551830b32e4f9a8842802a45a4ae49744fec
Successfully built efficientnet-pytorch
Installing collected packages: efficientnet-pytorch
  Attempting uninstall: efficientnet-pytorch
    Found existing installation: efficientnet-pytorch 0.7.0
    Uninstalling efficientnet-pytorch-0.7.0:
      Successfully uninstalled efficientnet-pytorch-0.7.0
Successfully installed efficientnet-pytorch-0.6.3
TypeError
unsupported operand type(s) for %: 'dict' and 'int'

junliu-cn avatar May 25 '21 02:05 junliu-cn