title: Keras - Freezing A Model And Then Adding Trainable Layers
body: <p>I am taking a CNN model that is pretrained, and then trying to implement a CNN-LSTM with parallel CNNs all with the same weights from the pretraining.</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code># load in CNN
  weightsfile = 'final_weights.h5'
  modelfile = '2dcnn_model.json'
  
  # load model from json
  json_file = open(modelfile, 'r')
  loaded_model_json = json_file.read()
  json_file.close()
  fixed_cnn_model = keras.models.model_from_json(loaded_model_json)
  fixed_cnn_model.load_weights(weightsfile)
  
  # remove the last 2 dense FC layers and freeze it
  fixed_cnn_model.pop()
  fixed_cnn_model.pop()
  fixed_cnn_model.trainable = False
  
  print(fixed_cnn_model.summary())
  This will produce the summary:
  </code></pre>
  
  <p>_</p>
  
  <pre class="lang-python prettyprint-override"><code>________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  input_1 (InputLayer)         (None, 32, 32, 4)         0         
  _________________________________________________________________
  conv2d_1 (Conv2D)            (None, 30, 30, 32)        1184      
  _________________________________________________________________
  conv2d_2 (Conv2D)            (None, 28, 28, 32)        9248      
  _________________________________________________________________
  conv2d_3 (Conv2D)            (None, 26, 26, 32)        9248      
  _________________________________________________________________
  conv2d_4 (Conv2D)            (None, 24, 24, 32)        9248      
  _________________________________________________________________
  max_pooling2d_1 (MaxPooling2 (None, 12, 12, 32)        0         
  _________________________________________________________________
  conv2d_5 (Conv2D)            (None, 10, 10, 64)        18496     
  _________________________________________________________________
  conv2d_6 (Conv2D)            (None, 8, 8, 64)          36928     
  _________________________________________________________________
  max_pooling2d_2 (MaxPooling2 (None, 4, 4, 64)          0         
  _________________________________________________________________
  conv2d_7 (Conv2D)            (None, 2, 2, 128)         73856     
  _________________________________________________________________
  max_pooling2d_3 (MaxPooling2 (None, 1, 1, 128)         0         
  _________________________________________________________________
  flatten_1 (Flatten)          (None, 128)               0         
  _________________________________________________________________
  dropout_1 (Dropout)          (None, 128)               0         
  _________________________________________________________________
  dense_1 (Dense)              (None, 512)               66048     
  =================================================================
  Total params: 224,256
  Trainable params: 0
  Non-trainable params: 224,256
  _________________________________________________________________
  </code></pre>
  
  <p>Now, I will add to it and compile and show that the non-trainable all become trainable.</p>
  
  <pre class="lang-python prettyprint-override"><code># create sequential model to get this all before the LSTM
  
  # initialize loss function, SGD optimizer and metrics
  loss = 'binary_crossentropy'
  optimizer = keras.optimizers.Adam(lr=1e-4, 
                                  beta_1=0.9, 
                                  beta_2=0.999,
                                  epsilon=1e-08,
                                  decay=0.0)
  metrics = ['accuracy']
  
  currmodel = Sequential()
  currmodel.add(TimeDistributed(fixed_cnn_model, input_shape=(num_timewins, imsize, imsize, n_colors)))
  currmodel.add(LSTM(units=size_mem, 
              activation='relu', 
              return_sequences=False))
  currmodel.add(Dense(1024, activation='relu')
  currmodel.add(Dense(2, activation='softmax')
  
  currmodel = Model(inputs=currmodel.input, outputs = currmodel.output)
  config = currmodel.compile(optimizer=optimizer, loss=loss, metrics=metrics) 
  print(currmodel.summary())
  
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  time_distributed_3_input (In (None, 5, 32, 32, 4)      0         
  _________________________________________________________________
  time_distributed_3 (TimeDist (None, 5, 512)            224256    
  _________________________________________________________________
  lstm_3 (LSTM)                (None, 50)                112600    
  _________________________________________________________________
  dropout_1 (Dropout)          (None, 50)                0         
  _________________________________________________________________
  dense_1 (Dense)              (None, 1024)              52224     
  _________________________________________________________________
  dropout_2 (Dropout)          (None, 1024)              0         
  _________________________________________________________________
  dense_2 (Dense)              (None, 2)                 2050      
  =================================================================
  Total params: 391,130
  Trainable params: 391,130
  Non-trainable params: 0
  _________________________________________________________________
  </code></pre>
  
  <p>How am I supposed to freeze the layers in this case? I am almost 100% positive that I had working code in this format in an earlier keras version. It seems like this is the right direction, since you define a model and declare certain layers trainable, or not.</p>
  
  <p>Then you add layers, which are by default trainable. However, this seems to convert all the layers to trainable.</p>