Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define my models? Any documentations? #2

Open
BostonLobster opened this issue Jan 29, 2021 · 1 comment
Open

How to define my models? Any documentations? #2

BostonLobster opened this issue Jan 29, 2021 · 1 comment

Comments

@BostonLobster
Copy link

First of all, nice project!!!

In your cifar demo, your models are hard-coded into your lib. So, how do I define my own models and make them work well with the config system?

Hope you can give some detailed documentations on this!

@marsggbo
Copy link
Owner

Thanks for your interest. Designing a new model in torchline is very easy. You may refer to projects/fake_demo.

In main.py, I build a FakeNet, which is decorated by META_ARCH_REGISTRY. The code looks like

from torchline.models import META_ARCH_REGISTRY

@META_ARCH_REGISTRY.register()
class FakeNet(torch.nn.Module):
    def __init__(self, cfg):
        super(FakeNet, self).__init__()
        self.cfg = cfg
        h, w = cfg.input.size
        self.feat = torch.nn.Conv2d(3,16,3,1,1)
        self.clf = torch.nn.Linear(16, cfg.model.classes)

    def forward(self, x):
        b = x.shape[0]
        out = self.feat(x)
        out = torch.nn.AdaptiveAvgPool2d(1)(out).view(b, -1)
        out = self.clf(out)
        out = torch.softmax(out, dim=1)
        return out

You can just simply assign model.name=FakeNet in the configuration file, then torchline will automatically load your specified model.

By the way, if you want to build a model in another py file (e.g., mymodel.py), instead of main.py, you should import mymodel in main.py; otherwise, torchline will fail to load your model.

# mymodel.py
from torchline.models import META_ARCH_REGISTRY

@META_ARCH_REGISTRY.register()
class NewModel(torch.nn.Module):
    ....
# main.py
import mymodel
...

Building a new loss function or dataset loader is similar to designing a new model. Please contact me if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants