question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pytorch Model Usage [Support model validation and serialization]

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ozanpkrcommented, Dec 30, 2020

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.”“”

def _forward(self, X):
    """
    Implementation on the internal data forwarding in FusionClassifier.
    """
    batch_size = X.size()[0]
    proba = torch.zeros(batch_size, self.n_outputs).to(self.device)

    # Take the average over predictions from all base estimators.
    for estimator in self.estimators_:
        proba += estimator(X) / self.n_estimators

    return proba

def forward(self, X):
    """
    Implementation on the data forwarding in FusionClassifier.

    Parameters
    ----------
    X : tensor
        Input batch of data, which should be a valid input data batch for
        base estimators.

    Returns
    -------
    proba : tensor of shape (batch_size, n_classes)
        The predicted class distribution.
    """
    proba = self._forward(X)

    return F.softmax(proba, dim=1)

def fit(self,
        dataloaders,
        lr=1e-3,
        weight_decay=5e-4,
        epochs=100,
        optimizer="Adam",
        log_interval=100):
    train_losses = []
    train_acc = []
    val_losses = []
    val_acc = []
    best_model_wts=[]
    criterion = nn.CrossEntropyLoss()
    since = time.time()
    best_model_wts=copy.deepcopy(self.state_dict())
    best_acc = 0.0
    for _ in range(self.n_estimators):
        self.estimators_.append(self._make_estimator())
    self.n_outputs = self._decide_n_outputs(dataloaders['train'], True)
    optimizer = utils.set_optimizer(self, optimizer, lr, weight_decay)
    self._validate_parameters(lr, weight_decay, epochs, log_interval)
    for epoch in range(epochs):
        print('Epoch {}/{}'.format(epoch, epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                
                self.train()  # Set model to training mode
            else:
                self.eval()  # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0

            # Iterate over data.
            for batch_idx, (data, target) in enumerate(dataloaders[phase]):

                data = data.to(self.device)
                target = target.to(self.device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = self._forward(data)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, target)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        
                        loss.backward()
                        optimizer.step()

                # statistics
                running_loss += loss.detach() * data.size(0)  # .item()
                running_corrects += torch.sum(preds == target.data)

            if phase == 'train':
                size=8379#train dataset size.you can be dynamic with basic operation
            if phase == 'val':
                size=563

            epoch_loss = running_loss / size
            epoch_acc = running_corrects.double() / size

            # Losses and accuracy per epochs are stored in array for plot graphs
            if phase == 'train':
                train_losses.append(epoch_loss)
                train_acc.append(epoch_acc)
                print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                    phase, epoch_loss, epoch_acc))
            if phase == 'val':
                print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                    phase, epoch_loss, epoch_acc))
                val_losses.append(epoch_loss)
                val_acc.append(epoch_acc)
            torch.cuda.empty_cache()

            if phase == 'val' and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts=copy.deepcopy(self.state_dict())
      

        print()
        torch.cuda.empty_cache()
    # Determine total traning time
    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))

    # Print best validation accuracy 
    print('Best val Acc: {:4f}'.format(best_acc))

    self.load_state_dict(best_model_wts)
    torch.save(best_model_wts, "./ourwegiht.pth")
    return train_losses,train_acc,val_losses,val_acc
1reaction
xuyxucommented, Feb 1, 2021

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 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and Loading Models - PyTorch
torch.save: Saves a serialized object to disk. This function uses Python's pickle utility for serialization. Models, tensors, and dictionaries of all kinds ...
Read more >
Using PyTorch Models with Elastic Inference
Elastic Inference supports TorchScript saved models. TorchScript uses Torch.JIT, a just-in-time compiler, to produce models that can be serialized and optimized ...
Read more >
Getting Started with PyTorch Image Models (timm)
The purpose of this guide is to explore timm from a practitioner's point of view, focusing on how to use some of the...
Read more >
Export to ONNX - Transformers - Hugging Face
By exposing a graph with standardized operators and data types, ONNX makes it easy to switch between frameworks. For example, a model trained...
Read more >
mlflow.pytorch — MLflow 2.1.0 documentation
In particular, autologging support for vanilla PyTorch models that only subclass ... example with just a training # loop step, (no validation, no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found