Q: Keras 2.2: can't load pre-made model with imagenet weights I've got a block of code that used to work in older versions of Keras, but in Keras 2.2, I get an error about loading a model with not enough layers into a larger model:
import keras
from keras.layers import MaxPooling2D, AveragePooling2D,  Conv2D
from keras.applications import Xception
from keras.layers.normalization import BatchNormalization
from keras.layers import Input, Concatenate, Add
from keras.layers.advanced_activations import LeakyReLU

kernel_size = (3, 3)  
pool_size = (2, 2)  
nfilters = 3
inputs = Input(shape=(331, 331, 1))
x = inputs
x = Conv2D(nfilters, kernel_size, strides=(1,1), padding='same', use_bias=False)(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.1)(x)
x = MaxPooling2D(pool_size=pool_size)(x)
x =  Add()([x,AveragePooling2D(pool_size=pool_size)(inputs)])  # residual skip connection on shrunk image
base_model = Xception(weights='imagenet', include_top=False, input_tensor=x)

The error I get is on the line with Xception:
ValueError: You are trying to load a weight file containing 80 layers into a model with 82 layers.
Here's a link to a Google Colab notebook that reproduces this.
The problem occurs in regard to loading the imagenet weights; if I set weights to None there's no problem.  
This kind of error can be avoided in a load_model() call by passing by_name=True, but these premade models like Xception don't allow the by_name keyword. 
Can anyone explain how to get my code working again under Keras 2.2?
I suppose I could define Xception twice, once by itself with imagenet weights, and the other in my full model with weights=None, and then copy the weights from the former to the latter...but I'd rather not have to do that if possible. 
("Why are you putting those layers before Xception?" It's because I'm shrinking a larger image down to the size that Xception requires for its imagnet weights, and to convert my greyscale images to 3-channel images.)

A: Not complete sure how to explain your error, but you can make it work by treating the Xception model as a layer, calling it on your previous layers and wrapping the entire stack in a model instance. I verified the following in your colab notebook.
import keras
from keras.layers import MaxPooling2D, AveragePooling2D,  Conv2D
from keras.applications import Xception
from keras.layers.normalization import BatchNormalization
from keras.layers import Input, Concatenate, Add
from keras.layers.advanced_activations import LeakyReLU

kernel_size = (3, 3)  
pool_size = (2, 2)  
nfilters = 3
inputs = Input(shape=(331, 331, 1))
x = inputs
x = Conv2D(nfilters, kernel_size, strides=(1,1), padding='same', use_bias=False)(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.1)(x)
x = MaxPooling2D(pool_size=pool_size)(x)
x =  Add()([x,AveragePooling2D(pool_size=pool_size)(inputs)])  # residual skip connection on shrunk image

# Xception architecture is just another layer
base_model = Xception(weights='imagenet', include_top=False)
output = base_model(x)
# Wrap everything into a model
combined_model = keras.models.Model(inputs=inputs, outputs=output)

This will give you a model that looks as follows:
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_2 (InputLayer)            (None, 331, 331, 1)  0                                            
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 331, 331, 3)  27          input_2[0][0]                    
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 331, 331, 3)  12          conv2d_6[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_2 (LeakyReLU)       (None, 331, 331, 3)  0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 165, 165, 3)  0           leaky_re_lu_2[0][0]              
__________________________________________________________________________________________________
average_pooling2d_2 (AveragePoo (None, 165, 165, 1)  0           input_2[0][0]                    
__________________________________________________________________________________________________
add_14 (Add)                    (None, 165, 165, 3)  0           max_pooling2d_2[0][0]            
                                                                 average_pooling2d_2[0][0]        
__________________________________________________________________________________________________
xception (Model)                multiple             20861480    add_14[0][0]                     
==================================================================================================
Total params: 20,861,519
Trainable params: 20,806,985
Non-trainable params: 54,534
__________________________________________________________________________________________________

