Attribute error= module 'tensorflow.python.keras.backend' has no attribute 'slice'

2,706 views
Skip to first unread message

Sumod Nandanwar

unread,
Apr 27, 2021, 6:57:45 AM4/27/21
to Keras-users
I am trying to load a pretrained model.h5 in keras. But I get the attribute error as module 'tensorflow.python.keras.backend' has no attribute 'slice'. 
My versions of
tf = 2.4.1
keras = 2.4.0 
python = 3.7

The code I am using for loading the model is as:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Layer
model = keras.models.load_model('../input/potato-lateblight-trained/Potato_trained_model/LB_potato.h5', custom_objects={"MaxPoolingWithArgmax2D":MaxPoolingWithArgmax2D,"MaxUnpooling2D":MaxUnpooling2D})

However it stops at model.load line with the above mentioned attribute error.
The error traceback is as :
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-9-4de95ff11efe> in <module> 1 #Load saved/trained Model ----> 2 model = keras.models.load_model('../input/potato-lateblight-trained/Potato_trained_model/LB_potato.h5', custom_objects={"MaxPoolingWithArgmax2D":MaxPoolingWithArgmax2D,"MaxUnpooling2D":MaxUnpooling2D}) 3 # model.compile(loss='binary_crossentropy', 4 # optimizer='adam', 5 # metrics=['accuracy']) /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options) 205 (isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))): 206 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, --> 207 compile) 208 209 filepath = path_to_string(filepath) /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile) 182 model_config = json_utils.decode(model_config.decode('utf-8')) 183 model = model_config_lib.model_from_config(model_config, --> 184 custom_objects=custom_objects) 185 186 # set weights /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects) 62 '`Sequential.from_config(config)`?') 63 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top ---> 64 return deserialize(config, custom_objects=custom_objects) 65 66 /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects) 175 module_objects=LOCAL.ALL_OBJECTS, 176 custom_objects=custom_objects, --> 177 printable_module_name='layer') /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 356 custom_objects=dict( 357 list(_GLOBAL_CUSTOM_OBJECTS.items()) + --> 358 list(custom_objects.items()))) 359 with CustomObjectScope(custom_objects): 360 return cls.from_config(cls_config) /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in from_config(cls, config, custom_objects) 2260 from tensorflow.python.keras.engine import functional # pylint: disable=g-import-not-at-top 2261 return functional.Functional.from_config( -> 2262 config, custom_objects=custom_objects) 2263 2264 def to_json(self, **kwargs): /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in from_config(cls, config, custom_objects) 667 """ 668 input_tensors, output_tensors, created_layers = reconstruct_from_config( --> 669 config, custom_objects) 670 model = cls(inputs=input_tensors, outputs=output_tensors, 671 name=config.get('name')) /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in reconstruct_from_config(config, custom_objects, created_layers) 1283 if layer in unprocessed_nodes: 1284 for node_data in unprocessed_nodes.pop(layer): -> 1285 process_node(layer, node_data) 1286 1287 input_tensors = [] /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in process_node(layer, node_data) 1231 input_tensors = ( 1232 base_layer_utils.unnest_if_single_tensor(input_tensors)) -> 1233 output_tensors = layer(input_tensors, **kwargs) 1234 1235 # Update node index map. /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs) 950 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list): 951 return self._functional_construction_call(inputs, args, kwargs, --> 952 input_list) 953 954 # Maintains info about the `Layer.call` stack. /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list) 1089 # Check input assumptions set after layer building, e.g. input shape. 1090 outputs = self._keras_tensor_symbolic_call( -> 1091 inputs, input_masks, args, kwargs) 1092 1093 if outputs is None: /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs) 820 return nest.map_structure(keras_tensor.KerasTensor, output_signature) 821 else: --> 822 return self._infer_output_signature(inputs, args, kwargs, input_masks) 823 824 def _infer_output_signature(self, inputs, args, kwargs, input_masks): /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks) 861 # TODO(kaftan): do we maybe_build here, or have we already done it? 862 self._maybe_build(inputs) --> 863 outputs = call_fn(inputs, *args, **kwargs) 864 865 self._handle_activity_regularization(inputs, outputs) /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs, mask, training) 915 with backprop.GradientTape(watch_accessed_variables=True) as tape,\ 916 variable_scope.variable_creator_scope(_variable_creator): --> 917 result = self.function(inputs, **kwargs) 918 self._check_variables(created_variables, tape.watched_variables()) 919 return result /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py in get_slice(data, i, parts) 193 """ 194 --> 195 def __init__(self, rate, noise_shape=None, seed=None, **kwargs): 196 super(Dropout, self).__init__(**kwargs) 197 self.rate = rate AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'slice' 

Sumod Nandanwar

unread,
Apr 27, 2021, 7:00:42 AM4/27/21
to Keras-users
Help in solving this issue will be really appreciated. I have struggling to solve this since quite long.

Lance Norskog

unread,
Apr 27, 2021, 11:10:47 AM4/27/21
to Sumod Nandanwar, Keras-users
You will have to use the matching 'slice' function in Tensorflow.
You can probably also use array syntax.


Many keras.backend functions were retired since Keras is now a Tensorflow-only library.

Cheers,

Lance

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/22a588e0-30ff-447e-b1bb-8496a12f0396n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

pushpalatha murthy

unread,
Apr 29, 2021, 4:35:05 AM4/29/21
to Keras-users
Hi,
Pls try this it would resolev your error.

Keras.backend doesnt have slice operation. Instead you can go to the location /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py where core.py file is stored locally on your machine and do the following:

locate funtion get_slice(data, i, parts) 193 """ 194 --> 195 def __init__(self, rate, noise_shape=None, seed=None, **kwargs): 196 super(Dropout, self).__init__(**kwargs) 197 self.rate = rate

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.

or

reinstall proper version of TF and keras and refer to load the model using keras.

regards,
Pushpalatha M
Reply all
Reply to author
Forward
0 new messages