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

NameError: name 'keras' is not defined #317

Open
ShahariarRabby opened this issue Sep 14, 2024 · 7 comments
Open

NameError: name 'keras' is not defined #317

ShahariarRabby opened this issue Sep 14, 2024 · 7 comments

Comments

@ShahariarRabby
Copy link

2024-09-14 04:23:33,200 - CRITICAL - Can not load tensorflow or keras, stopping
2024-09-14 04:23:33,201 - CRITICAL - Can not load tensorflow or keras, stopping

NameError Traceback (most recent call last)
Cell In[17], line 4
2 import keras
3 watcher = ww.WeightWatcher(model=model)
----> 4 details = watcher.describe()

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:3769, in WeightWatcher.describe(self, model, layers, min_evals, max_evals, min_size, max_size, max_N, glorot_fix, savefig, ww2x, pool, conv2d_fft, fft, conv2d_norm, intra, channels, stacked, start_ids, base_model, peft)
3766 details = pd.DataFrame(columns=[])
3768 num_all_evals = 0
-> 3769 for ww_layer in layer_iterator:
3770 if not ww_layer.skipped and ww_layer.has_weights:
3771 #logger.debug("LAYER TYPE: {} {} layer type {}".format(ww_layer.layer_id, ww_layer.the_type, type(ww_layer.framework_layer)))
3772 #logger.debug("layer_id {} weights shape : {} max size {}".format(ww_layer.layer_id,ww_layer.weights.shape, params['max_evals']))
3773 if not pool:

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:1423, in ModelIterator.next(self)
1422 def next(self):
-> 1423 return self.next()

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:1426, in ModelIterator.next(self)
1425 def next(self):
-> 1426 curr_layer = next(self.layer_iter)
1427 if curr_layer:
1428 return curr_layer

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:1620, in WWLayerIterator.ww_layer_iter_(self)
1617 def ww_layer_iter_(self):
1618 """Create a generator for iterating over ww_layers"""
-> 1620 for curr_layer in self.model_iter:
1622 is_skipped = True
1623 is_supported = False

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:273, in KerasLayer.get_layer_iterator..layer_iter_()
271 yield keras_layer
272 for layer in model.layers:
--> 273 yield from traverse_(layer)

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:264, in KerasLayer.get_layer_iterator..layer_iter_..traverse_(layer)
262 nonlocal layer_id
263 if not hasattr(layer, 'submodules') or len(layer.submodules)==0:
--> 264 keras_layer = KerasLayer(layer, layer_id)
265 layer_id += 1
266 yield keras_layer

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:149, in KerasLayer.init(self, layer, layer_id, name, longname)
147 def init(self, layer, layer_id, name=None, longname = None):
--> 149 the_type = self.layer_type(layer)
150 channels = CHANNELS.FIRST
151 FrameworkLayer.init(self, layer, layer_id, name, longname=longname, the_type=the_type,
152 framework=FRAMEWORK.KERAS, channels=channels)

File ~/anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py:165, in KerasLayer.layer_type(self, layer)
162 typestr = (str(type(layer))).lower()
164 # Keras TF 2.x types
--> 165 if isinstance(layer, keras.layers.Dense) or 'dense' in typestr:
166 the_type = LAYER_TYPE.DENSE
168 elif isinstance(layer, keras.layers.Conv1D) or 'conv1d' in typestr:

NameError: name 'keras' is not defined

@charlesmartin14
Copy link
Member

Thanks Ill take a look

Was this from a notebook here in the repo or something else ?

@ShahariarRabby
Copy link
Author

It was on Jupyter notebook. I fixed it by modifying the file anaconda3/envs/tf216/lib/python3.10/site-packages/weightwatcher/weightwatcher.py
Added from tensorflow import keras.

However still getting some warning
Critical: tensorflow load failed
Critical: Keras load failed

I’m using TF 2.17.0

@charlesmartin14
Copy link
Member

charlesmartin14 commented Sep 14, 2024 via email

@charlesmartin14
Copy link
Member

im not able to reproduce it

is this one of the notebooks provided in the github repo
if not can you share it

and/or can you join the discord to discuss ?
https://discord.com/invite/uVVsEAcfyF

@ShahariarRabby
Copy link
Author

I tried to do this with my model and got the error then I tried the code from example notebook and got same error.

from tensorflow.keras.applications.vgg16 import VGG16
vgg16 = VGG16()
watcher = ww.WeightWatcher(model=vgg16)
details = watcher.describe()

@charlesmartin14
Copy link
Member

very odd..works fine for me locally


from tensorflow.keras.applications.vgg16 import VGG16
import weightwatcher as ww
print(ww.__version__)
vgg16 = VGG16()
watcher = ww.WeightWatcher(model=vgg16)
details = watcher.describe()

Screenshot 2024-09-16 at 9 37 45 PM

@charlesmartin14
Copy link
Member

also works fine on google colab

there must be something about your ENV that WW is not picking up some packages

What is your ENV ?

Screenshot 2024-09-16 at 9 40 07 PM

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

2 participants