keras-contrib icon indicating copy to clipboard operation
keras-contrib copied to clipboard

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

Open liaomingg opened this issue 5 years ago • 15 comments

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?

liaomingg avatar Apr 12 '19 09:04 liaomingg

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

TQuy avatar Apr 27 '19 08:04 TQuy

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

Shaimaaf16 avatar May 16 '19 14:05 Shaimaaf16

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

martinoamigo avatar May 23 '19 17:05 martinoamigo

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 avatar Jun 17 '19 19:06 pmeier-tiplu

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

pmeier-tiplu avatar Jun 18 '19 11:06 pmeier-tiplu

This still now working Tensorflow 1.13.1 and Keras 2.2.4

anilknayak avatar Jun 26 '19 23:06 anilknayak

@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?

iflament avatar Sep 25 '19 20:09 iflament

Not working

anilknayak avatar Sep 25 '19 20:09 anilknayak

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

@martinoamigo were you able to solve this issue?

Walid-Ahmed avatar Apr 29 '20 20:04 Walid-Ahmed

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.

martinoamigo avatar Apr 29 '20 23:04 martinoamigo

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.

Walid-Ahmed avatar May 21 '20 17:05 Walid-Ahmed

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!

HuiHuangEmi avatar Oct 13 '20 13:10 HuiHuangEmi

refer here for solution

tc9sachin12 avatar Oct 29 '20 06:10 tc9sachin12

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

rohanjoshi123 avatar Nov 03 '20 06:11 rohanjoshi123

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.

Rachelxuan11 avatar Dec 01 '20 04:12 Rachelxuan11