Multi-label model test of sewer pipe, error reported after modifying network model

pan_hongfei: <p>import os<br>
import numpy as np<br>
from argparse import ArgumentParser<br>
from torchvision import models as torch_models<br>
from torchvision import transforms<br>
from collections import OrderedDict<br>
import pandas as pd<br>
import torch</p>
<p>from dataloader import MultiLabelDatasetInference<br>
from torch.utils.data import DataLoader</p>
<p>import torch.nn as nn</p>
<p>import sewer_models<br>
import ml_models<br>
from lightning_trainer_modify import MultiheadAttention</p>
<p>CUSTOM_MODEL_NAMES = [‘xiaobo’]</p>
<p>TORCHVISION_MODEL_NAMES = sorted(name for name in torch_models.<strong>dict</strong> if name.islower() and not name.startswith(“<strong>“) and callable(torch_models.<strong>dict</strong>[name]))<br>
SEWER_MODEL_NAMES = sorted(name for name in sewer_models.<strong>dict</strong> if name.islower() and not name.startswith(”</strong>”) and callable(sewer_models.<strong>dict</strong>[name]))<br>
MULTILABEL_MODEL_NAMES = sorted(name for name in ml_models.<strong>dict</strong> if name.islower() and not name.startswith(“__”) and callable(ml_models.<strong>dict</strong>[name]))<br>
MODEL_NAMES =  TORCHVISION_MODEL_NAMES + SEWER_MODEL_NAMES + MULTILABEL_MODEL_NAMES</p>
<p>def evaluate(dataloader, model, device):<br>
model.eval()</p>
<pre><code>sigmoidPredictions = None
imgPathsList = []

sigmoid = nn.Sigmoid()

dataLen = len(dataloader)

with torch.no_grad():
    for i, (images, imgPaths) in enumerate(dataloader):
        if i % 100 == 0:
            print("{} / {}".format(i, dataLen))

        images = images.to(device)

        output = model(images)            

        sigmoidOutput = sigmoid(output).detach().cpu().numpy()

        if sigmoidPredictions is None:
            sigmoidPredictions = sigmoidOutput
        else:
            sigmoidPredictions = np.vstack((sigmoidPredictions, sigmoidOutput))

        imgPathsList.extend(list(imgPaths))
return sigmoidPredictions, imgPathsList
</code></pre>
<p>def load_model(model_path, best_weights=False):</p>
<pre><code>if best_weights:
    if not os.path.isfile(model_path):
        raise ValueError("The provided path does not lead to a valid file: {}".format(model_path))
    last_ckpt_path = model_path
else:
    last_ckpt_path = os.path.join(model_path, "last.ckpt")
    # last_ckpt_path = os.path.join(model_path, "events.out.tfevents.1714832370.PC-202310272034")
    if not os.path.isfile(last_ckpt_path):
        raise ValueError("The provided directory path does not contain a 'last.ckpt' file: {}".format(model_path))

model_last_ckpt = torch.load(last_ckpt_path)


model_name = model_last_ckpt["hyper_parameters"]["model"]
num_classes = model_last_ckpt["hyper_parameters"]["num_classes"]
training_mode = model_last_ckpt["hyper_parameters"]["training_mode"]
br_defect = model_last_ckpt["hyper_parameters"]["br_defect"]

# Load best checkpoint
best_model = model_last_ckpt
# if best_weights:
#     best_model = model_last_ckpt
# else:
#     best_model_path = model_last_ckpt["checkpoint_callback_best_model_path"]
#     best_model = torch.load(best_model_path)

best_model_state_dict = best_model["state_dict"]

updated_state_dict = OrderedDict()
for k,v in best_model_state_dict.items():
    name = k.replace("model.", "")
    if "criterion" in name:
        continue

    updated_state_dict[name] = v

return updated_state_dict, model_name, num_classes, training_mode, br_defect
</code></pre>
<p>def run_inference(args):</p>
<pre><code>ann_root = args["ann_root"]
data_root = args["data_root"]
model_path = args["model_path"]
outputPath = args["results_output"]
#best_weights = args["best_weights"]
best_weights = False
split = args["split"]

if not os.path.isdir(outputPath):
    os.makedirs(outputPath)

updated_state_dict, model_name, num_classes, training_mode, br_defect = load_model(model_path, best_weights)

if "model_version" not in args.keys():
    model_version = model_name
else:
    model_version = args["model_version"]

# Init model
if model_name in TORCHVISION_MODEL_NAMES:
    model = torch_models.__dict__[model_name](num_classes = num_classes)
elif model_name in SEWER_MODEL_NAMES:
    model = sewer_models.__dict__[model_name](num_classes = num_classes)
elif model_name in MULTILABEL_MODEL_NAMES:
    model = ml_models.__dict__[model_name](num_classes = num_classes)
else:
    raise ValueError("Got model {}, but no such model is in this codebase".format(model_name))

model.load_state_dict(updated_state_dict)

# initialize dataloaders
img_size = 299 if model in ["inception_v3", "chen2018_multilabel"] else 224

eval_transform=transforms.Compose([
        transforms.Resize((img_size, img_size)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.523, 0.453, 0.345], std=[0.210, 0.199, 0.154])
    ])

    
dataset = MultiLabelDatasetInference(ann_root, data_root, split=split, transform=eval_transform, onlyDefects=False)
dataloader = DataLoader(dataset, batch_size=args["batch_size"], num_workers = args["workers"], pin_memory=True)

if training_mode in ["e2e", "defect"]:
    labelNames = dataset.LabelNames
elif training_mode == "binary":
    labelNames = ["Defect"]
elif training_mode == "binaryrelevance":
    labelNames = [br_defect]

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

model = model.to(device)

# Validation results
print("VALIDATION")
sigmoid_predictions, val_imgPaths = evaluate(dataloader, model, device)

sigmoid_dict = {}
sigmoid_dict["Filename"] = val_imgPaths
for idx, header in enumerate(labelNames):
    sigmoid_dict[header] = sigmoid_predictions[:,idx]

sigmoid_df = pd.DataFrame(sigmoid_dict)
sigmoid_df.to_csv(os.path.join(outputPath, "{}_{}_sigmoid.csv".format(model_version, split.lower())), sep=",", index=False)
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
parser = ArgumentParser()<br>
parser.add_argument(‘–conda_env’, type=str, default=‘pytorch_gpu’)<br>
parser.add_argument(‘–notification_email’, type=str, default=‘’)<br>
parser.add_argument(‘–ann_root’, type=str, default=“D:\dataset\10\mytest”)<br>
parser.add_argument(‘–data_root’, type=str, default=“D:\dataset\10\mytest”)<br>
parser.add_argument(‘–batch_size’, type=int, default=16, help=“Size of the batch per GPU”)<br>
parser.add_argument(‘–workers’, type=int, default=2)<br>
# parser.add_argument(“–model_path”, type=str, default=“D:\dataset\10\log\xie2019_binary\binary-version_1\checkpoints”)<br>
parser.add_argument(“–model_path”, type=str,default=“D:\dataset\10\log\xiaobo\checkpoints”)<br>
parser.add_argument(“–best_weights”, action=“store_true”, help=“If true ‘model_path’ leads to a specific weight file. If False it leads to the output folder of lightning_trainer where the last.ckpt file is used to read the best model weights.”)<br>
# parser.add_argument(“–results_output”, type=str, default=“D:\dataset\10\log\resnet18\e2e-version_1\checkpoints”)<br>
parser.add_argument(“–results_output”, type=str, default=“D:\dataset\10\log\xiaobo\checkpoints”)<br>
parser.add_argument(“–split”, type=str, default = “Val”, choices=[“Train”, “Val”, “Test”])</p>
<pre><code>args = vars(parser.parse_args())

run_inference(args)
</code></pre>
<p>When running inference.py, is it necessary to ensure that the model architecture used to generate the checkpoints is exactly the same as the model architecture used to attempt to load the checkpoints? If the network is modified to train the model, running the inference.py test results in an error: Traceback (most recent call last):<br>
Traceback (most recent call last):<br>
File “E:\Multi-label-Sewer-Classification-main\inference.py”, line 187, in<br>
run_inference(args)<br>
File “E:\Multi-label-Sewer-Classification-main\inference.py”, line 129, in run_inference<br>
model.load_state_dict(updated_state_dict)<br>
File “C:\ProgramData\anaconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py”, line 2152, in load_state_dict<br>
raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
RuntimeError: Error(s) in loading state_dict for ResNet:<br>
Unexpected key(s) in state_dict: “attention.in_proj_weight”, “attention.in_proj_bias”, “attention.out_proj.weight”, “attention.out_proj.bias”, “conv_reduce.weight”, “conv_reduce.bias”, “features.0.weight”, “features.0.bias”, “classifier.weight”, “classifier.bias”.<br>
size mismatch for conv1.weight: copying a param with shape torch.Size([64, 11, 7, 7]) from checkpoint, the shape in current model is torch.Size([64, 3, 7, 7]). #</p>

ptrblck: <p>Your model modification changes the <code>in_channels</code> of <code>conv1</code> and the loading of the <code>state_dict</code> thus fails raising this shape mismatch. You could manipulate or replace the stored <code>weight</code> (and <code>bias</code>) in the <code>state_dict</code> making sure they have the expected shapes and load it afterwards.</p>