Torch has not attribute load_state_dict?

Losspost: <p>Hi. I am trying to load a model with:</p>
<pre><code class="lang-auto">import torch

import pyautogui as mouse
import cv2 


from ScreenRecorder import Record,IniRecord,Frame


def start(model):
    sc_ini = Frame()
    monitor = sc_ini.get()
    sc = IniRecord(monitor,1.6)
    while True:
        frame = sc.getFrame()
        cv2.imshow('frame',frame)
        output_xy,output_click = model.forward(frame)
        #print(output_xy,output_click)
        if cv2.waitKey(1) &amp; 0xFF == ord('q'):
            print("break")            
            break
        

model = torch.load_state_dict(torch.load('Model/model_save'))
</code></pre>
<p>But it says</p>
<pre><code class="lang-auto">
  File "D:/Nextcloud/Python/Gamebot/Bot.py", line 31, in &lt;module&gt;
    model = torch.load_state_dict(torch.load('Model/model_save'))

AttributeError: module 'torch' has no attribute 'load_state_dict'
</code></pre>

ptrblck: <p>You have to call it on your model:</p>
<pre><code>model.load_state_dict(torch.load(...))</code></pre>

Losspost: <p>Is there another way to load it? because else i have to import the whole model (network) in my file.<br>
And it still complains</p>
<pre><code class="lang-auto">.    output_xy,output_click = model(frame)

TypeError: 'NoneType' object is not callable
</code></pre>

ptrblck: <p>Did you try to assign the model? (<code>model = model.load_state_dict()</code>)<br>
Just skip it and load the <code>state_dict</code> directly:</p>
<pre><code class="lang-auto">model = MyModel()
model.load_state_dict(torch.load(...))
</code></pre>

Losspost: <p>If i test the model. Do i have to use the batch size as input as well or can i put single images in as well?</p>

ptrblck: <p>You can use single images, but have to make sure the tensor contains the batch dimension:</p>
<pre><code class="lang-python">image = ... # shape = [channels, height, width]
image = image.unsqueeze(0) # shape = [1, channels, height, width]
output = model(image)
</code></pre>

Losspost: <p>Hmm is it just not enough training or why does it always ouputs 0 and never tries 1 ?</p>

ptrblck: <p>I don’t know what kind of model and use case you are working on, but it might be worth starting a new thread if you encounter any errors to keep this topic clean.</p>

