Any way to get model name

I am asking if I have a resnet34 model. I can do something like model.__name__ and it returns a string as resnet34.

1 Like

Try model.__class__.__name__ to get the name.

6 Likes

It returns only the base class name which is resnet not resnet34

Could you post the model definition?
This seems to work or did I misunderstand your question?

class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
    
    def forward(self, x):
        return x + 1

model = MyModel()            
print(model.__class__.__name__)
>> MyModel
1 Like

I am using torchvision.models.resnet34.
What I want is

model = torchvision.models.resnet34(pretrained=True)
print(model.__class__.__name__)

to print

resnet34

but currently it prints
ResNet

Ah ok, I see.
resnet34() is just a function for constructing the appropriate model. The model itself is just the ResNet.

However, you could simply add a new parameter to your model:

model = MyModel()
model.name = 'ResNet34'
print(model.name)

Will this meet your needs?

2 Likes

Yeah! I also came up with a workaround for myself similar to this.

Thank you for the help!

Has this been somehow integrated in PyTorch? I often need the model name when storing results generated by a specific model. This requires me to either bring together a model_name string along with the model itself during the function calls or to monkey patch the class (or an instance) by adding a model.name attribute.

As of today, are there better ways to access the name of a model?