module object has no attribute v1 or gfile or reszie tensorflow 1.12

目录

问题一:module object has no attribute v1

问题二:module object has no attribute 'gfile'

问题三:module object has no attribute resize


问题一:module object has no attribute v1

Traceback (most recent call last):
  File "./remove_gt_colormap.py", line 32, in <module>
    tf.compat.v1.flags.DEFINE_string('original_gt_folder',
AttributeError: 'module' object has no attribute 'v1'

解决办法:

找到出错的地方,将tf.compat.v1.flag.DEFINE_string改成tf.flag.DEFINE_string就行了,当然文中可能有好几处用的compat.v1,一并删掉就好了。

问题二:module object has no attribute 'gfile'

  File "./remove_gt_colormap.py", line 84, in <module>
    tf.app.run()
  File "/home/gaoye/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "./remove_gt_colormap.py", line 69, in main
    if not tf.io.gfile.isdir(FLAGS.output_dir):
AttributeError: 'module' object has no attribute 'gfile'

解决办法:

找到出错的地方,将tf.io.gfile.isdir改成tf.gfile.isdir,但这时候仍然会报错module objecct has no attribute isdir,这是因为tensorflow1中没有isdir,将其改称tf.gfile.Exists即可。将代码中tf.io.gfile.XXX中的io都删掉,可能还会报错has no attribute makedirs 之类的问题,那是因为tensorflow1和tensorflow2版本对应的名字不太一样,比如tensorflow1中makedirs是MakeDirs。下面贴上对比,一一对应照着修改就可以。

Tensorflow 1 的gfile API介绍 https://blog.csdn.net/a373595475/article/details/79693430

Tensorflw 2 的gfile API介绍  https://zhuanlan.zhihu.com/p/85130507

问题三:module object has no attribute resize

AttributeError: module tensorflow.image has no attribute resize

解决办法:

找到出错的地方,将tf.image.resize()改成tf.image.resize_images()

e.g.

label = tf.image.resize(label,new_dim,method=get_label_resize_method(label),align_corners=True)
改成
label = tf.image.resize_images(label,new_dim,method=get_label_resize_method(label),align_corners=True)

若resize函数指明了method,比如插值,还可以用以下方式解决:  

label = tf.image.resize(label,new_dim,method='bilinear',align_corners=True)
改成
label = tf.image.resize_bilinear(label,new_dim,align_corners=True)

resize_images是总的接口,建议用resize_images,便于维护。

Tensorflow中image.resize API介绍:https://www.jianshu.com/p/9cccdb418674

 

碎碎念:其实这就是tensorflow 1.X版本和tensorflow2.X版本的区别(真是奇怪,官方deeplab代码,用tensorflow2跑会出问题,用tensorflow1又在标签处理代码那出版本问题。。。为啥就不统一一个版本呢,心累)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章