Closed rinkita22 closed 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.
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)
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