LoginSignup
6
5

More than 3 years have passed since last update.

TensorFlow v2 でattribute 'app'が無いと怒られるときの解決方法

Posted at

0.概要

Tensor Flow v2のサンプルコードを実行した際に,以下のError文が出たときの解決方法についてまとめます.
(Tensor Flow v1へのバックデートは行いません.)

1.背景

Tensor Flowの勉強をするにあたり,以下のURL先よりサンプルコード(classify_image.py)をダウンロードしました.
https://github.com/atong01/Imagenet-Tensorflow/blob/master/classify_image.py

2.環境

OS:Windows10 Enterprise 1809 64bit
Python:3.7.6
TensorFlow_gpu:v2.1.0

3.困ったこと

サンプルコードなので,問題なく実行できるかと思いきや,なぜか以下のErrorが出てきてしまいます.

Error文
Traceback (most recent call last):
  File "classify_image.py", line 49, in <module>
    FLAGS = tf.app.flags.FLAGS
AttributeError: module 'tensorflow' has no attribute 'app'

なんでやねーん.

で,調べてみたところ
  「TensorFlow v2ではtf.appがすでに削除されているので,
   『TensorFlow v1にバックデートする』か
   『TensorFlow v2のAPI用にコードを書き換える』
   のどちらかで対応してね」とのこと.

なんやねーん.せっかく苦労して環境構築したんやから,こっから環境をいじりたくないわー.

4.解決方法

で,さらに調べたところ,以下のimport文をコードに書き込んだら問題解決可能との情報を発見.

解決方法
import tensorflow.compat.v1 as tf

実際,サンプルコードに追記して実行してみたら,問題なく動きました.
よかったー!

 
参考URLhttps://stackoverflow.com/questions/58258003/attributeerror-module-tensorflow-has-no-attribute-app

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5