When i load a model i am seeing this error?

Teddy_Salas: <h2><a name="i-saved-a-model-but-when-i-load-the-model-i-am-seeing-this-error-1" class="anchor" href="#i-saved-a-model-but-when-i-load-the-model-i-am-seeing-this-error-1"></a>;;<br>
i saved a model, but  when i load the model i am seeing this error:-</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 3&gt;()<br>
1 saved_model_path = “/content/aI_genrtd_img_dectr.pth”<br>
2<br>
----&gt; 3 model.load_state_dict(torch.load(saved_model_path))</p>
<p>/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
2039<br>
2040         if len(error_msgs) &gt; 0:<br>
 → 2041             raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
2042                                self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
2043         return _IncompatibleKeys(missing_keys, unexpected_keys)</p>
<p>RuntimeError: Error(s) in loading state_dict for tinyvgg1:<br>
size mismatch for Conv_block1.0.weight: copying a param with shape torch.Size([76, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([3, 64, 3, 3]).<br>
size mismatch for Conv_block1.0.bias: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.1.weight: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.1.bias: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.1.running_mean: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.1.running_var: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.4.weight: copying a param with shape torch.Size([76, 76, 3, 3]) from checkpoint, the shape in current model is torch.Size([3, 3, 3, 3]).<br>
size mismatch for Conv_block1.4.bias: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.5.weight: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.5.bias: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.5.running_mean: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).<br>
size mismatch for Conv_block1.5.ru</p>

ptrblck: <p>These shape mismatches are raised if the parameters in the <code>state_dict</code> do not match the ones used in your model. Did you manipulate the model before saving this <code>state_dict</code> as it seems a lot of parameters changed?</p>

Teddy_Salas: <p>i trained the model.then saved it</p>

ptrblck: <p>Training the model won’t change the parameter shapes, so something else must have manipulated these.</p>

Teddy_Salas: <p>how to solve this problem?</p>

Teddy_Salas: <p>i trained several times  but when i load same problem appears</p>

ptrblck: <p>Figure out why the parameter shapes were changed and either remove it or re-apply these changes to the model before loading the <code>state_dict</code>.</p>

Teddy_Salas: <p>how to find out why parameter shapes are changed?</p>

ptrblck: <p>Read through your code and search for any lines of code which replace modules or parameters directly. Again, PyTorch won’t automatically change your model architecture. In case you are using 3rd party packages, which might perform some network surgery, look through it too. It’s your code so you should know why the model changed.</p>

Teddy_Salas: <p>i think i find the problem</p>

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> respected sir i tried , but didnt help me showing error,--------------------------------------------------------------------------<br>
RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 3&gt;()<br>
1 saved_model_path = “/content/aI_genrtd_img_dectr (2).pth”<br>
2<br>
----&gt; 3 model.load_state_dict(torch.load(saved_model_path))</p>
<p>/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
2039<br>
2040         if len(error_msgs) &gt; 0:<br>
 → 2041             raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
2042                                self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
2043         return _IncompatibleKeys(missing_keys, unexpected_keys)</p>
<p>RuntimeError: Error(s) in loading state_dict for tinyvgg1:<br>
size mismatch for Conv_block1.0.weight: copying a param with shape torch.Size([76, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([3, 76, 3, 3]).<br>
size mismatch for Conv_block1.0.bias: copying a param with shape torch.Size([76]) from checkpoint, the shape in current model is torch.Size([3]).</p>

ptrblck: <p>If you get stuck, post a minimal and executable code snippet reproducing the issue without any data dependencies, which I can use to debug the issue.</p>

Teddy_Salas: <p>import torch<br>
from torch import nn<br>
import torchvision<br>
from torchvision import transforms</p>
<p>train_transforms = transforms.Compose([<br>
transforms.Resize(size=(224,224)),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.RandomRotation(10),<br>
transforms.TrivialAugmentWide(num_magnitude_bins=31),<br>
transforms.ToTensor(),</p>
<p>])</p>
<p>test_transforms = transforms.Compose([<br>
transforms.Resize(size=(224,224)),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.RandomRotation(10),<br>
transforms.ToTensor(),<br>
from torchvision import datasets</p>
<p>train_data = datasets.ImageFolder(<br>
root=train_dir,<br>
transform=train_transforms<br>
)</p>
<p>test_data = datasets.ImageFolder(<br>
root=test_dir,<br>
transform=test_transforms<br>
)</p>
<p>])</p>
<p>from torch.utils.data import DataLoader</p>
<p>train_dataloader = DataLoader(<br>
dataset = train_data,<br>
batch_size = 28,<br>
num_workers = os.cpu_count(),<br>
shuffle=True<br>
)</p>
<p>test_dataloader = DataLoader(<br>
dataset = test_data,<br>
batch_size=28,<br>
num_workers = os.cpu_count(),<br>
shuffle=False<br>
)<br>
class_list = train_data.classes<br>
classclass tinyvgg1(nn.Module):<br>
def <strong>init</strong>(self,hidden,input,output):<br>
super().<strong>init</strong>()<br>
self.Conv_block1 = nn.Sequential(<br>
nn.Conv2d(<br>
in_channels= input,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Conv2d(<br>
in_channels = hidden,<br>
out_channels =hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.MaxPool2d(kernel_size=2,stride=2)<br>
)<br>
self.Conv_block2 = nn.Sequential(<br>
nn.Conv2d(<br>
in_channels=hidden,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Conv2d(<br>
in_channels=hidden,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),</p>
<pre><code class="lang-auto">    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)
self.Conv_block3 = nn.Sequential(
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)
self.Conv_block4 = nn.Sequential(
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)

self.classifier = nn.Sequential(
    nn.Flatten(),
    nn.Linear(
        in_features = hidden*14*14,
        out_features = output
    )
)
</code></pre>
<p>def forward(self,x):<br>
x = self.Conv_block1(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block2(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block3(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block4(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.classifier(x)<br>
return x<br>
return self.Conv_block4(self.Conv_block3(self.Conv_block2(self.Conv_block1(x))))</p>
<p>_list<br>
device = “cuda” if torch.cuda.is_available() else “cpu”<br>
device<br>
model_x = tinyvgg1(input=3,<br>
hidden=76,<br>
output=2<br>
).to(device)<br>
def accuracy_fn(y_true,y_pred):<br>
correct = torch.eq(y_true,y_pred).sum().item()<br>
acc = correct/len(y_pred)*100<br>
return acc<br>
losfn = nn.CrossEntropyLoss()<br>
optimizer =torch.optim.Adam(params = model_x.parameters(),<br>
lr=0.0001,weight_decay=0.00001)<br>
import torch<br>
from torch import nn</p>
<p>def train_step(model:torch.nn.Module,<br>
dataloader:torch.utils.data.DataLoader,<br>
optimizer:torch.optim.Optimizer,<br>
losfn:torch.nn.Module,<br>
accuracy_fn,<br>
device:device):<br>
train_acc,train_loss=0,0<br>
model.train()<br>
l2 = 0.001<br>
for batch,(X,y) in enumerate(dataloader):<br>
X,y = X.to(device),y.to(device)</p>
<pre><code class="lang-auto">y_pred = model(X)
loss = losfn(y_pred,y)
train_loss += loss.item()
train_acc += accuracy_fn(y_true=y,y_pred=y_pred.argmax(dim=1))
l2_reg = torch.tensor(0.,device=device)
for param in model.parameters():
  l2_reg += torch.norm(param,p=2)
loss = loss + l2 * l2_reg
optimizer.zero_grad()
loss.backward()
optimizer.step()
</code></pre>
<p>train_acc /= len(dataloader)<br>
train_loss /= len(dataloader)<br>
print(f"Epoch {epoch} | Train acc: {train_acc:.2f}% | Train Loss {train_loss:.4f}")</p>
<p>def test_step(model:torch.nn.Module,<br>
dataloader:torch.utils.data.DataLoader,<br>
losfn:torch.nn.Module,<br>
accuracy_fn,<br>
device:device<br>
):<br>
test_acc,test_loss = 0,0<br>
model.eval()<br>
with torch.inference_mode():<br>
for batch,(X,y) in enumerate(dataloader):<br>
X,y = X.to(device),y.to(device)<br>
test_pred = model(X)<br>
loss1 = losfn(test_pred,y)<br>
test_loss += loss1.item()<br>
test_acc += accuracy_fn(y_true=y,y_pred=test_pred.argmax(dim=1))<br>
test_acc /= len(dataloader)<br>
test_loss /= len(dataloader)<br>
print(f"Test Acc {test_acc:.2f}% | Test Loss {test_loss:.4f}")</p>
<p>from tqdm.auto import tqdm</p>
<p>torch.manual_seed(42)</p>
<p>epochs = 1000</p>
<p>from timeit import default_timer as timer</p>
<p>start = timer()</p>
<p>for epoch in tqdm(range(epochs)):<br>
train_step(model=model_x.to(device),</p>
<pre><code class="lang-auto">         dataloader=train_dataloader,
         optimizer=optimizer,
         losfn=losfn,
         accuracy_fn=accuracy_fn,
         device=device)
</code></pre>
<p>test_step(model=model_x.to(device),<br>
dataloader=test_dataloader,<br>
losfn=losfn,<br>
accuracy_fn=accuracy_fn,<br>
device=device)<br>
end = timer()</p>
<p>print(f"Total time {end-start}")<br>
from pathlib import Path</p>
<p>MODEL_PATH = Path(“models”)<br>
MODEL_PATH.mkdir(parents=True, exist_ok=True)<br>
MODEL_NAME = “aI_genrtd_img_dectr.pth”<br>
MODEL_SAVE_PATH = MODEL_PATH/MODEL_NAME</p>
<p>print(f"Saving model to: {MODEL_SAVE_PATH}")<br>
torch.save(obj=model_x.state_dict(),<br>
f=MODEL_SAVE_PATH)<br>
model = tinyvgg1(3,76,2).to(device)</p>
<p>saved_model_path = “/content/aI_genrtd_img_dectr (2).pth”</p>
<p>model.load_state_dict(torch.load(saved_model_path))</p>

ptrblck: <p>Your code is neither properly formatted nor is it executable since you have a data dependency.<br>
In any case, the error is raised since you are mixing up input arguments in the model creation.<br>
At first you are using:</p>
<pre><code class="lang-python">model_x = tinyvgg1(input=3, hidden=76, output=2).to(device)
</code></pre>
<p>and later:</p>
<pre><code class="lang-python">model = tinyvgg1(3,76,2).to(device)
</code></pre>
<p>which corresponds to <code>tinyvgg1(hidden=3, input=76, output=2)</code>.</p>

Teddy_Salas: <p>oksy i will check and inform you</p>

Teddy_Salas: <p>it worked thank you sir</p>

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> respected sir, the model i trained is well saved , after that i tried to train that saved model using this code import torch<br>
import torchvision.models as models</p>
<p>model = torchvision.models.resnet18(pretrained=True)<br>
model.load_state_dict(torch.load(‘/content/aI_genrtd_img_dectrx.pth’))<br>
then i load the model but again i saw an error :----------------------------------------------------------------------------<br>
RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 1&gt;()<br>
----&gt; 1 model.load_state_dict(torch.load(‘/content/aI_genrtd_img_dectrx.pth’))</p>
<p>/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
2039<br>
2040         if len(error_msgs) &gt; 0:<br>
 → 2041             raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
2042                                self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
2043         return _IncompatibleKeys(missing_keys, unexpected_keys)</p>
<p>RuntimeError: Error(s) in loading state_dict for ResNet:<br>
Missing key(s) in state_dict: “conv1.weight”, “bn1.weight”, “bn1.bias”, “bn1.running_mean”, “bn1.running_var”, “layer1.0.conv1.weight”, “layer1.0.bn1.weight”, “layer1.0.bn1.bias”, “layer1.0.bn1.running_mean”, “layer1.0.bn1.running_var”, “layer1.0.conv2.weight”, “layer1.0.bn2.weight”, “layer1.0.bn2.bias”, “layer1.0.bn2.running_mean”, “layer1.0.bn2.running_var”, “layer1.1.conv1.weight”, “layer1.1.bn1.weight”, “layer1.1.bn1.bias”, “layer1.1.bn1.running_mean”, “layer1.1.bn1.running_var”, “layer1.1.conv2.weight”, “layer1.1.bn2.weight”, “layer1.1.bn2.bias”, “layer1.1.bn2.running_mean”, “layer1.1.bn2.running_var”, “layer2.0.conv1.weight”, “layer2.0.bn1.weight”, “layer2.0.bn1.bias”, “layer2.0.bn1.running_mean”, “layer2.0.bn1.running_var”, “layer2.0.conv2.weight”, “layer2.0.bn2.weight”, “layer2.0.bn2.bias”, “layer2.0.bn2.running_mean”, “layer2.0.bn2.running_var”, “layer2.0.downsample.0.weight”, “layer2.0.downsample.1.weight”, “layer2.0.downsample.1.bias”, “layer2.0.downsample.1.running_mean”, “layer2.0.downsample.1.running_var”, “layer2.1.conv1.weight”, “layer2.1.bn1.weight”, “layer2.1.bn1.bias”, “layer2.1.bn1.running_mean”, “layer2.1.bn1.running_var”, “layer2.1.conv2.weight”, “layer2.1.bn2.weight”, “layer2.1.bn2.bias”, “layer2.1.bn2.running_mean”, “layer2.1.bn2.running_var”, “layer3.0.conv1.weight”, “layer3.0.bn1.weight”, “layer3.0.bn1.bias”, “layer3.0.bn1.running_mean”, “layer3.0.bn1.running_var”, “layer3.0.conv2.weight”, “layer3.0.bn2.weight”, “layer3.0.bn2.bias”, "layer3.0.bn2…<br>
Unexpected key(s) in state_dict: “Conv_block1.0.weight”, “Conv_block1.0.bias”, “Conv_block1.1.weight”, “Conv_block1.1.bias”, “Conv_block1.1.running_mean”, “Conv_block1.1.running_var”, “Conv_block1.1.num_batches_tracked”, “Conv_block1.4.weight”, “Conv_block1.4.bias”, “Conv_block1.5.weight”, “Conv_block1.5.bias”, “Conv_block1.5.running_mean”, “Conv_block1.5.running_var”, “Conv_block1.5.num_batches_tracked”, “Conv_block2.0.weight”, “Conv_block2.0.bias”, “Conv_block2.1.weight”, “Conv_block2.1.bias”, “Conv_block2.1.running_mean”, “Conv_block2.1.running_var”, “Conv_block2.1.num_batches_tracked”, “Conv_block2.4.weight”, “Conv_block2.4.bias”, “Conv_block2.5.weight”, “Conv_block2.5.bias”, “Conv_block2.5.running_mean”, “Conv_block2.5.running_var”, “Conv_block2.5.num_batches_tracked”, “Conv_block3.0.weight”, “Conv_block3.0.bias”, “Conv_block3.1.weight”, “Conv_block3.1.bias”, “Conv_block3.1.running_mean”, “Conv_block3.1.running_var”, “Conv_block3.1.num_batches_tracked”, “Conv_block3.4.weight”, “Conv_block3.4.bias”, “Conv_block3.5.weight”, “Conv_block3.5.bias”, “Conv_block3.5.running_mean”, “Conv_block3.5.running_var”, “Conv_block3.5.num_batches_tracked”, “Conv_block4.0.weight”, “Conv_block4.0.bias”, “Conv_block4.1.weight”, “Conv_block4.1.bias”, “Conv_block4.1.running_mean”, “Conv_block4.1.running_var”, “Conv_block4.1.num_batches_tracked”, “Conv_block4.4.weight”, “Conv_block4.4.bias”, “Conv_block4.5.weight”, “Conv_block4.5.bias”, “Conv_block4.5.running_mean”, "Conv_block4.5.running_va…</p>

ptrblck: <p>You cannot load your <code>state_dict</code> into a resnet.</p>

Teddy_Salas: <p>i tried to load the model to ,train the model,is there any way to do it?</p>

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> hello sir i tried to load the model to ,train the model,is there any way to do it?</p>