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

Image normalisation while predicting in demo.py #30

Open
ankushpanwar19 opened this issue Mar 29, 2022 · 1 comment
Open

Image normalisation while predicting in demo.py #30

ankushpanwar19 opened this issue Mar 29, 2022 · 1 comment

Comments

@ankushpanwar19
Copy link

I checked the demo.py script and its related files but I don't see any image normalization while prediction. However in train.py I could see image normalization using albumentations.
Is there a specific reason why normalization has been removed from the inference?

@stakanich
Copy link

Hi. Maybe too late but I have an answer for you.
In demo.py you can see the definition of the spoof_model:

    if args.spf_model.endswith('pth.tar'):
        if not args.config:
            raise ValueError('You should pass config file to work with a Pytorch model')
        config = utils.read_py_config(args.config)
        spoof_model = utils.build_model(config, args, strict=True, mode='eval')
        spoof_model = TorchCNN(spoof_model, args.spf_model, config, device=device)
    else:
        assert args.spf_model.endswith('.xml')
        spoof_model = VectorCNN(args.spf_model)

and in demo_tools/wrapers.py

class TorchCNN:
    ...
    def preprocessing(self, images):
        ''' making image preprocessing for pytorch pipeline '''
        mean = np.array(object=self.config.img_norm_cfg.mean).reshape((3,1,1))
        std = np.array(object=self.config.img_norm_cfg.std).reshape((3,1,1))
        height, width = list(self.config.resize.values())
        preprocessed_imges = []
        for img in images:
            img = cv.resize(img, (height, width) , interpolation=cv.INTER_CUBIC)
            img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
            img = np.transpose(img, (2, 0, 1)).astype(np.float32)
            img = img/255
            img = (img - mean)/std
            preprocessed_imges.append(img)
        return torch.tensor(preprocessed_imges, dtype=torch.float32)

    def forward(self, batch):
        batch = self.preprocessing(batch)
        self.model.eval()
        model1 = (self.model.module
                  if self.config.data_parallel.use_parallel
                  else self.model)
        with torch.no_grad():
            output = model1.forward_to_onnx(batch)
            return output.detach().numpy()

So, at least for .pth.tar models, here you can see normalization.

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

2 participants