Keras: errors when load nasnet weights

Created on 3 May 2018  路  6Comments  路  Source: keras-team/keras

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锛焧hanks for you attention!

Most helpful comment

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

All 6 comments

The same issue, please assist.

Solution for VGG16 also helps and for NasNet
https://github.com/keras-team/keras/issues/8998
I added additional parameter model.load_weights(weights_file, by_name=True) in nasnet.py
and it works.

@trueProgrammer , by name would avoid the the error, but would load only part of the layer.
Those 6 extra layers are not need?

You must set a correct parameter for input_shape or input_tensor. For example input_shape = (224, 224, 3) is an acceptable choice. If you don't set a parameter, the code fails in a non-informative way.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

farizrahman4u picture farizrahman4u  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

snakeztc picture snakeztc  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

yil8 picture yil8  路  3Comments