Error while importing tensorflow

Are you getting the same TypeError after changing to K.image_data_format() ?
Also can you check and post here the version of Keras installed in your system ?

Yes i am getting the same error. I think i am using Keras 2.0.8 as that’s what the it installs by default.

If i need to install some other version ,kindly please also post the exact command needed to install the saem.

Thank you so much for your help so far!

Looking forward to your reply.

I never encountered the error myself. So I am providing you a few details to check.

Keras 2 ----- > Installed
Tensorflow ----> Installed

Then in command line execute,

cd <your_own_home> / .keras
Now do
gedit keras.json

After the editor opens with keras json details, check if the lines are present or not.

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

the do a

sudo apt-get update

And check if its working now. If not replace the json with these lines. (Only if above steps do not work)

{
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow",
    "image_data_format": "channels_last",
    "image_dim_ordering":"tf"
}

Reminder do a sudo update again.

Here’s the doc link :-

https://keras.io/backend/


Optional:
Are you using correct syntaxes ? Like this one obtaininput_shape. Shouldn’t it be obtain_input_shape() ? I am just asking.

HI,

I ran the follow ing commands cd / .keras and then “gedit keras.json”, but got the error as

Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused.

Also, i changed the code to,

Determine proper input shape

input_shape = obtain_input_shape(input_shape,
                                  default_size=224,
                                  min_size=48,
                                  data_format=K.image_data_format(),
                                  include_top=include_top)

But now i am getting a new error which says,
TypeError Traceback (most recent call last)
in ()
----> 1 model = VGG16_Avg(include_top=False)

~/nbs/vgg16_avg.py in VGG16_Avg(include_top, weights, input_tensor, input_shape, classes)
36 min_size=48,
37 data_format=K.image_data_format(),
—> 38 include_top=include_top)
39
40 if input_tensor is None:

TypeError: obtain_input_shape() got an unexpected keyword argument ‘include_top’

Please help!

##Additionally,
I am also getting below error while running limit_mem() function.,

RuntimeError Traceback (most recent call last)
in ()
1 # Tell Tensorflow to use no more GPU RAM than necessary
----> 2 limit_mem()

~/nbs/utils2.py in limit_mem()
40
41 def limit_mem():
—> 42 K.get_session().close()
43 cfg = K.tf.ConfigProto()
44 cfg.gpu_options.allow_growth = True

~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in get_session()
173 # not already marked as initialized.
174 is_initialized = session.run(
–> 175 [tf.is_variable_initialized(v) for v in candidate_vars])
176 uninitialized_vars = []
177 for flag, v in zip(is_initialized, candidate_vars):

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
765 try:
766 result = self._run(None, fetches, feed_dict, options_ptr,
–> 767 run_metadata_ptr)
768 if run_metadata:
769 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
903 raise RuntimeError(‘Attempted to use a closed Session.’)
904 if self.graph.version == 0:
–> 905 raise RuntimeError('The Session graph is empty. Add operations to the '
906 ‘graph before calling run().’)
907

RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

Thanks,
Naman

For first error while opening gedit has something related to ssh login. Visit the thread below

For the “TypeError: obtain_input_shape() got an unexpected keyword argument 'include_top’” it seems like you need to define some variable and then pass it to the function
obtain_input_shape(include_top = some variable).

For limit_mem() issue, though I never faced it myself till date. As I see your code in stack trace rather than you provided. Here the limit code function. Do a cross check:-

def limit_mem():
    K.get_session().close()
    cfg = K.tf.ConfigProto()
    cfg.gpu_options.allow_growth = True
    K.set_session(K.tf.Session(config=cfg))

By the way you can create a new post for lim_mem() error as it has nothing to do with tensorflow import. :slight_smile:

As asked, after replacingg include_top with some variable ‘cf’ i am still getting the same error

# Determine proper input shape

input_shape = _obtain_input_shape(input_shape,
                                  default_size=224,
                                  min_size=48,
                                  data_format=K.image_data_format(),
                                  include_top=cf)

TypeError Traceback (most recent call last)
in ()
----> 1 model = VGG16_Avg(include_top=False)

~/nbs/vgg16_avg.py in VGG16_Avg(include_top, weights, input_tensor, input_shape, classes)
37 min_size=48,
38 data_format=K.image_data_format(),
—> 39 include_top=cf)
40
41 if input_tensor is None:

TypeError: obtain_input_shape() got an unexpected keyword argument ‘include_top’

Also, FYI i am using the files available on github:-

I also changed image_dim_ordering here:

load weights

if weights == 'imagenet':
    if K.image_data_format() == 'th':
        if include_top:

Moreover,
the gedit solution you posted is still not working. I am getting the same error.
Please help with this particular error.

TypeError Traceback (most recent call last)
in ()
----> 1 model = VGG16_Avg(include_top=False)

~/nbs/vgg16_avg.py in VGG16_Avg(include_top, weights, input_tensor, input_shape, classes)
37 min_size=48,
38 data_format=K.image_data_format(),
---> 39 include_top=cf)
40'

From the stack trace you posted It seems the parameter include_top in the model VGG16_Avg(include_top = False) is a boolean

  1. What value did you passed in cf that you mentioned ? Was it a Bool value ?

  2. The github link you posted also has VGG16_Avg(include_top = True). Try going through the code again.


For gedit issue please visit these threads and see if it helps.

https://ask.wireshark.org/questions/43205/failed-to-connect-to-mir-failed-to-connect-to-server-socket-no-such-file-or-directory-when-trying-to-run-wireshark

I just used cf= include_top as you had asked, but it’s the same thing as include_top = include_top

I dint asked to use cf = include_top. You have to set a value to include_top to either True or as False where the true and false are boolean values. It depends on your requirements. What I saw from the VGG16.py file is that include_top is required when you want to use the dense layers on the convolution. And false if do not want to include.

For Example,

cf = True
VGG16_Avg(include_top = cf, weights, input_tensor, input_shape, classes)