Load a model without initialising it

username: fabianslife
cooked: <p>Hello everyone,</p>
<p>I am quite new to image classification (stemming from the NLP side). I recently followed this Tutorial: <a href="https://towardsdatascience.com/binary-image-classification-in-pytorch-5adf64f8c781" rel="noopener nofollow ugc">https://towardsdatascience.com/binary-image-classification-in-pytorch-5adf64f8c781</a></p>
<p>However, because I want to integrate a GUI and other parts to the program I hesitate on using juPyter. Therefore I need to be able to save and load the model for further use.</p>
<p>In the Tutorial the Author used a pretrained model:</p>
<pre><code class="lang-auto">  model = models.resnet18(pretrained=True)

  #freeze all params
  for params in model.parameters():
    params.requires_grad_ = False

  #add a new final layer
  nr_filters = model.fc.in_features  #number of input features of last layer
  model.fc = nn.Linear(nr_filters, 1)

  model = model.to(device)
</code></pre>
<p>I am able to save this model using:</p>
<pre><code class="lang-auto">#save best model
if cum_loss &lt;= best_loss:
    best_model_wts = model.state_dict()
    PATH = './brain_tumor.pth'
    torch.save(best_model_wts, PATH)
</code></pre>
<p>However I do not know how I should initialise it, when loading it in. I do not have any Class as in the Documentation, and just writing:</p>
<pre><code class="lang-auto">model= torch.load("brain_tumor.pth")
</code></pre>
<p>does not seem to work, as it returns errors when working with the model later on.</p>
<p>Therefore I am stuck and can not get beyond this point.</p>

username: AlphaBetaGamma96
cooked: <p>The <code>.pth</code> file you have saves the weights of your model, not the model itself. So change,</p>
<aside class="quote no-group" data-username="fabianslife" data-post="1" data-topic="166732">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/b782af/48.png" class="avatar"> fabianslife:</div>
<blockquote>
<pre><code class="lang-auto">model= torch.load("brain_tumor.pth")
</code></pre>
</blockquote>
</aside>
<p>to,</p>
<pre><code class="lang-auto">loaded = torch.load("filename.pth")
model.load_state_dict(loaded)
</code></pre>

username: fabianslife
cooked: <p>Thank you for your fast answer.</p>
<p>However if I do not initialise the model how can I get this line to work:</p>
<aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="2" data-topic="166732">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>model.load_state_dict(loaded)</code></p>
</blockquote>
</aside>
<p>As model is not defined at this point.</p>
<p>My “call_model_and_predict” currently looks like this:</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets, transforms
from torchvision import datasets, models, transforms


#func to show images
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


#directory for testing files
testdir = "/Users/fabian/Documents/QB1 Medizinische Informatik/brain_tumor/test"

test_transforms = transforms.Compose([transforms.Resize((224,224)),
                                      transforms.ToTensor(),
                                      torchvision.transforms.Normalize(
                                          mean=[0.485, 0.456, 0.406],
                                          std=[0.229, 0.224, 0.225],
    ),
                                      ])

test_data = datasets.ImageFolder(testdir,transform=test_transforms)

testloader = torch.utils.data.DataLoader(test_data, shuffle = True, batch_size=4)

device = "cuda" if torch.cuda.is_available() else "cpu"

dataiter = iter(testloader)
images, labels = next(dataiter)

# print images
imshow(torchvision.utils.make_grid(images))
for j in range(4):
    print(labels[j])


loaded = torch.load("filename.pth")
model.load_state_dict(loaded)

</code></pre>

username: AlphaBetaGamma96
cooked: <p>Can’t you just repeat what you did above? Or do you not have access to the model class?</p>
<aside class="quote no-group" data-username="fabianslife" data-post="1" data-topic="166732">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/b782af/48.png" class="avatar"> fabianslife:</div>
<blockquote>
<pre><code class="lang-auto">  model = models.resnet18(pretrained=True)

  #freeze all params
  for params in model.parameters():
    params.requires_grad_ = False

  #add a new final layer
  nr_filters = model.fc.in_features  #number of input features of last layer
  model.fc = nn.Linear(nr_filters, 1)

  model = model.to(device)
</code></pre>
</blockquote>
</aside>