Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'keras.engine'" #4

Open
rholgado opened this issue Jul 14, 2024 · 16 comments
Open

ModuleNotFoundError: No module named 'keras.engine'" #4

rholgado opened this issue Jul 14, 2024 · 16 comments

Comments

@rholgado
Copy link

Hi!

I have the following requirements installed:

tensorflow>=2.8.0
keras_applications>=1.0.8
kapre>=0.3.7
I have also installed classification-models-1D using pip.

When I run the following example code in my Jupyter notebook:
from classification_models_1D.tfkeras import Classifiers
ResNet18, preprocess_input = Classifiers.get('resnet18')
model = ResNet18(input_shape=(224*224, 2), weights='imagenet')

I encounter the error:

ModuleNotFoundError: No module named 'keras.engine'

I have tried different versions of TensorFlow and Keras, but the issue persists. Can anyone help me resolve this?

Thank you!

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 15, 2024

Can you try exactly:

tensorflow==2.8.0
keras_applications==1.0.8
kapre==0.3.7

@rholgado
Copy link
Author

Oh thank you so much! It seems that it worked, but I've encountered a new error: "TypeError: list indices must be integers or slices, not str" It might be related to how indices are accessed internally in the code.

Could you provide some guidance on how to resolve this new error? I appreciate any help you can offer!

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 16, 2024

Oh thank you so much! It seems that it worked, but I've encountered a new error: "TypeError: list indices must be integers or slices, not str" It might be related to how indices are accessed internally in the code.

Could you provide some guidance on how to resolve this new error? I appreciate any help you can offer!

Can you post full error log?

@rholgado
Copy link
Author

Yeah sure! This is what I got:


TypeError Traceback (most recent call last)
Cell In[69], line 3
1 from classification_models_1D.tfkeras import Classifiers
2 ResNet18, preprocess_input = Classifiers.get('resnet18')
----> 3 model = ResNet18(input_shape=(224*224, 2), weights='imagenet')

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models_factory.py:94, in ModelsFactory.inject_submodules..wrapper(*args, **kwargs)
92 modules_kwargs = self.get_kwargs()
93 new_kwargs = dict(list(kwargs.items()) + list(modules_kwargs.items()))
---> 94 return func(*args, **new_kwargs)

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models/resnet.py:387, in ResNet18(input_shape, input_tensor, weights, pooling, classes, include_top, stride_size, kernel_size, repetitions, **kwargs)
375 def ResNet18(
376 input_shape=None,
377 input_tensor=None,
(...)
385 **kwargs
386 ):
--> 387 return ResNet(
388 MODELS_PARAMS['resnet18'],
389 input_shape=input_shape,
390 input_tensor=input_tensor,
391 include_top=include_top,
392 classes=classes,
393 weights=weights,
394 pooling=pooling,
395 stride_size=stride_size,
396 kernel_size=kernel_size,
397 repetitions=repetitions,
398 **kwargs
399 )

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models/resnet.py:269, in ResNet(model_params, input_shape, input_tensor, include_top, classes, weights, stride_size, kernel_size, init_filters, first_kernel_size, repetitions, pooling, **kwargs)
266 conv_params = get_conv_params()
268 # resnet bottom
--> 269 x = layers.BatchNormalization(name='bn_data', **bn_params)(img_input)
270 x = layers.ZeroPadding1D(padding=first_kernel_size // 2)(x)
271 x = layers.Conv1D(init_filters, first_kernel_size, strides=stride_size[0], name='conv0', **conv_params)(x)

File ~/jupyter_py3.5/lib/python3.10/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.traceback)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb

File ~/jupyter_py3.5/lib/python3.10/site-packages/google/protobuf/internal/containers.py:70, in BaseContainer.getitem(self, key)
68 def getitem(self, key):
69 """Retrieves item by the specified key."""
---> 70 return self._values[key]

TypeError: list indices must be integers or slices, not str

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 16, 2024

Can you try to run this code:

https://github.com/ZFTurbo/classification_models_1D/blob/main/tst_1D_classification_tfkeras.py

@rholgado
Copy link
Author

rholgado commented Jul 16, 2024

Hi. Yeah for sure, I run it and I got this error:

