But suddenly img.unsqueeze_(0) worked, just i tried line. Things are not getting in my head, probably because of my neurons are not trained yet in leanring pytorch.
I was using VGG16 model to predict dogs as assignments. I just tried both it all worked.


Summary
def VGG16_predict(img_path):
‘’’
Use pre-trained VGG-16 model to obtain index corresponding to
predicted ImageNet class for image at specified path
Args:
    img_path: path to an image
    
Returns:
    Index corresponding to VGG-16 model's prediction
'''

## TODO: Complete the function.
## Load and pre-process an image from the given img_path
img_to_tensor =  transforms.Compose([
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
img_pil = Image.open(img_path)
img = img_to_tensor(img_pil)
print("Image type: ", img.type())
print("shape without unsqueezing: ", img.shape)
print("After unsqueezing: ",img.unsqueeze_(0).shape)
prediction = VGG16(img.unsqueeze_(0))
## Return the *index* of the predicted class for that image


return prediction.data.numpy().argmax() # predicted class index


Oh sorry, I found tha problem happend previously that i was using
transforms.CenterCrop(40) previously , thats why it was giving errr.
When i change to  transforms.CenterCrop(224) , It all worked
Thank you for helping