title: difference in the calculation of accuracy between keras and scikit-learn
body: <p>I am currently working on multi-label image classification using the CNN in keras.
  In addition to the accuracy of keras, we have also reconfirmed the accuracy of scikit-learn using various evaluation methods (recall, precision, F1 score and accuracy).</p>
  <p>We found that the accuracy calculated by keras shows about 90%, while scikit-learn shows only about 60%.</p>
  <p>I do not know why this is happening, so please let me know.</p>
  <p>Is there something wrong with the keras calculation?</p>
  <p>We use sigmoid for the activation function, <code>binary_crossentropy</code> for the loss function, and adam for the optimizer.</p>
  <hr />
  <p>Keras training</p>
  <pre><code>input_tensor = Input(shape=(img_width, img_height, 3))
  
  base_model = MobileNetV2(include_top=False, weights='imagenet')
  
  #model.summary()
  
  x = base_model.output
  x = GlobalAveragePooling2D()(x)
  #x = Dense(2048, activation='relu')(x)
  #x = Dropout(0.5)(x)
  x = Dense(1024, activation = 'relu')(x)
  
  x = Dropout(0.5)(x)
  predictions = Dense(6, activation = 'sigmoid')(x)
  
  for layer in base_model.layers:
      layer.trainable = False
  
  
  model = Model(inputs = base_model.input, outputs = predictions)
  print(&quot;{}層&quot;.format(len(model.layers)))
  
  
  model.compile(optimizer=sgd, loss=&quot;binary_crossentropy&quot;, metrics=[&quot;acc&quot;])
  
  history = model.fit(X_train, y_train, epochs=50, validation_data=(X_val, y_val), batch_size=64, verbose=2)
  
  model_evaluate()
  </code></pre>
  <p>Keras showed 90% (Accuracy).</p>
  <hr />
  <p>scikit-learn check</p>
  <pre><code> from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
  thresholds=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
  
  y_pred = model.predict(X_test)
  
  for val in thresholds:
      print(&quot;For threshold: &quot;, val)
      pred=y_pred.copy()
    
      pred[pred&gt;=val]=1
      pred[pred&lt;val]=0
      
      accuracy = accuracy_score(y_test, pred)
      precision = precision_score(y_test, pred, average='micro')
      recall = recall_score(y_test, pred, average='micro')
      f1 = f1_score(y_test, pred, average='micro')
     
      print(&quot;Micro-average quality numbers&quot;)
      print(&quot;Acc: {:.4f}, Precision: {:.4f}, Recall: {:.4f}, F1-measure: {:.4f}&quot;.format(accuracy, precision, recall, f1))
  </code></pre>
  <hr />
  <p>Output(scikit-learn)</p>
  <pre><code>  For threshold:  0.1
  Micro-average quality numbers
  Acc: 0.0727, Precision: 0.3776, Recall: 0.8727, F1-measure: 0.5271
  For threshold:  0.2
  Micro-average quality numbers
  Acc: 0.1931, Precision: 0.4550, Recall: 0.8033, F1-measure: 0.5810
  For threshold:  0.3
  Micro-average quality numbers
  Acc: 0.3323, Precision: 0.5227, Recall: 0.7403, F1-measure: 0.6128
  For threshold:  0.4
  Micro-average quality numbers
  Acc: 0.4574, Precision: 0.5842, Recall: 0.6702, F1-measure: 0.6243
  For threshold:  0.5
  Micro-average quality numbers
  Acc: 0.5059, Precision: 0.6359, Recall: 0.5858, F1-measure: 0.6098
  For threshold:  0.6
  Micro-average quality numbers
  Acc: 0.4597, Precision: 0.6993, Recall: 0.4707, F1-measure: 0.5626
  For threshold:  0.7
  Micro-average quality numbers
  Acc: 0.3417, Precision: 0.7520, Recall: 0.3383, F1-measure: 0.4667
  For threshold:  0.8
  Micro-average quality numbers
  Acc: 0.2205, Precision: 0.7863, Recall: 0.2132, F1-measure: 0.3354
  For threshold:  0.9
  Micro-average quality numbers
  Acc: 0.1063, Precision: 0.8987, Recall: 0.1016, F1-measure: 0.1825
  </code></pre>