Accuracy does not change on training

Viewed 32

Is this related to the fact that I use Mobilenet_v2 for classifying pictures, where there are about 60 pictures in the train and 7 pictures in the test

data = Data()
train_data_gen, val_data_gen = Data.prepare_data(preprocess_input=preprocess_input, train_dir=train_dir, val_dir=test_dir)

Found 123 images belonging to 2 classes.

Found 15 images belonging to 2 classes.

class Classifier:
  def __init__(self):
    self.IMG_SHAPE = (150, 150, 3)
    # базовая модель -- MobileNet
    self.base_model = tf.keras.applications.MobileNetV2(input_shape=self.IMG_SHAPE, include_top=False, weights='imagenet')
    self.base_model.trainable = False # замораживаем всю базовую модель


  def extra_layers(self, loss='binary_crossentropy', metrics='accuracy', optimizer='adam', num_classes=None):
    if num_classes == 2:
      self.model = tf.keras.Sequential([
      self.base_model,
      tf.keras.layers.GlobalAveragePooling2D(),
      tf.keras.layers.Dense(1, activation='sigmoid')
      ])
    else:
      self.model = tf.keras.Sequential([
      self.base_model,
      tf.keras.layers.GlobalAveragePooling2D(),
      tf.keras.layers.Dense(num_classes, activation='softmax')
      ])

    self.model.compile(optimizer=optimizer, loss=loss, metrics=[metrics])

    print('!the model was built with additional layers!\n')
  
  
  def fit_train(self, epochs=10, train_data_gen=train_data_gen, val_data_gen=val_data_gen):      #change valid
    self.hist = self.model.fit_generator(
    train_data_gen,
    epochs=epochs,
    validation_data=val_data_gen)

    print('!The model has been trained!\n')


  def predict_classes(self, datage=val_data_gen):
    sample_validation_images, sample_validation_labels = next(datagen)
    self.predictions = (self.model.predict(sample_validation_images) > 0.5).astype("int32").flatten()
    self.sample_validation_images = sample_validation_images
    self.sample_validation_labels = sample_validation_labels

After training, I get the following result:

Epoch 134/300
123/123 [==============================] - 1s 11ms/step - loss: 6.4891e-07 - accuracy: 1.0000 - val_loss: 0.9736 - val_accuracy: 0.5333
Epoch 135/300
123/123 [==============================] - 1s 11ms/step - loss: 6.1259e-07 - accuracy: 1.0000 - val_loss: 0.9709 - val_accuracy: 0.5333
Epoch 136/300
123/123 [==============================] - 1s 11ms/step - loss: 5.7758e-07 - accuracy: 1.0000 - val_loss: 0.9643 - val_accuracy: 0.5333
Epoch 137/300
123/123 [==============================] - 1s 11ms/step - loss: 5.4502e-07 - accuracy: 1.0000 - val_loss: 0.9405 - val_accuracy: 0.5333
Epoch 138/300
123/123 [==============================] - 1s 11ms/step - loss: 5.0976e-07 - accuracy: 1.0000 - val_loss: 0.9808 - val_accuracy: 0.5333
Epoch 139/300
123/123 [==============================] - 1s 11ms/step - loss: 4.8050e-07 - accuracy: 1.0000 - val_loss: 0.9766 - val_accuracy: 0.5333
Epoch 140/300
123/123 [==============================] - 1s 11ms/step - loss: 4.5143e-07 - accuracy: 1.0000 - val_loss: 0.9673 - val_accuracy: 0.5333
Epoch 141/300
123/123 [==============================] - 1s 11ms/step - loss: 4.2582e-07 - accuracy: 1.0000 - val_loss: 0.9850 - val_accuracy: 0.5333
Epoch 142/300
123/123 [==============================] - 1s 11ms/step - loss: 3.9907e-07 - accuracy: 1.0000 - val_loss: 0.9781 - val_accuracy: 0.5333
1 Answers

In terms of data, You can add diverse data in both training and validation set. If it is impossible for adding new data then you may augment the available images such that augmented images from training data shouldn't be added to validation set. Augmented images from training set should be kept in training set and same goes for validation set. Augly package can help you in augmenting the images.

In terms of algorithms, you may use other available models in keras as base models.

Related