Pytorch - How to run inference of a model after thinning the model without the model.py file?












0















I am having a pytorch model with the following network structure.



from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

class lenet_mnist(nn.Module):
def __init__(self):
super(lenet_mnist, self).__init__()
self.cuda()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3,padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.relu2 = nn.ReLU(inplace=True)
self.relu3 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5,padding=2)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(1568, 256)
self.fc2 = nn.Linear(256, 10)

def forward(self,x):
#x = x.to(torch.device("cuda:0"))
out = self.conv1(x)
out = self.maxpool1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.conv2_drop(out)
out = self.maxpool2(out)
out = self.relu2(out)
out = out.view(-1,1568)
out = self.fc1(out)
out = self.fc2(out)
return out


I train this model and I remove few channels in the model, for example I made the self.conv2 as (in=16,out=24). I reloaded the state_dict model and modified the model for the new structure.



Now , I am trying to run an inference of the model, but pytorch still picks up the old model.py file where the original structure is defined. How do I stop pytorch looking for model.py?



Edit: (more information)
For thinning the model, I just get the state_dict of the model, edit the tensors by deleting few channels in it and then use load_state_dict on the model to load the model again. I also change the model._parameters with the updated weights and bias. I use this model to run a forward pass. While running, I get an error saying mismatch in dimensions at out.view layer. And, the backtrace shows that the pytorch is reading the model from model.py file where the above code snippet is present.










share|improve this question

























  • Do you use a jupyter notebook?

    – artona
    Nov 19 '18 at 8:19











  • no. I just use vim

    – vsoorya
    Nov 19 '18 at 8:20











  • Please provide the code with removing channels and way you are using the new model.

    – artona
    Nov 19 '18 at 8:23











  • + your project structure of files.

    – artona
    Nov 19 '18 at 8:41











  • Without the code of new model I cannot tell much about the error.

    – artona
    Nov 19 '18 at 8:52
















0















I am having a pytorch model with the following network structure.



from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

class lenet_mnist(nn.Module):
def __init__(self):
super(lenet_mnist, self).__init__()
self.cuda()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3,padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.relu2 = nn.ReLU(inplace=True)
self.relu3 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5,padding=2)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(1568, 256)
self.fc2 = nn.Linear(256, 10)

def forward(self,x):
#x = x.to(torch.device("cuda:0"))
out = self.conv1(x)
out = self.maxpool1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.conv2_drop(out)
out = self.maxpool2(out)
out = self.relu2(out)
out = out.view(-1,1568)
out = self.fc1(out)
out = self.fc2(out)
return out


I train this model and I remove few channels in the model, for example I made the self.conv2 as (in=16,out=24). I reloaded the state_dict model and modified the model for the new structure.



Now , I am trying to run an inference of the model, but pytorch still picks up the old model.py file where the original structure is defined. How do I stop pytorch looking for model.py?



Edit: (more information)
For thinning the model, I just get the state_dict of the model, edit the tensors by deleting few channels in it and then use load_state_dict on the model to load the model again. I also change the model._parameters with the updated weights and bias. I use this model to run a forward pass. While running, I get an error saying mismatch in dimensions at out.view layer. And, the backtrace shows that the pytorch is reading the model from model.py file where the above code snippet is present.










share|improve this question

























  • Do you use a jupyter notebook?

    – artona
    Nov 19 '18 at 8:19











  • no. I just use vim

    – vsoorya
    Nov 19 '18 at 8:20











  • Please provide the code with removing channels and way you are using the new model.

    – artona
    Nov 19 '18 at 8:23











  • + your project structure of files.

    – artona
    Nov 19 '18 at 8:41











  • Without the code of new model I cannot tell much about the error.

    – artona
    Nov 19 '18 at 8:52














0












0








0








I am having a pytorch model with the following network structure.



from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

class lenet_mnist(nn.Module):
def __init__(self):
super(lenet_mnist, self).__init__()
self.cuda()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3,padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.relu2 = nn.ReLU(inplace=True)
self.relu3 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5,padding=2)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(1568, 256)
self.fc2 = nn.Linear(256, 10)

def forward(self,x):
#x = x.to(torch.device("cuda:0"))
out = self.conv1(x)
out = self.maxpool1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.conv2_drop(out)
out = self.maxpool2(out)
out = self.relu2(out)
out = out.view(-1,1568)
out = self.fc1(out)
out = self.fc2(out)
return out


