title: How to implement Transfer-Learning with own model
body: <p>I'm trying to save a CNN model I implemented, and use it to perform transfer learning(TL) .
  I would like to clarify the following four points. <br>
  1.(CNN code) Whether the model saving method is correct or not. <br>
  2.(TL code) Whether the model loading method is correct or not. <br>
  3.(TL code) How is the &quot;trainable&quot; property of the loaded model usually set? <br>
  4.(TL code) Whether the pre-training model and the subsequent layers are correctly combined (size, etc.)<br></p>
  <p>The following are the model part of the CNN and the transfer learning code. <br>
  Both are regression models that predict two numbers. The input is images (+ supervised data).</p>
  <pre class="lang-py prettyprint-override"><code>
  #CNN
  
  input = Input(shape=(100,100,3))
  conv_0 = Conv2D(32,kernel_size=3,activation='relu')(input)
  pool_0 = MaxPooling2D(pool_size=(2,2))(conv_0)
  pool_0 = Dropout(0.25)(pool_0)
  conv_1 = Conv2D(64,kernel_size=3,activation='relu')(pool_0)
  pool_1 = MaxPooling2D(pool_size=(2,2))(conv_1)
  pool_1 = Dropout(0.25)(pool_1)
  conv_2 = Conv2D(32,kernel_size=3,activation='relu')(pool_1)
  pool_2 = MaxPooling2D(pool_size=(2,2))(conv_2)
  pool_2 = Dropout(0.25)(pool_2)
  conv_3 = Conv2D(16,kernel_size=3,activation='relu')(pool_2)
  pool_3 = MaxPooling2D(pool_size=(2,2))(conv_3)
  conv_4 = Conv2D(8,kernel_size=3,activation='relu')(pool_3)
  pool_4 = MaxPooling2D(pool_size=(2,2))(conv_4)
  flat = Flatten()(pool_4)
  denseL = Dense(64,activation='relu')(flat)
  denseL = Dropout(0.25)(denseL)
  A_output = Dense(1,name=&quot;a&quot;)(denseL)
  B_output = Dense(1,name=&quot;b&quot;)(denseL)
  
  model = Model(inputs=input, outputs=[A_output,B_output])
  model.compile(Adam(learning_rate=0.001),
                loss = {'a':'mae','b':'mae'} ,
                metrics =  {'a':'mae','b':'mae'})
  
  history = model.fit([np.array(Img_train)],[np.array(LabelA_train),np.array(LabelB_train)],
                      epochs=100, batch_size=16,
                      validation_data=([np.array(Img_test)],[np.array(LabelA_test),np.array(LabelB_test)]))
  
  model.save('forTransferL.h5')
  
  &quot;&quot;&quot;
  Outputs for sumally()
  __________________________________________________________________________________________________
  Layer (type)                    Output Shape         Param #     Connected to
  ==================================================================================================
  input_1 (InputLayer)            [(None, 100, 100, 3) 0
  __________________________________________________________________________________________________
  conv2d (Conv2D)                 (None, 98, 98, 32)   896         input_1[0][0]
  __________________________________________________________________________________________________
  max_pooling2d (MaxPooling2D)    (None, 49, 49, 32)   0           conv2d[0][0]
  __________________________________________________________________________________________________
  dropout (Dropout)               (None, 49, 49, 32)   0           max_pooling2d[0][0]
  __________________________________________________________________________________________________
  conv2d_1 (Conv2D)               (None, 47, 47, 64)   18496       dropout[0][0]
  __________________________________________________________________________________________________
  max_pooling2d_1 (MaxPooling2D)  (None, 23, 23, 64)   0           conv2d_1[0][0]
  __________________________________________________________________________________________________
  dropout_1 (Dropout)             (None, 23, 23, 64)   0           max_pooling2d_1[0][0]
  __________________________________________________________________________________________________
  conv2d_2 (Conv2D)               (None, 21, 21, 32)   18464       dropout_1[0][0]
  __________________________________________________________________________________________________
  max_pooling2d_2 (MaxPooling2D)  (None, 10, 10, 32)   0           conv2d_2[0][0]
  __________________________________________________________________________________________________
  dropout_2 (Dropout)             (None, 10, 10, 32)   0           max_pooling2d_2[0][0]
  __________________________________________________________________________________________________
  conv2d_3 (Conv2D)               (None, 8, 8, 16)     4624        dropout_2[0][0]
  __________________________________________________________________________________________________
  max_pooling2d_3 (MaxPooling2D)  (None, 4, 4, 16)     0           conv2d_3[0][0]
  __________________________________________________________________________________________________
  conv2d_4 (Conv2D)               (None, 2, 2, 8)      1160        max_pooling2d_3[0][0]
  __________________________________________________________________________________________________
  max_pooling2d_4 (MaxPooling2D)  (None, 1, 1, 8)      0           conv2d_4[0][0]
  __________________________________________________________________________________________________
  flatten (Flatten)               (None, 8)            0           max_pooling2d_4[0][0]
  __________________________________________________________________________________________________
  dense (Dense)                   (None, 64)           576         flatten[0][0]
  __________________________________________________________________________________________________
  dropout_3 (Dropout)             (None, 64)           0           dense[0][0]
  __________________________________________________________________________________________________
  a (Dense)                       (None, 1)            65          dropout_3[0][0]
  __________________________________________________________________________________________________
  b (Dense)                       (None, 1)            65          dropout_3[0][0]
  =========================================================================================
  &quot;&quot;&quot;
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>
  #TL
  
  model = load_model('forTransferL.h5')
  model.layers[0].trainable = False
  x = model.layers[10].output
  
  # The following is the same as part of CNN model.
  conv_3 = Conv2D(16,kernel_size=3,activation='relu')(x)
  pool_3 = MaxPooling2D(pool_size=(2,2))(conv_3)
  conv_4 = Conv2D(8,kernel_size=3,activation='relu')(pool_3)
  pool_4 = MaxPooling2D(pool_size=(2,2))(conv_4)
  flat = Flatten()(pool_4)
  denseL = Dense(64,activation='relu')(flat)
  denseL = Dropout(0.25)(denseL)
  A_output = Dense(1,name=&quot;a&quot;)(denseL)
  B_output = Dense(1,name=&quot;b&quot;)(denseL)
  
  model=Model(inputs=model.input,outputs=[A_output,B_output])
  </code></pre>
  <p>Just in case, I also include the error text that is currently appearing below, <br>
  but I think understanding the basic implementation part is a higher priority than fixing the error.</p>
  <p>Thank you for your cooperation.<br></p>
  <pre><code>#error message
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;c:/Users/userABC/OneDrive/Document/StudyAI/transferMymodel.py&quot;, line 158, in &lt;module&gt;
      pool_m4 = MaxPooling2D(pool_size=(2,2))(conv_m4)
    File &quot;C:\Users\userABC\anaconda3\lib\site-packages\keras\engine\base_layer.py&quot;, line 1006, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    〜Omitted due to the limited number of characters.
  
  ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node tf.compat.v1.nn.max_pool_1/MaxPool}} = MaxPool[T=DT_FLOAT, data_format=&quot;NHWC&quot;, explicit_paddings=[], ksize=[1, 2, 2, 1], padding=&quot;VALID&quot;, strides=[1, 2, 2, 1]](Placeholder)' with input shapes: [?,1,1,8].
  </code></pre>