"GPU use: 4
Tensorflow version: 2.8.0
Go for resnet18

TypeError Traceback (most recent call last)
Cell In[71], line 120
116 print(s)
119 if name == 'main':
--> 120 tst_keras()

Cell In[71], line 92, in tst_keras()
86 model = modelPoint(
87 input_shape=input_shape,
88 include_top=include_top,
89 weights=use_weights,
90 )
91 else:
---> 92 model = modelPoint(
93 input_shape=input_shape,
94 include_top=include_top,
95 weights=use_weights,
96 stride_size=stride_size,
97 kernel_size=9,
98 )
99 summary = []
100 model.summary(print_fn=lambda x: summary.append(x))

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models_factory.py:94, in ModelsFactory.inject_submodules..wrapper(*args, **kwargs)
92 modules_kwargs = self.get_kwargs()
93 new_kwargs = dict(list(kwargs.items()) + list(modules_kwargs.items()))
---> 94 return func(*args, **new_kwargs)

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models/resnet.py:387, in ResNet18(input_shape, input_tensor, weights, pooling, classes, include_top, stride_size, kernel_size, repetitions, **kwargs)
375 def ResNet18(
376 input_shape=None,
377 input_tensor=None,
(...)
385 **kwargs
386 ):
--> 387 return ResNet(
388 MODELS_PARAMS['resnet18'],
389 input_shape=input_shape,
390 input_tensor=input_tensor,
391 include_top=include_top,
392 classes=classes,
393 weights=weights,
394 pooling=pooling,
395 stride_size=stride_size,
396 kernel_size=kernel_size,
397 repetitions=repetitions,
398 **kwargs
399 )

File ~/jupyter_py3.5/lib/python3.10/site-packages/classification_models_1D/models/resnet.py:269, in ResNet(model_params, input_shape, input_tensor, include_top, classes, weights, stride_size, kernel_size, init_filters, first_kernel_size, repetitions, pooling, **kwargs)
266 conv_params = get_conv_params()
268 # resnet bottom
--> 269 x = layers.BatchNormalization(name='bn_data', **bn_params)(img_input)
270 x = layers.ZeroPadding1D(padding=first_kernel_size // 2)(x)
271 x = layers.Conv1D(init_filters, first_kernel_size, strides=stride_size[0], name='conv0', **conv_params)(x)

File ~/jupyter_py3.5/lib/python3.10/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.traceback)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb

File ~/jupyter_py3.5/lib/python3.10/site-packages/google/protobuf/internal/containers.py:70, in BaseContainer.getitem(self, key)
68 def getitem(self, key):
69 """Retrieves item by the specified key."""
---> 70 return self._values[key]

TypeError: list indices must be integers or slices, not str"

@rholgado
Copy link
Author

rholgado commented Jul 18, 2024

Hi again. Sorry I'm a little bit desperate and maybe someone can help me.

I've been analyzing the code, and it seems the issue arises when adjusting the input shape to be compatible with BatchNormalization after importing from tensorflow.keras using the following snippet:

from tensorflow.keras import layers, models

Example bn_params dictionary

bn_params = {
'axis': 1,
'momentum': 0.99,
'epsilon': 1e-5,
'center': True,
'scale': True,
}

Example usage in a model

input_tensor = layers.Input(shape=(224*224, 2))
x = layers.BatchNormalization(**bn_params)(input_tensor)

While layers.BatchNormalization(**bn_params) appears correct, the error arises when attempting to adjust the input shape to align with BatchNormalization. I'm unsure how to correct this issue."

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 18, 2024

I can't reproduce your issue. I cloned repo, then installed dependencies in fresh venv. After I run tst_1D_classification_tfkeras.py:

Tensorflow version: 2.8.4
Go for resnet18
(100, 441000, 2) (100, 431, 512)
Params: 11 M Memory: 0.416 GB Time: 0.0735 sec
Go for resnet34
...

@rholgado
Copy link
Author

Ok, i understand. And what is your version of python, keras and keras aplication? Tensor flow is 2.8.4 ok

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 18, 2024

  • keras==2.8.0
  • keras_application==1.0.8
  • python==3.8

