question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

errors when load nasnet weights

See original GitHub issue

Hi, everyone,when I run the code:

model = mynasnet.NASNetLarge(input_shape=None, include_top=True, weights='imagenet', input_tensor=None, pooling=None)

I got the errors:

weights_file ./NASNet-large.h5 Traceback (most recent call last): File "D:/Code/CodePy/ForPycharm/MYTestData/mtyunDLS/testkeras/myvggfinetun/finetunenasnet.py", line 61, in <module> model = mynasnet.NASNetLarge(input_shape=None, include_top=True, weights='imagenet', input_tensor=None, pooling=None) File "D:\Code\CodePy\ForPycharm\MYTestData\mtyunDLS\testkeras\myvggfinetun\mynasnet.py", line 362, in NASNetLarge default_size=331) File "D:\Code\CodePy\ForPycharm\MYTestData\mtyunDLS\testkeras\myvggfinetun\mynasnet.py", line 289, in NASNet model.load_weights(weights_file) File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2619, in load_weights load_weights_from_hdf5_group(f, self.layers) File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 3068, in load_weights_from_hdf5_group str(len(filtered_layers)) + ' layers.') ValueError: You are trying to load a weight file containing 533 layers into a model with 527 layers.

mynasnet.py copys the nasnet.py in the keras.applications,and I make some changes as follows:

NASNET_MOBILE_WEIGHT_PATH = './NASNet-mobile.h5' NASNET_MOBILE_WEIGHT_PATH_NO_TOP = './NASNet-mobile-no-top.h5' NASNET_LARGE_WEIGHT_PATH = './NASNet-large.h5' NASNET_LARGE_WEIGHT_PATH_NO_TOP = './NASNet-large-no-top.h5'

if weights == 'imagenet':
     if default_size == 224:  # mobile version
            if include_top:
                weights_file = NASNET_MOBILE_WEIGHT_PATH
                # model_name = 'nasnet_mobile.h5'
            else:
                weights_file = NASNET_MOBILE_WEIGHT_PATH_NO_TOP
                # model_name = 'nasnet_mobile_no_top.h5'
            # weights_file = get_file(model_name, weight_path,
            #                         cache_subdir='models')
            print('weights_file',weights_file)
            model.load_weights(weights_file)
        elif default_size == 331:  # large version
            if include_top:
                weights_file = NASNET_LARGE_WEIGHT_PATH
                # model_name = 'nasnet_large.h5'
            else:
                weights_file = NASNET_LARGE_WEIGHT_PATH_NO_TOP
                # model_name = 'nasnet_large_no_top.h5'
            # weights_file = get_file(model_name, weight_path,
                                    # cache_subdir='models')
            print('weights_file', weights_file)
            model.load_weights(weights_file)

I also got the errors when I run following code:

model = mynasnet.NASNetLarge(input_shape=None, include_top=False, weights='imagenet', input_tensor=None, pooling=None)

ValueError: You are trying to load a weight file containing 532 layers into a model with 526 layers.

and for nasnetmobile,it doesn’t work too

model = mynasnet.NASNetMobile(input_shape=None, include_top=False, weights='imagenet', input_tensor=None, pooling=None)

ValueError: You are trying to load a weight file containing 388 layers into a model with 382 layers.

my tensorflow’s version is 1.4.0, keras’s version is 2.0.8, can someone help me?thanks for you attention!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
KarthikMudlapurcommented, Sep 24, 2018

@djshu I think the problem you are facing is with respect to the input_shape. For NASNetLarge the input_shape is (331,331,3). For NASNetMobile the input_shape is (224,224,3). Just input these parameters instead of None and you will be able to download the weights. Hope it helps.

0reactions
taehoonleecommented, Jan 21, 2019

The issue has been resolved in keras-team/keras-applications#62.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras bug NasNetlarge no top - Data Science Stack Exchange
If I set the input shape to (331, 331, 3) and include_top to False , it downloads the weights with the top. Am...
Read more >
'str' object has no attribute 'decode' " , while Loading a Keras ...
Models and Weights were saved successfully and there was no error. I can successfully load the weights simply using model.load_weights and ...
Read more >
Supported Networks, Layers, and Classes - MATLAB & Simulink
The syntax mobilenetv2('Weights','none') is not supported for code generation. Yes. Yes. Yes. NASNet-Large. NASNet-Large convolutional neural network.
Read more >
Applications - Keras Documentation
Keras Applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, ...
Read more >
Model Zoo - Deep learning code and pretrained models for ...
A neat pytorch implementation of nasnet and the ported weights from tensorflow ... Note for a bug, stage2 and stage 4's output are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found