title: How do i access data after model.add(Flatten()) layer?
body: <p>I am trying to use CNN for feature extraction and XGboost for classification of a image data. I researched and found that it could be done by extracting the data after the convolution layers. I found some source code for similar problem and tried on my own.</p>
  <pre><code>model = Sequential()
  model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), padding='same', activation=&quot;relu&quot;, input_shape = data.shape[1:])) 
  model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) 
  
  model.add(Conv2D(64, kernel_size=(3,3), padding='same', strides=(1,1), activation=&quot;relu&quot;) )
  model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2
  
  model.add(Conv2D(128, kernel_size=(3,3), padding='same', strides=(1,1), activation=&quot;relu&quot;))
  model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2
  
  model.add(Conv2D(256, kernel_size=(3,3), padding='same', strides=(1,1), activation=&quot;relu&quot;))
  model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2
  
  model.add(Flatten()) 
  
  model.add(Dense(128, activation=&quot;relu&quot;, name='firstDenseLayer'))
  model.add(Dense(1, activation=&quot;sigmoid&quot;))
  
  # model.summary()
  
  # print(model)
  
  model.compile(loss=&quot;binary_crossentropy&quot;, optimizer=&quot;adam&quot;, metrics=[&quot;accuracy&quot;])
  
  model.fit(data, label, batch_size=16, epochs=10, validation_data=(val_data, val_label))
  </code></pre>
  <p>Below i accessed the dense layer named &quot;firstDenseLayer&quot;.</p>
  <pre><code>import xgboost as xgb
  from keras.models import Model
  
  layerName = 'firstDenseLayer'
  
  intermediate_layer_model = Model(inputs=model.input,
                                   outputs=model.get_layer(layerName).output)
  
  intermediate_output = intermediate_layer_model.predict(data) 
  
  
  from xgboost import XGBClassifier
  
  xgbmodel = XGBClassifier(objective='multi:softmax', num_class= 2)
  xgbmodel.fit(intermediate_output, label)
  </code></pre>
  <pre><code>xgbmodel.score(intermediate_output, label)
  </code></pre>
  <p>As i am new in this, i have several confusions.</p>
  <ol>
  <li>How the data is being flowed. After i extract the features of the pictures via convolution layers, how do i actually access the data from there?</li>
  <li>What is this line of code doing? What data is it extracting?</li>
  </ol>
  <pre><code>intermediate_output = intermediate_layer_model.predict(data) 
  </code></pre>
  <ol start="3">
  <li>When i omit(keep commented out) the below line,</li>
  </ol>
  <pre><code>model.fit(data, label, batch_size=16, epochs=10, validation_data=(val_data, val_label))
  </code></pre>
  <p>from the first snippet and run the XGboost model directly the XGboost gives low accuracy and when i don't it gives higher accuracy. Why is it being like that?</p>
  <p>Kindly help me out. I am stuck with this for quite a while. I am just trying to access the extracted features data from the last convolution layer and use that data to do classification using XGboost. As i tried to follow the method that i found from online, i am not sure if it is the the only way of doing it. If there is another way kindly let me know.</p>