Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on single image inference, thanks #11

Open
junyongyou opened this issue Apr 13, 2023 · 7 comments
Open

Error on single image inference, thanks #11

junyongyou opened this issue Apr 13, 2023 · 7 comments

Comments

@junyongyou
Copy link

junyongyou commented Apr 13, 2023

I have tried to use the model to do aesthetics assessment on a single image, and below is my code snippet:

`
class IAA:
def init(self, model_file, weights_path):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.init_model(model_file, weights_path)
normalize = transforms.Normalize(mean=IMAGE_NET_MEAN, std=IMAGE_NET_STD)
self.transform = transforms.Compose([
transforms.ToTensor(),
normalize
])

def init_model(self, model_file, weights_path):
    self.model = TANet(model_file)
    self.model.load_state_dict(torch.load(weights_path))
    self.model.to(self.device)
    self.model.eval()

def inference(self, image_path):
    with open(image_path, 'rb') as f:
        image = Image.open(f)
        image = image.convert('RGB')
        image = image.resize((224, 224))
        image = self.transform(image)
        image = torch.unsqueeze(image, dim=0)
        image = image.to(self.device)
        iia_score = self.model(image)
    return iia_score

if name == 'main':
weights_path = r'.\weights\SRCC_758_LCC_765.pth'
model_file = r'.\weights\resnet18_places365.pth.tar'
image_path = r'.\samples\00000.png'
iaa = IAA(model_file, weights_path)
score = iaa.inference(image_path)

`

However, I got an error: ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 1]).

It seems this is a reported error when using batchnorm on a single image (batch size =1), see here (https://discuss.pytorch.org/t/error-expected-more-than-1-value-per-channel-when-training/26274/67). However, it was suggested that adding model.eval() can solve the problem. As you can see, I used model.eval(), but still got the error.

Did you happen to encounter to this error? If so, how did you solve it? Thanks a lot.

@junyongyou
Copy link
Author

junyongyou commented Apr 13, 2023

My current workaround solution is to use multiple images in a batch. However, it might be better to figure out how to solve the single image issue.

In addition, the model produces a softmax output with 10 element for a single image, because I am using the AVA model. How can we compute a single score for an image? Thank you.

@woshidandan
Copy link
Owner

  1. I have never encountered such a problem before ,you may refine the function of "validate(opt, model=model, loader=dataloader_test……" when bs=1,to see if you can solve this problem,see issue 8.
  2. use the function of "def get_score(opt, y_pred)" in my code to obtain a score.

@junyongyou
Copy link
Author

  1. I have never encountered such a problem before ,you may refine the function of "validate(opt, model=model, loader=dataloader_test……" when bs=1,to see if you can solve this problem,see issue 8.

    1. use the function of "def get_score(opt, y_pred)" in my code to obtain a score.

I got it, thanks a lot.

@justlike-prog
Copy link

@junyongyou hi could you solve this issue without using the dataloader? For now I am doing

bn7 = nn.BatchNorm1d(q.shape[0])
bn7.eval()

because it seems that the problem is that the BatchNorm1d is in training mode, although we set the whole model to eval. I am sure there is nicer solution. I guess the BatchNorm should be created when creating the TargetNet and then the forward pass just calls it.

@justlike-prog
Copy link

@woshidandan Ok my solution doesn't work because it throws some wrong predictions. I would need the single image inference without dataloader for converting the model to onnx. Any ideas?

@junyongyou junyongyou reopened this Sep 7, 2023
@junyongyou
Copy link
Author

There are some sub-models that are not in eval mode even the whole model is set to eval. Thus, I don't know how to solve the problem.

@chenminupup
Copy link

@junyongyou 关于single image inference,想问下你这边解决了吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants