loaded model mismatch error #1111
-
while completing the exercises, i encounter this error. even by copying the exact code as given in Example solutions notebook for 05 on this section - 3 .Create a script to predict (such as predict.py) on a target image given a file path with a saved model. Error: [INFO] Predicting on data/pizza_steak_sushi/test/sushi/175783.jpg
[INFO] Loading in model from: models/going_modular_model.pth
E:\codebase\Python\PyTorch\learnpytorch.io\going_modular\predict.py:43: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
model.load_state_dict(torch.load(filepath))
Traceback (most recent call last):
File "E:\codebase\Python\PyTorch\learnpytorch.io\going_modular\predict.py", line 85, in <module>
predict_on_image()
File "E:\codebase\Python\PyTorch\learnpytorch.io\going_modular\predict.py", line 51, in predict_on_image
model = load_model(filepath)
^^^^^^^^^^^^^^^^^^^^
File "E:\codebase\Python\PyTorch\learnpytorch.io\going_modular\predict.py", line 43, in load_model
size mismatch for conv_block_1.0.weight: copying a param with shape torch.Size([10, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 3, 3, 3]).
size mismatch for conv_block_1.0.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([128]).
size mismatch for conv_block_1.2.weight: copying a param with shape torch.Size([10, 10, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 128, 3, 3]).
size mismatch for conv_block_1.2.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([128]).
size mismatch for conv_block_2.0.weight: copying a param with shape torch.Size([10, 10, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 128, 3, 3]).
size mismatch for conv_block_2.0.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([128]).
size mismatch for conv_block_2.2.weight: copying a param with shape torch.Size([10, 10, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 128, 3, 3]).
size mismatch for conv_block_2.2.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([128]).
size mismatch for classifier.1.weight: copying a param with shape torch.Size([3, 1690]) from checkpoint, the shape in current model is torch.Size([3, 21632]). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Looking at the error, the image is directly loaded to model without any transformation for classification. It suggests that the image you sent to the model must be of size 128*128. The image should be transformed, same as you transformation process when fitted the train image during training. # Function to transform the image
def load_and_preprocess_image(image_path):
"""
Load and preprocess the image from the specified path.
"""
custom_image_uint8 = torchvision.io.read_image(str(image_path))
custom_image = custom_image_uint8.type(torch.float32)
# Divide the image pixel values by 255 to get them between [0, 1]
custom_image = custom_image / 255.
custom_image_transform = transforms.Compose([
transforms.Resize((128, 128), antialias=True) # set antialias = True to remove warning
])
# Apply transformations
custom_image_transformed = custom_image_transform(custom_image)
return custom_image_transformed
def predict_image(model, image_tensor, class_names):
"""
Predict the class label for the given image tensor using the provided model.
"""
with torch.inference_mode():
# Add an extra dimension to image
image_tensor_with_batch_size = image_tensor.unsqueeze(dim=0)
# Make a prediction on image with an extra dimension
image_pred = model(image_tensor_with_batch_size.to(device))
image_pred_probs = torch.softmax(image_pred, dim=1)
# Convert prediction probabilities -> prediction labels
image_pred_label = torch.argmax(image_pred_probs, dim=1)
return class_names[image_pred_label.numpy()], image_pred_probs # Device Agnostic Code ...
# ...
# Load Model
MODEL_SAVE_PATH = "model.pth"
model_info = torch.load(MODEL_SAVE_PATH, map_location=torch.device('cpu'))
# Instantiate Model
model = model_class() # pass ur model class name and values
# Load and preprocess the image
custom_image_transformed = load_and_preprocess_image(custom_image_path)
# Load the model
model.load_state_dict(model_info)
model.eval()
# Predict the label for the image
class_names = np.array(['Donuts', 'Dumplings', 'Ice Cream',
'Pizza', 'Ramen', 'Samosa',
'Steak', 'Sushi'])
predicted_label, image_pred_probs = predict_image(model,
custom_image_transformed,
class_names)
print(f"Label: {predicted_label[0]} and Confidence Rate: {image_pred_probs.max()}") |
Beta Was this translation helpful? Give feedback.
Looking at the error, the image is directly loaded to model without any transformation for classification. It suggests that the image you sent to the model must be of size 128*128. The image should be transformed, same as you transformation process when fitted the train image during training.