Python Beginner
Python Beginner

Reputation: 53

How to fix AttributeError: module 'tensorflow' has no attribute 'Session' in Handwriting OCR

there are a lot of questions about this, but I didn't find the solution. I want to make Handwriting OCR from this site handwriting-ocr

When I import the library, I found this error

AttributeError                            Traceback (most recent call last)
<ipython-input-22-1c5011de3819> in <module>
      8 sys.path.append('../src/')
      9 from ocr.normalization import word_normalization, letter_normalization
---> 10 from ocr import page, words, characters
     11 from ocr.helpers import implt, resize
     12 from ocr.tfhelpers import Model

D:\Master\handwriting-ocr-master\handwriting-ocr-master\src\ocr\characters.py in <module>
     14 location = os.path.dirname(os.path.abspath(__file__))
     15 CNN_model = Model(
---> 16     os.path.join(location, '../../models/gap-clas/CNN-CG'))
     17 CNN_slider = (60, 30)
     18 RNN_model = Model(

D:\Master\handwriting-ocr-master\handwriting-ocr-master\src\ocr\tfhelpers.py in __init__(self, loc, operation, input_name)
     18         self.input = input_name + ":0"
     19         self.graph = tf.Graph()
---> 20         self.sess = tf.Session(graph=self.graph)
     21         with self.graph.as_default():
     22             saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)

AttributeError: module 'tensorflow' has no attribute 'Session

Because I use tensorflow 2.1.0 so I try to change Into this library

import tensorflow.compat.v1 as tf

And try this

hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

It's successful. The output is b'Hello, TensorFlow!'. If use import tensorflow as tf I change tf.Session() into this tf.compat.v1.Session() successful but if I implement it in ocr.py it still doesn't work and returns the same error no session

I have try re-install tensorflow too.

I use jupyter notebook , python 3.6, and opencv 3.3.1 Thanks for the help guys.

Upvotes: 0

Views: 455

Answers (1)

user11530462
user11530462

Reputation:

You need disable Tf2 behavior

import tensorflow.compat.v1 as tf
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.Session()

Upvotes: 1

Related Questions