saba: <p>Hi ptrblck</p>
<p>I saved my trained Nets on GPU and now wants to use them on CPU.</p>
<p>I read your comments but still have same problem as (AttributeError: ‘list’ object has no attribute ‘load_state_dict’<br>
My code is:</p>
<pre><code>            checkpoint = torch.load(Path1,map_location=torch.device('cpu'))
</code></pre>
<h1>model.load_state_dict(torch.load(Path1,map_location=torch.device(‘cpu’))[‘model_state_dict’])</h1>
<pre><code>            model.load_state_dict(torch.load(Path1)['model_state_dict'])
            optimizer.load_state_dict(torch.load(Path1,map_location=torch.device('cpu'))['optimizer_state_dict'])</code></pre>

ptrblck: <p>How did you create your <code>model</code> instance?<br>
Based on the error message it seems it’s a <code>list</code> instead of a subclass of <code>nn.Module</code>.</p>

saba: <p>Hi , Many thnaks fo ryour reply. muy model is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
import torch<br>
import torchvision<br>
import torchvision.transforms as transforms<br>
from torch.utils.data import TensorDataset, DataLoader<br>
import torch.optim as optim<br>
import torch.nn as nn<br>
from torch.utils.data.dataset import random_split<br>
from torch.nn import functional as F<br>
import matplotlib.pyplot as plt<br>
from torch.autograd import Variable</p>
<p>class ConvNet(nn.Module):<br>
def <strong>init</strong>(self,numf1,numf2,fz1,fz2,nn2,nn3):<br>
super(ConvNet, self).<strong>init</strong>()<br>
self.numf1=numf1<br>
self.numf2=numf2<br>
self.fz1=fz1<br>
self.fz2=fz2<br>
self.nn2=nn2<br>
self.nn3=nn3<br>
self.layer1 = nn.Sequential(nn.Conv3d(1, self.numf1, kernel_size=self.fz1, stride=1, padding=2),nn.ReLU(),nn.MaxPool3d(kernel_size=2, stride=2))<br>
self.layer2 = nn.Sequential(nn.Conv3d(32,self.numf2, kernel_size=self.fz2, stride=1, padding=2),nn.ReLU(),nn.MaxPool3d(kernel_size=2, stride=2))<br>
self.fc1 = nn.Linear(3072, self.nn2) #<span class="hashtag">#3027</span><br>
self.drop_out = nn.Dropout(0.3)<br>
print(“Dropout”)<br>
self.fc2 = nn.Linear( self.nn2, self.nn3) # FULLY CONNECTED LAYERS<br>
self.fc3 = nn.Linear( self.nn3, 1) # FULLY CONNECTED LAYERS</p>
<pre><code>    self.sigmoid = nn.Sigmoid()
    
def forward(self, x):
</code></pre>
<h1>print(“here: {}”.format(x.shape))</h1>
<pre><code>   # print(type(x))
</code></pre>
<h1>x=np.expand_dims(x,axis=0)</h1>
<h1>print(“xthen”,x.shape)</h1>
<h1>x=x.astype(int)</h1>
<h1>x=torch.from_numpy(x)</h1>
<pre><code>    x=x.unsqueeze(1).float()
</code></pre>
<h1>print(type(x))</h1>
<pre><code>    #print(x.shape)
    out = self.layer1(x)
    
   # print(out.shape)
  #  print(out)
    out = self.layer2(out)
</code></pre>
<h1>print(out.shape)</h1>
<pre><code>    out = out.view(out.size(0), -1)
</code></pre>
<h1>print(out.shape)</h1>
<pre><code>    out = self.fc1(out)
    out = self.fc2(out)
    out = self.fc3(out)
    out = self.sigmoid(out)
</code></pre>
<h1>print(“outsize”,out.shape)</h1>
<pre><code>    return out</code></pre>

saba: <p>and I saved it in this way:</p>
<p>Path=root_dir1+’/Fold_’+str(FoldNum)+‘NumDarw=’+str(NumDraw)+‘Iteration’+str(Iteration)+".pth"<br>
checkpoint = {‘model_state_dict’: model.state_dict(),‘optimizer_state_dict’: optimizer.state_dict()}<br>
# print(checkpoint)<br>
torch.save(checkpoint,Path) ## save for each 10 iteration</p>

saba: <p>one time I run my code when I saved the trained Nets on CPU. but now I am loading the trained Nets from GPU to use on CPU  and get this error</p>

ptrblck: <p>Thanks for the code. Could you post the code you are using to restore the model?</p>

saba: <pre><code>        model=[]
        optimizer=[]
        TargetWholev2=[]
        for Iteration1 in range(9):
            Path1=root_dir2+'/Fold_'+str(FoldNum)+'NumDarw='+str(NumDraw)+'Iteration'+str(Iteration1+1)+'.pth'

            
            checkpoint = torch.load(Path1,map_location=torch.device('cpu'))

            model.load_state_dict(torch.load(Path1,map_location=torch.device('cpu'))['model_state_dict'])


            optimizer.load_state_dict(torch.load(Path1,map_location=torch.device('cpu'))['optimizer_state_dict'])</code></pre>

ptrblck: <p>You are currently initializing <code>model</code> and <code>optimizer</code> as an empty Python <code>list</code>, which creates this error.<br>
Initialize both as you have done in your training script, i.e.:</p>
<pre><code class="lang-python">model = ConvNet(...)
optimizer = optim.SGD(model.parameters(), ...)

# Now load the state_dicts
model.load_state_dict(...)
optimizer.load_state_dict(...)
</code></pre>

saba: <p>I used this but give me this error:(Error(s) in loading state_dict for ConvNet:<br>
size mismatch for layer1.0.weight: copying a param with shape torch.Size([32, 1, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([2, 1, 3, 3, 3]).<br>
size mismatch for layer1.0.bias: copying a param with shape torch.Size([32]) from checkpoint, the shape in current model is torch.Size([2]).)</p>
<p>model = ConvNet(2,64,3,3,300,20)<br>
optimizer =torch.optim.Adam(model.parameters(), lr=LR)<br>
TargetWholev2=[]<br>
for Iteration1 in range(9):<br>
Path1=root_dir2+’/Fold_’+str(FoldNum)+‘NumDarw=’+str(NumDraw)+‘Iteration’+str(Iteration1+1)+’.pth’</p>
<h1>checkpoint = torch.load(Path1)</h1>
<pre><code>            checkpoint = torch.load(Path1,map_location=torch.device('cpu'))

            model.load_state_dict(torch.load(Path1,map_location=torch.device('cpu'))['model_state_dict'])


            optimizer.load_state_dict(torch.load(Path1,map_location=torch.device('cpu'))['optimizer_state_dict'])</code></pre>

ptrblck: <p>The error points to different shapes of your parameters, which means that you’ve initialized the model in a different way.<br>
Could you check, how you’ve initialized the <code>ConvNet</code> before saving the <code>state_dict</code> and use the same arguments?</p>

saba: <p>yes you are right 2 is 32 i missed the 3 <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
I really appreciate your time <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

saba: <p>excuse me, the time of training, despite the CNN was shallow was 4 days for just one draw. How I can speed up the GPU? The number of workers in data loader are important for speed?</p>