Pytorch Model Usage [Support model validation and serialization]
See original GitHub issueHi @xuyxu , I wanna use this repo with pytorch cnn models such as resnet,densenet,etc. Can I use this repo for that?I tried but I got error.After then I created ensemble model with two submodel(hrnet_w18_small).When I use this ensemble model with your repo I got error again 😄 Have you any suggestion?
m1 = timm.create_model('hrnet_w18_small', pretrained=False)
m2=timm.create_model('hrnet_w18_small', pretrained=False)
m1.classifier = nn.Sequential(nn.Linear(2048, 2), nn.Sigmoid())
m2.classifier = nn.Sequential(nn.Linear(2048, 2), nn.Sigmoid())
m1.load_state_dict(torch.load('./hr18_1.pth'))
m2.load_state_dict(torch.load('./hr18_2.pth'))
class MyEnsemble(nn.Module):
def __init__(self,nb_classes=2):
super(MyEnsemble, self).__init__()
self.modelA = m1
self.modelB = m2
self.classifier =nn.Linear(2, 2)
def forward(self,x):
x1 = self.modelA(x)
x1 = x1.view(x1.size(0), -1)
x2 = self.modelB(x)
x2 = x2.view(x2.size(0), -1)
x = torch.cat((x1, x2), dim=1)
x = self.classifier(torch.softmax(x, dim=1))
return x
model = FusionClassifier(
estimator=myens,
n_estimators=n_estimators,
cuda=True
)
model.fit(dataloaders['train'],
lr=1e-3,
weight_decay=5e-4,
epochs=10)
model.fit(dataloaders['train'], lr, weight_decay, epochs, "Adam")
TypeError Traceback (most recent call last) <ipython-input-16-b7b5e5b0fa5e> in <module> 1 tic = time.time() ----> 2 model.fit(dataloaders[‘train’], lr, weight_decay, epochs, “Adam”) 3 toc = time.time() 4 training_time = toc - tic
~\Desktop\Ensemble-Pytorch-master\torchensemble\fusion.py in fit(self, train_loader, lr, weight_decay, epochs, optimizer, log_interval) 77 # Instantiate base estimators and set attributes 78 for _ in range(self.n_estimators): —> 79 self.estimators_.append(self._make_estimator()) 80 self.n_outputs = self._decide_n_outputs(train_loader, True) 81 optimizer = utils.set_optimizer(self, optimizer, lr, weight_decay)
~\Desktop\Ensemble-Pytorch-master\torchensemble_base.py in make_estimator(self) 86 “”“Make and configure a copy of the
base_estimator_
.”“” 87 if self.estimator_args is None: —> 88 estimator = self.base_estimator() 89 else: 90 estimator = self.base_estimator_(**self.estimator_args)~\anaconda3\envs\pytorchenv\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs) 725 result = self._slow_forward(*input, **kwargs) 726 else: –> 727 result = self.forward(*input, **kwargs) 728 for hook in itertools.chain( 729 _global_forward_hooks.values(),
TypeError: forward() missing 1 required positional argument: ‘x’
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
Hi @xuyxu . I have news for you 😄 I recreate fussionclassifier that include validation step.You can your repo after check.I used that and I didn’t see any problem. You send dataloader dict. that has train and validation dataset.After all epoch this function save best weight on your machine and load your model finally return val train acc loss list for draw graphs 😃 next task is add learning rate scheduler 😄
class FusionClassifier(BaseModule): “”“Implementation of the FusionClassifier.”“”
Hi @ozanpkr, I have not heard from you for a while.
This issue has already been fixed, you can check the changelog for details: https://ensemble-pytorch.readthedocs.io/en/latest/changelog.html. Feel free to re-open the issue if you still have any problem 😉