3

I am using AutoKeras to train my dataset and then save it as AutoKeras file and Keras file(h5).

My problem is that evaluating those 2 models gives me different results.

This is the code to train the dataset and save the models:

if __name__ == '__main__':
    x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test")
    print(x_test.shape)
    print(y_test.shape)

    x_train, y_train = load_image_dataset(csv_file_path="train/label.csv", images_path="train")
    print(x_train.shape)
    print(y_train.shape)

    clf = ImageClassifier(path="~/automodels/", verbose=True)
    clf.fit(x_train, y_train, time_limit= 1 * 10 * 60)
    clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    y = clf.evaluate(x_test, y_test)
    print(y)

    clf.export_autokeras_model('my_autokeras_model.h5ak')
    clf.export_keras_model('my_model.h5')

This is the code that load the models and evaluating it:

from keras.models import load_model
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Input, LSTM, Bidirectional, Conv1D, Activation
from keras.models import Sequential, Model
from autokeras.image.image_supervised import load_image_dataset
from sklearn.metrics import confusion_matrix
import numpy as np
from keras.utils import to_categorical
from autokeras.utils import pickle_from_file

x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test")
print(x_test.shape)
print(y_test.shape)

model = pickle_from_file('my_autokeras_model.h5ak')
results = model.evaluate(x_test, y_test)
print(results)

keras_model = load_model('model.h5')
x = keras_model.output
x = Activation('softmax', name='activation_add')(x)
new_model = Model(keras_model.input, x)
new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
y_test = to_categorical(y_test)
score = new_model.evaluate(x_test, y_test)
print(score)

But this it the output:

0.7238095238095238
[8.135800362768627, 0.49523809523809526]

which is different.

The reason for adding activation function for the h5 model is explained in this thread (But maybe I got it wrong)

What am i doing wrong ? I will appropriate any help. thank you!

user1623454
  • 274
  • 3
  • 14

0 Answers0