@rholgado
Copy link
Author

I don't know what is happening. Because i have this:
TensorFlow version: 2.8.0
Keras version: 2.8.0
Keras Applications version: 1.0.8
Kapre version: 0.3.7
Python version: 3.8.10 (default, Jun 16 2021, 14:20:20)
[GCC 9.3.0]

And then I run this:
from classification_models_1D.tfkeras import Classifiers

ResNet18, preprocess_input = Classifiers.get('resnet18')
model = ResNet18(input_shape=(224*224, 2), weights='imagenet')

And i get the same error "TypeError: list indices must be integers or slices, not str"

@ZFTurbo
Copy link
Owner

ZFTurbo commented Jul 19, 2024

Can you add print(bn_params)

before x = layers.BatchNormalization(name='bn_data', **bn_params)(img_input)

@rholgado
Copy link
Author

Hi ! I got this "{'axis': 2, 'momentum': 0.99, 'epsilon': 2e-05, 'center': True, 'scale': True}" and then the "TypeError: list indices must be integers or slices, not str"

@EswarRB
Copy link

EswarRB commented Aug 8, 2024

from classification_models_1D.tfkeras import Classifiers

ResNet18, preprocess_input = Classifiers.get('resnet18')
model = ResNet18(input_shape=(224*224, 2), weights='imagenet')

"{
"name": "ValueError",
"message": "There is no weights for such configuration: model = model, dataset = imagenet, classes = 1000, include_top = True.",
"stack": "---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 4
1 from classification_models_1D.tfkeras import Classifiers
3 ResNet18, preprocess_input = Classifiers.get('resnet18')
----> 4 model = ResNet18(input_shape=(224*224, 2), weights='imagenet')

File ./anaconda3/lib/python3.11/site-packages/classification_models_1D/models_factory.py:94, in ModelsFactory.inject_submodules..wrapper(*args, **kwargs)
92 modules_kwargs = self.get_kwargs()
93 new_kwargs = dict(list(kwargs.items()) + list(modules_kwargs.items()))
---> 94 return func(*args, **new_kwargs)

File ./anaconda3/lib/python3.11/site-packages/classification_models_1D/models/resnet.py:387, in ResNet18(input_shape, input_tensor, weights, pooling, classes, include_top, stride_size, kernel_size, repetitions, **kwargs)
375 def ResNet18(
376 input_shape=None,
377 input_tensor=None,
(...)
385 **kwargs
386 ):
--> 387 return ResNet(
388 MODELS_PARAMS['resnet18'],
389 input_shape=input_shape,
390 input_tensor=input_tensor,
391 include_top=include_top,
392 classes=classes,
393 weights=weights,
394 pooling=pooling,
395 stride_size=stride_size,
396 kernel_size=kernel_size,
397 repetitions=repetitions,
398 **kwargs
399 )

File./anaconda3/lib/python3.11/site-packages/classification_models_1D/models/resnet.py:346, in ResNet(model_params, input_shape, input_tensor, include_top, classes, weights, stride_size, kernel_size, init_filters, first_kernel_size, repetitions, pooling, **kwargs)
344 model.load_weights(weights)
345 else:
--> 346 load_model_weights(
347 model,
348 model_params.model_name,
349 weights,
350 classes,
351 include_top,
352 kernel_size,
353 input_shape[-1],
354 **kwargs
355 )
357 return model

File ./anaconda3/lib/python3.11/site-packages/classification_models_1D/weights.py:52, in load_model_weights(model, model_name, dataset, classes, include_top, kernel_size, channel, **kwargs)
49 model.load_weights(weights_path)
51 else:
---> 52 raise ValueError('There is no weights for such configuration: ' +
53 'model = {}, dataset = {}, '.format(model.name, dataset) +
54 'classes = {}, include_top = {}.'.format(classes, include_top))

ValueError: There is no weights for such configuration: model = model, dataset = imagenet, classes = 1000, include_top = True."
}"

@ZFTurbo
Copy link
Owner

ZFTurbo commented Aug 8, 2024

Either set weights=None or use include_top=False.

@EswarRB
Copy link

EswarRB commented Aug 12, 2024

Thanks worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants