title: ValueError: You are trying to load a weight file containing 14 layers into a model with 3 layers
body: <p>(I couldn't find the solution with the previosly asked questions.)
  I use VGG16 to test on my data. My class number is 2 and I used <a href="https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html" rel="nofollow noreferrer">this page</a> to freeze conv layers and train top layers. Here is the code:</p>
  
  <pre><code>from keras.applications import VGG16
  model = VGG16(include_top=False,classes=2,input_shape(224,224,3),weights='imagenet')
  </code></pre>
  
  <p>Then I created top_model which will be top model of my Vgg16:</p>
  
  <pre><code>top_model=Sequential()
  top_model.add(Flatten(input_shape=model.output_shape[1:]))
  top_model.add(Dense(4096,activation='relu'))
  top_model.add(Dense(4096,activation='relu'))
  top_model.add(Dense(2,activation='softmax'))
  model= Model(inputs=model.input, output=top_model(model.output))
  </code></pre>
  
  <p>Then I freezed some layers and compiled model:</p>
  
  <pre><code>for layer in model.layers[:19]:
   layer.trainable=False
  
  model.compile(loss='binary_crossentropy',optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
             metrics=['accuracy'])
  </code></pre>
  
  <p>After some data augmentation process, I trained the model and saved the weights like that:</p>
  
  <pre><code>model.fit_generator(trainGenerator,samples_per_epoch=numTraining//batchSize,
                    epochs=numEpoch,
                    validation_data=valGenerator,
                    validation_steps=numValid//batchSize)
  
  model.save_weights('top3_weights.h5') 
  </code></pre>
  
  <p>After that, my weights are saved and I changed second part of my code to be able to test whole model with my data:</p>
  
  <pre><code>model = VGG16(include_top=False,classes=2,input_shape=(224,224,3),weights='imagenet')
  
  top_model=Sequential()
  top_model.add(Flatten(input_shape=model.output_shape[1:]))
  top_model.add(Dense(4096,activation='relu'))
  top_model.add(Dense(4096,activation='relu'))
  top_model.add(Dense(2,activation='softmax'))
  top_model.load_weights(r'C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\top3_weights.h5') #(this line is added)
  model= Model(inputs=model.input, output=top_model(model.output)) 
  </code></pre>
  
  <p>Finally when I tried to summary the model with the code:</p>
  
  <pre><code>print(model.summary())
  </code></pre>
  
  <p>I am getting that errors and outputs:</p>
  
  <pre><code>Using TensorFlow backend.
  Traceback (most recent call last):
    File "C:\Users\Umit Kilic\Komodomyfiles\umit\test_vgg16.py", line 38, in &lt;module&gt;
      top_model.load_weights(r'C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\top3_weights.h5')
    File "C:\Users\Umit Kilic\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1166, in load_weights
  f, self.layers, reshape=reshape)
    File "C:\Users\Umit Kilic\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\saving.py", line 1030, in load_weights_from_hdf5_group
  str(len(filtered_layers)) + ' layers.')
  ValueError: You are trying to load a weight file containing 14 layers into a model with 3 layers.
  </code></pre>
  
  <p>Any help, please?</p>