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

Importing tensorflow_hub interferes with python logging #263

Closed
matteby opened this issue Mar 28, 2019 · 1 comment
Closed

Importing tensorflow_hub interferes with python logging #263

matteby opened this issue Mar 28, 2019 · 1 comment
Assignees
Labels
hub For all issues related to tf hub library and tf hub tutorials or examples posted by hub team type:support

Comments

@matteby
Copy link

matteby commented Mar 28, 2019

I've run into an issue where importing tensorflow_hub interferes with and prevents python logging.

For example, this script runs fine:

#!/usr/bin/env python3
import argparse
import logging
import time

import tensorflow as tf
#import tensorflow_hub as hub

logger = logging.getLogger(__name__)
    
parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version='1.0.0')
subparsers = parser.add_subparsers()

subparser = subparsers.add_parser('train', help='train the Is Data Plate model')
subparser.add_argument('--batch-size', help='training batch size', type=int)
subparser.add_argument('--validation-batch-size', help='validation batch size', type=int)

if __name__ == '__main__':
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)
    args = parser.parse_args()

    logger.info("Processing...")

    logger.info(f'Done - elapsed time: {time.process_time()} seconds')

and produces the following output:

$ ./tf-hub-logging.py train
2019-03-28 17:15:31,635 - __main__ - INFO - Processing...
2019-03-28 17:15:31,635 - __main__ - INFO - Done - elapsed time: 2.383287 seconds

However, when tensorflow_hub is imported as in this script:

#!/usr/bin/env python3
import argparse
import logging
import time

import tensorflow as tf
import tensorflow_hub as hub

logger = logging.getLogger(__name__)
    
parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version='1.0.0')
subparsers = parser.add_subparsers()

subparser = subparsers.add_parser('train', help='train the Is Data Plate model')
subparser.add_argument('--batch-size', help='training batch size', type=int)
subparser.add_argument('--validation-batch-size', help='validation batch size', type=int)

if __name__ == '__main__':
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)
    args = parser.parse_args()

    logger.info("Processing...")

    logger.info(f'Done - elapsed time: {time.process_time()} seconds')

The python logger is broken and produces the following output which displays a warning and also causes lost logging messages:

$ ./tf-hub-logging.py train
WARNING: Logging before flag parsing goes to stderr.
W0328 17:15:19.787199 4598539712 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14

Has anyone run into a similar issue?

@rmothukuru rmothukuru self-assigned this Mar 29, 2019
@rmothukuru rmothukuru added type:support hub For all issues related to tf hub library and tf hub tutorials or examples posted by hub team labels Mar 29, 2019
@arnoegw arnoegw assigned arnoegw and unassigned Harshini-Gadige Mar 29, 2019
@arnoegw
Copy link
Contributor

arnoegw commented Mar 29, 2019

Hi matteby. I can reproduce your problem. The crucial point is that import tensorflow_hub triggers from absl import logging, which in turn fiddles with logging.root.handlers; see
absl/logging/__init__.py:1151.

Here is a minimal example:

import logging
# from absl import logging as absl_logging  # Uncomment to trigger the issue.
logger = logging.getLogger("yourlogger")
logging.basicConfig(level=logging.INFO)
logger.info("Hello, world")

This is a problem with absl.logging and cannot be fixed at the level of tensorflow_hub. We do as all of TensorFlow does for version 2.0 and switch from tf.logging to absl.logging (see tensorflow.org/alpha/guide/effective_tf2).

Please report at https://github.com/abseil/abseil-py/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hub For all issues related to tf hub library and tf hub tutorials or examples posted by hub team type:support
Projects
None yet
Development

No branches or pull requests

4 participants