victoresque / pytorch-template

PyTorch deep learning projects made easy.
MIT License
4.75k stars 1.09k forks source link

changing the model to use resnet #33

Closed rinkita22 closed 5 years ago

rinkita22 commented 6 years ago

I am using your code base for my project and I want to change the model to use torchvision.models.resnet18() instead of customized layers.But I am facing issues. I just changed the model.py to : import torch.nn as nn import torch.nn.functional as F from base import BaseModel import torchvision.models as models

class MnistModel(BaseModel): def init(self, num_classes=10): super(MnistModel, self).init()

self.conv1 = nn.Conv2d(1, 10, kernel_size=5)

    # self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
    # self.conv2_drop = nn.Dropout2d()
    # self.fc1 = nn.Linear(320, 50)
    # self.fc2 = nn.Linear(50, num_classes)
    models.resnet18()

def forward(self, x):
    x = F.relu(F.max_pool2d(self.conv1(x), 2))
    x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
    x = x.view(-1, 320)
    x = F.relu(self.fc1(x))
    x = F.dropout(x, training=self.training)
    x = self.fc2(x)
    return F.log_softmax(x, dim=1)

The error I got :Trainable parameters: 0 MnistModel() Traceback (most recent call last): File "/home/rinkita/MLMI/pytorch-template/train.py", line 72, in main(config, args.resume) File "/home/rinkita/MLMI/pytorch-template/train.py", line 35, in main optimizer = get_instance(torch.optim, 'optimizer', config, trainable_params) File "/home/rinkita/MLMI/pytorch-template/train.py", line 15, in get_instance return getattr(module, config[name]['type'])(*args, **config[name]['args']) File "/usr/local/lib/python3.6/dist-packages/torch/optim/adam.py", line 41, in init super(Adam, self).init(params, defaults) File "/usr/local/lib/python3.6/dist-packages/torch/optim/optimizer.py", line 38, in init raise ValueError("optimizer got an empty parameter list") ValueError: optimizer got an empty parameter list

Can you please help me with this

SunQpark commented 5 years ago

import torch.nn as nn
import torch.nn.functional as F
from base import BaseModel
import torchvision.models as models

class MnistModel(BaseModel):
    def init(self, num_classes=10):
        super(MnistModel, self).init()
        models.resnet18()

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

This is what your code looks like. The torchvision.models.resnet18() is a function that builds and returns the nn.Module object(which is resnet here). In your code, this function is properly called but returned object(containing parameters) is not saved in class member.

class Resnet18(BaseModel):
    def __init__(self, num_classes=10):
        super(Resnet18, self).__init__()
        self.resnet = models.resnet18()
        self.resnet.avgpool = nn.AdaptiveMaxPool2d(1)
        self.resnet.fc = nn.Linear(512, num_classes)

    def forward(self, x_input):
        output = self.resnet(x_input)
        return torch.sigmoid(output)

This is my example code for doing this. ask google 'transfer learning in pytorch' for more information.