model = mynasnet.NASNetLarge(input_shape=None, include_top=True, weights='imagenet', input_tensor=None, pooling=None)
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.
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)
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.
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.
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.
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.