Repository: keras-team/autokeras

Issue:
Export ImgaeRegressor Class
This is an extension of the work done for Issue #79: Save and export autokeras model. We want to be able to export ImageRegressor as well as ImageClassifier dynamically based on the instance. 


Discussion:

Resolving patch:
diff --git a/autokeras/image_supervised.py b/autokeras/image_supervised.py
--- a/autokeras/image_supervised.py
+++ b/autokeras/image_supervised.py
@@ -284,7 +284,7 @@ def inverse_transform_y(self, output):
     def evaluate(self, x_test, y_test):
         """Return the accuracy score between predict value and `y_test`."""
         y_predict = self.predict(x_test)
-        return accuracy_score(y_test, y_predict)
+        return self.metric().compute(y_test, y_predict)
 
     def save_searcher(self, searcher):
         pickle.dump(searcher, open(os.path.join(self.path, 'searcher'), 'wb'))
@@ -330,7 +330,8 @@ def export_keras_model(self, model_file_name):
     def export_autokeras_model(self, model_file_name):
         """ Creates and Exports the AutoKeras model to the given filename. """
         portable_model = PortableImageSupervised(graph=self.load_searcher().load_best_model(), \
-                                                 y_encoder=self.y_encoder, data_transformer=self.data_transformer)
+                                                 y_encoder=self.y_encoder, data_transformer=self.data_transformer, \
+                                                 metric=self.metric, inverse_transform_y_method=self.inverse_transform_y)
         pickle_to_file(portable_model, model_file_name)
 
 
@@ -378,7 +379,7 @@ def inverse_transform_y(self, output):
 
 
 class PortableImageSupervised(PortableClass):
-    def __init__(self, graph, data_transformer, y_encoder):
+    def __init__(self, graph, data_transformer, y_encoder, metric, inverse_transform_y_method):
         """Initialize the instance.
         Args:
             graph: The graph form of the learned model
@@ -386,6 +387,8 @@ def __init__(self, graph, data_transformer, y_encoder):
         super().__init__(graph)
         self.data_transformer = data_transformer
         self.y_encoder = y_encoder
+        self.metric = metric
+        self.inverse_transform_y_method = inverse_transform_y_method
 
     def predict(self, x_test):
         """Return predict results for the testing data.
@@ -410,9 +413,9 @@ def predict(self, x_test):
         return self.inverse_transform_y(output)
 
     def inverse_transform_y(self, output):
-        return self.y_encoder.inverse_transform(output)
+        return self.inverse_transform_y_method(output)
 
     def evaluate(self, x_test, y_test):
         """Return the accuracy score between predict value and `y_test`."""
         y_predict = self.predict(x_test)
-        return accuracy_score(y_test, y_predict)
+        return self.metric().compute(y_test, y_predict)
\ No newline at end of file


Test patch:
diff --git a/tests/test_image_supervised.py b/tests/test_image_supervised.py
--- a/tests/test_image_supervised.py
+++ b/tests/test_image_supervised.py
@@ -235,3 +235,27 @@ def test_export_keras_model(_):
     score = model.evaluate(train_x, train_y)
     assert score <= 1.0
     clean_dir(path)
+
+    clf = ImageRegressor(path=path, verbose=False, resume=False)
+    clf.n_epochs = 100
+    clf.fit(train_x, train_y)
+    score = clf.evaluate(train_x, train_y)
+    assert score >= 0.0
+
+    model_file_name = os.path.join(path, 'test_keras_model.h5')
+    clf.export_keras_model(model_file_name)
+    from keras.models import load_model
+    model = load_model(model_file_name)
+    results = model.predict(test_x)
+    assert len(results) == len(test_x)
+    del model, results, model_file_name
+
+    model_file_name = os.path.join(path, 'test_autokeras_model.pkl')
+    clf.export_autokeras_model(model_file_name)
+    from autokeras.utils import pickle_from_file
+    model = pickle_from_file(model_file_name)
+    results = model.predict(test_x)
+    assert len(results) == len(test_x)
+    score = model.evaluate(train_x, train_y)
+    assert score >= 0.0
+    clean_dir(path)