I got a warning "A batch with no more than one sample ---" when try to predict a single image using Resnet50 with Chainer

I got a warning "A batch with no more than one sample has been given to F.batch_normalization. F.batch_normalization will always output a zero tensor for such batches" when trying to predict a single a image.

I am using Resnet50 model (binary classification output) with Chainer, when training, the accuracy of both train and test set is > 98%. But when i tried to predict a single image, the accuracy is only about 50%.

I guess I have to predict images in batch (because Batch Normalization) to avoid warning and get high accuracy, right? Is it possible to predict single image?

P/s : I tried chainer.using_config('train', False) in prediction script but it didn't help.

2 answers

  • answered 2018-07-11 04:18 Kenichi Maehashi

    Do you set chainer.config.train = False correctly? Note that chainer.using_config is a context manager.

    with chainer.using_config('train', False):
        do_something()
    

    is (almost) equivalent to:

    chainer.config.train = False
    try:
        do_something()
    finally:
        chainer.config.train = True
    

    See also: https://docs.chainer.org/en/stable/reference/configuration.html#changing-configuration

  • answered 2018-07-12 02:14 Yuki Hashimoto

    Could you please upload the full stack-trace? I'm very interested in where from the error was raised.

    If it is from "chainer/links/normalization/batch_normalization.py", line 271~273, the error can be attributed to chainer.config.train == True.

    In other cases, something wrong is happening.

    Or, you can try print("chainer.config.train") just before your call of L.BatchNormalization.