keras-team / keras-contrib

Keras community contributions
MIT License
1.58k stars 650 forks source link

module 'keras.backend' has no attribute 'slice' #488

Open liaomingg opened 5 years ago

liaomingg commented 5 years ago

Env: python=3.5, tensorflow=1.12.0, keras=2.1.4, keras-contrib=2.0.8 when i use the file: keras_contrib/layers/crf.py, i get this AttributeError, what's wrong with me?

TQuy commented 5 years ago

I have the same error. python = 3.5, tensorflow = 1.54.0, keras = 2.0.8, keras-contrib = 2.0.8

Shaimaaf16 commented 5 years ago

I have the same error. python = 3.7, tensorflow = 1.13, keras = 2.2.4, keras-contrib = 2.0.8

martinoamigo commented 5 years ago

I also have the same error, but when converting from a keras model file to tflite

pmeier-tiplu commented 5 years ago

tensorflow.keras has no K.slice, they use tf.slice instead (infact that is what K.slice is calling).

This is happing in keras_contrib.layers.crf.py (specifically the call to K.slice in L463). I am looking into a solution currently, meanwhile you could just monkey patch it by adding something like this below the imports:

# address some inteeface discrepancies when using tensorflow.keras
if "slice" not in K.__dict__ and K.backend() == "tensorflow":
    # this is a good indicator that we are using tensorflow.keras

    try:
        # at first try to monkey patch what we need, will only work if keras-team keras is installed
        from keras import backend as KKK

        try:
            K.__dict__.update(
                is_tensor=KKK.is_tensor,
                slice=KKK.slice,
            )
        finally:
            del KKK
    except ImportError:
        # if that doesn't work we do a dirty copy of the code required
        import tensorflow as tf
        from tensorflow.python.framework import ops as tf_ops

        def is_tensor(x):
            return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)

        def slice(x, start, size):
            x_shape = K.int_shape(x)
            if (x_shape is not None) and (x_shape[0] is not None):
                len_start = K.int_shape(start)[0] if is_tensor(start) else len(start)
                len_size = K.int_shape(size)[0] if is_tensor(size) else len(size)
                if not (len(K.int_shape(x)) == len_start == len_size):
                    raise ValueError('The dimension and the size of indices should match.')
            return tf.slice(x, start, size)
pmeier-tiplu commented 5 years ago

This package is fundamentally incompatible with the current tensorflow 1.13.1 and later version when using tensorflow.keras.

anilknayak commented 5 years ago

This still now working Tensorflow 1.13.1 and Keras 2.2.4

iflament commented 4 years ago

@anilknayak still not working or now working?

i have version incompatibilities with keras 2.2.0 and tf 1.8 (segmentation faults). however when i downgrade keras to solve this, i get this slice error...

has anyone resolved this?

anilknayak commented 4 years ago

Not working

Walid-Ahmed commented 4 years ago

I also have the same error, but when converting from a keras model file to tflite

@martinoamigo were you able to solve this issue?

martinoamigo commented 4 years ago

I also have the same error, but when converting from a keras model file to tflite

@martinoamigo were you able to solve this issue?

I don’t recall, but if I did I probably just switched tensorflow or keras versions.

Walid-Ahmed commented 4 years ago

I think you are using a multi GPU model, if this is the case better chage it into a single GPU model and try again.

HuiHuangEmi commented 3 years ago

tensorflow.keras has no K.slice, they use tf.slice instead (infact that is what K.slice is calling).

This is happing in keras_contrib.layers.crf.py (specifically the call to K.slice in L463). I am looking into a solution currently, meanwhile you could just monkey patch it by adding something like this below the imports:

# address some inteeface discrepancies when using tensorflow.keras
if "slice" not in K.__dict__ and K.backend() == "tensorflow":
    # this is a good indicator that we are using tensorflow.keras

    try:
        # at first try to monkey patch what we need, will only work if keras-team keras is installed
        from keras import backend as KKK

        try:
            K.__dict__.update(
                is_tensor=KKK.is_tensor,
                slice=KKK.slice,
            )
        finally:
            del KKK
    except ImportError:
        # if that doesn't work we do a dirty copy of the code required
        import tensorflow as tf
        from tensorflow.python.framework import ops as tf_ops

        def is_tensor(x):
            return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)

        def slice(x, start, size):
            x_shape = K.int_shape(x)
            if (x_shape is not None) and (x_shape[0] is not None):
                len_start = K.int_shape(start)[0] if is_tensor(start) else len(start)
                len_size = K.int_shape(size)[0] if is_tensor(size) else len(size)
                if not (len(K.int_shape(x)) == len_start == len_size):
                    raise ValueError('The dimension and the size of indices should match.')
            return tf.slice(x, start, size)

It works!

tc9sachin12 commented 3 years ago

refer here for solution

rohanjoshi123 commented 3 years ago

Keras.backend doesnt have slice operation. Instead you can go to the location where crf.py file is stored locally on your machine (this you can find mentioned in the error dialogue, i.e. /home//anaconda3/lib/python3.8/site-packages/keras_contrib/layers/crf.py) and do the following:

add the line --> import tensrflow as tf goto line where it is mentioned K.slice --> you can do a search for "slice" --> replace K.slice by tf.slice

restart the jupyter notebook session. This should work.

I found this somewhere. This solved my problem

Rachelxuan11 commented 3 years ago

I have the same problem with keras=2.1.4, keras_contrib=2.0.8, python=3.6. The above monkey patch does not work for me. Then I modified the line where the error came from as following:

if len(states) > 3:
            if K.backend() == 'theano':
                m = states[3][:, t:(t + 2)]
            else:
                import tensorflow as tf  # modified K.slice to tf.slice, keras=2.1.4, keras_contrib=2.0.8, tensorflow=1.0.0, python=3.6
                m = tf.slice(states[3], [0, t], [-1, 2])
            input_energy_t = input_energy_t * K.expand_dims(m[:, 0])
            # (1, F, F)*(B, 1, 1) -> (B, F, F)
            chain_energy = chain_energy * K.expand_dims(
                K.expand_dims(m[:, 0] * m[:, 1]))

This works for me.