AttributeError: module 'tensorflow' has no attribute 'gfile'
Asked Answered
M

6

23

I trained a simple mnist model with tensorflow 2.0 on Google Colab and saved it in the .json format. Click here to check out the Colab Notebook where I've written the code. Then on running the command

!simple_tensorflow_serving --model_base_path="/" --model_platform="tensorflow"

It is showing the error AttributeError: module 'tensorflow' has no attribute 'gfile'

simple_tensorflow_serving helps in easily deploying trained tensorflow model into production.

Versions I'm using:

(1) TensorFlow - 2.0

(2) simple_tensorflow_serving - 0.6.4

Thank you in advance :)

Mopboard answered 9/4, 2019 at 11:8 Comment(1)
I'm having the same issue with FullTokenizer(vocab_file, do_lower_case) in TF 2Chemosphere
U
26

Simple Tensorflow Serving is not ready for Tensorflow 2.0, since it is using the old API. In Tensorflow 2.0 the gfile package has been moved into tf.io.

Then, you have to downgrade your Tensorflow instance to TF 1.13 use Simple Tensorflow Serving

Urethroscope answered 9/4, 2019 at 14:32 Comment(3)
How do you downgrade it?Synovia
pip install tensorflow==1.13Urethroscope
Incase anyone gets here and is getting "ERROR: Could not find a version that satisfies the requirement tensorflow==1.13". Please use ----> pip install tensorflow==1.13.1Gyromagnetic
A
47

In 2.0, tf.gfile.* is replaced by tf.io.gfile.*.

when I get error:

  File "/Users/MRJ/anaconda3/envs/python37-tf2.1/lib/python3.7/
site-packages/object_detection/utils/label_map_util.py",
line 137, in load_labelmap

with tf.gfile.GFile(path, 'r') as fid:
  AttributeError: module 'tensorflow' has no attribute 'gfile'

1.Find label_map_util.py line 137.

2.Replace tf.gfile.GFile to tf.io.gfile.GFile

It's worked for me.

tensorflow issue #31315

Alisonalissa answered 12/5, 2020 at 9:18 Comment(2)
Did you have any errors for other commands in tensorflow? I want to know if changing this will cause any other issuesUnsparing
No. "In 2.0, tf.gfile.* is replaced by tf.io.gfile.*." So tf.io.gfile.GFile alway is right.Alisonalissa
U
26

Simple Tensorflow Serving is not ready for Tensorflow 2.0, since it is using the old API. In Tensorflow 2.0 the gfile package has been moved into tf.io.

Then, you have to downgrade your Tensorflow instance to TF 1.13 use Simple Tensorflow Serving

Urethroscope answered 9/4, 2019 at 14:32 Comment(3)
How do you downgrade it?Synovia
pip install tensorflow==1.13Urethroscope
Incase anyone gets here and is getting "ERROR: Could not find a version that satisfies the requirement tensorflow==1.13". Please use ----> pip install tensorflow==1.13.1Gyromagnetic
A
5

Instead, try

tf.io.gfile.GFile(
    name, mode='r'
)

i.e ".io" will solve your all problems instead of downgrading your tf

Abc answered 5/12, 2021 at 16:23 Comment(0)
S
0

change tf.gfile.GFile to tf.io.gfile.GFile.

it works for this instance but not for all other files

example tf.io.gfile.FastGFile throws an error saying

AttributeError: module 'tensorflow._api.v2.io.gfile' has no attribute 'FastGFile'
Scruggs answered 20/11, 2021 at 23:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Backstop
S
0

This is deprecated in TensorFlow 2.0. You can use older tensorflow version to use this api like 1.13.1

In my older project I needed this api Created a virtual environment in ananconda with this version like:

conda create -n tf python=3.7 tensorflow=1.13.1 
// here more modules with specific version can be added

conda activate tf // to activate environment tf

(base) D:\ff\testM>  --> (tf) D:\ff\testM> 
// After this environment changed to (base) --> (tf)
Simas answered 16/8, 2022 at 5:17 Comment(0)
H
0

The error AttributeError: module 'tensorflow' has no attribute 'gfile' indicates that the TensorFlow module being used in your code does not have the gfile attribute. This could be due to a version mismatch or changes in the TensorFlow API.

In older versions of TensorFlow, gfile was part of the tensorflow module and could be accessed like tensorflow.gfile. However, in newer versions, it has been moved to tf.compat.v1.gfile.

To fix this change your import from tensorflow.gfile to tf.compat.v1.gfile

And there should be no need to downgrade your version of tensorflow.

Hobble answered 3/2 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.