errors when load nasnet weights
See original GitHub issueHi, 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:
- Created 5 years ago
- Reactions:2
- Comments:6 (1 by maintainers)
@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.