gpt4 book ai didi

python - absl.flags 错误 : Trying to access flag before flags were parsed

转载 作者:行者123 更新时间:2023-12-05 09:05:37 72 4
gpt4 key购买 nike

我正在尝试解析这个标志“dataroot”(字符串类型)。

将此视为演示代码:

from absl import flags
from absl.flags import FLAGS

flags.DEFINE_string('dataroot',"D:\College",'path to root folder of dataset')


if __name__ == '__main__':

#Hyperparameter
# Root directory for dataset
dataroot = FLAGS.dataroot

这就是错误的来源:

Traceback (most recent call last):
File "d:/Github/cloned repo/Image-Restoration-in-Occluded-Images-using-GANs/main.py", line 49, in <module>
dataroot = FLAGS.dataroot
File "D:\anaconda\lib\site-packages\absl\flags\_flagvalues.py", line 498, in __getattr__
raise _exceptions.UnparsedFlagAccessError(error_message)
absl.flags._exceptions.UnparsedFlagAccessError: Trying to access flag --dataroot before flags were parsed.

知道我哪里出错了吗?

最佳答案

在访问 FLAGS.xxx 之前,您需要告诉标志库使用 FLAGS(sys.argv) 解析 argv。调用 app.run() 是一种隐式执行此操作的方法。

通过显式调用 FLAGS(sys.argv) 而不使用 app.run() 的示例:

  from absl import flags
import sys
flags.DEFINE_string('dataroot',"D:\\College",'path to root folder of dataset')
FLAGS = flags.FLAGS
FLAGS(sys.argv) # need to explicitly to tell flags library to parse argv before you can access FLAGS.xxx
dataroot = FLAGS.dataroot

关于python - absl.flags 错误 : Trying to access flag before flags were parsed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66953343/

72 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com