syb7573330 / im2avatar

MIT License
136 stars 37 forks source link

AttributeError: module 'tensorflow.python.platform.flags' has no attribute '_global_parser' #10

Open uree opened 4 years ago

uree commented 4 years ago

I got this error with tensorflow:latest, tensorflow:1.8.0-gpu-py3, 1.14.0-gpu-py3 and 1.14.0-gpu. I got them all via docker.

python train_shape.py --cat_id 02958343
Traceback (most recent call last):
  File "train_shape.py", line 51, in <module>
    LOG_FOUT.write(str(tf.flags._global_parser.parse_args())+'\n')
AttributeError: module 'tensorflow.python.platform.flags' has no attribute '_global_parser'
Li-rr commented 4 years ago

Hi, I got same problem with you, do you solve it?

uree commented 4 years ago

I solved it by making the code tensorflow 2.x compatible. I had trouble making earlier versions/docker images of tf work so this was the reasonable thing to do from my perspective. I used the latest tf docker image with gpu support.

First of you can use this tool to convert the code and it will then tell you what else you need to clean up. One such thing is to replace the _global_parser with argparse, which mostly means you need to rewrite the flags like this (from train_shape.py):

# tf.app.flags.DEFINE_string('train_dir', './train_shape',
#                            """Directory where to write summaries and checkpoint.""")
parser.add_argument('--train_dir', default='./train_shape', help= """Directory where to write summaries and checkpoint.""")

# tf.app.flags.DEFINE_string('base_dir', './data/ShapeNetCore_im2avatar',
#                            """The path containing all the samples.""")
parser.add_argument('--base_dir', default='./data/ShapeNetCore_im2avatar', help="""The path containing all the samples.""")

# tf.app.flags.DEFINE_string('cat_id', '02958343',
#                            """The category id for each category: 02958343, 03001627, 03467517, 04379243""")
parser.add_argument('--cat_id', default='02958343', help= """The category id for each category: 02958343, 03001627, 03467517, 04379243""")

# tf.app.flags.DEFINE_string('data_list_path', './data_list',
#                           """The path containing data lists.""")
parser.add_argument('--data_list_path', default='./data_list', help= """The path containing data lists.""")

# tf.app.flags.DEFINE_integer('train_epochs', 501, """Training epochs.""")
parser.add_argument('--train_epochs', default=501, help= """The path containing data lists.""", type=int)

# tf.app.flags.DEFINE_integer('batch_size', 60, """Batch size.""")
parser.add_argument('--batch_size', default=30, help= """Batch size.""", type=int)

# tf.app.flags.DEFINE_integer('gpu', 0, """""")
parser.add_argument('--gpu', default=0, help= """""", type=int)

# tf.app.flags.DEFINE_float('learning_rate', 0.0003, """""")
parser.add_argument('--learning_rate', default=0.0003, help= """""", type=float)

# tf.app.flags.DEFINE_float('wd', 0.00001, """""")
parser.add_argument('--wd', default=0.00001, help= """""", type=float)

# tf.app.flags.DEFINE_integer('epochs_to_save',20, """""")
parser.add_argument('--epochs_to_save', default=20, help="""""", type=int)

# tf.app.flags.DEFINE_integer('decay_step',20000, """for lr""")
parser.add_argument('--decay_step', default=2000, help="""for lr""", type=int)

# tf.app.flags.DEFINE_float('decay_rate', 0.7, """for lr""")
parser.add_argument('--decay_rate', default=0.7, help="""for lr""", type=int)

and replace all other occurrences of flags, so that the code uses values from parser/argparse.

The other issue I had to solve was to avoid using xavier_initializer (deprecated to). Presumably can just comment it out and go with the truncated_normal_initializer here. People have suggested using GlorotUniform() too.

I'll share the code once I'm sure I can get the rest of it to work too (inference ...)

YakamiNad commented 3 years ago

I solved it by making the code tensorflow 2.x compatible. I had trouble making earlier versions/docker images of tf work so this was the reasonable thing to do from my perspective. I used the latest tf docker image with gpu support.

First of you can use this tool to convert the code and it will then tell you what else you need to clean up. One such thing is to replace the _global_parser with argparse, which mostly means you need to rewrite the flags like this (from train_shape.py):

# tf.app.flags.DEFINE_string('train_dir', './train_shape',
#                            """Directory where to write summaries and checkpoint.""")
parser.add_argument('--train_dir', default='./train_shape', help= """Directory where to write summaries and checkpoint.""")

# tf.app.flags.DEFINE_string('base_dir', './data/ShapeNetCore_im2avatar',
#                            """The path containing all the samples.""")
parser.add_argument('--base_dir', default='./data/ShapeNetCore_im2avatar', help="""The path containing all the samples.""")

# tf.app.flags.DEFINE_string('cat_id', '02958343',
#                            """The category id for each category: 02958343, 03001627, 03467517, 04379243""")
parser.add_argument('--cat_id', default='02958343', help= """The category id for each category: 02958343, 03001627, 03467517, 04379243""")

# tf.app.flags.DEFINE_string('data_list_path', './data_list',
#                           """The path containing data lists.""")
parser.add_argument('--data_list_path', default='./data_list', help= """The path containing data lists.""")

# tf.app.flags.DEFINE_integer('train_epochs', 501, """Training epochs.""")
parser.add_argument('--train_epochs', default=501, help= """The path containing data lists.""", type=int)

# tf.app.flags.DEFINE_integer('batch_size', 60, """Batch size.""")
parser.add_argument('--batch_size', default=30, help= """Batch size.""", type=int)

# tf.app.flags.DEFINE_integer('gpu', 0, """""")
parser.add_argument('--gpu', default=0, help= """""", type=int)

# tf.app.flags.DEFINE_float('learning_rate', 0.0003, """""")
parser.add_argument('--learning_rate', default=0.0003, help= """""", type=float)

# tf.app.flags.DEFINE_float('wd', 0.00001, """""")
parser.add_argument('--wd', default=0.00001, help= """""", type=float)

# tf.app.flags.DEFINE_integer('epochs_to_save',20, """""")
parser.add_argument('--epochs_to_save', default=20, help="""""", type=int)

# tf.app.flags.DEFINE_integer('decay_step',20000, """for lr""")
parser.add_argument('--decay_step', default=2000, help="""for lr""", type=int)

# tf.app.flags.DEFINE_float('decay_rate', 0.7, """for lr""")
parser.add_argument('--decay_rate', default=0.7, help="""for lr""", type=int)

and replace all other occurrences of flags, so that the code uses values from parser/argparse.

The other issue I had to solve was to avoid using xavier_initializer (deprecated to). Presumably can just comment it out and go with the truncated_normal_initializer here. People have suggested using GlorotUniform() too.

I'll share the code once I'm sure I can get the rest of it to work too (inference ...)

Hi did you solve the issue as i have quite a few bugs when compiling, much thanks!

uree commented 3 years ago

What a coincidence. I uploaded an adapted version of the code which worked for me just now. It might help you ... https://github.com/uree/im2avatar-debian