Accuracy decrease after load the saved model

username: Ph_m_H_i
cooked: <p>Hi. Can you tell me why accuracy of my model <strong>decreases</strong> when I save  and load it to predict. My accuracy after training is about 88%</p>
<p>I use resnet model of pytorch</p>
<pre><code class="lang-auto">model_ft = models.resnet34(pretrained=True)
num_ftrs = model_ft.fc.in_features

# replace the last fc layer with an untrained one (requires grad by default)
model_ft.fc = nn.Linear(num_ftrs, 216)
model_ft = model_ft.to(device)

# uncomment this block for half precision model
# """
model_ft = model_ft.half()


for layer in model_ft.modules():
    if isinstance(layer, nn.BatchNorm2d):
        layer.float()
# """
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_ft.parameters(), lr=0.01, momentum=0.9)

lrscheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=3, threshold = 0.9)
</code></pre>
<p>I use this code to save the model</p>
<pre><code class="lang-auto">PATH = './car_classifier.pth'
torch.save(model_ft.state_dict(), PATH)
</code></pre>
<p>then I load it with</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/2/22717848cb95b1c79410f43d7096b130eecbac5d.png" data-download-href="https://discuss.pytorch.org/uploads/default/22717848cb95b1c79410f43d7096b130eecbac5d" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/2/22717848cb95b1c79410f43d7096b130eecbac5d.png" alt="image" data-base62-sha1="4UHlcQgX7SoU3VrKDQqohunYfkV" width="690" height="166" data-dominant-color="362B42"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">713×172 5.99 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
Thanks in advanced!</p>

username: jmandivarapu1
cooked: <aside class="quote no-group" data-username="Ph_m_H_i" data-post="1" data-topic="81628">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ph_m_h_i/48/24342_2.png" class="avatar"> Ph_m_H_i:</div>
<blockquote>
<p>me why accuracy of my model <strong>decreases</strong> when I save and load it to predict. My accuracy after training is about 88%</p>
<p>I use resnet model of pytorch</p>
<pre><code class="lang-auto">model_ft = models.resnet34(pretrained=True)
num_ftrs = model_ft.fc.in_features

# replace the last fc layer with an untrained one (requires grad by default)
model_ft.fc = nn.Linear(num_ftrs, 216)
model_ft = model_ft.to(device)

# uncomment this block for half precision model
# """
model_ft = model_ft.half()


for layer in model_ft.modules
</code></pre>
</blockquote>
</aside>
<p>My guess is here model accuracy was depend on the batches that go through the testing phase so instead of performing once test for 5 time and average it then save the model and then redo it</p>

username: Ph_m_H_i
cooked: <p>Sorry I don’t really understand what you want me to do. Can you make it more clearly</p>

username: Ph_m_H_i
cooked: <p>I use this code to transform the input images.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/8/f/8fcd6f82b1a48300aacae5ea42688b149f31cd8d.png" alt="image" data-base62-sha1="kw8qa8oP0rcNUwee9349RPMPyhv" width="601" height="271"></p>

username: ptrblck
cooked: <p>How large is the decrease in accuracy?<br>
Are you using the same data for both use cases or are you loading another dataset for the validation use case?</p>

username: Ph_m_H_i
cooked: <p>I trained the model on jupyter note book and get 88% validation accuracy. Then I use it to predict some random images and it go well. After that I save and load it to my web app to predict. But none of them is correct. Is that because of the way I load my model to the web app?</p>

username: Ph_m_H_i
cooked: <p>can you please help me with it</p>

username: saluei
cooked: <p>Hi<br>
I Have the same problem with ResNet50?<br>
my validation and train accuracy decreased 50% when I loaded the saved model with load_state_dict() function !!!<br>
However when I save training results everything was OK</p>