I have an ubermodel that uses a submodel as a layer for feature extraction. My code is modular such that I can easily switch which submodel I'm using to perform feature extraction simply by changing whichever submodel I assign:
...
elif FEATURE_EXTRACTOR == "VGG16":
Features = keras.applications.VGG16(
weights = "imagenet",
pooling = FEATURE_POOLING,
include_top = False
)
elif FEATURE_EXTRACTOR == "EfficientNetB0":
Features = keras_applications_master.keras_applications.efficientnet.EfficientNetB0(
# ^ Local copy of official keras repo: https://github.com/keras-team/keras-applications
# because pip install --upgrade keras doesn't install version with efficientnet.
weights = "imagenet",
include_top = False,
pooling = FEATURE_POOLING,
classes = None
)
...
My routines for saving and loading the ubermodel and its weights are also aware of which submodel is being used for feature extraction:
model.load_weights(submodel_specific_path)
With any submodel, I can perform the initial training run and save the ubermodel to disk. And if I try to continue training or fine tune any ubermodel containing a VGG16 submodel, using load_weights
to load the weights, everything works fine. But when I load_weights
any ubermodel with an efficientnet submodel (or, say, keras.applications.xception.Xception), I get the following error:
Traceback (most recent call last):
File "image_model.py", line 284, in <module>
model.load_weights(model_path)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\network.py", line 1227, in load_weights
reshape=reshape)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\saving.py", line 1294, in load_weights_from_hdf5_group_by_name
reshape=reshape)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\saving.py", line 861, in preprocess_weights_for_loading
weights = convert_nested_model(weights)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\saving.py", line 836, in convert_nested_model
original_backend=original_backend))
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\keras\engine\saving.py", line 980, in preprocess_weights_for_loading
weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
File "<__array_function__ internals>", line 6, in transpose
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\numpy\core\fromnumeric.py", line 651, in transpose
return _wrapfunc(a, 'transpose', axes)
File "C:\Users\Username\Anaconda3\envs\tensorflow1\lib\site-packages\numpy\core\fromnumeric.py", line 61, in _wrapfunc
return bound(*args, **kwds)
ValueError: axes don't match array
What am I doing wrong?