I train this model and I remove few channels in the model, for example I made the self.conv2 as (in=16,out=24). I reloaded the state_dict model and modified the model for the new structure.



Now , I am trying to run an inference of the model, but pytorch still picks up the old model.py file where the original structure is defined. How do I stop pytorch looking for model.py?



Edit: (more information)
For thinning the model, I just get the state_dict of the model, edit the tensors by deleting few channels in it and then use load_state_dict on the model to load the model again. I also change the model._parameters with the updated weights and bias. I use this model to run a forward pass. While running, I get an error saying mismatch in dimensions at out.view layer. And, the backtrace shows that the pytorch is reading the model from model.py file where the above code snippet is present.










share|improve this question
















I am having a pytorch model with the following network structure.



from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

class lenet_mnist(nn.Module):
def __init__(self):
super(lenet_mnist, self).__init__()
self.cuda()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3,padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.relu2 = nn.ReLU(inplace=True)
self.relu3 = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5,padding=2)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(1568, 256)
self.fc2 = nn.Linear(256, 10)

def forward(self,x):
#x = x.to(torch.device("cuda:0"))
out = self.conv1(x)
out = self.maxpool1(out)
out = self.relu1(out)
out = self.conv2(out)
out = self.conv2_drop(out)
out = self.maxpool2(out)
out = self.relu2(out)
out = out.view(-1,1568)
out = self.fc1(out)
out = self.fc2(out)
return out


I train this model and I remove few channels in the model, for example I made the self.conv2 as (in=16,out=24). I reloaded the state_dict model and modified the model for the new structure.



Now , I am trying to run an inference of the model, but pytorch still picks up the old model.py file where the original structure is defined. How do I stop pytorch looking for model.py?



Edit: (more information)
For thinning the model, I just get the state_dict of the model, edit the tensors by deleting few channels in it and then use load_state_dict on the model to load the model again. I also change the model._parameters with the updated weights and bias. I use this model to run a forward pass. While running, I get an error saying mismatch in dimensions at out.view layer. And, the backtrace shows that the pytorch is reading the model from model.py file where the above code snippet is present.







python conv-neural-network pytorch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 8:47







vsoorya

















asked Nov 19 '18 at 7:55









vsooryavsoorya

13




13













  • Do you use a jupyter notebook?

    – artona
    Nov 19 '18 at 8:19











  • no. I just use vim

    – vsoorya
    Nov 19 '18 at 8:20











  • Please provide the code with removing channels and way you are using the new model.

    – artona
    Nov 19 '18 at 8:23











  • + your project structure of files.

    – artona
    Nov 19 '18 at 8:41











  • Without the code of new model I cannot tell much about the error.

    – artona
    Nov 19 '18 at 8:52



















  • Do you use a jupyter notebook?

    – artona
    Nov 19 '18 at 8:19











  • no. I just use vim

    – vsoorya
    Nov 19 '18 at 8:20











  • Please provide the code with removing channels and way you are using the new model.

    – artona
    Nov 19 '18 at 8:23











  • + your project structure of files.

    – artona
    Nov 19 '18 at 8:41











  • Without the code of new model I cannot tell much about the error.

    – artona
    Nov 19 '18 at 8:52

















Do you use a jupyter notebook?

– artona
Nov 19 '18 at 8:19





Do you use a jupyter notebook?

– artona
Nov 19 '18 at 8:19













no. I just use vim

– vsoorya
Nov 19 '18 at 8:20





no. I just use vim

– vsoorya
Nov 19 '18 at 8:20













Please provide the code with removing channels and way you are using the new model.

– artona
Nov 19 '18 at 8:23





Please provide the code with removing channels and way you are using the new model.

– artona
Nov 19 '18 at 8:23













+ your project structure of files.

– artona
Nov 19 '18 at 8:41





+ your project structure of files.

– artona
Nov 19 '18 at 8:41













Without the code of new model I cannot tell much about the error.

– artona
Nov 19 '18 at 8:52





Without the code of new model I cannot tell much about the error.

– artona
Nov 19 '18 at 8:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370431%2fpytorch-how-to-run-inference-of-a-model-after-thinning-the-model-without-the-m%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370431%2fpytorch-how-to-run-inference-of-a-model-after-thinning-the-model-without-the-m%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini