pythontensorflowmachine-learningneural-network

Model Load get different result after restart runtime


I have written a Resnet50 model using google colab. After train my model then saving models afterwards load model without restart run time get same result, but when restarting runtime google colab and running xtrain, ytest, x_val, y_val then loading model again, I am getting different result.

here is my code:

#hyperparameter and callback
batch_size = 128
num_epochs = 120
input_shape = (48, 48, 1)
num_classes = 7

#Compile the model.
from keras.optimizers import Adam, SGD
model = ResNet50(input_shape = (48, 48, 1), classes = 7)
optimizer = SGD(learning_rate=0.0005)
model.compile(optimizer= optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.summary()
history = model.fit(
    data_generator.flow(xtrain, ytrain,),
    steps_per_epoch=len(xtrain) / batch_size,
    epochs=num_epochs, 
    verbose=1,
    validation_data= (x_val,y_val))

import matplotlib.pyplot as plt 
model.save('Fix_Model_resnet50editSGD5st.h5')

#plot graph
accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
num_epochs = range(len(accuracy))
plt.plot(num_epochs, accuracy, 'r', label='Training acc')
plt.plot(num_epochs, val_accuracy, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.ylabel('accuracy')  
plt.xlabel('epoch')
plt.legend()
plt.figure()
plt.plot(num_epochs, loss, 'r', label='Training loss')
plt.plot(num_epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.ylabel('loss')  
plt.xlabel('epoch')
plt.legend()
plt.show()

#load model
from keras.models import load_model
model_load = load_model('Fix_Model_resnet50editSGD5st.h5')

model_load.summary()


testdatamodel = model_load.evaluate(xtest, ytest) 
print("Test Loss " + str(testdatamodel[0]))
print("Test Acc: " + str(testdatamodel[1]))

traindata = model_load.evaluate(xtrain, ytrain) 
print("Test Loss " + str(traindata[0]))
print("Test Acc: " + str(traindata[1]))

valdata = model_load.evaluate(x_val, y_val) 
print("Test Loss " + str(valdata[0]))
print("Test Acc: " + str(valdata[1]))

-after training and saving model then run load model without restart runtime google colab : as you can see the

Test loss: 0.9411 - accuracy: 0.6514

Train loss: 0.7796 - accuracy: 0.7091

ModelEvaluateTest & Train

just run load model again after restart runtime colab:

Test loss: 0.7928 - accuracy: 0.6999

Train loss: 0.8189 - accuracy: 0.6965

after Restart Runtime Evaluate test and train


Solution

  • You need to set random seed to get same results on every iteration either in same session or post restarting.

    tf.random.set_seed(
        seed
    )
    

    check https://www.tensorflow.org/api_docs/python/tf/random/set_seed