Closed sjdhola closed 3 years ago
I've trained the model again. now we are getting 88.50% validation accuracy on evaluation using saved model.
Noted. Can you upload your files in this issue as well?
import tensorflow as tf from tensorflow.keras import from tensorflow.keras.layers import from tensorflow.keras.models import Model from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.preprocessing import image from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.models import Sequential from keras.applications.mobilenet import MobileNet, preprocess_input import numpy as np from glob import glob import matplotlib.pyplot as plt
IMAGE_SIZE = [227, 227]
model =MobileNet(input_shape= [227,227, 3], weights='imagenet', include_top=False)
for layer in model.layers[:-23]: layer.trainable = False
folders = glob('/content/drive/MyDrive/Colab Notebooks/Project/Cars96/training/*')
x = Flatten()(model.layers[-6].output) prediction = Dense(len(folders), activation='softmax')(x) model = Model(inputs = model.input, outputs = prediction)
model.summary()
adam=tf.keras.optimizers.Adam( learning_rate=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-07, decay = 0.0 ) model.compile( loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'] )
from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range = 0,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/Colab Notebooks/Project/Cars96/training', target_size = (227, 227), batch_size = 256, class_mode = 'categorical', shuffle=False) test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/Colab Notebooks/Project/Cars96/testing', target_size = (227, 227), batch_size = 256, class_mode = 'categorical', shuffle=False)
checkpoint_filepath = '/content/drive/MyDrive/Colab Notebooks/Project/sample/./weights.{epoch:02d}.hdf5' callback = [tf.keras.callbacks.EarlyStopping(monitor='loss', patience=4,mode = 'auto', restore_best_weights=True), tf.keras.callbacks.CSVLogger('traininglog.csv', separator=",", append=False), tf.keras.callbacks.ReduceLROnPlateau(monitor="val_loss",factor=0.1, patience=2, mode="auto",min_delta=0.0001,min_lr=0), tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath, save_weights_only=True,monitor='val_accuracy',mode='max',save_best_only=True), ] r = model.fit( training_set, validation_data=test_set, epochs=15, steps_per_epoch=len(training_set), validation_steps=len(test_set), callbacks = [callback] )
plt.plot(r.history['loss'], label='train loss') plt.plot(r.history['val_loss'], label='val loss') plt.legend() plt.show() plt.plot(r.history['accuracy'], label='train acc') plt.plot(r.history['val_accuracy'], label='val acc') plt.legend() plt.show()
model.save('New_model.h5') model.save('/content/drive/MyDrive/Colab Notebooks/Project/Cars96/New_model_100_classes_2.h5') model.evaluate(test_set)
Noted.
Here when we train the model we are getting really good accuracy about 87.30% but when we save this model and evaluate the image dataset using that saved model the accuracy is decreasing to 21%.