titu1994 / keras-coordconv

Keras implementation of CoordConv for all Convolution layers
MIT License
148 stars 33 forks source link

Converting to tensorflow.keras gives strange error #14

Open mettafore opened 5 years ago

mettafore commented 5 years ago

Thank you for this great library. It is certainly very useful. I am using the tensorflow.keras framework. I've changed all the library imports in coord.py to tensorflow.keras . In eager execution mode, it works fine, however, I get this error in tensorflow static graph mode:

AttributeError: 'Tensor' object has no attribute 'is_initialized'

Could you help me solve this issue?

Thanks

titu1994 commented 5 years ago

Could you provide a full stack trace. That would be more helpful.

awhobbs commented 5 years ago

I'm getting the same error while trying to convert to an estimator:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-0cfc1dbdeb5c> in <module>
----> 1 estimator = tf.keras.estimator.model_to_estimator(keras_model=model)

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/estimator/__init__.py in model_to_estimator(keras_model, keras_model_path, custom_objects, model_dir, config)
     71       custom_objects=custom_objects,
     72       model_dir=model_dir,
---> 73       config=config)
     74 
     75 # LINT.ThenChange(//third_party/tensorflow_estimator/python/estimator/keras.py)

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/keras.py in model_to_estimator(keras_model, keras_model_path, custom_objects, model_dir, config)
    484   if keras_model._is_graph_network:
    485     warm_start_path = _save_first_checkpoint(keras_model, custom_objects,
--> 486                                              config)
    487   elif keras_model.built:
    488     logging.warning('You are creating an Estimator from a Keras model manually '

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/keras.py in _save_first_checkpoint(keras_model, custom_objects, config)
    352       training_util.create_global_step()
    353       model = _clone_and_build_model(model_fn_lib.ModeKeys.TRAIN, keras_model,
--> 354                                      custom_objects)
    355       # save to checkpoint
    356       with session.Session(config=config.session_config) as sess:

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/keras.py in _clone_and_build_model(mode, keras_model, custom_objects, features, labels)
    199       compile_clone=compile_clone,
    200       in_place_reset=(not keras_model._is_graph_network),
--> 201       optimizer_iterations=global_step)
    202 
    203   return clone

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/models.py in clone_and_build_model(model, input_tensors, target_tensors, custom_objects, compile_clone, in_place_reset, optimizer_iterations)
    496       K.track_tf_optimizer(optimizer)
    497     else:
--> 498       optimizer_config = model.optimizer.get_config()
    499       optimizer = model.optimizer.__class__.from_config(optimizer_config)
    500       if optimizer_iterations is not None:

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/optimizers.py in get_config(self)
    529   def get_config(self):
    530     config = {
--> 531         'lr': float(K.get_value(self.lr)),
    532         'beta_1': float(K.get_value(self.beta_1)),
    533         'beta_2': float(K.get_value(self.beta_2)),

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in get_value(x)
   2796   elif ops.inside_function():
   2797     raise RuntimeError('Cannot get value inside Tensorflow graph function.')
-> 2798   return x.eval(session=get_session())
   2799 
   2800 

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in get_session()
    480   if not _MANUAL_VAR_INIT:
    481     with session.graph.as_default():
--> 482       _initialize_variables(session)
    483   return session
    484 

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in _initialize_variables(session)
    756     # marked as initialized.
    757     is_initialized = session.run(
--> 758         [variables_module.is_variable_initialized(v) for v in candidate_vars])
    759     uninitialized_vars = []
    760     for flag, v in zip(is_initialized, candidate_vars):

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in <listcomp>(.0)
    756     # marked as initialized.
    757     is_initialized = session.run(
--> 758         [variables_module.is_variable_initialized(v) for v in candidate_vars])
    759     uninitialized_vars = []
    760     for flag, v in zip(is_initialized, candidate_vars):

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py in wrapped(*args, **kwargs)
    191   """
    192   def wrapped(*args, **kwargs):
--> 193     return _add_should_use_warning(fn(*args, **kwargs))
    194   return tf_decorator.make_decorator(
    195       fn, wrapped, 'should_use_result',

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in is_variable_initialized(variable)
   2922     initialized, `False` otherwise.
   2923   """
-> 2924   return state_ops.is_variable_initialized(variable)
   2925 
   2926 

~/.local/share/virtualenvs/workspace-wJJAg-g4/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py in is_variable_initialized(ref, name)
    131     return gen_state_ops.is_variable_initialized(ref=ref, name=name)
    132   # Handle resource variables.
--> 133   return ref.is_initialized(name=name)
    134 
    135 

AttributeError: 'Tensor' object has no attribute 'is_initialized'
awhobbs commented 5 years ago

Here's a simple model that will reproduce the error:

import tensorflow as tf
from tensorflow.keras import layers, initializers
from coord import CoordinateChannel2D

model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=[65,65,3], name='image'))

model.add(CoordinateChannel2D())

model.add(layers.Conv2D(filters=6, kernel_size=(35, 35), activation='relu'))
model.add(layers.AveragePooling2D())

model.add(layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'))
model.add(layers.AveragePooling2D())

model.add(layers.Flatten())

model.add(layers.Dense(units=120, activation='relu'))

model.add(layers.Dense(units=84, activation='relu'))

model.add(layers.Dense(units=1, activation = 'sigmoid'))

model.compile(loss=tf.keras.losses.mean_squared_error, 
              optimizer=tf.keras.optimizers.Adam(), 
              metrics=['mse'])

estimator = tf.keras.estimator.model_to_estimator(keras_model=model)
styloInt commented 4 years ago

I had the same issue and I solved it by installing tensorflow 2.0

thomasaarholt commented 4 years ago

Even with 2.1 I still get this error, when running the above example. Any idea?

ysyyork commented 4 years ago

i changed all the K to tf and it works lol. Very weird solution but it works. I'm on tf 1.14.0

ysyyork commented 4 years ago

i changed all the K to tf and it works lol. Very weird solution but it works. I'm on tf 1.14.0

Actually only need to change all the K.ones to tf.ones and this issue go away

Taram1980 commented 3 years ago

i changed all the K to tf and it works lol. Very weird solution but it works. I'm on tf 1.14.0

Actually only need to change all the K.ones to tf.ones and this issue go away

Sorry, but what means 'change all the K.ones to tf.ones '? What tf.ones?